Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===// |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 6 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 7 | // |
| 8 | // This file implements semantic analysis for C++ templates. |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 9 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 10 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 11 | #include "TreeTransform.h" |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
| 17 | #include "clang/AST/ExprCXX.h" |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeVisitor.h" |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Builtins.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" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 34 | |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 35 | #include <iterator> |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 36 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 37 | using namespace sema; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 38 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 39 | // Exported for use by Parser. |
| 40 | SourceRange |
| 41 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 42 | unsigned N) { |
| 43 | if (!N) return SourceRange(); |
| 44 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 45 | } |
| 46 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 47 | namespace clang { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 48 | /// [temp.constr.decl]p2: A template's associated constraints are |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 49 | /// defined as a single constraint-expression derived from the introduced |
| 50 | /// constraint-expressions [ ... ]. |
| 51 | /// |
| 52 | /// \param Params The template parameter list and optional requires-clause. |
| 53 | /// |
| 54 | /// \param FD The underlying templated function declaration for a function |
| 55 | /// template. |
| 56 | static Expr *formAssociatedConstraints(TemplateParameterList *Params, |
| 57 | FunctionDecl *FD); |
| 58 | } |
| 59 | |
| 60 | static Expr *clang::formAssociatedConstraints(TemplateParameterList *Params, |
| 61 | FunctionDecl *FD) { |
| 62 | // FIXME: Concepts: collect additional introduced constraint-expressions |
| 63 | assert(!FD && "Cannot collect constraints from function declaration yet."); |
| 64 | return Params->getRequiresClause(); |
| 65 | } |
| 66 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 67 | /// Determine whether the declaration found is acceptable as the name |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 68 | /// of a template and, if so, return that template declaration. Otherwise, |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 69 | /// returns null. |
| 70 | /// |
| 71 | /// Note that this may return an UnresolvedUsingValueDecl if AllowDependent |
| 72 | /// is true. In all other cases it will return a TemplateDecl (or null). |
| 73 | NamedDecl *Sema::getAsTemplateNameDecl(NamedDecl *D, |
| 74 | bool AllowFunctionTemplates, |
| 75 | bool AllowDependent) { |
| 76 | D = D->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 78 | if (isa<TemplateDecl>(D)) { |
| 79 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 80 | return nullptr; |
| 81 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 82 | return D; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 83 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 85 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 86 | // C++ [temp.local]p1: |
| 87 | // Like normal (non-template) classes, class templates have an |
| 88 | // injected-class-name (Clause 9). The injected-class-name |
| 89 | // can be used with or without a template-argument-list. When |
| 90 | // it is used without a template-argument-list, it is |
| 91 | // equivalent to the injected-class-name followed by the |
| 92 | // template-parameters of the class template enclosed in |
| 93 | // <>. When it is used with a template-argument-list, it |
| 94 | // refers to the specified class template specialization, |
| 95 | // which could be the current specialization or another |
| 96 | // specialization. |
| 97 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 98 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 99 | if (Record->getDescribedClassTemplate()) |
| 100 | return Record->getDescribedClassTemplate(); |
| 101 | |
| 102 | if (ClassTemplateSpecializationDecl *Spec |
| 103 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 104 | return Spec->getSpecializedTemplate(); |
| 105 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 107 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 108 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 110 | // 'using Dependent::foo;' can resolve to a template name. |
| 111 | // 'using typename Dependent::foo;' cannot (not even if 'foo' is an |
| 112 | // injected-class-name). |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 113 | if (AllowDependent && isa<UnresolvedUsingValueDecl>(D)) |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 114 | return D; |
| 115 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 116 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 119 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 120 | bool AllowFunctionTemplates, |
| 121 | bool AllowDependent) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 122 | LookupResult::Filter filter = R.makeFilter(); |
| 123 | while (filter.hasNext()) { |
| 124 | NamedDecl *Orig = filter.next(); |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 125 | if (!getAsTemplateNameDecl(Orig, AllowFunctionTemplates, AllowDependent)) |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 126 | filter.erase(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 127 | } |
| 128 | filter.done(); |
| 129 | } |
| 130 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 131 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 132 | bool AllowFunctionTemplates, |
| 133 | bool AllowDependent) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 134 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 135 | if (getAsTemplateNameDecl(*I, AllowFunctionTemplates, AllowDependent)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 136 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 138 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 141 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 142 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 143 | bool hasTemplateKeyword, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 144 | const UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 145 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 146 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 147 | TemplateTy &TemplateResult, |
| 148 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 149 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 151 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 152 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 153 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 154 | switch (Name.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 155 | case UnqualifiedIdKind::IK_Identifier: |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 156 | TName = DeclarationName(Name.Identifier); |
| 157 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 158 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 159 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 160 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 161 | Name.OperatorFunctionId.Operator); |
| 162 | break; |
| 163 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 164 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 165 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 166 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 167 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 168 | default: |
| 169 | return TNK_Non_template; |
| 170 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 172 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 174 | LookupResult R(*this, TName, Name.getBeginLoc(), LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 175 | if (LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 176 | MemberOfUnknownSpecialization)) |
| 177 | return TNK_Non_template; |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 178 | if (R.empty()) return TNK_Non_template; |
Richard Smith | 40bd10b | 2019-02-15 00:29:04 +0000 | [diff] [blame] | 179 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 180 | NamedDecl *D = nullptr; |
| 181 | if (R.isAmbiguous()) { |
| 182 | // If we got an ambiguity involving a non-function template, treat this |
| 183 | // as a template name, and pick an arbitrary template for error recovery. |
| 184 | bool AnyFunctionTemplates = false; |
| 185 | for (NamedDecl *FoundD : R) { |
| 186 | if (NamedDecl *FoundTemplate = getAsTemplateNameDecl(FoundD)) { |
| 187 | if (isa<FunctionTemplateDecl>(FoundTemplate)) |
| 188 | AnyFunctionTemplates = true; |
| 189 | else { |
| 190 | D = FoundTemplate; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // If we didn't find any templates at all, this isn't a template name. |
| 197 | // Leave the ambiguity for a later lookup to diagnose. |
| 198 | if (!D && !AnyFunctionTemplates) { |
| 199 | R.suppressDiagnostics(); |
| 200 | return TNK_Non_template; |
| 201 | } |
| 202 | |
| 203 | // If the only templates were function templates, filter out the rest. |
| 204 | // We'll diagnose the ambiguity later. |
| 205 | if (!D) |
| 206 | FilterAcceptableTemplateNames(R); |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 207 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 208 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 209 | // At this point, we have either picked a single template name declaration D |
| 210 | // or we have a non-empty set of results R containing either one template name |
| 211 | // declaration or a set of function templates. |
| 212 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 213 | TemplateName Template; |
| 214 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 216 | unsigned ResultCount = R.end() - R.begin(); |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 217 | if (!D && ResultCount > 1) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 218 | // We assume that we'll preserve the qualifier from a function |
| 219 | // template name in other ways. |
| 220 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 221 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 222 | |
| 223 | // We'll do this lookup again later. |
| 224 | R.suppressDiagnostics(); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 225 | } else { |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 226 | if (!D) { |
| 227 | D = getAsTemplateNameDecl(*R.begin()); |
| 228 | assert(D && "unambiguous result is not a template name"); |
| 229 | } |
| 230 | |
| 231 | if (isa<UnresolvedUsingValueDecl>(D)) { |
| 232 | // We don't yet know whether this is a template-name or not. |
| 233 | MemberOfUnknownSpecialization = true; |
| 234 | return TNK_Non_template; |
| 235 | } |
| 236 | |
| 237 | TemplateDecl *TD = cast<TemplateDecl>(D); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 238 | |
| 239 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 240 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 241 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 242 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 243 | } else { |
| 244 | Template = TemplateName(TD); |
| 245 | } |
| 246 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 247 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 248 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 249 | |
| 250 | // We'll do this lookup again later. |
| 251 | R.suppressDiagnostics(); |
| 252 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 253 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 254 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 255 | isa<BuiltinTemplateDecl>(TD)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 256 | TemplateKind = |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 257 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 258 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 259 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 261 | TemplateResult = TemplateTy::make(Template); |
| 262 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Richard Smith | 278890f | 2017-02-10 20:39:58 +0000 | [diff] [blame] | 265 | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, |
| 266 | SourceLocation NameLoc, |
| 267 | ParsedTemplateTy *Template) { |
| 268 | CXXScopeSpec SS; |
| 269 | bool MemberOfUnknownSpecialization = false; |
| 270 | |
| 271 | // We could use redeclaration lookup here, but we don't need to: the |
| 272 | // syntactic form of a deduction guide is enough to identify it even |
| 273 | // if we can't look up the template name at all. |
| 274 | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 275 | if (LookupTemplateName(R, S, SS, /*ObjectType*/ QualType(), |
| 276 | /*EnteringContext*/ false, |
| 277 | MemberOfUnknownSpecialization)) |
| 278 | return false; |
Richard Smith | 278890f | 2017-02-10 20:39:58 +0000 | [diff] [blame] | 279 | |
| 280 | if (R.empty()) return false; |
| 281 | if (R.isAmbiguous()) { |
| 282 | // FIXME: Diagnose an ambiguity if we find at least one template. |
| 283 | R.suppressDiagnostics(); |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | // We only treat template-names that name type templates as valid deduction |
| 288 | // guide names. |
| 289 | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); |
| 290 | if (!TD || !getAsTypeTemplateDecl(TD)) |
| 291 | return false; |
| 292 | |
| 293 | if (Template) |
| 294 | *Template = TemplateTy::make(TemplateName(TD)); |
| 295 | return true; |
| 296 | } |
| 297 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 298 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 299 | SourceLocation IILoc, |
| 300 | Scope *S, |
| 301 | const CXXScopeSpec *SS, |
| 302 | TemplateTy &SuggestedTemplate, |
| 303 | TemplateNameKind &SuggestedKind) { |
| 304 | // We can't recover unless there's a dependent scope specifier preceding the |
| 305 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 306 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 307 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 308 | computeDeclContext(*SS)) |
| 309 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 310 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 311 | // The code is missing a 'template' keyword prior to the dependent template |
| 312 | // name. |
| 313 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 314 | Diag(IILoc, diag::err_template_kw_missing) |
| 315 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 316 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 317 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 318 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 319 | SuggestedKind = TNK_Dependent_template_name; |
| 320 | return true; |
| 321 | } |
| 322 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 323 | bool Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 324 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 325 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 326 | bool EnteringContext, |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 327 | bool &MemberOfUnknownSpecialization, |
| 328 | SourceLocation TemplateKWLoc) { |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 329 | Found.setTemplateNameLookup(true); |
| 330 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 331 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 332 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 333 | DeclContext *LookupCtx = nullptr; |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 334 | bool IsDependent = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 335 | if (!ObjectType.isNull()) { |
| 336 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 337 | // x->B::f, and we are looking into the type of the object. |
| 338 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 339 | LookupCtx = computeDeclContext(ObjectType); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 340 | IsDependent = !LookupCtx; |
| 341 | assert((IsDependent || !ObjectType->isIncompleteType() || |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 342 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 343 | "Caller should have completed object type"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 345 | // Template names cannot appear inside an Objective-C class or object type. |
| 346 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 347 | Found.clear(); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 348 | return false; |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 349 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 350 | } else if (SS.isSet()) { |
| 351 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 352 | // so long into the context associated with the prior nested-name-specifier. |
| 353 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 354 | IsDependent = !LookupCtx; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 355 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 356 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 357 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 358 | return true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 362 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 363 | if (LookupCtx) { |
| 364 | // Perform "qualified" name lookup into the declaration context we |
| 365 | // computed, which is either the type of the base of a member access |
| 366 | // expression or the declaration context associated with a prior |
| 367 | // nested-name-specifier. |
| 368 | LookupQualifiedName(Found, LookupCtx); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 369 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 370 | // FIXME: The C++ standard does not clearly specify what happens in the |
| 371 | // case where the object type is dependent, and implementations vary. In |
| 372 | // Clang, we treat a name after a . or -> as a template-name if lookup |
| 373 | // finds a non-dependent member or member of the current instantiation that |
| 374 | // is a type template, or finds no such members and lookup in the context |
| 375 | // of the postfix-expression finds a type template. In the latter case, the |
| 376 | // name is nonetheless dependent, and we may resolve it to a member of an |
| 377 | // unknown specialization when we come to instantiate the template. |
| 378 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 381 | if (!SS.isSet() && (ObjectType.isNull() || Found.empty())) { |
| 382 | // C++ [basic.lookup.classref]p1: |
| 383 | // In a class member access expression (5.2.5), if the . or -> token is |
| 384 | // immediately followed by an identifier followed by a <, the |
| 385 | // identifier must be looked up to determine whether the < is the |
| 386 | // beginning of a template argument list (14.2) or a less-than operator. |
| 387 | // The identifier is first looked up in the class of the object |
| 388 | // expression. If the identifier is not found, it is then looked up in |
| 389 | // the context of the entire postfix-expression and shall name a class |
| 390 | // template. |
| 391 | if (S) |
| 392 | LookupName(Found, S); |
| 393 | |
| 394 | if (!ObjectType.isNull()) { |
| 395 | // FIXME: We should filter out all non-type templates here, particularly |
| 396 | // variable templates and concepts. But the exclusion of alias templates |
| 397 | // and template template parameters is a wording defect. |
| 398 | AllowFunctionTemplatesInLookup = false; |
| 399 | ObjectTypeSearchedInScope = true; |
| 400 | } |
| 401 | |
| 402 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
| 403 | } |
| 404 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 405 | if (Found.isAmbiguous()) |
| 406 | return false; |
| 407 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 408 | if (Found.empty() && !IsDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 409 | // If we did not find any names, attempt to correct any typos. |
| 410 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 411 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 412 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 413 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 414 | FilterCCC->WantTypeSpecifiers = false; |
| 415 | FilterCCC->WantExpressionKeywords = false; |
| 416 | FilterCCC->WantRemainingKeywords = false; |
| 417 | FilterCCC->WantCXXNamedCasts = true; |
| 418 | if (TypoCorrection Corrected = CorrectTypo( |
| 419 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 420 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 421 | Found.setLookupName(Corrected.getCorrection()); |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 422 | if (auto *ND = Corrected.getFoundDecl()) |
| 423 | Found.addDecl(ND); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 424 | FilterAcceptableTemplateNames(Found); |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 425 | if (Found.isAmbiguous()) { |
| 426 | Found.clear(); |
| 427 | } else if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 428 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 429 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 430 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 431 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 432 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 433 | << Name << LookupCtx << DroppedSpecifier |
| 434 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 435 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 436 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 437 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 438 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 439 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 440 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 444 | NamedDecl *ExampleLookupResult = |
| 445 | Found.empty() ? nullptr : Found.getRepresentativeDecl(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 446 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 447 | if (Found.empty()) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 448 | if (IsDependent) { |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 449 | MemberOfUnknownSpecialization = true; |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 450 | return false; |
| 451 | } |
| 452 | |
| 453 | // If a 'template' keyword was used, a lookup that finds only non-template |
| 454 | // names is an error. |
| 455 | if (ExampleLookupResult && TemplateKWLoc.isValid()) { |
| 456 | Diag(Found.getNameLoc(), diag::err_template_kw_refers_to_non_template) |
| 457 | << Found.getLookupName() << SS.getRange(); |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 458 | Diag(ExampleLookupResult->getUnderlyingDecl()->getLocation(), |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 459 | diag::note_template_kw_refers_to_non_template) |
| 460 | << Found.getLookupName(); |
| 461 | return true; |
| 462 | } |
| 463 | |
| 464 | return false; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 465 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 467 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 468 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 469 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 470 | // [...] If the lookup in the class of the object expression finds a |
| 471 | // template, the name is also looked up in the context of the entire |
| 472 | // postfix-expression and [...] |
| 473 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 474 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 475 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 476 | LookupOrdinaryName); |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 477 | FoundOuter.setTemplateNameLookup(true); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 478 | LookupName(FoundOuter, S); |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 479 | // FIXME: We silently accept an ambiguous lookup here, in violation of |
| 480 | // [basic.lookup]/1. |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 481 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 482 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 483 | NamedDecl *OuterTemplate; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 484 | if (FoundOuter.empty()) { |
| 485 | // - if the name is not found, the name found in the class of the |
| 486 | // object expression is used, otherwise |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 487 | } else if (FoundOuter.isAmbiguous() || !FoundOuter.isSingleResult() || |
| 488 | !(OuterTemplate = |
| 489 | getAsTemplateNameDecl(FoundOuter.getFoundDecl()))) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 490 | // - if the name is found in the context of the entire |
| 491 | // postfix-expression and does not name a class template, the name |
| 492 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 493 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 494 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 495 | // - if the name found is a class template, it must refer to the same |
| 496 | // entity as the one found in the class of the object expression, |
| 497 | // otherwise the program is ill-formed. |
| 498 | if (!Found.isSingleResult() || |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 499 | getAsTemplateNameDecl(Found.getFoundDecl())->getCanonicalDecl() != |
| 500 | OuterTemplate->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 501 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 502 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 503 | << Found.getLookupName() |
| 504 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 505 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 506 | diag::note_ambig_member_ref_object_type) |
| 507 | << ObjectType; |
| 508 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 509 | diag::note_ambig_member_ref_scope); |
| 510 | |
| 511 | // Recover by taking the template that we found in the object |
| 512 | // expression's type. |
| 513 | } |
| 514 | } |
| 515 | } |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 516 | |
| 517 | return false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 520 | void Sema::diagnoseExprIntendedAsTemplateName(Scope *S, ExprResult TemplateName, |
| 521 | SourceLocation Less, |
| 522 | SourceLocation Greater) { |
| 523 | if (TemplateName.isInvalid()) |
| 524 | return; |
| 525 | |
| 526 | DeclarationNameInfo NameInfo; |
| 527 | CXXScopeSpec SS; |
| 528 | LookupNameKind LookupKind; |
| 529 | |
| 530 | DeclContext *LookupCtx = nullptr; |
| 531 | NamedDecl *Found = nullptr; |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 532 | bool MissingTemplateKeyword = false; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 533 | |
| 534 | // Figure out what name we looked up. |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 535 | if (auto *DRE = dyn_cast<DeclRefExpr>(TemplateName.get())) { |
| 536 | NameInfo = DRE->getNameInfo(); |
| 537 | SS.Adopt(DRE->getQualifierLoc()); |
| 538 | LookupKind = LookupOrdinaryName; |
| 539 | Found = DRE->getFoundDecl(); |
| 540 | } else if (auto *ME = dyn_cast<MemberExpr>(TemplateName.get())) { |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 541 | NameInfo = ME->getMemberNameInfo(); |
| 542 | SS.Adopt(ME->getQualifierLoc()); |
| 543 | LookupKind = LookupMemberName; |
| 544 | LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl(); |
| 545 | Found = ME->getMemberDecl(); |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 546 | } else if (auto *DSDRE = |
| 547 | dyn_cast<DependentScopeDeclRefExpr>(TemplateName.get())) { |
| 548 | NameInfo = DSDRE->getNameInfo(); |
| 549 | SS.Adopt(DSDRE->getQualifierLoc()); |
| 550 | MissingTemplateKeyword = true; |
| 551 | } else if (auto *DSME = |
| 552 | dyn_cast<CXXDependentScopeMemberExpr>(TemplateName.get())) { |
| 553 | NameInfo = DSME->getMemberNameInfo(); |
| 554 | SS.Adopt(DSME->getQualifierLoc()); |
| 555 | MissingTemplateKeyword = true; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 556 | } else { |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 557 | llvm_unreachable("unexpected kind of potential template name"); |
| 558 | } |
| 559 | |
| 560 | // If this is a dependent-scope lookup, diagnose that the 'template' keyword |
| 561 | // was missing. |
| 562 | if (MissingTemplateKeyword) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 563 | Diag(NameInfo.getBeginLoc(), diag::err_template_kw_missing) |
| 564 | << "" << NameInfo.getName().getAsString() << SourceRange(Less, Greater); |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 565 | return; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // Try to correct the name by looking for templates and C++ named casts. |
| 569 | struct TemplateCandidateFilter : CorrectionCandidateCallback { |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 570 | Sema &S; |
| 571 | TemplateCandidateFilter(Sema &S) : S(S) { |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 572 | WantTypeSpecifiers = false; |
| 573 | WantExpressionKeywords = false; |
| 574 | WantRemainingKeywords = false; |
| 575 | WantCXXNamedCasts = true; |
| 576 | }; |
| 577 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| 578 | if (auto *ND = Candidate.getCorrectionDecl()) |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 579 | return S.getAsTemplateNameDecl(ND); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 580 | return Candidate.isKeyword(); |
| 581 | } |
| 582 | }; |
| 583 | |
| 584 | DeclarationName Name = NameInfo.getName(); |
| 585 | if (TypoCorrection Corrected = |
| 586 | CorrectTypo(NameInfo, LookupKind, S, &SS, |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 587 | llvm::make_unique<TemplateCandidateFilter>(*this), |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 588 | CTK_ErrorRecovery, LookupCtx)) { |
| 589 | auto *ND = Corrected.getFoundDecl(); |
| 590 | if (ND) |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 591 | ND = getAsTemplateNameDecl(ND); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 592 | if (ND || Corrected.isKeyword()) { |
| 593 | if (LookupCtx) { |
| 594 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 595 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| 596 | Name.getAsString() == CorrectedStr; |
| 597 | diagnoseTypo(Corrected, |
| 598 | PDiag(diag::err_non_template_in_member_template_id_suggest) |
| 599 | << Name << LookupCtx << DroppedSpecifier |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 600 | << SS.getRange(), false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 601 | } else { |
| 602 | diagnoseTypo(Corrected, |
| 603 | PDiag(diag::err_non_template_in_template_id_suggest) |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 604 | << Name, false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 605 | } |
| 606 | if (Found) |
| 607 | Diag(Found->getLocation(), |
| 608 | diag::note_non_template_in_template_id_found); |
| 609 | return; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) |
| 614 | << Name << SourceRange(Less, Greater); |
| 615 | if (Found) |
| 616 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); |
| 617 | } |
| 618 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 619 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 620 | /// was just parsed. This is only possible with an explicit scope |
| 621 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 622 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 623 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 624 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 625 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 626 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 627 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 628 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 629 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 630 | // C++11 [expr.prim.general]p12: |
| 631 | // An id-expression that denotes a non-static data member or non-static |
| 632 | // member function of a class can only be used: |
| 633 | // (...) |
| 634 | // - if that id-expression denotes a non-static data member and it |
| 635 | // appears in an unevaluated operand. |
| 636 | // |
| 637 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 638 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 639 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 640 | // always a MemberExpr. |
| 641 | bool MightBeCxx11UnevalField = |
| 642 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 643 | |
Akira Hatanaka | d644e02 | 2016-12-16 03:19:41 +0000 | [diff] [blame] | 644 | // Check if the nested name specifier is an enum type. |
| 645 | bool IsEnum = false; |
| 646 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
| 647 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
| 648 | |
| 649 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 650 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 651 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 652 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 653 | // Since the 'this' expression is synthesized, we don't need to |
| 654 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 655 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 656 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 657 | return CXXDependentScopeMemberExpr::Create( |
| 658 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 659 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 660 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 663 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 664 | } |
| 665 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 666 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 667 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 668 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 669 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 670 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 671 | return DependentScopeDeclRefExpr::Create( |
| 672 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 673 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 676 | |
| 677 | /// Determine whether we would be unable to instantiate this template (because |
| 678 | /// it either has no definition, or is in the process of being instantiated). |
| 679 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 680 | NamedDecl *Instantiation, |
| 681 | bool InstantiatedFromMember, |
| 682 | const NamedDecl *Pattern, |
| 683 | const NamedDecl *PatternDef, |
| 684 | TemplateSpecializationKind TSK, |
| 685 | bool Complain /*= true*/) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 686 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 687 | isa<VarDecl>(Instantiation)); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 688 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 689 | bool IsEntityBeingDefined = false; |
| 690 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 691 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 692 | |
| 693 | if (PatternDef && !IsEntityBeingDefined) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 694 | NamedDecl *SuggestedDef = nullptr; |
| 695 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 696 | /*OnlyNeedComplete*/false)) { |
| 697 | // If we're allowed to diagnose this and recover, do so. |
| 698 | bool Recover = Complain && !isSFINAEContext(); |
| 699 | if (Complain) |
| 700 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 701 | Sema::MissingImportKind::Definition, Recover); |
| 702 | return !Recover; |
| 703 | } |
| 704 | return false; |
| 705 | } |
| 706 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 707 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 708 | return true; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 709 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 710 | llvm::Optional<unsigned> Note; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 711 | QualType InstantiationTy; |
| 712 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 713 | InstantiationTy = Context.getTypeDeclType(TD); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 714 | if (PatternDef) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 715 | Diag(PointOfInstantiation, |
| 716 | diag::err_template_instantiate_within_definition) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 717 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 718 | << InstantiationTy; |
| 719 | // Not much point in noting the template declaration here, since |
| 720 | // we're lexically inside it. |
| 721 | Instantiation->setInvalidDecl(); |
| 722 | } else if (InstantiatedFromMember) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 723 | if (isa<FunctionDecl>(Instantiation)) { |
| 724 | Diag(PointOfInstantiation, |
| 725 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 726 | << /*member function*/ 1 << Instantiation->getDeclName() |
| 727 | << Instantiation->getDeclContext(); |
| 728 | Note = diag::note_explicit_instantiation_here; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 729 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 730 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 731 | Diag(PointOfInstantiation, |
| 732 | diag::err_implicit_instantiate_member_undefined) |
| 733 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 734 | Note = diag::note_member_declared_at; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 735 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 736 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 737 | if (isa<FunctionDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 738 | Diag(PointOfInstantiation, |
| 739 | diag::err_explicit_instantiation_undefined_func_template) |
| 740 | << Pattern; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 741 | Note = diag::note_explicit_instantiation_here; |
| 742 | } else if (isa<TagDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 743 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 744 | << (TSK != TSK_ImplicitInstantiation) |
| 745 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 746 | Note = diag::note_template_decl_here; |
| 747 | } else { |
| 748 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 749 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 750 | Diag(PointOfInstantiation, |
| 751 | diag::err_explicit_instantiation_undefined_var_template) |
| 752 | << Instantiation; |
| 753 | Instantiation->setInvalidDecl(); |
| 754 | } else |
| 755 | Diag(PointOfInstantiation, |
| 756 | diag::err_explicit_instantiation_undefined_member) |
| 757 | << /*static data member*/ 2 << Instantiation->getDeclName() |
| 758 | << Instantiation->getDeclContext(); |
| 759 | Note = diag::note_explicit_instantiation_here; |
| 760 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 761 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 762 | if (Note) // Diagnostics were emitted. |
| 763 | Diag(Pattern->getLocation(), Note.getValue()); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 764 | |
| 765 | // In general, Instantiation isn't marked invalid to get more than one |
| 766 | // error for multiple undefined instantiations. But the code that does |
| 767 | // explicit declaration -> explicit definition conversion can't handle |
| 768 | // invalid declarations, so mark as invalid in that case. |
| 769 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 770 | Instantiation->setInvalidDecl(); |
| 771 | return true; |
| 772 | } |
| 773 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 774 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 775 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 776 | /// declaration at location Loc. Returns true to indicate that this is |
| 777 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 778 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 779 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 780 | |
| 781 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 782 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 783 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 784 | |
| 785 | // C++ [temp.local]p4: |
| 786 | // A template-parameter shall not be redeclared within its |
| 787 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 789 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 790 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 793 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 794 | /// the parameter D to reference the templated declaration and return a pointer |
| 795 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 796 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 797 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 798 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 799 | return Temp; |
| 800 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 801 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 804 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 805 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 806 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 807 | "Only template template arguments can be pack expansions here"); |
| 808 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 809 | "Template template argument pack expansion without packs"); |
| 810 | ParsedTemplateArgument Result(*this); |
| 811 | Result.EllipsisLoc = EllipsisLoc; |
| 812 | return Result; |
| 813 | } |
| 814 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 815 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 816 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 817 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 818 | switch (Arg.getKind()) { |
| 819 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 820 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 821 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 822 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 823 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 824 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 825 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 826 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 827 | case ParsedTemplateArgument::NonType: { |
| 828 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 829 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 830 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 832 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 833 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 834 | TemplateArgument TArg; |
| 835 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 836 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 837 | else |
| 838 | TArg = Template; |
| 839 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 840 | Arg.getScopeSpec().getWithLocInContext( |
| 841 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 842 | Arg.getLocation(), |
| 843 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 844 | } |
| 845 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 846 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 847 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 848 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 849 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 850 | /// Translates template arguments as provided by the parser |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 851 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 852 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 853 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 854 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 855 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 856 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 857 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 858 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 859 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 860 | SourceLocation Loc, |
| 861 | IdentifierInfo *Name) { |
| 862 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 863 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 864 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 865 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 866 | } |
| 867 | |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 868 | /// Convert a parsed type into a parsed template argument. This is mostly |
| 869 | /// trivial, except that we may have parsed a C++17 deduced class template |
| 870 | /// specialization type, in which case we should form a template template |
| 871 | /// argument instead of a type template argument. |
| 872 | ParsedTemplateArgument Sema::ActOnTemplateTypeArgument(TypeResult ParsedType) { |
| 873 | TypeSourceInfo *TInfo; |
| 874 | QualType T = GetTypeFromParser(ParsedType.get(), &TInfo); |
| 875 | if (T.isNull()) |
| 876 | return ParsedTemplateArgument(); |
| 877 | assert(TInfo && "template argument with no location"); |
| 878 | |
| 879 | // If we might have formed a deduced template specialization type, convert |
| 880 | // it to a template template argument. |
| 881 | if (getLangOpts().CPlusPlus17) { |
| 882 | TypeLoc TL = TInfo->getTypeLoc(); |
| 883 | SourceLocation EllipsisLoc; |
| 884 | if (auto PET = TL.getAs<PackExpansionTypeLoc>()) { |
| 885 | EllipsisLoc = PET.getEllipsisLoc(); |
| 886 | TL = PET.getPatternLoc(); |
| 887 | } |
| 888 | |
| 889 | CXXScopeSpec SS; |
| 890 | if (auto ET = TL.getAs<ElaboratedTypeLoc>()) { |
| 891 | SS.Adopt(ET.getQualifierLoc()); |
| 892 | TL = ET.getNamedTypeLoc(); |
| 893 | } |
| 894 | |
| 895 | if (auto DTST = TL.getAs<DeducedTemplateSpecializationTypeLoc>()) { |
| 896 | TemplateName Name = DTST.getTypePtr()->getTemplateName(); |
| 897 | if (SS.isSet()) |
| 898 | Name = Context.getQualifiedTemplateName(SS.getScopeRep(), |
| 899 | /*HasTemplateKeyword*/ false, |
| 900 | Name.getAsTemplateDecl()); |
| 901 | ParsedTemplateArgument Result(SS, TemplateTy::make(Name), |
| 902 | DTST.getTemplateNameLoc()); |
| 903 | if (EllipsisLoc.isValid()) |
| 904 | Result = Result.getTemplatePackExpansion(EllipsisLoc); |
| 905 | return Result; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | // This is a normal type template argument. Note, if the type template |
| 910 | // argument is an injected-class-name for a template, it has a dual nature |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 911 | // and can be used as either a type or a template. We handle that in |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 912 | // convertTypeTemplateArgumentToTemplate. |
| 913 | return ParsedTemplateArgument(ParsedTemplateArgument::Type, |
| 914 | ParsedType.get().getAsOpaquePtr(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 915 | TInfo->getTypeLoc().getBeginLoc()); |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 918 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 919 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 920 | /// the keyword "typename" was used to declare the type parameter |
| 921 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 922 | /// "class" or "typename" keyword. ParamName is the name of the |
| 923 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 924 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 925 | /// If the type parameter has a default argument, it will be added |
| 926 | /// later via ActOnTypeParameterDefault. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 927 | NamedDecl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 928 | SourceLocation EllipsisLoc, |
| 929 | SourceLocation KeyLoc, |
| 930 | IdentifierInfo *ParamName, |
| 931 | SourceLocation ParamNameLoc, |
| 932 | unsigned Depth, unsigned Position, |
| 933 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 934 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | assert(S->isTemplateParamScope() && |
| 936 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 937 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 938 | SourceLocation Loc = ParamNameLoc; |
| 939 | if (!ParamName) |
| 940 | Loc = KeyLoc; |
| 941 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 942 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 943 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 944 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 945 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 946 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 947 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 948 | |
| 949 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 950 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 951 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 952 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 953 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 954 | IdResolver.AddDecl(Param); |
| 955 | } |
| 956 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 957 | // C++0x [temp.param]p9: |
| 958 | // A default template-argument may be specified for any kind of |
| 959 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 960 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 961 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 962 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 965 | // Handle the default argument, if provided. |
| 966 | if (DefaultArg) { |
| 967 | TypeSourceInfo *DefaultTInfo; |
| 968 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 970 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 971 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 972 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 973 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 974 | UPPC_DefaultArgument)) |
| 975 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 976 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 977 | // Check the template argument itself. |
| 978 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 979 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 980 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 981 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 982 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 983 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 984 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 985 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 986 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 989 | /// Check that the type of a non-type template parameter is |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 990 | /// well-formed. |
| 991 | /// |
| 992 | /// \returns the (possibly-promoted) parameter type if valid; |
| 993 | /// otherwise, produces a diagnostic and returns a NULL type. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 994 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
| 995 | SourceLocation Loc) { |
| 996 | if (TSI->getType()->isUndeducedType()) { |
Erik Pilkington | 9f9462a | 2018-08-07 22:59:02 +0000 | [diff] [blame] | 997 | // C++17 [temp.dep.expr]p3: |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 998 | // An id-expression is type-dependent if it contains |
| 999 | // - an identifier associated by name lookup with a non-type |
| 1000 | // template-parameter declared with a type that contains a |
| 1001 | // placeholder type (7.1.7.4), |
| 1002 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
| 1003 | } |
| 1004 | |
| 1005 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
| 1006 | } |
| 1007 | |
| 1008 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
| 1009 | SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 1010 | // We don't allow variably-modified types as the type of non-type template |
| 1011 | // parameters. |
| 1012 | if (T->isVariablyModifiedType()) { |
| 1013 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 1014 | << T; |
| 1015 | return QualType(); |
| 1016 | } |
| 1017 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1018 | // C++ [temp.param]p4: |
| 1019 | // |
| 1020 | // A non-type template-parameter shall have one of the following |
| 1021 | // (optionally cv-qualified) types: |
| 1022 | // |
| 1023 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1024 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1026 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1028 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1029 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1030 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1031 | // -- std::nullptr_t. |
| 1032 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1033 | // If T is a dependent type, we can't do the check now, so we |
| 1034 | // assume that it is well-formed. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1035 | T->isDependentType() || |
| 1036 | // Allow use of auto in template parameter declarations. |
| 1037 | T->isUndeducedType()) { |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 1038 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 1039 | // are ignored when determining its type. |
| 1040 | return T.getUnqualifiedType(); |
| 1041 | } |
| 1042 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1043 | // C++ [temp.param]p8: |
| 1044 | // |
| 1045 | // A non-type template-parameter of type "array of T" or |
| 1046 | // "function returning T" is adjusted to be of type "pointer to |
| 1047 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1048 | else if (T->isArrayType() || T->isFunctionType()) |
| 1049 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1050 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1051 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 1052 | << T; |
| 1053 | |
| 1054 | return QualType(); |
| 1055 | } |
| 1056 | |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1057 | NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1058 | unsigned Depth, |
| 1059 | unsigned Position, |
| 1060 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1061 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1062 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1063 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1064 | // Check that we have valid decl-specifiers specified. |
| 1065 | auto CheckValidDeclSpecifiers = [this, &D] { |
| 1066 | // C++ [temp.param] |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1067 | // p1 |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 1068 | // template-parameter: |
| 1069 | // ... |
| 1070 | // parameter-declaration |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1071 | // p2 |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1072 | // ... A storage class shall not be specified in a template-parameter |
| 1073 | // declaration. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1074 | // [dcl.typedef]p1: |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1075 | // The typedef specifier [...] shall not be used in the decl-specifier-seq |
| 1076 | // of a parameter-declaration |
| 1077 | const DeclSpec &DS = D.getDeclSpec(); |
| 1078 | auto EmitDiag = [this](SourceLocation Loc) { |
| 1079 | Diag(Loc, diag::err_invalid_decl_specifier_in_nontype_parm) |
| 1080 | << FixItHint::CreateRemoval(Loc); |
| 1081 | }; |
| 1082 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) |
| 1083 | EmitDiag(DS.getStorageClassSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1084 | |
Sam McCall | 1371cba | 2017-12-22 07:09:51 +0000 | [diff] [blame] | 1085 | if (DS.getThreadStorageClassSpec() != TSCS_unspecified) |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1086 | EmitDiag(DS.getThreadStorageClassSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1087 | |
| 1088 | // [dcl.inline]p1: |
| 1089 | // The inline specifier can be applied only to the declaration or |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1090 | // definition of a variable or function. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1091 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1092 | if (DS.isInlineSpecified()) |
| 1093 | EmitDiag(DS.getInlineSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1094 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1095 | // [dcl.constexpr]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1096 | // The constexpr specifier shall be applied only to the definition of a |
| 1097 | // variable or variable template or the declaration of a function or |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1098 | // function template. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1099 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1100 | if (DS.isConstexprSpecified()) |
| 1101 | EmitDiag(DS.getConstexprSpecLoc()); |
| 1102 | |
| 1103 | // [dcl.fct.spec]p1: |
| 1104 | // Function-specifiers can be used only in function declarations. |
| 1105 | |
| 1106 | if (DS.isVirtualSpecified()) |
| 1107 | EmitDiag(DS.getVirtualSpecLoc()); |
| 1108 | |
| 1109 | if (DS.isExplicitSpecified()) |
| 1110 | EmitDiag(DS.getExplicitSpecLoc()); |
| 1111 | |
| 1112 | if (DS.isNoreturnSpecified()) |
| 1113 | EmitDiag(DS.getNoreturnSpecLoc()); |
| 1114 | }; |
| 1115 | |
| 1116 | CheckValidDeclSpecifiers(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1117 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1118 | if (TInfo->getType()->isUndeducedType()) { |
| 1119 | Diag(D.getIdentifierLoc(), |
| 1120 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 1121 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
| 1122 | } |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1124 | assert(S->isTemplateParamScope() && |
| 1125 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1126 | bool Invalid = false; |
| 1127 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1128 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1129 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1130 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 1131 | Invalid = true; |
| 1132 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1133 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1134 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1135 | bool IsParameterPack = D.hasEllipsis(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1136 | NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create( |
| 1137 | Context, Context.getTranslationUnitDecl(), D.getBeginLoc(), |
| 1138 | D.getIdentifierLoc(), Depth, Position, ParamName, T, IsParameterPack, |
| 1139 | TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1140 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1141 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1142 | if (Invalid) |
| 1143 | Param->setInvalidDecl(); |
| 1144 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1145 | if (ParamName) { |
| 1146 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 1147 | ParamName); |
| 1148 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1149 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1150 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1151 | IdResolver.AddDecl(Param); |
| 1152 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1153 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1154 | // C++0x [temp.param]p9: |
| 1155 | // A default template-argument may be specified for any kind of |
| 1156 | // template-parameter that is not a template parameter pack. |
| 1157 | if (Default && IsParameterPack) { |
| 1158 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1159 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1162 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1163 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1164 | // Check for unexpanded parameter packs. |
| 1165 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 1166 | return Param; |
| 1167 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1168 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1169 | ExprResult DefaultRes = |
| 1170 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1171 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1172 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1173 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1174 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1175 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1176 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1177 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1178 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1179 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1180 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1181 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1182 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1183 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1184 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1185 | /// has been parsed. S is the current scope. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1186 | NamedDecl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1187 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1188 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1189 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1190 | IdentifierInfo *Name, |
| 1191 | SourceLocation NameLoc, |
| 1192 | unsigned Depth, |
| 1193 | unsigned Position, |
| 1194 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1195 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1196 | assert(S->isTemplateParamScope() && |
| 1197 | "Template template parameter not in template parameter scope!"); |
| 1198 | |
| 1199 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1200 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1201 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 1202 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1203 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 1204 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1205 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1206 | Param->setAccess(AS_public); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1207 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1208 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1209 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1210 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1211 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 1212 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1213 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1214 | IdResolver.AddDecl(Param); |
| 1215 | } |
| 1216 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1217 | if (Params->size() == 0) { |
| 1218 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 1219 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 1220 | Param->setInvalidDecl(); |
| 1221 | } |
| 1222 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1223 | // C++0x [temp.param]p9: |
| 1224 | // A default template-argument may be specified for any kind of |
| 1225 | // template-parameter that is not a template parameter pack. |
| 1226 | if (IsParameterPack && !Default.isInvalid()) { |
| 1227 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 1228 | Default = ParsedTemplateArgument(); |
| 1229 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1230 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1231 | if (!Default.isInvalid()) { |
| 1232 | // Check only that we have a template template argument. We don't want to |
| 1233 | // try to check well-formedness now, because our template template parameter |
| 1234 | // might have dependent types in its template parameters, which we wouldn't |
| 1235 | // be able to match now. |
| 1236 | // |
| 1237 | // If none of the template template parameter's template arguments mention |
| 1238 | // other template parameters, we could actually perform more checking here. |
| 1239 | // However, it isn't worth doing. |
| 1240 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 1241 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 1242 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1243 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1244 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1245 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1246 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1247 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1248 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1249 | DefaultArg.getArgument().getAsTemplate(), |
| 1250 | UPPC_DefaultArgument)) |
| 1251 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1252 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1253 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1254 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1255 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1256 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1259 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 1260 | /// constrained by RequiresClause, that contains the template parameters in |
| 1261 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1262 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1263 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 1264 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1266 | SourceLocation LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1267 | ArrayRef<NamedDecl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1268 | SourceLocation RAngleLoc, |
| 1269 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1270 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1271 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1272 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1273 | return TemplateParameterList::Create( |
| 1274 | Context, TemplateLoc, LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1275 | llvm::makeArrayRef(Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 1276 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1277 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1278 | |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 1279 | static void SetNestedNameSpecifier(Sema &S, TagDecl *T, |
| 1280 | const CXXScopeSpec &SS) { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1281 | if (SS.isSet()) |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 1282 | T->setQualifierInfo(SS.getWithLocInContext(S.Context)); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1285 | DeclResult Sema::CheckClassTemplate( |
| 1286 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
| 1287 | CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc, |
| 1288 | const ParsedAttributesView &Attr, TemplateParameterList *TemplateParams, |
| 1289 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
| 1290 | SourceLocation FriendLoc, unsigned NumOuterTemplateParamLists, |
| 1291 | TemplateParameterList **OuterTemplateParamLists, SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1292 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1293 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1294 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1295 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1296 | |
| 1297 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1298 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1299 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1300 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1301 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 1302 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1303 | |
| 1304 | // There is no such thing as an unnamed class template. |
| 1305 | if (!Name) { |
| 1306 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1307 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1310 | // Find any previous declaration with this name. For a friend with no |
| 1311 | // scope explicitly specified, we only look for tag declarations (per |
| 1312 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1313 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1314 | LookupResult Previous(*this, Name, NameLoc, |
| 1315 | (SS.isEmpty() && TUK == TUK_Friend) |
| 1316 | ? LookupTagName : LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1317 | forRedeclarationInCurContext()); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1318 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 1319 | SemanticContext = computeDeclContext(SS, true); |
| 1320 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1321 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 1322 | // in the AST, and historically we have just ignored such friend |
| 1323 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1324 | Diag(NameLoc, TUK == TUK_Friend |
| 1325 | ? diag::warn_template_qualified_friend_ignored |
| 1326 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1327 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1328 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1329 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1331 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 1332 | return true; |
| 1333 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1334 | // If we're adding a template to a dependent context, we may need to |
| 1335 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 1336 | // now that we know what the current instantiation is. |
| 1337 | if (SemanticContext->isDependentContext()) { |
| 1338 | ContextRAII SavedContext(*this, SemanticContext); |
| 1339 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 1340 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1341 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 1342 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc, false); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1343 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1344 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1345 | } else { |
| 1346 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 1347 | |
| 1348 | // C++14 [class.mem]p14: |
| 1349 | // If T is the name of a class, then each of the following shall have a |
| 1350 | // name different from T: |
| 1351 | // -- every member template of class T |
| 1352 | if (TUK != TUK_Friend && |
| 1353 | DiagnoseClassNameShadow(SemanticContext, |
| 1354 | DeclarationNameInfo(Name, NameLoc))) |
| 1355 | return true; |
| 1356 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1357 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1358 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1360 | if (Previous.isAmbiguous()) |
| 1361 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1362 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1363 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1364 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1365 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1366 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1367 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1368 | // Maybe we will complain about the shadowed template parameter. |
| 1369 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1370 | // Just pretend that we didn't see the previous declaration. |
| 1371 | PrevDecl = nullptr; |
| 1372 | } |
| 1373 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1374 | // If there is a previous declaration with the same name, check |
| 1375 | // whether this is a valid redeclaration. |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1376 | ClassTemplateDecl *PrevClassTemplate = |
| 1377 | dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1378 | |
| 1379 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1380 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1381 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1382 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1383 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1384 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1385 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1386 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1387 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1388 | PrevClassTemplate |
| 1389 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1390 | ->getSpecializedTemplate(); |
| 1391 | } |
| 1392 | } |
| 1393 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1394 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1395 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1396 | // [...] When looking for a prior declaration of a class or a function |
| 1397 | // 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] | 1398 | // function is neither a qualified name nor a template-id, scopes outside |
| 1399 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1400 | if (!SS.isSet()) { |
| 1401 | DeclContext *OutermostContext = CurContext; |
| 1402 | while (!OutermostContext->isFileContext()) |
| 1403 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1404 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 1405 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1406 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1407 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1408 | SemanticContext = PrevDecl->getDeclContext(); |
| 1409 | } else { |
| 1410 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1411 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1412 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1413 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1414 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1415 | |
| 1416 | // Check that the chosen semantic context doesn't already contain a |
| 1417 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1418 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1419 | DeclContext *LookupContext = SemanticContext; |
| 1420 | while (LookupContext->isTransparentContext()) |
| 1421 | LookupContext = LookupContext->getLookupParent(); |
| 1422 | LookupQualifiedName(Previous, LookupContext); |
| 1423 | |
| 1424 | if (Previous.isAmbiguous()) |
| 1425 | return true; |
| 1426 | |
| 1427 | if (Previous.begin() != Previous.end()) |
| 1428 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1429 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1430 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1431 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1432 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1433 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1434 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1435 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1436 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1437 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1438 | if (SS.isEmpty() && |
| 1439 | !(PrevClassTemplate && |
| 1440 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1441 | SemanticContext->getRedeclContext()))) { |
| 1442 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1443 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1444 | diag::note_using_decl_target); |
| 1445 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1446 | // Recover by ignoring the old declaration. |
| 1447 | PrevDecl = PrevClassTemplate = nullptr; |
| 1448 | } |
| 1449 | } |
| 1450 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1451 | // TODO Memory management; associated constraints are not always stored. |
| 1452 | Expr *const CurAC = formAssociatedConstraints(TemplateParams, nullptr); |
| 1453 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1454 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1455 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1456 | // for a friend in a dependent context: the template parameter list itself |
| 1457 | // could be dependent. |
| 1458 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1459 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1460 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1461 | /*Complain=*/true, |
| 1462 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1463 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1464 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1465 | // Check for matching associated constraints on redeclarations. |
| 1466 | const Expr *const PrevAC = PrevClassTemplate->getAssociatedConstraints(); |
| 1467 | const bool RedeclACMismatch = [&] { |
| 1468 | if (!(CurAC || PrevAC)) |
| 1469 | return false; // Nothing to check; no mismatch. |
| 1470 | if (CurAC && PrevAC) { |
| 1471 | llvm::FoldingSetNodeID CurACInfo, PrevACInfo; |
| 1472 | CurAC->Profile(CurACInfo, Context, /*Canonical=*/true); |
| 1473 | PrevAC->Profile(PrevACInfo, Context, /*Canonical=*/true); |
| 1474 | if (CurACInfo == PrevACInfo) |
| 1475 | return false; // All good; no mismatch. |
| 1476 | } |
| 1477 | return true; |
| 1478 | }(); |
| 1479 | |
| 1480 | if (RedeclACMismatch) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1481 | Diag(CurAC ? CurAC->getBeginLoc() : NameLoc, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1482 | diag::err_template_different_associated_constraints); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1483 | Diag(PrevAC ? PrevAC->getBeginLoc() : PrevClassTemplate->getLocation(), |
| 1484 | diag::note_template_prev_declaration) |
| 1485 | << /*declaration*/ 0; |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1486 | return true; |
| 1487 | } |
| 1488 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1489 | // C++ [temp.class]p4: |
| 1490 | // In a redeclaration, partial specialization, explicit |
| 1491 | // specialization or explicit instantiation of a class template, |
| 1492 | // the class-key shall agree in kind with the original class |
| 1493 | // template declaration (7.1.5.3). |
| 1494 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1495 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1496 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1498 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1499 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1500 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1501 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1504 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1505 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1506 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1507 | // If we have a prior definition that is not visible, treat this as |
| 1508 | // simply making that previous definition visible. |
| 1509 | NamedDecl *Hidden = nullptr; |
| 1510 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1511 | SkipBody->ShouldSkip = true; |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1512 | SkipBody->Previous = Def; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1513 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1514 | assert(Tmpl && "original definition of a class template is not a " |
| 1515 | "class template?"); |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 1516 | makeMergedDefinitionVisible(Hidden); |
| 1517 | makeMergedDefinitionVisible(Tmpl); |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1518 | } else { |
| 1519 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1520 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1521 | // FIXME: Would it make sense to try to "forget" the previous |
| 1522 | // definition, as part of error recovery? |
| 1523 | return true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1524 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1525 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1526 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1527 | } else if (PrevDecl) { |
| 1528 | // C++ [temp]p5: |
| 1529 | // A class template shall not have the same name as any other |
| 1530 | // template, class, function, object, enumeration, enumerator, |
| 1531 | // namespace, or type in the same scope (3.3), except as specified |
| 1532 | // in (14.5.4). |
| 1533 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1534 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1535 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1538 | // Check the template parameter list of this declaration, possibly |
| 1539 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1540 | // template declaration. Skip this check for a friend in a dependent |
| 1541 | // context, because the template parameter list might be dependent. |
| 1542 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1543 | CheckTemplateParameterList( |
| 1544 | TemplateParams, |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1545 | PrevClassTemplate |
| 1546 | ? PrevClassTemplate->getMostRecentDecl()->getTemplateParameters() |
| 1547 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1548 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1549 | SemanticContext->isDependentContext()) |
| 1550 | ? TPC_ClassTemplateMember |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1551 | : TUK == TUK_Friend ? TPC_FriendClassTemplate : TPC_ClassTemplate, |
| 1552 | SkipBody)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1553 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1555 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1556 | // 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] | 1557 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1558 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1559 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1560 | : diag::err_member_decl_does_not_match) |
| 1561 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1562 | Invalid = true; |
| 1563 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1566 | // If this is a templated friend in a dependent context we should not put it |
| 1567 | // on the redecl chain. In some cases, the templated friend can be the most |
| 1568 | // recent declaration tricking the template instantiator to make substitutions |
| 1569 | // there. |
| 1570 | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
| 1571 | bool ShouldAddRedecl |
| 1572 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
| 1573 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1575 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1576 | PrevClassTemplate && ShouldAddRedecl ? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1577 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1578 | /*DelayTypeCreation=*/true); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 1579 | SetNestedNameSpecifier(*this, NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1580 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1581 | NewClass->setTemplateParameterListsInfo( |
| 1582 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1583 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1584 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1585 | // Add alignment attributes if necessary; these attributes are checked when |
| 1586 | // the ASTContext lays out the structure. |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1587 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1588 | AddAlignmentAttributesForRecord(NewClass); |
| 1589 | AddMsStructLayoutForRecord(NewClass); |
| 1590 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1591 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1592 | // Attach the associated constraints when the declaration will not be part of |
| 1593 | // a decl chain. |
| 1594 | Expr *const ACtoAttach = |
| 1595 | PrevClassTemplate && ShouldAddRedecl ? nullptr : CurAC; |
| 1596 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1597 | ClassTemplateDecl *NewTemplate |
| 1598 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1599 | DeclarationName(Name), TemplateParams, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1600 | NewClass, ACtoAttach); |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1601 | |
| 1602 | if (ShouldAddRedecl) |
| 1603 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
| 1604 | |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1605 | NewClass->setDescribedClassTemplate(NewTemplate); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1606 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1607 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1608 | NewTemplate->setModulePrivate(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1609 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1610 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1611 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1612 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1613 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1614 | (void)T; |
| 1615 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1616 | // 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] | 1617 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1618 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1619 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1620 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1621 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1622 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1623 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1624 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1626 | // Set the lexical context of these templates |
| 1627 | NewClass->setLexicalDeclContext(CurContext); |
| 1628 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1629 | |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1630 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1631 | NewClass->startDefinition(); |
| 1632 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1633 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1634 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1635 | if (PrevClassTemplate) |
| 1636 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1637 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1638 | AddPushedVisibilityAttribute(NewClass); |
| 1639 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1640 | if (TUK != TUK_Friend) { |
| 1641 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1642 | Scope *Outer = S; |
| 1643 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1644 | Outer = Outer->getParent(); |
| 1645 | PushOnScopeChains(NewTemplate, Outer); |
| 1646 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1647 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1648 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1649 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1650 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1651 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1652 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1653 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1654 | // Friend templates are visible in fairly strange ways. |
| 1655 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1656 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1657 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1658 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1659 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1660 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1661 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1662 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1663 | FriendDecl *Friend = FriendDecl::Create( |
| 1664 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1665 | Friend->setAccess(AS_public); |
| 1666 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1667 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1668 | |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1669 | if (PrevClassTemplate) |
| 1670 | CheckRedeclarationModuleOwnership(NewTemplate, PrevClassTemplate); |
| 1671 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1672 | if (Invalid) { |
| 1673 | NewTemplate->setInvalidDecl(); |
| 1674 | NewClass->setInvalidDecl(); |
| 1675 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1676 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1677 | ActOnDocumentableDecl(NewTemplate); |
| 1678 | |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1679 | if (SkipBody && SkipBody->ShouldSkip) |
| 1680 | return SkipBody->Previous; |
| 1681 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1682 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1685 | namespace { |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1686 | /// Tree transform to "extract" a transformed type from a class template's |
| 1687 | /// constructor to a deduction guide. |
| 1688 | class ExtractTypeForDeductionGuide |
| 1689 | : public TreeTransform<ExtractTypeForDeductionGuide> { |
| 1690 | public: |
| 1691 | typedef TreeTransform<ExtractTypeForDeductionGuide> Base; |
| 1692 | ExtractTypeForDeductionGuide(Sema &SemaRef) : Base(SemaRef) {} |
| 1693 | |
| 1694 | TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); } |
| 1695 | |
| 1696 | QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) { |
| 1697 | return TransformType( |
| 1698 | TLB, |
| 1699 | TL.getTypedefNameDecl()->getTypeSourceInfo()->getTypeLoc()); |
| 1700 | } |
| 1701 | }; |
| 1702 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1703 | /// Transform to convert portions of a constructor declaration into the |
| 1704 | /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1. |
| 1705 | struct ConvertConstructorToDeductionGuideTransform { |
| 1706 | ConvertConstructorToDeductionGuideTransform(Sema &S, |
| 1707 | ClassTemplateDecl *Template) |
| 1708 | : SemaRef(S), Template(Template) {} |
| 1709 | |
| 1710 | Sema &SemaRef; |
| 1711 | ClassTemplateDecl *Template; |
| 1712 | |
| 1713 | DeclContext *DC = Template->getDeclContext(); |
| 1714 | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
| 1715 | DeclarationName DeductionGuideName = |
| 1716 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
| 1717 | |
| 1718 | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
| 1719 | |
| 1720 | // Index adjustment to apply to convert depth-1 template parameters into |
| 1721 | // depth-0 template parameters. |
| 1722 | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
| 1723 | |
| 1724 | /// Transform a constructor declaration into a deduction guide. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1725 | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
| 1726 | CXXConstructorDecl *CD) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1727 | SmallVector<TemplateArgument, 16> SubstArgs; |
| 1728 | |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1729 | LocalInstantiationScope Scope(SemaRef); |
| 1730 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1731 | // C++ [over.match.class.deduct]p1: |
| 1732 | // -- For each constructor of the class template designated by the |
| 1733 | // template-name, a function template with the following properties: |
| 1734 | |
| 1735 | // -- The template parameters are the template parameters of the class |
| 1736 | // template followed by the template parameters (including default |
| 1737 | // template arguments) of the constructor, if any. |
| 1738 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 1739 | if (FTD) { |
| 1740 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
| 1741 | SmallVector<NamedDecl *, 16> AllParams; |
| 1742 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
| 1743 | AllParams.insert(AllParams.begin(), |
| 1744 | TemplateParams->begin(), TemplateParams->end()); |
| 1745 | SubstArgs.reserve(InnerParams->size()); |
| 1746 | |
| 1747 | // Later template parameters could refer to earlier ones, so build up |
| 1748 | // a list of substituted template arguments as we go. |
| 1749 | for (NamedDecl *Param : *InnerParams) { |
| 1750 | MultiLevelTemplateArgumentList Args; |
| 1751 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1752 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1753 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
| 1754 | if (!NewParam) |
| 1755 | return nullptr; |
| 1756 | AllParams.push_back(NewParam); |
| 1757 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
| 1758 | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
| 1759 | } |
| 1760 | TemplateParams = TemplateParameterList::Create( |
| 1761 | SemaRef.Context, InnerParams->getTemplateLoc(), |
| 1762 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
| 1763 | /*FIXME: RequiresClause*/ nullptr); |
| 1764 | } |
| 1765 | |
| 1766 | // If we built a new template-parameter-list, track that we need to |
| 1767 | // substitute references to the old parameters into references to the |
| 1768 | // new ones. |
| 1769 | MultiLevelTemplateArgumentList Args; |
| 1770 | if (FTD) { |
| 1771 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1772 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1775 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1776 | .getAsAdjusted<FunctionProtoTypeLoc>(); |
| 1777 | assert(FPTL && "no prototype for constructor declaration"); |
| 1778 | |
| 1779 | // Transform the type of the function, adjusting the return type and |
| 1780 | // replacing references to the old parameters with references to the |
| 1781 | // new ones. |
| 1782 | TypeLocBuilder TLB; |
| 1783 | SmallVector<ParmVarDecl*, 8> Params; |
| 1784 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args); |
| 1785 | if (NewType.isNull()) |
| 1786 | return nullptr; |
| 1787 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
| 1788 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1789 | return buildDeductionGuide(TemplateParams, CD->isExplicit(), NewTInfo, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1790 | CD->getBeginLoc(), CD->getLocation(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1791 | CD->getEndLoc()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | /// Build a deduction guide with the specified parameter types. |
| 1795 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
| 1796 | SourceLocation Loc = Template->getLocation(); |
| 1797 | |
| 1798 | // Build the requested type. |
| 1799 | FunctionProtoType::ExtProtoInfo EPI; |
| 1800 | EPI.HasTrailingReturn = true; |
| 1801 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
| 1802 | DeductionGuideName, EPI); |
| 1803 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
| 1804 | |
| 1805 | FunctionProtoTypeLoc FPTL = |
| 1806 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
| 1807 | |
| 1808 | // Build the parameters, needed during deduction / substitution. |
| 1809 | SmallVector<ParmVarDecl*, 4> Params; |
| 1810 | for (auto T : ParamTypes) { |
| 1811 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
| 1812 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
| 1813 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
| 1814 | NewParam->setScopeInfo(0, Params.size()); |
| 1815 | FPTL.setParam(Params.size(), NewParam); |
| 1816 | Params.push_back(NewParam); |
| 1817 | } |
| 1818 | |
| 1819 | return buildDeductionGuide(Template->getTemplateParameters(), false, TSI, |
| 1820 | Loc, Loc, Loc); |
| 1821 | } |
| 1822 | |
| 1823 | private: |
| 1824 | /// Transform a constructor template parameter into a deduction guide template |
| 1825 | /// parameter, rebuilding any internal references to earlier parameters and |
| 1826 | /// renumbering as we go. |
| 1827 | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
| 1828 | MultiLevelTemplateArgumentList &Args) { |
| 1829 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
| 1830 | // TemplateTypeParmDecl's index cannot be changed after creation, so |
| 1831 | // substitute it directly. |
| 1832 | auto *NewTTP = TemplateTypeParmDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1833 | SemaRef.Context, DC, TTP->getBeginLoc(), TTP->getLocation(), |
| 1834 | /*Depth*/ 0, Depth1IndexAdjustment + TTP->getIndex(), |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1835 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
| 1836 | TTP->isParameterPack()); |
| 1837 | if (TTP->hasDefaultArgument()) { |
| 1838 | TypeSourceInfo *InstantiatedDefaultArg = |
| 1839 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
| 1840 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
| 1841 | if (InstantiatedDefaultArg) |
| 1842 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
| 1843 | } |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1844 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
| 1845 | NewTTP); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1846 | return NewTTP; |
| 1847 | } |
| 1848 | |
| 1849 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
| 1850 | return transformTemplateParameterImpl(TTP, Args); |
| 1851 | |
| 1852 | return transformTemplateParameterImpl( |
| 1853 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
| 1854 | } |
| 1855 | template<typename TemplateParmDecl> |
| 1856 | TemplateParmDecl * |
| 1857 | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
| 1858 | MultiLevelTemplateArgumentList &Args) { |
| 1859 | // Ask the template instantiator to do the heavy lifting for us, then adjust |
| 1860 | // the index of the parameter once it's done. |
| 1861 | auto *NewParam = |
| 1862 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
| 1863 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
| 1864 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
| 1865 | return NewParam; |
| 1866 | } |
| 1867 | |
| 1868 | QualType transformFunctionProtoType(TypeLocBuilder &TLB, |
| 1869 | FunctionProtoTypeLoc TL, |
| 1870 | SmallVectorImpl<ParmVarDecl*> &Params, |
| 1871 | MultiLevelTemplateArgumentList &Args) { |
| 1872 | SmallVector<QualType, 4> ParamTypes; |
| 1873 | const FunctionProtoType *T = TL.getTypePtr(); |
| 1874 | |
| 1875 | // -- The types of the function parameters are those of the constructor. |
| 1876 | for (auto *OldParam : TL.getParams()) { |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1877 | ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1878 | if (!NewParam) |
| 1879 | return QualType(); |
| 1880 | ParamTypes.push_back(NewParam->getType()); |
| 1881 | Params.push_back(NewParam); |
| 1882 | } |
| 1883 | |
| 1884 | // -- The return type is the class template specialization designated by |
| 1885 | // the template-name and template arguments corresponding to the |
| 1886 | // template parameters obtained from the class template. |
| 1887 | // |
| 1888 | // We use the injected-class-name type of the primary template instead. |
| 1889 | // This has the convenient property that it is different from any type that |
| 1890 | // the user can write in a deduction-guide (because they cannot enter the |
| 1891 | // context of the template), so implicit deduction guides can never collide |
| 1892 | // with explicit ones. |
| 1893 | QualType ReturnType = DeducedType; |
| 1894 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
| 1895 | |
| 1896 | // Resolving a wording defect, we also inherit the variadicness of the |
| 1897 | // constructor. |
| 1898 | FunctionProtoType::ExtProtoInfo EPI; |
| 1899 | EPI.Variadic = T->isVariadic(); |
| 1900 | EPI.HasTrailingReturn = true; |
| 1901 | |
| 1902 | QualType Result = SemaRef.BuildFunctionType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1903 | ReturnType, ParamTypes, TL.getBeginLoc(), DeductionGuideName, EPI); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1904 | if (Result.isNull()) |
| 1905 | return QualType(); |
| 1906 | |
| 1907 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
| 1908 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
| 1909 | NewTL.setLParenLoc(TL.getLParenLoc()); |
| 1910 | NewTL.setRParenLoc(TL.getRParenLoc()); |
| 1911 | NewTL.setExceptionSpecRange(SourceRange()); |
| 1912 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
| 1913 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I) |
| 1914 | NewTL.setParam(I, Params[I]); |
| 1915 | |
| 1916 | return Result; |
| 1917 | } |
| 1918 | |
| 1919 | ParmVarDecl * |
| 1920 | transformFunctionTypeParam(ParmVarDecl *OldParam, |
| 1921 | MultiLevelTemplateArgumentList &Args) { |
| 1922 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
Richard Smith | 479ba8e | 2017-04-20 01:15:31 +0000 | [diff] [blame] | 1923 | TypeSourceInfo *NewDI; |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1924 | if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { |
Richard Smith | 479ba8e | 2017-04-20 01:15:31 +0000 | [diff] [blame] | 1925 | // Expand out the one and only element in each inner pack. |
| 1926 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); |
| 1927 | NewDI = |
| 1928 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, |
| 1929 | OldParam->getLocation(), OldParam->getDeclName()); |
| 1930 | if (!NewDI) return nullptr; |
| 1931 | NewDI = |
| 1932 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), |
| 1933 | PackTL.getTypePtr()->getNumExpansions()); |
| 1934 | } else |
| 1935 | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
| 1936 | OldParam->getDeclName()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1937 | if (!NewDI) |
| 1938 | return nullptr; |
| 1939 | |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1940 | // Extract the type. This (for instance) replaces references to typedef |
| 1941 | // members of the current instantiations with the definitions of those |
| 1942 | // typedefs, avoiding triggering instantiation of the deduced type during |
| 1943 | // deduction. |
| 1944 | NewDI = ExtractTypeForDeductionGuide(SemaRef).transform(NewDI); |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1945 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1946 | // Resolving a wording defect, we also inherit default arguments from the |
| 1947 | // constructor. |
| 1948 | ExprResult NewDefArg; |
| 1949 | if (OldParam->hasDefaultArg()) { |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1950 | NewDefArg = SemaRef.SubstExpr(OldParam->getDefaultArg(), Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1951 | if (NewDefArg.isInvalid()) |
| 1952 | return nullptr; |
| 1953 | } |
| 1954 | |
| 1955 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
| 1956 | OldParam->getInnerLocStart(), |
| 1957 | OldParam->getLocation(), |
| 1958 | OldParam->getIdentifier(), |
| 1959 | NewDI->getType(), |
| 1960 | NewDI, |
| 1961 | OldParam->getStorageClass(), |
| 1962 | NewDefArg.get()); |
| 1963 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
| 1964 | OldParam->getFunctionScopeIndex()); |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1965 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1966 | return NewParam; |
| 1967 | } |
| 1968 | |
| 1969 | NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams, |
| 1970 | bool Explicit, TypeSourceInfo *TInfo, |
| 1971 | SourceLocation LocStart, SourceLocation Loc, |
| 1972 | SourceLocation LocEnd) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1973 | DeclarationNameInfo Name(DeductionGuideName, Loc); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1974 | ArrayRef<ParmVarDecl *> Params = |
| 1975 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
| 1976 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1977 | // Build the implicit deduction guide template. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1978 | auto *Guide = |
| 1979 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, Explicit, |
| 1980 | Name, TInfo->getType(), TInfo, LocEnd); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1981 | Guide->setImplicit(); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1982 | Guide->setParams(Params); |
| 1983 | |
| 1984 | for (auto *Param : Params) |
| 1985 | Param->setDeclContext(Guide); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1986 | |
| 1987 | auto *GuideTemplate = FunctionTemplateDecl::Create( |
| 1988 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
| 1989 | GuideTemplate->setImplicit(); |
| 1990 | Guide->setDescribedFunctionTemplate(GuideTemplate); |
| 1991 | |
| 1992 | if (isa<CXXRecordDecl>(DC)) { |
| 1993 | Guide->setAccess(AS_public); |
| 1994 | GuideTemplate->setAccess(AS_public); |
| 1995 | } |
| 1996 | |
| 1997 | DC->addDecl(GuideTemplate); |
| 1998 | return GuideTemplate; |
| 1999 | } |
| 2000 | }; |
| 2001 | } |
| 2002 | |
| 2003 | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
| 2004 | SourceLocation Loc) { |
| 2005 | DeclContext *DC = Template->getDeclContext(); |
| 2006 | if (DC->isDependentContext()) |
| 2007 | return; |
| 2008 | |
| 2009 | ConvertConstructorToDeductionGuideTransform Transform( |
| 2010 | *this, cast<ClassTemplateDecl>(Template)); |
| 2011 | if (!isCompleteType(Loc, Transform.DeducedType)) |
| 2012 | return; |
| 2013 | |
| 2014 | // Check whether we've already declared deduction guides for this template. |
| 2015 | // FIXME: Consider storing a flag on the template to indicate this. |
| 2016 | auto Existing = DC->lookup(Transform.DeductionGuideName); |
| 2017 | for (auto *D : Existing) |
| 2018 | if (D->isImplicit()) |
| 2019 | return; |
| 2020 | |
| 2021 | // In case we were expanding a pack when we attempted to declare deduction |
| 2022 | // guides, turn off pack expansion for everything we're about to do. |
| 2023 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
| 2024 | // Create a template instantiation record to track the "instantiation" of |
| 2025 | // constructors into deduction guides. |
| 2026 | // FIXME: Add a kind for this to give more meaningful diagnostics. But can |
| 2027 | // this substitution process actually fail? |
| 2028 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
Volodymyr Sapsai | 2f649f3 | 2018-05-14 22:49:44 +0000 | [diff] [blame] | 2029 | if (BuildingDeductionGuides.isInvalid()) |
| 2030 | return; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Convert declared constructors into deduction guide templates. |
| 2033 | // FIXME: Skip constructors for which deduction must necessarily fail (those |
| 2034 | // for which some class template parameter without a default argument never |
| 2035 | // appears in a deduced context). |
| 2036 | bool AddedAny = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2037 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
| 2038 | D = D->getUnderlyingDecl(); |
| 2039 | if (D->isInvalidDecl() || D->isImplicit()) |
| 2040 | continue; |
| 2041 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 2042 | |
| 2043 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2044 | auto *CD = |
| 2045 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2046 | // Class-scope explicit specializations (MS extension) do not result in |
| 2047 | // deduction guides. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2048 | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization())) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2049 | continue; |
| 2050 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2051 | Transform.transformConstructor(FTD, CD); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2052 | AddedAny = true; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2055 | // C++17 [over.match.class.deduct] |
| 2056 | // -- If C is not defined or does not declare any constructors, an |
| 2057 | // additional function template derived as above from a hypothetical |
| 2058 | // constructor C(). |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2059 | if (!AddedAny) |
| 2060 | Transform.buildSimpleDeductionGuide(None); |
| 2061 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2062 | // -- An additional function template derived as above from a hypothetical |
| 2063 | // constructor C(C), called the copy deduction candidate. |
| 2064 | cast<CXXDeductionGuideDecl>( |
| 2065 | cast<FunctionTemplateDecl>( |
| 2066 | Transform.buildSimpleDeductionGuide(Transform.DeducedType)) |
| 2067 | ->getTemplatedDecl()) |
| 2068 | ->setIsCopyDeductionCandidate(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2071 | /// Diagnose the presence of a default template argument on a |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2072 | /// template parameter, which is ill-formed in certain contexts. |
| 2073 | /// |
| 2074 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2075 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2076 | Sema::TemplateParamListContext TPC, |
| 2077 | SourceLocation ParamLoc, |
| 2078 | SourceRange DefArgRange) { |
| 2079 | switch (TPC) { |
| 2080 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2081 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2082 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2083 | return false; |
| 2084 | |
| 2085 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2086 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2087 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2088 | // A default template-argument shall not be specified in a |
| 2089 | // function template declaration or a function template |
| 2090 | // definition [...] |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2091 | // If a friend function template declaration specifies a default |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2092 | // template-argument, that declaration shall be a definition and shall be |
| 2093 | // the only declaration of the function template in the translation unit. |
| 2094 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2095 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2096 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 2097 | : diag::ext_template_parameter_default_in_function_template) |
| 2098 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2099 | return false; |
| 2100 | |
| 2101 | case Sema::TPC_ClassTemplateMember: |
| 2102 | // C++0x [temp.param]p9: |
| 2103 | // A default template-argument shall not be specified in the |
| 2104 | // template-parameter-lists of the definition of a member of a |
| 2105 | // class template that appears outside of the member's class. |
| 2106 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 2107 | << DefArgRange; |
| 2108 | return true; |
| 2109 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 2110 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2111 | case Sema::TPC_FriendFunctionTemplate: |
| 2112 | // C++ [temp.param]p9: |
| 2113 | // A default template-argument shall not be specified in a |
| 2114 | // friend template declaration. |
| 2115 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 2116 | << DefArgRange; |
| 2117 | return true; |
| 2118 | |
| 2119 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 2120 | // for friend function templates if there is only a single |
| 2121 | // declaration (and it is a definition). Strange! |
| 2122 | } |
| 2123 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2124 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2127 | /// Check for unexpanded parameter packs within the template parameters |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2128 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 2129 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 2130 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2131 | // A template template parameter which is a parameter pack is also a pack |
| 2132 | // expansion. |
| 2133 | if (TTP->isParameterPack()) |
| 2134 | return false; |
| 2135 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2136 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 2137 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 2138 | NamedDecl *P = Params->getParam(I); |
| 2139 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2140 | if (!NTTP->isParameterPack() && |
| 2141 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2142 | NTTP->getTypeSourceInfo(), |
| 2143 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 2144 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2146 | continue; |
| 2147 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2148 | |
| 2149 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2150 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 2151 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 2152 | return true; |
| 2153 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2154 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2155 | return false; |
| 2156 | } |
| 2157 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2158 | /// Checks the validity of a template parameter list, possibly |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2159 | /// considering the template parameter list from a previous |
| 2160 | /// declaration. |
| 2161 | /// |
| 2162 | /// If an "old" template parameter list is provided, it must be |
| 2163 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 2164 | /// template parameter list. |
| 2165 | /// |
| 2166 | /// \param NewParams Template parameter list for a new template |
| 2167 | /// declaration. This template parameter list will be updated with any |
| 2168 | /// default arguments that are carried through from the previous |
| 2169 | /// template parameter list. |
| 2170 | /// |
| 2171 | /// \param OldParams If provided, template parameter list from a |
| 2172 | /// previous declaration of the same template. Default template |
| 2173 | /// arguments will be merged from the old template parameter list to |
| 2174 | /// the new template parameter list. |
| 2175 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2176 | /// \param TPC Describes the context in which we are checking the given |
| 2177 | /// template parameter list. |
| 2178 | /// |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2179 | /// \param SkipBody If we might have already made a prior merged definition |
| 2180 | /// of this template visible, the corresponding body-skipping information. |
| 2181 | /// Default argument redefinition is not an error when skipping such a body, |
| 2182 | /// because (under the ODR) we can assume the default arguments are the same |
| 2183 | /// as the prior merged definition. |
| 2184 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2185 | /// \returns true if an error occurred, false otherwise. |
| 2186 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2187 | TemplateParameterList *OldParams, |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2188 | TemplateParamListContext TPC, |
| 2189 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2190 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2192 | // C++ [temp.param]p10: |
| 2193 | // The set of default template-arguments available for use with a |
| 2194 | // template declaration or definition is obtained by merging the |
| 2195 | // default arguments from the definition (if in scope) and all |
| 2196 | // declarations in scope in the same way default function |
| 2197 | // arguments are (8.3.6). |
| 2198 | bool SawDefaultArgument = false; |
| 2199 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2200 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 2201 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 2202 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2203 | if (OldParams) |
| 2204 | OldParam = OldParams->begin(); |
| 2205 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2206 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2207 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2208 | NewParamEnd = NewParams->end(); |
| 2209 | NewParam != NewParamEnd; ++NewParam) { |
| 2210 | // Variables used to diagnose redundant default arguments |
| 2211 | bool RedundantDefaultArg = false; |
| 2212 | SourceLocation OldDefaultLoc; |
| 2213 | SourceLocation NewDefaultLoc; |
| 2214 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2215 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2216 | bool MissingDefaultArg = false; |
| 2217 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2218 | // Variable used to diagnose non-final parameter packs |
| 2219 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2221 | if (TemplateTypeParmDecl *NewTypeParm |
| 2222 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2223 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2224 | if (NewTypeParm->hasDefaultArgument() && |
| 2225 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2226 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2227 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2228 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2229 | NewTypeParm->removeDefaultArgument(); |
| 2230 | |
| 2231 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2233 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2234 | if (NewTypeParm->isParameterPack()) { |
| 2235 | assert(!NewTypeParm->hasDefaultArgument() && |
| 2236 | "Parameter packs can't have a default argument!"); |
| 2237 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2238 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2239 | NewTypeParm->hasDefaultArgument() && |
| 2240 | (!SkipBody || !SkipBody->ShouldSkip)) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2241 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2242 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2243 | SawDefaultArgument = true; |
| 2244 | RedundantDefaultArg = true; |
| 2245 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2246 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 2247 | // Merge the default argument from the old declaration to the |
| 2248 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2249 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2250 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2251 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 2252 | SawDefaultArgument = true; |
| 2253 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2254 | } else if (SawDefaultArgument) |
| 2255 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2256 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2257 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2258 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2259 | if (!NewNonTypeParm->isParameterPack() && |
| 2260 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2261 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2262 | UPPC_NonTypeTemplateParameterType)) { |
| 2263 | Invalid = true; |
| 2264 | continue; |
| 2265 | } |
| 2266 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2267 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2268 | if (NewNonTypeParm->hasDefaultArgument() && |
| 2269 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2270 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2271 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2272 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2275 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2276 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2277 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2278 | if (NewNonTypeParm->isParameterPack()) { |
| 2279 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 2280 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2281 | if (!NewNonTypeParm->isPackExpansion()) |
| 2282 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2283 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2284 | NewNonTypeParm->hasDefaultArgument() && |
| 2285 | (!SkipBody || !SkipBody->ShouldSkip)) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2286 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2287 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2288 | SawDefaultArgument = true; |
| 2289 | RedundantDefaultArg = true; |
| 2290 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2291 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 2292 | // Merge the default argument from the old declaration to the |
| 2293 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2294 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2295 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2296 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 2297 | SawDefaultArgument = true; |
| 2298 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2299 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2301 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2302 | TemplateTemplateParmDecl *NewTemplateParm |
| 2303 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2304 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2305 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 2306 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2307 | Invalid = true; |
| 2308 | continue; |
| 2309 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2310 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2311 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2312 | if (NewTemplateParm->hasDefaultArgument() && |
| 2313 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2314 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2315 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2316 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2317 | |
| 2318 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2319 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2320 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2321 | if (NewTemplateParm->isParameterPack()) { |
| 2322 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 2323 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2324 | if (!NewTemplateParm->isPackExpansion()) |
| 2325 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2326 | } else if (OldTemplateParm && |
| 2327 | hasVisibleDefaultArgument(OldTemplateParm) && |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2328 | NewTemplateParm->hasDefaultArgument() && |
| 2329 | (!SkipBody || !SkipBody->ShouldSkip)) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2330 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 2331 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2332 | SawDefaultArgument = true; |
| 2333 | RedundantDefaultArg = true; |
| 2334 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2335 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 2336 | // Merge the default argument from the old declaration to the |
| 2337 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2338 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2339 | PreviousDefaultArgLoc |
| 2340 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2341 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 2342 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2343 | PreviousDefaultArgLoc |
| 2344 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2345 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2346 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2349 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2350 | // If a template parameter of a primary class template or alias template |
| 2351 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2352 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2353 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 2354 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2355 | Diag((*NewParam)->getLocation(), |
| 2356 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 2357 | Invalid = true; |
| 2358 | } |
| 2359 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2360 | if (RedundantDefaultArg) { |
| 2361 | // C++ [temp.param]p12: |
| 2362 | // A template-parameter shall not be given default arguments |
| 2363 | // by two different declarations in the same scope. |
| 2364 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 2365 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 2366 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 2367 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2368 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2369 | // If a template-parameter of a class template has a default |
| 2370 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 2371 | // have a default template-argument supplied or be a template parameter |
| 2372 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2374 | diag::err_template_param_default_arg_missing); |
| 2375 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 2376 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2377 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | // If we have an old template parameter list that we're merging |
| 2381 | // in, move on to the next parameter. |
| 2382 | if (OldParams) |
| 2383 | ++OldParam; |
| 2384 | } |
| 2385 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2386 | // We were missing some default arguments at the end of the list, so remove |
| 2387 | // all of the default arguments. |
| 2388 | if (RemoveDefaultArguments) { |
| 2389 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2390 | NewParamEnd = NewParams->end(); |
| 2391 | NewParam != NewParamEnd; ++NewParam) { |
| 2392 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 2393 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2394 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2395 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 2396 | NTTP->removeDefaultArgument(); |
| 2397 | else |
| 2398 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 2399 | } |
| 2400 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2401 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2402 | return Invalid; |
| 2403 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2404 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2405 | namespace { |
| 2406 | |
| 2407 | /// A class which looks for a use of a certain level of template |
| 2408 | /// parameter. |
| 2409 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 2410 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 2411 | |
| 2412 | unsigned Depth; |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Whether we're looking for a use of a template parameter that makes the |
| 2415 | // overall construct type-dependent / a dependent type. This is strictly |
| 2416 | // best-effort for now; we may fail to match at all for a dependent type |
| 2417 | // in some cases if this is set. |
| 2418 | bool IgnoreNonTypeDependent; |
| 2419 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2420 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2421 | SourceLocation MatchLoc; |
| 2422 | |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2423 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
| 2424 | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
| 2425 | Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2426 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2427 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2428 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
| 2429 | NamedDecl *ND = Params->getParam(0); |
| 2430 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 2431 | Depth = PD->getDepth(); |
| 2432 | } else if (NonTypeTemplateParmDecl *PD = |
| 2433 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 2434 | Depth = PD->getDepth(); |
| 2435 | } else { |
| 2436 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 2437 | } |
| 2438 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2439 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2440 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2441 | if (ParmDepth >= Depth) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2442 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2443 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2444 | return true; |
| 2445 | } |
| 2446 | return false; |
| 2447 | } |
| 2448 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2449 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 2450 | // Prune out non-type-dependent expressions if requested. This can |
| 2451 | // sometimes result in us failing to find a template parameter reference |
| 2452 | // (if a value-dependent expression creates a dependent type), but this |
| 2453 | // mode is best-effort only. |
| 2454 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 2455 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 2456 | return true; |
| 2457 | return super::TraverseStmt(S, Q); |
| 2458 | } |
| 2459 | |
| 2460 | bool TraverseTypeLoc(TypeLoc TL) { |
| 2461 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 2462 | !TL.getType()->isDependentType()) |
| 2463 | return true; |
| 2464 | return super::TraverseTypeLoc(TL); |
| 2465 | } |
| 2466 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2467 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2468 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 2469 | } |
| 2470 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2471 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2472 | // For a best-effort search, keep looking until we find a location. |
| 2473 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | bool TraverseTemplateName(TemplateName N) { |
| 2477 | if (TemplateTemplateParmDecl *PD = |
| 2478 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2479 | if (Matches(PD->getDepth())) |
| 2480 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2481 | return super::TraverseTemplateName(N); |
| 2482 | } |
| 2483 | |
| 2484 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 2485 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2486 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 2487 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2488 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2489 | return super::VisitDeclRefExpr(E); |
| 2490 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2491 | |
| 2492 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 2493 | return TraverseType(T->getReplacementType()); |
| 2494 | } |
| 2495 | |
| 2496 | bool |
| 2497 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 2498 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 2499 | } |
| 2500 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 2501 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 2502 | return TraverseType(T->getInjectedSpecializationType()); |
| 2503 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2504 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2505 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2506 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2507 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2508 | /// list. |
| 2509 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2510 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2511 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2512 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2513 | return Checker.Match; |
| 2514 | } |
| 2515 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2516 | // Find the source range corresponding to the named type in the given |
| 2517 | // nested-name-specifier, if any. |
| 2518 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 2519 | QualType T, |
| 2520 | const CXXScopeSpec &SS) { |
| 2521 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 2522 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 2523 | if (const Type *CurType = NNS->getAsType()) { |
| 2524 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 2525 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 2526 | } else |
| 2527 | break; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2528 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2529 | NNSLoc = NNSLoc.getPrefix(); |
| 2530 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2531 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2532 | return SourceRange(); |
| 2533 | } |
| 2534 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2535 | /// Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2536 | /// specifier, returning the template parameter list that applies to the |
| 2537 | /// name. |
| 2538 | /// |
| 2539 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 2540 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2542 | /// \param DeclLoc The location of the declaration itself. |
| 2543 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2544 | /// \param SS the scope specifier that will be matched to the given template |
| 2545 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 2546 | /// being declared. |
| 2547 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2548 | /// \param TemplateId The template-id following the scope specifier, if there |
| 2549 | /// is one. Used to check for a missing 'template<>'. |
| 2550 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2551 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 2552 | /// innermost template parameter lists. |
| 2553 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2554 | /// \param IsFriend Whether to apply the slightly different rules for |
| 2555 | /// matching template parameters to scope specifiers in friend |
| 2556 | /// declarations. |
| 2557 | /// |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2558 | /// \param IsMemberSpecialization will be set true if the scope specifier |
| 2559 | /// denotes a fully-specialized type, and therefore this is a declaration of |
| 2560 | /// a member specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2561 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2563 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2564 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2566 | /// 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] | 2567 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2568 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 2569 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2570 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2571 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2572 | bool &IsMemberSpecialization, bool &Invalid) { |
| 2573 | IsMemberSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2574 | Invalid = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2575 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2576 | // The sequence of nested types to which we will match up the template |
| 2577 | // parameter lists. We first build this list by starting with the type named |
| 2578 | // 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] | 2579 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2580 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2581 | if (SS.getScopeRep()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2582 | if (CXXRecordDecl *Record |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2583 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 2584 | T = Context.getTypeDeclType(Record); |
| 2585 | else |
| 2586 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 2587 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2588 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2589 | // If we found an explicit specialization that prevents us from needing |
| 2590 | // 'template<>' headers, this will be set to the location of that |
| 2591 | // explicit specialization. |
| 2592 | SourceLocation ExplicitSpecLoc; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2593 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2594 | while (!T.isNull()) { |
| 2595 | NestedTypes.push_back(T); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2596 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2597 | // Retrieve the parent of a record type. |
| 2598 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2599 | // If this type is an explicit specialization, we're done. |
| 2600 | if (ClassTemplateSpecializationDecl *Spec |
| 2601 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2602 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2603 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 2604 | ExplicitSpecLoc = Spec->getLocation(); |
| 2605 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 2606 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2607 | } else if (Record->getTemplateSpecializationKind() |
| 2608 | == TSK_ExplicitSpecialization) { |
| 2609 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2610 | break; |
| 2611 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2612 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2613 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 2614 | T = Context.getTypeDeclType(Parent); |
| 2615 | else |
| 2616 | T = QualType(); |
| 2617 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2620 | if (const TemplateSpecializationType *TST |
| 2621 | = T->getAs<TemplateSpecializationType>()) { |
| 2622 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 2623 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 2624 | T = Context.getTypeDeclType(Parent); |
| 2625 | else |
| 2626 | T = QualType(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2627 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2628 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2629 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2630 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2631 | // Look one step prior in a dependent template specialization type. |
| 2632 | if (const DependentTemplateSpecializationType *DependentTST |
| 2633 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2634 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 2635 | T = QualType(NNS->getAsType(), 0); |
| 2636 | else |
| 2637 | T = QualType(); |
| 2638 | continue; |
| 2639 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2640 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2641 | // Look one step prior in a dependent name type. |
| 2642 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 2643 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 2644 | T = QualType(NNS->getAsType(), 0); |
| 2645 | else |
| 2646 | T = QualType(); |
| 2647 | continue; |
| 2648 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2649 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2650 | // Retrieve the parent of an enumeration type. |
| 2651 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 2652 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 2653 | // check here. |
| 2654 | EnumDecl *Enum = EnumT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2655 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2656 | // Get to the parent type. |
| 2657 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 2658 | T = Context.getTypeDeclType(Parent); |
| 2659 | else |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2660 | T = QualType(); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2661 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2662 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2664 | T = QualType(); |
| 2665 | } |
| 2666 | // Reverse the nested types list, since we want to traverse from the outermost |
| 2667 | // to the innermost while checking template-parameter-lists. |
| 2668 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2669 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2670 | // C++0x [temp.expl.spec]p17: |
| 2671 | // A member or a member template may be nested within many |
| 2672 | // enclosing class templates. In an explicit specialization for |
| 2673 | // such a member, the member declaration shall be preceded by a |
| 2674 | // template<> for each enclosing class template that is |
| 2675 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2676 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2677 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2678 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2679 | if (SawNonEmptyTemplateParameterList) { |
| 2680 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 2681 | << !Recovery << Range; |
| 2682 | Invalid = true; |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2683 | IsMemberSpecialization = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2684 | return true; |
| 2685 | } |
| 2686 | |
| 2687 | return false; |
| 2688 | }; |
| 2689 | |
| 2690 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 2691 | // Check that we can have an explicit specialization here. |
| 2692 | if (CheckExplicitSpecialization(Range, true)) |
| 2693 | return true; |
| 2694 | |
| 2695 | // We don't have a template header, but we should. |
| 2696 | SourceLocation ExpectedTemplateLoc; |
| 2697 | if (!ParamLists.empty()) |
| 2698 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 2699 | else |
| 2700 | ExpectedTemplateLoc = DeclStartLoc; |
| 2701 | |
| 2702 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 2703 | << Range |
| 2704 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 2705 | return false; |
| 2706 | }; |
| 2707 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2708 | unsigned ParamIdx = 0; |
| 2709 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 2710 | ++TypeIdx) { |
| 2711 | T = NestedTypes[TypeIdx]; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2712 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2713 | // Whether we expect a 'template<>' header. |
| 2714 | bool NeedEmptyTemplateHeader = false; |
| 2715 | |
| 2716 | // Whether we expect a template header with parameters. |
| 2717 | bool NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2718 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2719 | // For a dependent type, the set of template parameters that we |
| 2720 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2721 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2722 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2723 | // C++0x [temp.expl.spec]p15: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2724 | // A member or a member template may be nested within many enclosing |
| 2725 | // class templates. In an explicit specialization for such a member, the |
| 2726 | // member declaration shall be preceded by a template<> for each |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2727 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2728 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2729 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 2730 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 2731 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 2732 | NeedNonemptyTemplateHeader = true; |
| 2733 | } else if (Record->isDependentType()) { |
| 2734 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2735 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2736 | ->getTemplateParameters(); |
| 2737 | NeedNonemptyTemplateHeader = true; |
| 2738 | } |
| 2739 | } else if (ClassTemplateSpecializationDecl *Spec |
| 2740 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 2741 | // C++0x [temp.expl.spec]p4: |
| 2742 | // Members of an explicitly specialized class template are defined |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2743 | // in the same manner as members of normal classes, and not using |
| 2744 | // the template<> syntax. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2745 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 2746 | NeedEmptyTemplateHeader = true; |
| 2747 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 2748 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2749 | } else if (Record->getTemplateSpecializationKind()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2750 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2751 | != TSK_ExplicitSpecialization && |
| 2752 | TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2753 | IsMemberSpecialization = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2754 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2755 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2756 | } |
| 2757 | } else if (const TemplateSpecializationType *TST |
| 2758 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 2759 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2760 | ExpectedTemplateParams = Template->getTemplateParameters(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2761 | NeedNonemptyTemplateHeader = true; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2762 | } |
| 2763 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2764 | // FIXME: We actually could/should check the template arguments here |
| 2765 | // against the corresponding template parameter list. |
| 2766 | NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2769 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2770 | // In an explicit specialization declaration for a member of a class |
| 2771 | // template or a member template that ap- pears in namespace scope, the |
| 2772 | // member template and some of its enclosing class templates may remain |
| 2773 | // unspecialized, except that the declaration shall not explicitly |
| 2774 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2775 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2776 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2777 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2778 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2779 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2780 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2781 | } else |
| 2782 | SawNonEmptyTemplateParameterList = true; |
| 2783 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2784 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2785 | if (NeedEmptyTemplateHeader) { |
| 2786 | // If we're on the last of the types, and we need a 'template<>' header |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2787 | // here, then it's a member specialization. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2788 | if (TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2789 | IsMemberSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2790 | |
| 2791 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2792 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2793 | // The header has template parameters when it shouldn't. Complain. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2794 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2795 | diag::err_template_param_list_matches_nontemplate) |
| 2796 | << T |
| 2797 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2798 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2799 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2800 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2801 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2802 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2804 | // Consume this template header. |
| 2805 | ++ParamIdx; |
| 2806 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2807 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2808 | |
| 2809 | if (!IsFriend) |
| 2810 | if (DiagnoseMissingExplicitSpecialization( |
| 2811 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2812 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2814 | continue; |
| 2815 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2817 | if (NeedNonemptyTemplateHeader) { |
| 2818 | // In friend declarations we can have template-ids which don't |
| 2819 | // depend on the corresponding template parameter lists. But |
| 2820 | // assume that empty parameter lists are supposed to match this |
| 2821 | // template-id. |
| 2822 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2823 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2824 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2825 | ExpectedTemplateParams = nullptr; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2826 | else |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2827 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2829 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2830 | if (ParamIdx < ParamLists.size()) { |
| 2831 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2832 | if (ExpectedTemplateParams && |
| 2833 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2834 | ExpectedTemplateParams, |
| 2835 | true, TPL_TemplateMatch)) |
| 2836 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2837 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2838 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2839 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2840 | TPC_ClassTemplateMember)) |
| 2841 | Invalid = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2842 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2843 | ++ParamIdx; |
| 2844 | continue; |
| 2845 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2846 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2847 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2848 | << T |
| 2849 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2850 | Invalid = true; |
| 2851 | continue; |
| 2852 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2853 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2854 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2855 | // If there were at least as many template-ids as there were template |
| 2856 | // parameter lists, then there are no template parameter lists remaining for |
| 2857 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2858 | if (ParamIdx >= ParamLists.size()) { |
| 2859 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2860 | // We don't have a template header for the declaration itself, but we |
| 2861 | // should. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2862 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2863 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2864 | |
| 2865 | // Fabricate an empty template parameter list for the invented header. |
| 2866 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2867 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2868 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2871 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2872 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2874 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2875 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2876 | bool HasAnyExplicitSpecHeader = false; |
| 2877 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2878 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2879 | if (ParamLists[I]->size() == 0) |
| 2880 | HasAnyExplicitSpecHeader = true; |
| 2881 | else |
| 2882 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2883 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2884 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2885 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2886 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2887 | : diag::err_template_spec_extra_headers) |
| 2888 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2889 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2890 | |
| 2891 | // If there was a specialization somewhere, such that 'template<>' is |
| 2892 | // not required, and there were any 'template<>' headers, note where the |
| 2893 | // specialization occurred. |
| 2894 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2895 | Diag(ExplicitSpecLoc, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2896 | diag::note_explicit_template_spec_does_not_need_header) |
| 2897 | << NestedTypes.back(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2898 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2899 | // We have a template parameter list with no corresponding scope, which |
| 2900 | // means that the resulting template declaration can't be instantiated |
| 2901 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2902 | if (!AllExplicitSpecHeaders) |
| 2903 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2904 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2906 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2907 | // In an explicit specialization declaration for a member of a class |
| 2908 | // template or a member template that ap- pears in namespace scope, the |
| 2909 | // member template and some of its enclosing class templates may remain |
| 2910 | // unspecialized, except that the declaration shall not explicitly |
| 2911 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2912 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2913 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2914 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2915 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2916 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2917 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2918 | // Return the last template parameter list, which corresponds to the |
| 2919 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2920 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2923 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2924 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2925 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2926 | << (isa<FunctionTemplateDecl>(Template) |
| 2927 | ? 0 |
| 2928 | : isa<ClassTemplateDecl>(Template) |
| 2929 | ? 1 |
| 2930 | : isa<VarTemplateDecl>(Template) |
| 2931 | ? 2 |
| 2932 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2933 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2934 | return; |
| 2935 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2937 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2938 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2939 | IEnd = OST->end(); |
| 2940 | I != IEnd; ++I) |
| 2941 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2942 | << 0 << (*I)->getDeclName(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2943 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2944 | return; |
| 2945 | } |
| 2946 | } |
| 2947 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2948 | static QualType |
| 2949 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2950 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2951 | SourceLocation TemplateLoc, |
| 2952 | TemplateArgumentListInfo &TemplateArgs) { |
| 2953 | ASTContext &Context = SemaRef.getASTContext(); |
| 2954 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2955 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2956 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2957 | // S<T, 0, ..., N-1>. |
| 2958 | |
| 2959 | // C++14 [inteseq.intseq]p1: |
| 2960 | // T shall be an integer type. |
| 2961 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2962 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2963 | diag::err_integer_sequence_integral_element_type); |
| 2964 | return QualType(); |
| 2965 | } |
| 2966 | |
| 2967 | // C++14 [inteseq.make]p1: |
| 2968 | // If N is negative the program is ill-formed. |
| 2969 | TemplateArgument NumArgsArg = Converted[2]; |
| 2970 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2971 | if (NumArgs < 0) { |
| 2972 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2973 | diag::err_integer_sequence_negative_length); |
| 2974 | return QualType(); |
| 2975 | } |
| 2976 | |
| 2977 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2978 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2979 | // The type argument gets reused as the first template argument in the |
| 2980 | // synthetic template argument list. |
| 2981 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2982 | // Expand N into 0 ... N-1. |
| 2983 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2984 | I < NumArgs; ++I) { |
| 2985 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2986 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2987 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2988 | } |
| 2989 | // The first template argument will be reused as the template decl that |
| 2990 | // our synthetic template arguments will be applied to. |
| 2991 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2992 | TemplateLoc, SyntheticTemplateArgs); |
| 2993 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2994 | |
| 2995 | case BTK__type_pack_element: |
| 2996 | // Specializations of |
| 2997 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2998 | // are treated like T_Index. |
| 2999 | assert(Converted.size() == 2 && |
| 3000 | "__type_pack_element should be given an index and a parameter pack"); |
| 3001 | |
| 3002 | // If the Index is out of bounds, the program is ill-formed. |
| 3003 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 3004 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 3005 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 3006 | "type std::size_t, and hence be non-negative"); |
| 3007 | if (Index >= Ts.pack_size()) { |
| 3008 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 3009 | diag::err_type_pack_element_out_of_bounds); |
| 3010 | return QualType(); |
| 3011 | } |
| 3012 | |
| 3013 | // We simply return the type at index `Index`. |
| 3014 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 3015 | return Nth->getAsType(); |
| 3016 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3017 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 3018 | } |
| 3019 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3020 | /// Determine whether this alias template is "enable_if_t". |
| 3021 | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { |
| 3022 | return AliasTemplate->getName().equals("enable_if_t"); |
| 3023 | } |
| 3024 | |
| 3025 | /// Collect all of the separable terms in the given condition, which |
| 3026 | /// might be a conjunction. |
| 3027 | /// |
| 3028 | /// FIXME: The right answer is to convert the logical expression into |
| 3029 | /// disjunctive normal form, so we can find the first failed term |
| 3030 | /// within each possible clause. |
| 3031 | static void collectConjunctionTerms(Expr *Clause, |
| 3032 | SmallVectorImpl<Expr *> &Terms) { |
| 3033 | if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { |
| 3034 | if (BinOp->getOpcode() == BO_LAnd) { |
| 3035 | collectConjunctionTerms(BinOp->getLHS(), Terms); |
| 3036 | collectConjunctionTerms(BinOp->getRHS(), Terms); |
| 3037 | } |
| 3038 | |
| 3039 | return; |
| 3040 | } |
| 3041 | |
| 3042 | Terms.push_back(Clause); |
| 3043 | } |
| 3044 | |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3045 | // The ranges-v3 library uses an odd pattern of a top-level "||" with |
| 3046 | // a left-hand side that is value-dependent but never true. Identify |
| 3047 | // the idiom and ignore that term. |
| 3048 | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { |
| 3049 | // Top-level '||'. |
| 3050 | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); |
| 3051 | if (!BinOp) return Cond; |
| 3052 | |
| 3053 | if (BinOp->getOpcode() != BO_LOr) return Cond; |
| 3054 | |
| 3055 | // With an inner '==' that has a literal on the right-hand side. |
| 3056 | Expr *LHS = BinOp->getLHS(); |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3057 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3058 | if (!InnerBinOp) return Cond; |
| 3059 | |
| 3060 | if (InnerBinOp->getOpcode() != BO_EQ || |
| 3061 | !isa<IntegerLiteral>(InnerBinOp->getRHS())) |
| 3062 | return Cond; |
| 3063 | |
| 3064 | // If the inner binary operation came from a macro expansion named |
| 3065 | // CONCEPT_REQUIRES or CONCEPT_REQUIRES_, return the right-hand side |
| 3066 | // of the '||', which is the real, user-provided condition. |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3067 | SourceLocation Loc = InnerBinOp->getExprLoc(); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3068 | if (!Loc.isMacroID()) return Cond; |
| 3069 | |
| 3070 | StringRef MacroName = PP.getImmediateMacroName(Loc); |
| 3071 | if (MacroName == "CONCEPT_REQUIRES" || MacroName == "CONCEPT_REQUIRES_") |
| 3072 | return BinOp->getRHS(); |
| 3073 | |
| 3074 | return Cond; |
| 3075 | } |
| 3076 | |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3077 | namespace { |
| 3078 | |
| 3079 | // A PrinterHelper that prints more helpful diagnostics for some sub-expressions |
| 3080 | // within failing boolean expression, such as substituting template parameters |
| 3081 | // for actual types. |
| 3082 | class FailedBooleanConditionPrinterHelper : public PrinterHelper { |
| 3083 | public: |
| 3084 | explicit FailedBooleanConditionPrinterHelper(const PrintingPolicy &P) |
| 3085 | : Policy(P) {} |
| 3086 | |
| 3087 | bool handledStmt(Stmt *E, raw_ostream &OS) override { |
| 3088 | const auto *DR = dyn_cast<DeclRefExpr>(E); |
| 3089 | if (DR && DR->getQualifier()) { |
| 3090 | // If this is a qualified name, expand the template arguments in nested |
| 3091 | // qualifiers. |
| 3092 | DR->getQualifier()->print(OS, Policy, true); |
| 3093 | // Then print the decl itself. |
| 3094 | const ValueDecl *VD = DR->getDecl(); |
| 3095 | OS << VD->getName(); |
| 3096 | if (const auto *IV = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
| 3097 | // This is a template variable, print the expanded template arguments. |
| 3098 | printTemplateArgumentList(OS, IV->getTemplateArgs().asArray(), Policy); |
| 3099 | } |
| 3100 | return true; |
Clement Courbet | 9d432e0 | 2018-12-04 07:59:57 +0000 | [diff] [blame] | 3101 | } |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3102 | return false; |
Clement Courbet | 9d432e0 | 2018-12-04 07:59:57 +0000 | [diff] [blame] | 3103 | } |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3104 | |
| 3105 | private: |
| 3106 | const PrintingPolicy Policy; |
| 3107 | }; |
| 3108 | |
| 3109 | } // end anonymous namespace |
Clement Courbet | 9d432e0 | 2018-12-04 07:59:57 +0000 | [diff] [blame] | 3110 | |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3111 | std::pair<Expr *, std::string> |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3112 | Sema::findFailedBooleanCondition(Expr *Cond) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3113 | Cond = lookThroughRangesV3Condition(PP, Cond); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3114 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3115 | // Separate out all of the terms in a conjunction. |
| 3116 | SmallVector<Expr *, 4> Terms; |
| 3117 | collectConjunctionTerms(Cond, Terms); |
| 3118 | |
| 3119 | // Determine which term failed. |
| 3120 | Expr *FailedCond = nullptr; |
| 3121 | for (Expr *Term : Terms) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3122 | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); |
| 3123 | |
Clement Courbet | d872041 | 2018-12-10 08:53:17 +0000 | [diff] [blame] | 3124 | // Literals are uninteresting. |
| 3125 | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || |
| 3126 | isa<IntegerLiteral>(TermAsWritten)) |
| 3127 | continue; |
| 3128 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3129 | // The initialization of the parameter from the argument is |
| 3130 | // a constant-evaluated context. |
| 3131 | EnterExpressionEvaluationContext ConstantEvaluated( |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3132 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3133 | |
| 3134 | bool Succeeded; |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3135 | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3136 | !Succeeded) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3137 | FailedCond = TermAsWritten; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3138 | break; |
| 3139 | } |
| 3140 | } |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3141 | if (!FailedCond) |
Clement Courbet | d872041 | 2018-12-10 08:53:17 +0000 | [diff] [blame] | 3142 | FailedCond = Cond->IgnoreParenImpCasts(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3143 | |
| 3144 | std::string Description; |
| 3145 | { |
| 3146 | llvm::raw_string_ostream Out(Description); |
Clement Courbet | fb2c74d | 2018-12-20 09:05:15 +0000 | [diff] [blame] | 3147 | PrintingPolicy Policy = getPrintingPolicy(); |
| 3148 | Policy.PrintCanonicalTypes = true; |
| 3149 | FailedBooleanConditionPrinterHelper Helper(Policy); |
| 3150 | FailedCond->printPretty(Out, &Helper, Policy, 0, "\n", nullptr); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3151 | } |
| 3152 | return { FailedCond, Description }; |
| 3153 | } |
| 3154 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3155 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 3156 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3157 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3158 | DependentTemplateName *DTN |
| 3159 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3160 | if (DTN && DTN->isIdentifier()) |
| 3161 | // When building a template-id where the template-name is dependent, |
| 3162 | // assume the template is a type template. Either our assumption is |
| 3163 | // correct, or the code is ill-formed and will be diagnosed when the |
| 3164 | // dependent name is substituted. |
| 3165 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 3166 | DTN->getQualifier(), |
| 3167 | DTN->getIdentifier(), |
| 3168 | TemplateArgs); |
| 3169 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3170 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 3171 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 3172 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3173 | // We might have a substituted template template parameter pack. If so, |
| 3174 | // build a template specialization type for it. |
| 3175 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 3176 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3177 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3178 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 3179 | << Name; |
| 3180 | NoteAllFoundTemplates(Name); |
| 3181 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3182 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3183 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3184 | // Check that the template argument list is well-formed for this |
| 3185 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3186 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3187 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3188 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3189 | return QualType(); |
| 3190 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3191 | QualType CanonType; |
| 3192 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3193 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3194 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 3195 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3196 | // Find the canonical type for this type alias template specialization. |
| 3197 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 3198 | if (Pattern->isInvalidDecl()) |
| 3199 | return QualType(); |
| 3200 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3201 | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, |
| 3202 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3203 | |
| 3204 | // Only substitute for the innermost template argument list. |
| 3205 | MultiLevelTemplateArgumentList TemplateArgLists; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3206 | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 3207 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 3208 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 3209 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3210 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3211 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3212 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3213 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3214 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3215 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3216 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 3217 | TemplateArgLists, AliasTemplate->getLocation(), |
| 3218 | AliasTemplate->getDeclName()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3219 | if (CanonType.isNull()) { |
| 3220 | // If this was enable_if and we failed to find the nested type |
| 3221 | // within enable_if in a SFINAE context, dig out the specific |
| 3222 | // enable_if condition that failed and present that instead. |
| 3223 | if (isEnableIfAliasTemplate(AliasTemplate)) { |
| 3224 | if (auto DeductionInfo = isSFINAEContext()) { |
| 3225 | if (*DeductionInfo && |
| 3226 | (*DeductionInfo)->hasSFINAEDiagnostic() && |
| 3227 | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == |
| 3228 | diag::err_typename_nested_not_found_enable_if && |
| 3229 | TemplateArgs[0].getArgument().getKind() |
| 3230 | == TemplateArgument::Expression) { |
| 3231 | Expr *FailedCond; |
| 3232 | std::string FailedDescription; |
| 3233 | std::tie(FailedCond, FailedDescription) = |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3234 | findFailedBooleanCondition(TemplateArgs[0].getSourceExpression()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3235 | |
| 3236 | // Remove the old SFINAE diagnostic. |
| 3237 | PartialDiagnosticAt OldDiag = |
| 3238 | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; |
| 3239 | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); |
| 3240 | |
| 3241 | // Add a new SFINAE diagnostic specifying which condition |
| 3242 | // failed. |
| 3243 | (*DeductionInfo)->addSFINAEDiagnostic( |
| 3244 | OldDiag.first, |
| 3245 | PDiag(diag::err_typename_nested_not_found_requirement) |
| 3246 | << FailedDescription |
| 3247 | << FailedCond->getSourceRange()); |
| 3248 | } |
| 3249 | } |
| 3250 | } |
| 3251 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3252 | return QualType(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3253 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3254 | } else if (Name.isDependent() || |
| 3255 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3256 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3257 | // This class template specialization is a dependent |
| 3258 | // type. Therefore, its canonical type is another class template |
| 3259 | // specialization type that contains all of the converted |
| 3260 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 3261 | // A<T, T> have identical types when A is declared as: |
| 3262 | // |
| 3263 | // template<typename T, typename U = T> struct A; |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 3264 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3265 | |
| 3266 | // This might work out to be a current instantiation, in which |
| 3267 | // case the canonical type needs to be the InjectedClassNameType. |
| 3268 | // |
| 3269 | // TODO: in theory this could be a simple hashtable lookup; most |
| 3270 | // changes to CurContext don't change the set of current |
| 3271 | // instantiations. |
| 3272 | if (isa<ClassTemplateDecl>(Template)) { |
| 3273 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 3274 | // If we get out to a namespace, we're done. |
| 3275 | if (Ctx->isFileContext()) break; |
| 3276 | |
| 3277 | // If this isn't a record, keep looking. |
| 3278 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 3279 | if (!Record) continue; |
| 3280 | |
| 3281 | // Look for one of the two cases with InjectedClassNameTypes |
| 3282 | // and check whether it's the same template. |
| 3283 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 3284 | !Record->getDescribedClassTemplate()) |
| 3285 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3286 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3287 | // Fetch the injected class name type and check whether its |
| 3288 | // injected type is equal to the type we just built. |
| 3289 | QualType ICNT = Context.getTypeDeclType(Record); |
| 3290 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 3291 | ->getInjectedSpecializationType(); |
| 3292 | |
| 3293 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 3294 | continue; |
| 3295 | |
| 3296 | // If so, the canonical type of this TST is the injected |
| 3297 | // class name type of the record we just found. |
| 3298 | assert(ICNT.isCanonical()); |
| 3299 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3300 | break; |
| 3301 | } |
| 3302 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3304 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3305 | // Find the class template specialization declaration that |
| 3306 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3307 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3308 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3309 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3310 | if (!Decl) { |
| 3311 | // This is the first time we have referenced this class template |
| 3312 | // specialization. Create the canonical declaration and add it to |
| 3313 | // the set of specializations. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3314 | Decl = ClassTemplateSpecializationDecl::Create( |
| 3315 | Context, ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 3316 | ClassTemplate->getDeclContext(), |
| 3317 | ClassTemplate->getTemplatedDecl()->getBeginLoc(), |
| 3318 | ClassTemplate->getLocation(), ClassTemplate, Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 3319 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 3320 | if (ClassTemplate->isOutOfLine()) |
| 3321 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 3324 | if (Decl->getSpecializationKind() == TSK_Undeclared) { |
| 3325 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3326 | TemplateArgLists.addOuterTemplateArguments(Converted); |
| 3327 | InstantiateAttrsForDecl(TemplateArgLists, ClassTemplate->getTemplatedDecl(), |
| 3328 | Decl); |
| 3329 | } |
| 3330 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 3331 | // Diagnose uses of this specialization. |
| 3332 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 3333 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3334 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3335 | assert(isa<RecordType>(CanonType) && |
| 3336 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3337 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 3338 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 3339 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3340 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3342 | // Build the fully-sugared type for this class template |
| 3343 | // specialization, which refers back to the class template |
| 3344 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 3345 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3348 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3349 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3350 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
| 3351 | SourceLocation TemplateIILoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3353 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3354 | SourceLocation RAngleLoc, |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3355 | bool IsCtorOrDtorName, bool IsClassName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3356 | if (SS.isInvalid()) |
| 3357 | return true; |
| 3358 | |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3359 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { |
| 3360 | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); |
| 3361 | |
| 3362 | // C++ [temp.res]p3: |
| 3363 | // A qualified-id that refers to a type and in which the |
| 3364 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 3365 | // shall be prefixed by the keyword typename to indicate that the |
| 3366 | // qualified-id denotes a type, forming an |
| 3367 | // elaborated-type-specifier (7.1.5.3). |
| 3368 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { |
Richard Smith | 3411fbf | 2017-02-01 21:41:18 +0000 | [diff] [blame] | 3369 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3370 | << SS.getScopeRep() << TemplateII->getName(); |
| 3371 | // Recover as if 'typename' were specified. |
| 3372 | // FIXME: This is not quite correct recovery as we don't transform SS |
| 3373 | // into the corresponding dependent form (and we don't diagnose missing |
| 3374 | // 'template' keywords within SS as a result). |
| 3375 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
| 3376 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
| 3377 | TemplateArgsIn, RAngleLoc); |
| 3378 | } |
| 3379 | |
| 3380 | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
| 3381 | // it's not actually allowed to be used as a type in most cases. Because |
| 3382 | // we annotate it before we know whether it's valid, we have to check for |
| 3383 | // this case here. |
| 3384 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3385 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 3386 | Diag(TemplateIILoc, |
| 3387 | TemplateKWLoc.isInvalid() |
| 3388 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
| 3389 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 3390 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 3391 | << 1 /*if any keyword was present, it was 'template'*/; |
| 3392 | } |
| 3393 | } |
| 3394 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3395 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3396 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3397 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3398 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3399 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3401 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3402 | QualType T |
| 3403 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 3404 | DTN->getQualifier(), |
| 3405 | DTN->getIdentifier(), |
| 3406 | TemplateArgs); |
| 3407 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3408 | TypeLocBuilder TLB; |
| 3409 | DependentTemplateSpecializationTypeLoc SpecTL |
| 3410 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3411 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 3412 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3413 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3414 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3415 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3416 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3417 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3418 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3419 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3420 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3421 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3422 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3423 | if (Result.isNull()) |
| 3424 | return true; |
| 3425 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3426 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3427 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3428 | TemplateSpecializationTypeLoc SpecTL |
| 3429 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3430 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3431 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3432 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3433 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3434 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3435 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3436 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3437 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 3438 | // constructor or destructor name (in such a case, the scope specifier |
| 3439 | // will be attached to the enclosing Decl or Expr node). |
| 3440 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3441 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 3442 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 3443 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3444 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3445 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3446 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3447 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3448 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3449 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3450 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3451 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3452 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3453 | SourceLocation TagLoc, |
| 3454 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3455 | SourceLocation TemplateKWLoc, |
| 3456 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3457 | SourceLocation TemplateLoc, |
| 3458 | SourceLocation LAngleLoc, |
| 3459 | ASTTemplateArgsPtr TemplateArgsIn, |
| 3460 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3461 | TemplateName Template = TemplateD.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3462 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3463 | // Translate the parser's template argument list in our AST format. |
| 3464 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 3465 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3466 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3467 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3468 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3469 | ElaboratedTypeKeyword Keyword |
| 3470 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3472 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 3473 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3474 | DTN->getQualifier(), |
| 3475 | DTN->getIdentifier(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3476 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3477 | |
| 3478 | // Build type-source information. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3479 | TypeLocBuilder TLB; |
| 3480 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3481 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 3482 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 3483 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3484 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3485 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3486 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3487 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3488 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3489 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3490 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3491 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3492 | |
| 3493 | if (TypeAliasTemplateDecl *TAT = |
| 3494 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 3495 | // C++0x [dcl.type.elab]p2: |
| 3496 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 3497 | // resolves to an alias template specialization, the |
| 3498 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 3499 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 3500 | << TAT << NTK_TypeAliasTemplate << TagKind; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3501 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 3502 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3503 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3504 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 3505 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 3506 | return TypeResult(true); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3507 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3508 | // Check the tag kind |
| 3509 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3510 | RecordDecl *D = RT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3511 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3512 | IdentifierInfo *Id = D->getIdentifier(); |
| 3513 | assert(Id && "templated class must have an identifier"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3514 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 3515 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 3516 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3517 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3518 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3519 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3520 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3521 | } |
| 3522 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3523 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3524 | // Provide source-location information for the template specialization. |
| 3525 | TypeLocBuilder TLB; |
| 3526 | TemplateSpecializationTypeLoc SpecTL |
| 3527 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3528 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3529 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 3530 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3531 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3532 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3533 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3534 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3535 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3536 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3537 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 3538 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3539 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3540 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3541 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3544 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 3545 | NamedDecl *PrevDecl, |
| 3546 | SourceLocation Loc, |
| 3547 | bool IsPartialSpecialization); |
| 3548 | |
| 3549 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3550 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3551 | static bool isTemplateArgumentTemplateParameter( |
| 3552 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 3553 | switch (Arg.getKind()) { |
| 3554 | case TemplateArgument::Null: |
| 3555 | case TemplateArgument::NullPtr: |
| 3556 | case TemplateArgument::Integral: |
| 3557 | case TemplateArgument::Declaration: |
| 3558 | case TemplateArgument::Pack: |
| 3559 | case TemplateArgument::TemplateExpansion: |
| 3560 | return false; |
| 3561 | |
| 3562 | case TemplateArgument::Type: { |
| 3563 | QualType Type = Arg.getAsType(); |
| 3564 | const TemplateTypeParmType *TPT = |
| 3565 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 3566 | return TPT && !Type.hasQualifiers() && |
| 3567 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 3568 | } |
| 3569 | |
| 3570 | case TemplateArgument::Expression: { |
| 3571 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 3572 | if (!DRE || !DRE->getDecl()) |
| 3573 | return false; |
| 3574 | const NonTypeTemplateParmDecl *NTTP = |
| 3575 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 3576 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 3577 | } |
| 3578 | |
| 3579 | case TemplateArgument::Template: |
| 3580 | const TemplateTemplateParmDecl *TTP = |
| 3581 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 3582 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 3583 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 3584 | } |
| 3585 | llvm_unreachable("unexpected kind of template argument"); |
| 3586 | } |
| 3587 | |
| 3588 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 3589 | ArrayRef<TemplateArgument> Args) { |
| 3590 | if (Params->size() != Args.size()) |
| 3591 | return false; |
| 3592 | |
| 3593 | unsigned Depth = Params->getDepth(); |
| 3594 | |
| 3595 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3596 | TemplateArgument Arg = Args[I]; |
| 3597 | |
| 3598 | // If the parameter is a pack expansion, the argument must be a pack |
| 3599 | // whose only element is a pack expansion. |
| 3600 | if (Params->getParam(I)->isParameterPack()) { |
| 3601 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 3602 | !Arg.pack_begin()->isPackExpansion()) |
| 3603 | return false; |
| 3604 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 3605 | } |
| 3606 | |
| 3607 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 3608 | return false; |
| 3609 | } |
| 3610 | |
| 3611 | return true; |
| 3612 | } |
| 3613 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3614 | /// Convert the parser's template argument list representation into our form. |
| 3615 | static TemplateArgumentListInfo |
| 3616 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 3617 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 3618 | TemplateId.RAngleLoc); |
| 3619 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 3620 | TemplateId.NumArgs); |
| 3621 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 3622 | return TemplateArgs; |
| 3623 | } |
| 3624 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3625 | template<typename PartialSpecDecl> |
| 3626 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 3627 | if (Partial->getDeclContext()->isDependentContext()) |
| 3628 | return; |
| 3629 | |
| 3630 | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
| 3631 | // for non-substitution-failure issues? |
| 3632 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 3633 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 3634 | return; |
| 3635 | |
| 3636 | auto *Template = Partial->getSpecializedTemplate(); |
| 3637 | S.Diag(Partial->getLocation(), |
Richard Smith | fa4a09d | 2016-12-27 20:03:09 +0000 | [diff] [blame] | 3638 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 3639 | << isa<VarTemplateDecl>(Template); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3640 | |
| 3641 | if (Info.hasSFINAEDiagnostic()) { |
| 3642 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 3643 | PartialDiagnostic::NullDiagnostic()}; |
| 3644 | Info.takeSFINAEDiagnostic(Diag); |
| 3645 | SmallString<128> SFINAEArgString; |
| 3646 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 3647 | S.Diag(Diag.first, |
| 3648 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 3649 | << SFINAEArgString; |
| 3650 | } |
| 3651 | |
| 3652 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 3653 | } |
| 3654 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3655 | static void |
| 3656 | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
| 3657 | const llvm::SmallBitVector &DeducibleParams) { |
| 3658 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3659 | if (!DeducibleParams[I]) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 3660 | NamedDecl *Param = TemplateParams->getParam(I); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3661 | if (Param->getDeclName()) |
| 3662 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3663 | << Param->getDeclName(); |
| 3664 | else |
| 3665 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3666 | << "(anonymous)"; |
| 3667 | } |
| 3668 | } |
| 3669 | } |
| 3670 | |
| 3671 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3672 | template<typename PartialSpecDecl> |
| 3673 | static void checkTemplatePartialSpecialization(Sema &S, |
| 3674 | PartialSpecDecl *Partial) { |
| 3675 | // C++1z [temp.class.spec]p8: (DR1495) |
| 3676 | // - The specialization shall be more specialized than the primary |
| 3677 | // template (14.5.5.2). |
| 3678 | checkMoreSpecializedThanPrimary(S, Partial); |
| 3679 | |
| 3680 | // C++ [temp.class.spec]p8: (DR1315) |
| 3681 | // - Each template-parameter shall appear at least once in the |
| 3682 | // template-id outside a non-deduced context. |
| 3683 | // C++1z [temp.class.spec.match]p3 (P0127R2) |
| 3684 | // If the template arguments of a partial specialization cannot be |
| 3685 | // deduced because of the structure of its template-parameter-list |
| 3686 | // and the template-id, the program is ill-formed. |
| 3687 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 3688 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3689 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 3690 | TemplateParams->getDepth(), DeducibleParams); |
| 3691 | |
| 3692 | if (!DeducibleParams.all()) { |
| 3693 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3694 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 3695 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 3696 | << (NumNonDeducible > 1) |
| 3697 | << SourceRange(Partial->getLocation(), |
| 3698 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3699 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | |
| 3703 | void Sema::CheckTemplatePartialSpecialization( |
| 3704 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 3705 | checkTemplatePartialSpecialization(*this, Partial); |
| 3706 | } |
| 3707 | |
| 3708 | void Sema::CheckTemplatePartialSpecialization( |
| 3709 | VarTemplatePartialSpecializationDecl *Partial) { |
| 3710 | checkTemplatePartialSpecialization(*this, Partial); |
| 3711 | } |
| 3712 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3713 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
| 3714 | // C++1z [temp.param]p11: |
| 3715 | // A template parameter of a deduction guide template that does not have a |
| 3716 | // default-argument shall be deducible from the parameter-type-list of the |
| 3717 | // deduction guide template. |
| 3718 | auto *TemplateParams = TD->getTemplateParameters(); |
| 3719 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3720 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
| 3721 | for (unsigned I = 0; I != TemplateParams->size(); ++I) { |
| 3722 | // A parameter pack is deducible (to an empty pack). |
| 3723 | auto *Param = TemplateParams->getParam(I); |
| 3724 | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)) |
| 3725 | DeducibleParams[I] = true; |
| 3726 | } |
| 3727 | |
| 3728 | if (!DeducibleParams.all()) { |
| 3729 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3730 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
| 3731 | << (NumNonDeducible > 1); |
| 3732 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
| 3733 | } |
| 3734 | } |
| 3735 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3736 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3737 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 3738 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3739 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3740 | // D must be variable template id. |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 3741 | assert(D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3742 | "Variable template specialization is declared with a template it."); |
| 3743 | |
| 3744 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3745 | TemplateArgumentListInfo TemplateArgs = |
| 3746 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3747 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 3748 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 3749 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3750 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3751 | TemplateName Name = TemplateId->Template.get(); |
| 3752 | |
| 3753 | // The template-id must name a variable template. |
| 3754 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3755 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 3756 | if (!VarTemplate) { |
| 3757 | NamedDecl *FnTemplate; |
| 3758 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 3759 | FnTemplate = *OTS->begin(); |
| 3760 | else |
| 3761 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 3762 | if (FnTemplate) |
| 3763 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 3764 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3765 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 3766 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3767 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3768 | |
| 3769 | // Check for unexpanded parameter packs in any of the template arguments. |
| 3770 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 3771 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 3772 | UPPC_PartialSpecialization)) |
| 3773 | return true; |
| 3774 | |
| 3775 | // Check that the template argument list is well-formed for this |
| 3776 | // template. |
| 3777 | SmallVector<TemplateArgument, 4> Converted; |
| 3778 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 3779 | false, Converted)) |
| 3780 | return true; |
| 3781 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3782 | // Find the variable template (partial) specialization declaration that |
| 3783 | // corresponds to these arguments. |
| 3784 | if (IsPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3785 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 3786 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3787 | return true; |
| 3788 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3789 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
| 3790 | // also do them during instantiation. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3791 | bool InstantiationDependent; |
| 3792 | if (!Name.isDependent() && |
| 3793 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3794 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3795 | InstantiationDependent)) { |
| 3796 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3797 | << VarTemplate->getDeclName(); |
| 3798 | IsPartialSpecialization = false; |
| 3799 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3800 | |
| 3801 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 3802 | Converted)) { |
| 3803 | // C++ [temp.class.spec]p9b3: |
| 3804 | // |
| 3805 | // -- The argument list of the specialization shall not be identical |
| 3806 | // to the implicit argument list of the primary template. |
| 3807 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 3808 | << /*variable template*/ 1 |
| 3809 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 3810 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 3811 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 3812 | // of the primary template. |
| 3813 | return true; |
| 3814 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3815 | } |
| 3816 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3817 | void *InsertPos = nullptr; |
| 3818 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3819 | |
| 3820 | if (IsPartialSpecialization) |
| 3821 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3822 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3823 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3824 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3825 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3826 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3827 | |
| 3828 | // Check whether we can declare a variable template specialization in |
| 3829 | // the current scope. |
| 3830 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 3831 | TemplateNameLoc, |
| 3832 | IsPartialSpecialization)) |
| 3833 | return true; |
| 3834 | |
| 3835 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 3836 | // Since the only prior variable template specialization with these |
| 3837 | // arguments was referenced but not declared, reuse that |
| 3838 | // declaration node as our own, updating its source location and |
| 3839 | // the list of outer template parameters to reflect our new declaration. |
| 3840 | Specialization = PrevDecl; |
| 3841 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3842 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3843 | } else if (IsPartialSpecialization) { |
| 3844 | // Create a new class template partial specialization declaration node. |
| 3845 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 3846 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3847 | VarTemplatePartialSpecializationDecl *Partial = |
| 3848 | VarTemplatePartialSpecializationDecl::Create( |
| 3849 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 3850 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3851 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3852 | |
| 3853 | if (!PrevPartial) |
| 3854 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 3855 | Specialization = Partial; |
| 3856 | |
| 3857 | // If we are providing an explicit specialization of a member variable |
| 3858 | // template specialization, make a note of that. |
| 3859 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3860 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3861 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3862 | CheckTemplatePartialSpecialization(Partial); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3863 | } else { |
| 3864 | // Create a new class template specialization declaration node for |
| 3865 | // this explicit specialization or friend declaration. |
| 3866 | Specialization = VarTemplateSpecializationDecl::Create( |
| 3867 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3868 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3869 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 3870 | |
| 3871 | if (!PrevDecl) |
| 3872 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 3873 | } |
| 3874 | |
| 3875 | // C++ [temp.expl.spec]p6: |
| 3876 | // If a template, a member template or the member of a class template is |
| 3877 | // explicitly specialized then that specialization shall be declared |
| 3878 | // before the first use of that specialization that would cause an implicit |
| 3879 | // instantiation to take place, in every translation unit in which such a |
| 3880 | // use occurs; no diagnostic is required. |
| 3881 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3882 | bool Okay = false; |
| 3883 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 3884 | // Is there any previous explicit specialization declaration? |
| 3885 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3886 | Okay = true; |
| 3887 | break; |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | if (!Okay) { |
| 3892 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3893 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3894 | << Name << Range; |
| 3895 | |
| 3896 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3897 | diag::note_instantiation_required_here) |
| 3898 | << (PrevDecl->getTemplateSpecializationKind() != |
| 3899 | TSK_ImplicitInstantiation); |
| 3900 | return true; |
| 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 3905 | Specialization->setLexicalDeclContext(CurContext); |
| 3906 | |
| 3907 | // Add the specialization into its lexical context, so that it can |
| 3908 | // be seen when iterating through the list of declarations in that |
| 3909 | // context. However, specializations are not found by name lookup. |
| 3910 | CurContext->addDecl(Specialization); |
| 3911 | |
| 3912 | // Note that this is an explicit specialization. |
| 3913 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 3914 | |
| 3915 | if (PrevDecl) { |
| 3916 | // Check that this isn't a redefinition of this specialization, |
| 3917 | // merging with previous declarations. |
| 3918 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 3919 | forRedeclarationInCurContext()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3920 | PrevSpec.addDecl(PrevDecl); |
| 3921 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3922 | } else if (Specialization->isStaticDataMember() && |
| 3923 | Specialization->isOutOfLine()) { |
| 3924 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | // Link instantiations of static data members back to the template from |
| 3928 | // which they were instantiated. |
| 3929 | if (Specialization->isStaticDataMember()) |
| 3930 | Specialization->setInstantiationOfStaticDataMember( |
| 3931 | VarTemplate->getTemplatedDecl(), |
| 3932 | Specialization->getSpecializationKind()); |
| 3933 | |
| 3934 | return Specialization; |
| 3935 | } |
| 3936 | |
| 3937 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3938 | /// A partial specialization whose template arguments have matched |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3939 | /// a given template-id. |
| 3940 | struct PartialSpecMatchResult { |
| 3941 | VarTemplatePartialSpecializationDecl *Partial; |
| 3942 | TemplateArgumentList *Args; |
| 3943 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 3944 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3945 | |
| 3946 | DeclResult |
| 3947 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3948 | SourceLocation TemplateNameLoc, |
| 3949 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3950 | assert(Template && "A variable template id without template?"); |
| 3951 | |
| 3952 | // Check that the template argument list is well-formed for this template. |
| 3953 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3954 | if (CheckTemplateArgumentList( |
| 3955 | Template, TemplateNameLoc, |
| 3956 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3957 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3958 | return true; |
| 3959 | |
| 3960 | // Find the variable template specialization declaration that |
| 3961 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3962 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3963 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3964 | Converted, InsertPos)) { |
| 3965 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3966 | // If we already have a variable template specialization, return it. |
| 3967 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3968 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3969 | |
| 3970 | // This is the first time we have referenced this variable template |
| 3971 | // specialization. Create the canonical declaration and add it to |
| 3972 | // the set of specializations, based on the closest partial specialization |
| 3973 | // that it represents. That is, |
| 3974 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 3975 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3976 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3977 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 3978 | bool AmbiguousPartialSpec = false; |
| 3979 | typedef PartialSpecMatchResult MatchResult; |
| 3980 | SmallVector<MatchResult, 4> Matched; |
| 3981 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3982 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 3983 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3984 | |
| 3985 | // 1. Attempt to find the closest partial specialization that this |
| 3986 | // specializes, if any. |
| 3987 | // If any of the template arguments is dependent, then this is probably |
| 3988 | // a placeholder for an incomplete declarative context; which must be |
| 3989 | // complete by instantiation time. Thus, do not search through the partial |
| 3990 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3991 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 3992 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 3993 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3994 | bool InstantiationDependent = false; |
| 3995 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3996 | TemplateArgs, InstantiationDependent)) { |
| 3997 | |
| 3998 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3999 | Template->getPartialSpecializations(PartialSpecs); |
| 4000 | |
| 4001 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 4002 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 4003 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 4004 | |
| 4005 | if (TemplateDeductionResult Result = |
| 4006 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 4007 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 4008 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4009 | FailedCandidates.addCandidate().set( |
| 4010 | DeclAccessPair::make(Template, AS_public), Partial, |
| 4011 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4012 | (void)Result; |
| 4013 | } else { |
| 4014 | Matched.push_back(PartialSpecMatchResult()); |
| 4015 | Matched.back().Partial = Partial; |
| 4016 | Matched.back().Args = Info.take(); |
| 4017 | } |
| 4018 | } |
| 4019 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4020 | if (Matched.size() >= 1) { |
| 4021 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 4022 | if (Matched.size() == 1) { |
| 4023 | // -- If exactly one matching specialization is found, the |
| 4024 | // instantiation is generated from that specialization. |
| 4025 | // We don't need to do anything for this. |
| 4026 | } else { |
| 4027 | // -- If more than one matching specialization is found, the |
| 4028 | // partial order rules (14.5.4.2) are used to determine |
| 4029 | // whether one of the specializations is more specialized |
| 4030 | // than the others. If none of the specializations is more |
| 4031 | // specialized than all of the other matching |
| 4032 | // specializations, then the use of the variable template is |
| 4033 | // ambiguous and the program is ill-formed. |
| 4034 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 4035 | PEnd = Matched.end(); |
| 4036 | P != PEnd; ++P) { |
| 4037 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 4038 | PointOfInstantiation) == |
| 4039 | P->Partial) |
| 4040 | Best = P; |
| 4041 | } |
| 4042 | |
| 4043 | // Determine if the best partial specialization is more specialized than |
| 4044 | // the others. |
| 4045 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 4046 | PEnd = Matched.end(); |
| 4047 | P != PEnd; ++P) { |
| 4048 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 4049 | P->Partial, Best->Partial, |
| 4050 | PointOfInstantiation) != Best->Partial) { |
| 4051 | AmbiguousPartialSpec = true; |
| 4052 | break; |
| 4053 | } |
| 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | // Instantiate using the best variable template partial specialization. |
| 4058 | InstantiationPattern = Best->Partial; |
| 4059 | InstantiationArgs = Best->Args; |
| 4060 | } else { |
| 4061 | // -- If no match is found, the instantiation is generated |
| 4062 | // from the primary template. |
| 4063 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 4064 | } |
| 4065 | } |
| 4066 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4067 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4068 | // Note that we do not instantiate a definition until we see an odr-use |
| 4069 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4070 | // FIXME: LateAttrs et al.? |
| 4071 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 4072 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 4073 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 4074 | if (!Decl) |
| 4075 | return true; |
| 4076 | |
| 4077 | if (AmbiguousPartialSpec) { |
| 4078 | // Partial ordering did not produce a clear winner. Complain. |
| 4079 | Decl->setInvalidDecl(); |
| 4080 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 4081 | << Decl; |
| 4082 | |
| 4083 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 4084 | for (MatchResult P : Matched) |
| 4085 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 4086 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 4087 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4088 | return true; |
| 4089 | } |
| 4090 | |
| 4091 | if (VarTemplatePartialSpecializationDecl *D = |
| 4092 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 4093 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 4094 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4095 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 4096 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4097 | assert(Decl && "No variable template specialization?"); |
| 4098 | return Decl; |
| 4099 | } |
| 4100 | |
| 4101 | ExprResult |
| 4102 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 4103 | const DeclarationNameInfo &NameInfo, |
| 4104 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 4105 | const TemplateArgumentListInfo *TemplateArgs) { |
| 4106 | |
| 4107 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 4108 | *TemplateArgs); |
| 4109 | if (Decl.isInvalid()) |
| 4110 | return ExprError(); |
| 4111 | |
| 4112 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 4113 | if (!Var->getTemplateSpecializationKind()) |
| 4114 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 4115 | NameInfo.getLoc()); |
| 4116 | |
| 4117 | // Build an ordinary singleton decl ref. |
| 4118 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4119 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Richard Smith | ecad88d | 2018-04-26 01:08:00 +0000 | [diff] [blame] | 4122 | void Sema::diagnoseMissingTemplateArguments(TemplateName Name, |
| 4123 | SourceLocation Loc) { |
| 4124 | Diag(Loc, diag::err_template_missing_args) |
| 4125 | << (int)getTemplateNameKindForDiagnostics(Name) << Name; |
| 4126 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) { |
| 4127 | Diag(TD->getLocation(), diag::note_template_decl_here) |
| 4128 | << TD->getTemplateParameters()->getSourceRange(); |
| 4129 | } |
| 4130 | } |
| 4131 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4132 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4133 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4134 | LookupResult &R, |
| 4135 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4136 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4137 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 4138 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | // 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] | 4140 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4141 | // foo<int> could identify a single function unambiguously |
| 4142 | // This approach does NOT work, since f<int>(1); |
| 4143 | // gets resolved prior to resorting to overload resolution |
| 4144 | // i.e., template<class T> void f(double); |
| 4145 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4146 | |
| 4147 | // These should be filtered out by our callers. |
| 4148 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 4149 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 4150 | |
Richard Smith | 0410094 | 2018-04-26 02:10:22 +0000 | [diff] [blame] | 4151 | // Non-function templates require a template argument list. |
| 4152 | if (auto *TD = R.getAsSingle<TemplateDecl>()) { |
| 4153 | if (!TemplateArgs && !isa<FunctionTemplateDecl>(TD)) { |
| 4154 | diagnoseMissingTemplateArguments(TemplateName(TD), R.getNameLoc()); |
| 4155 | return ExprError(); |
| 4156 | } |
| 4157 | } |
| 4158 | |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4159 | auto AnyDependentArguments = [&]() -> bool { |
| 4160 | bool InstantiationDependent; |
| 4161 | return TemplateArgs && |
| 4162 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 4163 | *TemplateArgs, InstantiationDependent); |
| 4164 | }; |
| 4165 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4166 | // In C++1y, check variable template ids. |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4167 | if (R.getAsSingle<VarTemplateDecl>() && !AnyDependentArguments()) { |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 4168 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 4169 | R.getAsSingle<VarTemplateDecl>(), |
| 4170 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4173 | // We don't want lookup warnings at this point. |
| 4174 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4175 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4176 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4177 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4178 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4179 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4180 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 4182 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4183 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4184 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4187 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4188 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4189 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4190 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4191 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4192 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4193 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4194 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4195 | DeclContext *DC; |
| 4196 | if (!(DC = computeDeclContext(SS, false)) || |
| 4197 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4198 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 4199 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4200 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4201 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4202 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4203 | if (LookupTemplateName(R, (Scope *)nullptr, SS, QualType(), |
| 4204 | /*Entering*/false, MemberOfUnknownSpecialization, |
| 4205 | TemplateKWLoc)) |
| 4206 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4208 | if (R.isAmbiguous()) |
| 4209 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4210 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4211 | if (R.empty()) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4212 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 4213 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4214 | return ExprError(); |
| 4215 | } |
| 4216 | |
| 4217 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4218 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4219 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 4220 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4221 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 4222 | return ExprError(); |
| 4223 | } |
| 4224 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4225 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4226 | } |
| 4227 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4228 | /// Form a dependent template name. |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4229 | /// |
| 4230 | /// This action forms a dependent template name given the template |
| 4231 | /// name and its (presumably dependent) scope specifier. For |
| 4232 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 4233 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 4234 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4235 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4236 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4237 | SourceLocation TemplateKWLoc, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 4238 | const UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4239 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4240 | bool EnteringContext, |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4241 | TemplateTy &Result, |
| 4242 | bool AllowInjectedClassName) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4243 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 4244 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4245 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4246 | diag::warn_cxx98_compat_template_outside_of_template : |
| 4247 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4248 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 4249 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4250 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4251 | if (SS.isSet()) |
| 4252 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 4253 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4254 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4255 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4256 | // C++0x [temp.names]p5: |
| 4257 | // If a name prefixed by the keyword template is not the name of |
| 4258 | // a template, the program is ill-formed. [Note: the keyword |
| 4259 | // template may not be applied to non-template members of class |
| 4260 | // templates. -end note ] [ Note: as is the case with the |
| 4261 | // typename prefix, the template prefix is allowed in cases |
| 4262 | // where it is not strictly necessary; i.e., when the |
| 4263 | // nested-name-specifier or the expression on the left of the -> |
| 4264 | // or . is not dependent on a template-parameter, or the use |
| 4265 | // does not appear in the scope of a template. -end note] |
| 4266 | // |
| 4267 | // Note: C++03 was more strict here, because it banned the use of |
| 4268 | // the "template" keyword prior to a template-name that was not a |
| 4269 | // dependent name. C++ DR468 relaxed this requirement (the |
| 4270 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 4271 | // 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] | 4272 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 4273 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 4274 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4275 | MemberOfUnknownSpecialization); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4276 | if (TNK == TNK_Non_template && MemberOfUnknownSpecialization) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4277 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4278 | } else if (TNK == TNK_Non_template) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4279 | // Do the lookup again to determine if this is a "nothing found" case or |
| 4280 | // a "not a template" case. FIXME: Refactor isTemplateName so we don't |
| 4281 | // need to do this. |
| 4282 | DeclarationNameInfo DNI = GetNameFromUnqualifiedId(Name); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4283 | LookupResult R(*this, DNI.getName(), Name.getBeginLoc(), |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4284 | LookupOrdinaryName); |
| 4285 | bool MOUS; |
| 4286 | if (!LookupTemplateName(R, S, SS, ObjectType.get(), EnteringContext, |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame^] | 4287 | MOUS, TemplateKWLoc) && !R.isAmbiguous()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4288 | Diag(Name.getBeginLoc(), diag::err_no_member) |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4289 | << DNI.getName() << LookupCtx << SS.getRange(); |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4290 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4291 | } else { |
| 4292 | // We found something; return it. |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4293 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
| 4294 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4295 | Name.getKind() == UnqualifiedIdKind::IK_Identifier && |
| 4296 | Name.Identifier && LookupRD->getIdentifier() == Name.Identifier) { |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4297 | // C++14 [class.qual]p2: |
| 4298 | // In a lookup in which function names are not ignored and the |
| 4299 | // nested-name-specifier nominates a class C, if the name specified |
| 4300 | // [...] is the injected-class-name of C, [...] the name is instead |
| 4301 | // considered to name the constructor |
| 4302 | // |
| 4303 | // We don't get here if naming the constructor would be valid, so we |
| 4304 | // just reject immediately and recover by treating the |
| 4305 | // injected-class-name as naming the template. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4306 | Diag(Name.getBeginLoc(), |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4307 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4308 | << Name.Identifier |
| 4309 | << 0 /*injected-class-name used as template name*/ |
| 4310 | << 1 /*'template' keyword was used*/; |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4311 | } |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4312 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4313 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4316 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4317 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4318 | switch (Name.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4319 | case UnqualifiedIdKind::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4320 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4321 | Name.Identifier)); |
| 4322 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4324 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4325 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4326 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 4327 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4328 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4329 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 4330 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4331 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4332 | default: |
| 4333 | break; |
| 4334 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4335 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4336 | Diag(Name.getBeginLoc(), diag::err_template_kw_refers_to_non_template) |
| 4337 | << GetNameFromUnqualifiedId(Name).getName() << Name.getSourceRange() |
| 4338 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4339 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4342 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4343 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4344 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4345 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4346 | QualType ArgType; |
| 4347 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4348 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4349 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4350 | switch(Arg.getKind()) { |
| 4351 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4352 | // C++ [temp.arg.type]p1: |
| 4353 | // A template-argument for a template-parameter which is a |
| 4354 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4355 | ArgType = Arg.getAsType(); |
| 4356 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4357 | break; |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 4358 | case TemplateArgument::Template: |
| 4359 | case TemplateArgument::TemplateExpansion: { |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4360 | // We have a template type parameter but the template argument |
| 4361 | // is a template without any arguments. |
| 4362 | SourceRange SR = AL.getSourceRange(); |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 4363 | TemplateName Name = Arg.getAsTemplateOrTemplatePattern(); |
Richard Smith | ecad88d | 2018-04-26 01:08:00 +0000 | [diff] [blame] | 4364 | diagnoseMissingTemplateArguments(Name, SR.getEnd()); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4365 | return true; |
| 4366 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4367 | case TemplateArgument::Expression: { |
| 4368 | // We have a template type parameter but the template argument is an |
| 4369 | // expression; see if maybe it is missing the "typename" keyword. |
| 4370 | CXXScopeSpec SS; |
| 4371 | DeclarationNameInfo NameInfo; |
| 4372 | |
| 4373 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 4374 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4375 | NameInfo = ArgExpr->getNameInfo(); |
| 4376 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 4377 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 4378 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4379 | NameInfo = ArgExpr->getNameInfo(); |
| 4380 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 4381 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 4382 | if (ArgExpr->isImplicitAccess()) { |
| 4383 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4384 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 4385 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4388 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4389 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 4390 | LookupParsedName(Result, CurScope, &SS); |
| 4391 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 4392 | if (Result.getAsSingle<TypeDecl>() || |
| 4393 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4394 | LookupResult::NotFoundInCurrentInstantiation) { |
| 4395 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4396 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4397 | Diag(Loc, getLangOpts().MSVCCompat |
| 4398 | ? diag::ext_ms_template_type_arg_missing_typename |
| 4399 | : diag::err_template_arg_must_be_type_suggest) |
| 4400 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4401 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4402 | |
| 4403 | // Recover by synthesizing a type using the location information that we |
| 4404 | // already have. |
| 4405 | ArgType = |
| 4406 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 4407 | TypeLocBuilder TLB; |
| 4408 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 4409 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 4410 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 4411 | TL.setNameLoc(NameInfo.getLoc()); |
| 4412 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 4413 | |
| 4414 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 4415 | // properly. |
| 4416 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 4417 | TemplateArgumentLocInfo(TSI)); |
| 4418 | |
| 4419 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4420 | } |
| 4421 | } |
| 4422 | // fallthrough |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 4423 | LLVM_FALLTHROUGH; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4424 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4425 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4426 | // We have a template type parameter but the template argument |
| 4427 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 4428 | SourceRange SR = AL.getSourceRange(); |
| 4429 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4430 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4431 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4432 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4433 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4434 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4435 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4436 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4437 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4438 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4439 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4440 | ArgType = Context.getCanonicalType(ArgType); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4441 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4442 | // Objective-C ARC: |
| 4443 | // If an explicitly-specified template argument type is a lifetime type |
| 4444 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4445 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4446 | ArgType->isObjCLifetimeType() && |
| 4447 | !ArgType.getObjCLifetime()) { |
| 4448 | Qualifiers Qs; |
| 4449 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 4450 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 4451 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4453 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4454 | return false; |
| 4455 | } |
| 4456 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4457 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4458 | /// the given template type parameter. |
| 4459 | /// |
| 4460 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4461 | /// the substitution. |
| 4462 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4463 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4464 | /// for. |
| 4465 | /// |
| 4466 | /// \param TemplateLoc the location of the template name that started the |
| 4467 | /// template-id we are checking. |
| 4468 | /// |
| 4469 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4470 | /// terminates the template-id. |
| 4471 | /// |
| 4472 | /// \param Param the template template parameter whose default we are |
| 4473 | /// substituting into. |
| 4474 | /// |
| 4475 | /// \param Converted the list of template arguments provided for template |
| 4476 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4477 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4478 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4479 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4480 | TemplateDecl *Template, |
| 4481 | SourceLocation TemplateLoc, |
| 4482 | SourceLocation RAngleLoc, |
| 4483 | TemplateTypeParmDecl *Param, |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 4484 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4485 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4486 | |
| 4487 | // If the argument type is dependent, instantiate it now based |
| 4488 | // on the previously-computed template arguments. |
Erik Pilkington | ba88e21 | 2018-11-12 21:31:06 +0000 | [diff] [blame] | 4489 | if (ArgType->getType()->isInstantiationDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4490 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4491 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4492 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4493 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4494 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4496 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4497 | |
| 4498 | // Only substitute for the innermost template argument list. |
| 4499 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4500 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4501 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4502 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4503 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4504 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4505 | ArgType = |
| 4506 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 4507 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | return ArgType; |
| 4511 | } |
| 4512 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4513 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4514 | /// the given non-type template parameter. |
| 4515 | /// |
| 4516 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4517 | /// the substitution. |
| 4518 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4519 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4520 | /// for. |
| 4521 | /// |
| 4522 | /// \param TemplateLoc the location of the template name that started the |
| 4523 | /// template-id we are checking. |
| 4524 | /// |
| 4525 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4526 | /// terminates the template-id. |
| 4527 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4528 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4529 | /// substituting into. |
| 4530 | /// |
| 4531 | /// \param Converted the list of template arguments provided for template |
| 4532 | /// parameters that precede \p Param in the template parameter list. |
| 4533 | /// |
| 4534 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4535 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4536 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4537 | TemplateDecl *Template, |
| 4538 | SourceLocation TemplateLoc, |
| 4539 | SourceLocation RAngleLoc, |
| 4540 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4541 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4542 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4543 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4544 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4545 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4546 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4547 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4548 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4549 | |
| 4550 | // Only substitute for the innermost template argument list. |
| 4551 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4552 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4553 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4554 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4555 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4556 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 4557 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4558 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4561 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4562 | /// the given template template parameter. |
| 4563 | /// |
| 4564 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4565 | /// the substitution. |
| 4566 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4567 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4568 | /// for. |
| 4569 | /// |
| 4570 | /// \param TemplateLoc the location of the template name that started the |
| 4571 | /// template-id we are checking. |
| 4572 | /// |
| 4573 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4574 | /// terminates the template-id. |
| 4575 | /// |
| 4576 | /// \param Param the template template parameter whose default we are |
| 4577 | /// substituting into. |
| 4578 | /// |
| 4579 | /// \param Converted the list of template arguments provided for template |
| 4580 | /// parameters that precede \p Param in the template parameter list. |
| 4581 | /// |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4582 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4583 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4584 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4585 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 4586 | static TemplateName |
| 4587 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4588 | TemplateDecl *Template, |
| 4589 | SourceLocation TemplateLoc, |
| 4590 | SourceLocation RAngleLoc, |
| 4591 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4592 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4593 | NestedNameSpecifierLoc &QualifierLoc) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4594 | Sema::InstantiatingTemplate Inst( |
| 4595 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 4596 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4597 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4598 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4599 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4600 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4601 | |
| 4602 | // Only substitute for the innermost template argument list. |
| 4603 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4604 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4605 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4606 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4607 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4608 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4609 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4610 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4611 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4612 | QualifierLoc = |
| 4613 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4614 | if (!QualifierLoc) |
| 4615 | return TemplateName(); |
| 4616 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4617 | |
| 4618 | return SemaRef.SubstTemplateName( |
| 4619 | QualifierLoc, |
| 4620 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 4621 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 4622 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4625 | /// If the given template parameter has a default template |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4626 | /// argument, substitute into that default template argument and |
| 4627 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4628 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4629 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 4630 | SourceLocation TemplateLoc, |
| 4631 | SourceLocation RAngleLoc, |
| 4632 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4633 | SmallVectorImpl<TemplateArgument> |
| 4634 | &Converted, |
| 4635 | bool &HasDefaultArg) { |
| 4636 | HasDefaultArg = false; |
| 4637 | |
| 4638 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4639 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4640 | return TemplateArgumentLoc(); |
| 4641 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4642 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4643 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4644 | TemplateLoc, |
| 4645 | RAngleLoc, |
| 4646 | TypeParm, |
| 4647 | Converted); |
| 4648 | if (DI) |
| 4649 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 4650 | |
| 4651 | return TemplateArgumentLoc(); |
| 4652 | } |
| 4653 | |
| 4654 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 4655 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4656 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4657 | return TemplateArgumentLoc(); |
| 4658 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4659 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4660 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4661 | TemplateLoc, |
| 4662 | RAngleLoc, |
| 4663 | NonTypeParm, |
| 4664 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4665 | if (Arg.isInvalid()) |
| 4666 | return TemplateArgumentLoc(); |
| 4667 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4668 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4669 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 4670 | } |
| 4671 | |
| 4672 | TemplateTemplateParmDecl *TempTempParm |
| 4673 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4674 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4675 | return TemplateArgumentLoc(); |
| 4676 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4677 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4678 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4679 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4680 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4681 | RAngleLoc, |
| 4682 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4683 | Converted, |
| 4684 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4685 | if (TName.isNull()) |
| 4686 | return TemplateArgumentLoc(); |
| 4687 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4689 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4690 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 4691 | } |
| 4692 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4693 | /// Convert a template-argument that we parsed as a type into a template, if |
| 4694 | /// possible. C++ permits injected-class-names to perform dual service as |
| 4695 | /// template template arguments and as template type arguments. |
| 4696 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { |
| 4697 | // Extract and step over any surrounding nested-name-specifier. |
| 4698 | NestedNameSpecifierLoc QualLoc; |
| 4699 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { |
| 4700 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
| 4701 | return TemplateArgumentLoc(); |
| 4702 | |
| 4703 | QualLoc = ETLoc.getQualifierLoc(); |
| 4704 | TLoc = ETLoc.getNamedTypeLoc(); |
| 4705 | } |
| 4706 | |
| 4707 | // If this type was written as an injected-class-name, it can be used as a |
| 4708 | // template template argument. |
| 4709 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) |
| 4710 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), |
| 4711 | QualLoc, InjLoc.getNameLoc()); |
| 4712 | |
| 4713 | // If this type was written as an injected-class-name, it may have been |
| 4714 | // converted to a RecordType during instantiation. If the RecordType is |
| 4715 | // *not* wrapped in a TemplateSpecializationType and denotes a class |
| 4716 | // template specialization, it must have come from an injected-class-name. |
| 4717 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) |
| 4718 | if (auto *CTSD = |
| 4719 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
| 4720 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), |
| 4721 | QualLoc, RecLoc.getNameLoc()); |
| 4722 | |
| 4723 | return TemplateArgumentLoc(); |
| 4724 | } |
| 4725 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4726 | /// Check that the given template argument corresponds to the given |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4727 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4728 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4729 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4730 | /// checked. |
| 4731 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4732 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4733 | /// |
| 4734 | /// \param Template The template in which the template argument resides. |
| 4735 | /// |
| 4736 | /// \param TemplateLoc The location of the template name for the template |
| 4737 | /// whose argument list we're matching. |
| 4738 | /// |
| 4739 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 4740 | /// the template argument list. |
| 4741 | /// |
| 4742 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 4743 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 4744 | /// |
| 4745 | /// \param Converted The checked, converted argument will be added to the |
| 4746 | /// end of this small vector. |
| 4747 | /// |
| 4748 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 4749 | /// explicitly written, deduced, etc. |
| 4750 | /// |
| 4751 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4752 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4753 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4754 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4755 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4756 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4757 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4758 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4759 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4760 | // Check template type parameters. |
| 4761 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4762 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4764 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4765 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4766 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4767 | // with the template arguments we've seen thus far. But if the |
| 4768 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4769 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4770 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 4771 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4772 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4773 | // FIXME: Do we need to substitute into parameters here if they're |
| 4774 | // instantiation-dependent but not dependent? |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4775 | if (NTTPType->isDependentType() && |
| 4776 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4777 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4778 | // Do substitution on the type of the non-type template parameter. |
| 4779 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4780 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4781 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4782 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4783 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4784 | |
| 4785 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4786 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4787 | NTTPType = SubstType(NTTPType, |
| 4788 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 4789 | NTTP->getLocation(), |
| 4790 | NTTP->getDeclName()); |
| 4791 | // If that worked, check the non-type template parameter type |
| 4792 | // for validity. |
| 4793 | if (!NTTPType.isNull()) |
| 4794 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 4795 | NTTP->getLocation()); |
| 4796 | if (NTTPType.isNull()) |
| 4797 | return true; |
| 4798 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4799 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4800 | switch (Arg.getArgument().getKind()) { |
| 4801 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4802 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4803 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4804 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4805 | TemplateArgument Result; |
Erich Keane | c90bb6d | 2018-05-07 17:05:20 +0000 | [diff] [blame] | 4806 | unsigned CurSFINAEErrors = NumSFINAEErrors; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4807 | ExprResult Res = |
| 4808 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 4809 | Result, CTAK); |
| 4810 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4811 | return true; |
Erich Keane | c90bb6d | 2018-05-07 17:05:20 +0000 | [diff] [blame] | 4812 | // If the current template argument causes an error, give up now. |
| 4813 | if (CurSFINAEErrors < NumSFINAEErrors) |
| 4814 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4815 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4816 | // If the resulting expression is new, then use it in place of the |
| 4817 | // old expression in the template argument. |
| 4818 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 4819 | TemplateArgument TA(Res.get()); |
| 4820 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 4821 | } |
| 4822 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4823 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4824 | break; |
| 4825 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4826 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4827 | case TemplateArgument::Declaration: |
| 4828 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4829 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4830 | // We've already checked this template argument, so just copy |
| 4831 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4832 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4833 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4834 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4835 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4836 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4837 | // We were given a template template argument. It may not be ill-formed; |
| 4838 | // see below. |
| 4839 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4840 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 4841 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4842 | // We have a template argument such as \c T::template X, which we |
| 4843 | // parsed as a template template argument. However, since we now |
| 4844 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4845 | // template name into an expression. |
| 4846 | |
| 4847 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 4848 | Arg.getTemplateNameLoc()); |
| 4849 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 4850 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4851 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4852 | // FIXME: the template-template arg was a DependentTemplateName, |
| 4853 | // so it was provided with a template keyword. However, its source |
| 4854 | // location is not stored in the template argument structure. |
| 4855 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4856 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 4857 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 4858 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4859 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4860 | // If we parsed the template argument as a pack expansion, create a |
| 4861 | // pack expansion expression. |
| 4862 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4863 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4864 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4865 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4866 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4867 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4868 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4869 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4870 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4871 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4872 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4873 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4874 | break; |
| 4875 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4876 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4877 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4878 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4879 | // therefore cannot be a non-type template argument. |
| 4880 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 4881 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4882 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4883 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4884 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4885 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4886 | case TemplateArgument::Type: { |
| 4887 | // We have a non-type template parameter but the template |
| 4888 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4889 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4890 | // C++ [temp.arg]p2: |
| 4891 | // In a template-argument, an ambiguity between a type-id and |
| 4892 | // an expression is resolved to a type-id, regardless of the |
| 4893 | // form of the corresponding template-parameter. |
| 4894 | // |
| 4895 | // We warn specifically about this case, since it can be rather |
| 4896 | // confusing for users. |
| 4897 | QualType T = Arg.getArgument().getAsType(); |
| 4898 | SourceRange SR = Arg.getSourceRange(); |
| 4899 | if (T->isFunctionType()) |
| 4900 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 4901 | else |
| 4902 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 4903 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4904 | return true; |
| 4905 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4906 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4907 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4908 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4909 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4910 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4911 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4915 | // Check template template parameters. |
| 4916 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4917 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4918 | TemplateParameterList *Params = TempParm->getTemplateParameters(); |
| 4919 | if (TempParm->isExpandedParameterPack()) |
| 4920 | Params = TempParm->getExpansionTemplateParameters(ArgumentPackIndex); |
| 4921 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4922 | // Substitute into the template parameter list of the template |
| 4923 | // template parameter, since previously-supplied template arguments |
| 4924 | // may appear within the template template parameter. |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4925 | // |
| 4926 | // FIXME: Skip this if the parameters aren't instantiation-dependent. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4927 | { |
| 4928 | // Set up a template instantiation context. |
| 4929 | LocalInstantiationScope Scope(*this); |
| 4930 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4931 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4932 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4933 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4934 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4936 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4937 | Params = SubstTemplateParams(Params, CurContext, |
| 4938 | MultiLevelTemplateArgumentList(TemplateArgs)); |
| 4939 | if (!Params) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4940 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4941 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4942 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4943 | // C++1z [temp.local]p1: (DR1004) |
| 4944 | // When [the injected-class-name] is used [...] as a template-argument for |
| 4945 | // a template template-parameter [...] it refers to the class template |
| 4946 | // itself. |
| 4947 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { |
| 4948 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
| 4949 | Arg.getTypeSourceInfo()->getTypeLoc()); |
| 4950 | if (!ConvertedArg.getArgument().isNull()) |
| 4951 | Arg = ConvertedArg; |
| 4952 | } |
| 4953 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4954 | switch (Arg.getArgument().getKind()) { |
| 4955 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4956 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4958 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4959 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4960 | if (CheckTemplateTemplateArgument(Params, Arg)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4961 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4962 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4963 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4964 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4966 | case TemplateArgument::Expression: |
| 4967 | case TemplateArgument::Type: |
| 4968 | // We have a template template parameter but the template |
| 4969 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4970 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4971 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4972 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4973 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4974 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4975 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4976 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4977 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4978 | case TemplateArgument::NullPtr: |
| 4979 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4980 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4981 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4982 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4983 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4984 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4985 | return false; |
| 4986 | } |
| 4987 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4988 | /// Check whether the template parameter is a pack expansion, and if so, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4989 | /// determine the number of parameters produced by that expansion. For instance: |
| 4990 | /// |
| 4991 | /// \code |
| 4992 | /// template<typename ...Ts> struct A { |
| 4993 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 4994 | /// }; |
| 4995 | /// \endcode |
| 4996 | /// |
| 4997 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 4998 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4999 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5000 | if (NonTypeTemplateParmDecl *NTTP |
| 5001 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 5002 | if (NTTP->isExpandedParameterPack()) |
| 5003 | return NTTP->getNumExpansionTypes(); |
| 5004 | } |
| 5005 | |
| 5006 | if (TemplateTemplateParmDecl *TTP |
| 5007 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 5008 | if (TTP->isExpandedParameterPack()) |
| 5009 | return TTP->getNumExpansionTemplateParameters(); |
| 5010 | } |
| 5011 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 5012 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5015 | /// Diagnose a missing template argument. |
| 5016 | template<typename TemplateParmDecl> |
| 5017 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 5018 | TemplateDecl *TD, |
| 5019 | const TemplateParmDecl *D, |
| 5020 | TemplateArgumentListInfo &Args) { |
| 5021 | // Dig out the most recent declaration of the template parameter; there may be |
| 5022 | // declarations of the template that are more recent than TD. |
| 5023 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 5024 | ->getTemplateParameters() |
| 5025 | ->getParam(D->getIndex())); |
| 5026 | |
| 5027 | // If there's a default argument that's not visible, diagnose that we're |
| 5028 | // missing a module import. |
| 5029 | llvm::SmallVector<Module*, 8> Modules; |
| 5030 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 5031 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 5032 | D->getDefaultArgumentLoc(), Modules, |
| 5033 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 5034 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5035 | return true; |
| 5036 | } |
| 5037 | |
| 5038 | // FIXME: If there's a more recent default argument that *is* visible, |
| 5039 | // diagnose that it was declared too late. |
| 5040 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5041 | TemplateParameterList *Params = TD->getTemplateParameters(); |
| 5042 | |
| 5043 | S.Diag(Loc, diag::err_template_arg_list_different_arity) |
| 5044 | << /*not enough args*/0 |
| 5045 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(TD)) |
| 5046 | << TD; |
| 5047 | S.Diag(TD->getLocation(), diag::note_template_decl_here) |
| 5048 | << Params->getSourceRange(); |
| 5049 | return true; |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5052 | /// Check that the given template argument list is well-formed |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5053 | /// for specializing the given template. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5054 | bool Sema::CheckTemplateArgumentList( |
| 5055 | TemplateDecl *Template, SourceLocation TemplateLoc, |
| 5056 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
| 5057 | SmallVectorImpl<TemplateArgument> &Converted, |
| 5058 | bool UpdateArgsWithConversions) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5059 | // Make a copy of the template arguments for processing. Only make the |
| 5060 | // changes at the end when successful in matching the arguments to the |
| 5061 | // template. |
| 5062 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 5063 | |
Erich Keane | af0795b | 2017-10-24 01:39:56 +0000 | [diff] [blame] | 5064 | // Make sure we get the template parameter list from the most |
| 5065 | // recentdeclaration, since that is the only one that has is guaranteed to |
| 5066 | // have all the default template argument information. |
| 5067 | TemplateParameterList *Params = |
| 5068 | cast<TemplateDecl>(Template->getMostRecentDecl()) |
| 5069 | ->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5070 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5071 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5072 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5073 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5074 | // [...] The type and form of each template-argument specified in |
| 5075 | // a template-id shall match the type and form specified for the |
| 5076 | // corresponding parameter declared by the template in its |
| 5077 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5078 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5079 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5080 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 5081 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5082 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5083 | ParamEnd = Params->end(); |
| 5084 | Param != ParamEnd; /* increment in loop */) { |
| 5085 | // If we have an expanded parameter pack, make sure we don't have too |
| 5086 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 5087 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5088 | if (*Expansions == ArgumentPack.size()) { |
| 5089 | // We're done with this parameter pack. Pack up its arguments and add |
| 5090 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5091 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5092 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5093 | ArgumentPack.clear(); |
| 5094 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5095 | // This argument is assigned to the next parameter. |
| 5096 | ++Param; |
| 5097 | continue; |
| 5098 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 5099 | // Not enough arguments for this parameter pack. |
| 5100 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5101 | << /*not enough args*/0 |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 5102 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5103 | << Template; |
| 5104 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5105 | << Params->getSourceRange(); |
| 5106 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5107 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5108 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5109 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5110 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5111 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5112 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5113 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5114 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5115 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5116 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5117 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5118 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5119 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 5120 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5121 | // 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] | 5122 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5123 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5124 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5125 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5126 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5127 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 5128 | return true; |
| 5129 | } |
| 5130 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5131 | // We're now done with this argument. |
| 5132 | ++ArgIdx; |
| 5133 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5134 | if ((*Param)->isTemplateParameterPack()) { |
| 5135 | // The template parameter was a template parameter pack, so take the |
| 5136 | // deduced argument and place it on the argument pack. Note that we |
| 5137 | // stay on the same template parameter so that we can deduce more |
| 5138 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 5139 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5140 | } else { |
| 5141 | // Move to the next template parameter. |
| 5142 | ++Param; |
| 5143 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5144 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5145 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 5146 | // the remaining arguments, because we don't know what parameters they'll |
| 5147 | // match up with. |
| 5148 | if (PackExpansionIntoNonPack) { |
| 5149 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5150 | // If we were part way through filling in an expanded parameter pack, |
| 5151 | // fall back to just producing individual arguments. |
| 5152 | Converted.insert(Converted.end(), |
| 5153 | ArgumentPack.begin(), ArgumentPack.end()); |
| 5154 | ArgumentPack.clear(); |
| 5155 | } |
| 5156 | |
| 5157 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5158 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5159 | ++ArgIdx; |
| 5160 | } |
| 5161 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5162 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5163 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5164 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5165 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5166 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5167 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5168 | // If we're checking a partial template argument list, we're done. |
| 5169 | if (PartialTemplateArgs) { |
| 5170 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5171 | Converted.push_back( |
| 5172 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 5173 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5174 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5177 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5178 | // 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] | 5179 | if ((*Param)->isTemplateParameterPack()) { |
| 5180 | assert(!getExpandedPackSize(*Param) && |
| 5181 | "Should have dealt with this already"); |
| 5182 | |
| 5183 | // A non-expanded parameter pack before the end of the parameter list |
| 5184 | // only occurs for an ill-formed template parameter list, unless we've |
| 5185 | // got a partial argument list for a function template, so just bail out. |
| 5186 | if (Param + 1 != ParamEnd) |
| 5187 | return true; |
| 5188 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5189 | Converted.push_back( |
| 5190 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5191 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5192 | |
| 5193 | ++Param; |
| 5194 | continue; |
| 5195 | } |
| 5196 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5197 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5198 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5199 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5200 | // Retrieve the default template argument from the template |
| 5201 | // parameter. For each kind of template parameter, we substitute the |
| 5202 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5203 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5204 | // the default argument. |
| 5205 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5206 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5207 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 5208 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5209 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5210 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5211 | Template, |
| 5212 | TemplateLoc, |
| 5213 | RAngleLoc, |
| 5214 | TTP, |
| 5215 | Converted); |
| 5216 | if (!ArgType) |
| 5217 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5218 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5219 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 5220 | ArgType); |
| 5221 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5222 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5223 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5224 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 5225 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5226 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5227 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5228 | TemplateLoc, |
| 5229 | RAngleLoc, |
| 5230 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5231 | Converted); |
| 5232 | if (E.isInvalid()) |
| 5233 | return true; |
| 5234 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5235 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5236 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 5237 | } else { |
| 5238 | TemplateTemplateParmDecl *TempParm |
| 5239 | = cast<TemplateTemplateParmDecl>(*Param); |
| 5240 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5241 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5242 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 5243 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5244 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 5245 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5246 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5247 | TemplateLoc, |
| 5248 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5249 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5250 | Converted, |
| 5251 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5252 | if (Name.isNull()) |
| 5253 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5254 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5255 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 5256 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5257 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5258 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5259 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 5260 | // the default template argument. We're not actually instantiating a |
| 5261 | // template here, we just create this object to put a note into the |
| 5262 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 5263 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 5264 | SourceRange(TemplateLoc, RAngleLoc)); |
| 5265 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 5266 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5267 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5268 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 5269 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5270 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5271 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5272 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5273 | // Core issue 150 (assumed resolution): if this is a template template |
| 5274 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5275 | // template definition. |
| 5276 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5277 | NewArgs.addArgument(Arg); |
| 5278 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5279 | // Move to the next template parameter and argument. |
| 5280 | ++Param; |
| 5281 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5282 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5283 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5284 | // If we're performing a partial argument substitution, allow any trailing |
| 5285 | // pack expansions; they might be empty. This can happen even if |
| 5286 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 5287 | // still dependent). |
| 5288 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 5289 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5290 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 5291 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5294 | // If we have any leftover arguments, then there were too many arguments. |
| 5295 | // Complain and fail. |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5296 | if (ArgIdx < NumArgs) { |
| 5297 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 5298 | << /*too many args*/1 |
| 5299 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
| 5300 | << Template |
| 5301 | << SourceRange(NewArgs[ArgIdx].getLocation(), NewArgs.getRAngleLoc()); |
| 5302 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5303 | << Params->getSourceRange(); |
| 5304 | return true; |
| 5305 | } |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5306 | |
| 5307 | // No problems found with the new argument list, propagate changes back |
| 5308 | // to caller. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5309 | if (UpdateArgsWithConversions) |
| 5310 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5311 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5312 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5313 | } |
| 5314 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5315 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5316 | class UnnamedLocalNoLinkageFinder |
| 5317 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5318 | { |
| 5319 | Sema &S; |
| 5320 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5321 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5322 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5323 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5324 | public: |
| 5325 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 5326 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5327 | bool Visit(QualType T) { |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5328 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5329 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5330 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5331 | #define TYPE(Class, Parent) \ |
| 5332 | bool Visit##Class##Type(const Class##Type *); |
| 5333 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 5334 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5335 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 5336 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5337 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5338 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5339 | bool VisitTagDecl(const TagDecl *Tag); |
| 5340 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 5341 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 5342 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5343 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5344 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5345 | return false; |
| 5346 | } |
| 5347 | |
| 5348 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 5349 | return Visit(T->getElementType()); |
| 5350 | } |
| 5351 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5352 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5353 | return Visit(T->getPointeeType()); |
| 5354 | } |
| 5355 | |
| 5356 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5357 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5358 | return Visit(T->getPointeeType()); |
| 5359 | } |
| 5360 | |
| 5361 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5362 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5363 | return Visit(T->getPointeeType()); |
| 5364 | } |
| 5365 | |
| 5366 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5367 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5368 | return Visit(T->getPointeeType()); |
| 5369 | } |
| 5370 | |
| 5371 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5372 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5373 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 5374 | } |
| 5375 | |
| 5376 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5377 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5378 | return Visit(T->getElementType()); |
| 5379 | } |
| 5380 | |
| 5381 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5382 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5383 | return Visit(T->getElementType()); |
| 5384 | } |
| 5385 | |
| 5386 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5387 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5388 | return Visit(T->getElementType()); |
| 5389 | } |
| 5390 | |
| 5391 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5392 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5393 | return Visit(T->getElementType()); |
| 5394 | } |
| 5395 | |
| 5396 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5397 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5398 | return Visit(T->getElementType()); |
| 5399 | } |
| 5400 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 5401 | bool UnnamedLocalNoLinkageFinder::VisitDependentAddressSpaceType( |
| 5402 | const DependentAddressSpaceType *T) { |
| 5403 | return Visit(T->getPointeeType()); |
| 5404 | } |
| 5405 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5406 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 5407 | return Visit(T->getElementType()); |
| 5408 | } |
| 5409 | |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 5410 | bool UnnamedLocalNoLinkageFinder::VisitDependentVectorType( |
| 5411 | const DependentVectorType *T) { |
| 5412 | return Visit(T->getElementType()); |
| 5413 | } |
| 5414 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5415 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 5416 | return Visit(T->getElementType()); |
| 5417 | } |
| 5418 | |
| 5419 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 5420 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 5421 | for (const auto &A : T->param_types()) { |
| 5422 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5423 | return true; |
| 5424 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5425 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5426 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 5430 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5431 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 5435 | const UnresolvedUsingType*) { |
| 5436 | return false; |
| 5437 | } |
| 5438 | |
| 5439 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 5440 | return false; |
| 5441 | } |
| 5442 | |
| 5443 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 5444 | return Visit(T->getUnderlyingType()); |
| 5445 | } |
| 5446 | |
| 5447 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 5448 | return false; |
| 5449 | } |
| 5450 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5451 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 5452 | const UnaryTransformType*) { |
| 5453 | return false; |
| 5454 | } |
| 5455 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5456 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 5457 | return Visit(T->getDeducedType()); |
| 5458 | } |
| 5459 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5460 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
| 5461 | const DeducedTemplateSpecializationType *T) { |
| 5462 | return Visit(T->getDeducedType()); |
| 5463 | } |
| 5464 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5465 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 5466 | return VisitTagDecl(T->getDecl()); |
| 5467 | } |
| 5468 | |
| 5469 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 5470 | return VisitTagDecl(T->getDecl()); |
| 5471 | } |
| 5472 | |
| 5473 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 5474 | const TemplateTypeParmType*) { |
| 5475 | return false; |
| 5476 | } |
| 5477 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 5478 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 5479 | const SubstTemplateTypeParmPackType *) { |
| 5480 | return false; |
| 5481 | } |
| 5482 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5483 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 5484 | const TemplateSpecializationType*) { |
| 5485 | return false; |
| 5486 | } |
| 5487 | |
| 5488 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 5489 | const InjectedClassNameType* T) { |
| 5490 | return VisitTagDecl(T->getDecl()); |
| 5491 | } |
| 5492 | |
| 5493 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 5494 | const DependentNameType* T) { |
| 5495 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5496 | } |
| 5497 | |
| 5498 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 5499 | const DependentTemplateSpecializationType* T) { |
| 5500 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5501 | } |
| 5502 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5503 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 5504 | const PackExpansionType* T) { |
| 5505 | return Visit(T->getPattern()); |
| 5506 | } |
| 5507 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5508 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 5509 | return false; |
| 5510 | } |
| 5511 | |
| 5512 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 5513 | const ObjCInterfaceType *) { |
| 5514 | return false; |
| 5515 | } |
| 5516 | |
| 5517 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 5518 | const ObjCObjectPointerType *) { |
| 5519 | return false; |
| 5520 | } |
| 5521 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5522 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 5523 | return Visit(T->getValueType()); |
| 5524 | } |
| 5525 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5526 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 5527 | return false; |
| 5528 | } |
| 5529 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5530 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 5531 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5532 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5533 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5534 | diag::warn_cxx98_compat_template_arg_local_type : |
| 5535 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5536 | << S.Context.getTypeDeclType(Tag) << SR; |
| 5537 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 5540 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5541 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5542 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5543 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 5544 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5545 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 5546 | return true; |
| 5547 | } |
| 5548 | |
| 5549 | return false; |
| 5550 | } |
| 5551 | |
| 5552 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 5553 | NestedNameSpecifier *NNS) { |
| 5554 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 5555 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5556 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5557 | switch (NNS->getKind()) { |
| 5558 | case NestedNameSpecifier::Identifier: |
| 5559 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5560 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5561 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5562 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5563 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5564 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5565 | case NestedNameSpecifier::TypeSpec: |
| 5566 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 5567 | return Visit(QualType(NNS->getAsType(), 0)); |
| 5568 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5569 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5572 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5573 | /// template type parameter. |
| 5574 | /// |
| 5575 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 5576 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5577 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5578 | TypeSourceInfo *ArgInfo) { |
| 5579 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5580 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 5581 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 5582 | |
| 5583 | if (Arg->isVariablyModifiedType()) { |
| 5584 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5585 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5586 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5587 | } |
| 5588 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5589 | // C++03 [temp.arg.type]p2: |
| 5590 | // A local type, a type with no linkage, an unnamed type or a type |
| 5591 | // compounded from any of these types shall not be used as a |
| 5592 | // template-argument for a template type-parameter. |
| 5593 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5594 | // 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] | 5595 | // a warning. |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5596 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5597 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 5598 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 5599 | } |
| 5600 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5601 | return false; |
| 5602 | } |
| 5603 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5604 | enum NullPointerValueKind { |
| 5605 | NPV_NotNullPointer, |
| 5606 | NPV_NullPointer, |
| 5607 | NPV_Error |
| 5608 | }; |
| 5609 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5610 | /// Determine whether the given template argument is a null pointer |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5611 | /// value of the appropriate type. |
| 5612 | static NullPointerValueKind |
| 5613 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5614 | QualType ParamType, Expr *Arg, |
| 5615 | Decl *Entity = nullptr) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5616 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 5617 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5618 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5619 | // dllimport'd entities aren't constant but are available inside of template |
| 5620 | // arguments. |
| 5621 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 5622 | return NPV_NotNullPointer; |
| 5623 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5624 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 5625 | llvm_unreachable( |
| 5626 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5627 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 5628 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5629 | return NPV_NotNullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5630 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5631 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5632 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 5633 | if (ArgRV.isInvalid()) |
| 5634 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5635 | Arg = ArgRV.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5636 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5637 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5638 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5639 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5640 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5641 | EvalResult.HasSideEffects) { |
| 5642 | SourceLocation DiagLoc = Arg->getExprLoc(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5643 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5644 | // If our only note is the usual "invalid subexpression" note, just point |
| 5645 | // the caret at its location rather than producing an essentially |
| 5646 | // redundant note. |
| 5647 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 5648 | diag::note_invalid_subexpr_in_const_expr) { |
| 5649 | DiagLoc = Notes[0].first; |
| 5650 | Notes.clear(); |
| 5651 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5652 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5653 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 5654 | << Arg->getType() << Arg->getSourceRange(); |
| 5655 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 5656 | S.Diag(Notes[I].first, Notes[I].second); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5657 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5658 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5659 | return NPV_Error; |
| 5660 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5661 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5662 | // C++11 [temp.arg.nontype]p1: |
| 5663 | // - an address constant expression of type std::nullptr_t |
| 5664 | if (Arg->getType()->isNullPtrType()) |
| 5665 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5666 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5667 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 5668 | // - a constant expression that evaluates to a null member pointer value |
| 5669 | // (4.11); or |
| 5670 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 5671 | (EvalResult.Val.isMemberPointer() && |
| 5672 | !EvalResult.Val.getMemberPointerDecl())) { |
| 5673 | // If our expression has an appropriate type, we've succeeded. |
| 5674 | bool ObjCLifetimeConversion; |
| 5675 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 5676 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 5677 | ObjCLifetimeConversion)) |
| 5678 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5679 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5680 | // The types didn't match, but we know we got a null pointer; complain, |
| 5681 | // then recover as if the types were correct. |
| 5682 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 5683 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 5684 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5685 | return NPV_NullPointer; |
| 5686 | } |
| 5687 | |
| 5688 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 5689 | // constant, suggest a cast to the appropriate type. |
| 5690 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 5691 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 5692 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5693 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), Code) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5694 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getEndLoc()), |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5695 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5696 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5697 | return NPV_NullPointer; |
| 5698 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5699 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5700 | // FIXME: If we ever want to support general, address-constant expressions |
| 5701 | // as non-type template arguments, we should return the ExprResult here to |
| 5702 | // be interpreted by the caller. |
| 5703 | return NPV_NotNullPointer; |
| 5704 | } |
| 5705 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5706 | /// Checks whether the given template argument is compatible with its |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5707 | /// template parameter. |
| 5708 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 5709 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 5710 | Expr *Arg, QualType ArgType) { |
| 5711 | bool ObjCLifetimeConversion; |
| 5712 | if (ParamType->isPointerType() && |
| 5713 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 5714 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 5715 | ObjCLifetimeConversion)) { |
| 5716 | // For pointer-to-object types, qualification conversions are |
| 5717 | // permitted. |
| 5718 | } else { |
| 5719 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 5720 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 5721 | // C++ [temp.arg.nontype]p5b3: |
| 5722 | // For a non-type template-parameter of type reference to |
| 5723 | // object, no conversions apply. The type referred to by the |
| 5724 | // reference may be more cv-qualified than the (otherwise |
| 5725 | // identical) type of the template- argument. The |
| 5726 | // template-parameter is bound directly to the |
| 5727 | // template-argument, which shall be an lvalue. |
| 5728 | |
| 5729 | // FIXME: Other qualifiers? |
| 5730 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 5731 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 5732 | |
| 5733 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5734 | S.Diag(Arg->getBeginLoc(), |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5735 | diag::err_template_arg_ref_bind_ignores_quals) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5736 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5737 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5738 | return true; |
| 5739 | } |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | // At this point, the template argument refers to an object or |
| 5744 | // function with external linkage. We now need to check whether the |
| 5745 | // argument and parameter types are compatible. |
| 5746 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 5747 | ParamType.getNonReferenceType())) { |
| 5748 | // We can't perform this conversion or binding. |
| 5749 | if (ParamType->isReferenceType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5750 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_no_ref_bind) |
| 5751 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5752 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5753 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 5754 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5755 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5756 | return true; |
| 5757 | } |
| 5758 | } |
| 5759 | |
| 5760 | return false; |
| 5761 | } |
| 5762 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5763 | /// Checks whether the given template argument is the address |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5764 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5765 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5766 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 5767 | NonTypeTemplateParmDecl *Param, |
| 5768 | QualType ParamType, |
| 5769 | Expr *ArgIn, |
| 5770 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5771 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5772 | Expr *Arg = ArgIn; |
| 5773 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5774 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5775 | bool AddressTaken = false; |
| 5776 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5777 | if (S.getLangOpts().MicrosoftExt) { |
| 5778 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 5779 | // dereference and address-of operators. |
| 5780 | Arg = Arg->IgnoreParenCasts(); |
| 5781 | |
| 5782 | bool ExtWarnMSTemplateArg = false; |
| 5783 | UnaryOperatorKind FirstOpKind; |
| 5784 | SourceLocation FirstOpLoc; |
| 5785 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5786 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 5787 | if (UnOpKind == UO_Deref) |
| 5788 | ExtWarnMSTemplateArg = true; |
| 5789 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 5790 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 5791 | if (!AddrOpLoc.isValid()) { |
| 5792 | FirstOpKind = UnOpKind; |
| 5793 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 5794 | } |
| 5795 | } else |
| 5796 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5797 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5798 | if (FirstOpLoc.isValid()) { |
| 5799 | if (ExtWarnMSTemplateArg) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5800 | S.Diag(ArgIn->getBeginLoc(), diag::ext_ms_deref_template_argument) |
| 5801 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5802 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5803 | if (FirstOpKind == UO_AddrOf) |
| 5804 | AddressTaken = true; |
| 5805 | else if (Arg->getType()->isPointerType()) { |
| 5806 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 5807 | // constant expression. |
| 5808 | assert(FirstOpKind == UO_Deref); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5809 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 5810 | << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5811 | } |
| 5812 | } |
| 5813 | } else { |
| 5814 | // See through any implicit casts we added to fix the type. |
| 5815 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5816 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5817 | // C++ [temp.arg.nontype]p1: |
| 5818 | // |
| 5819 | // A template-argument for a non-type, non-template |
| 5820 | // template-parameter shall be one of: [...] |
| 5821 | // |
| 5822 | // -- the address of an object or function with external |
| 5823 | // linkage, including function templates and function |
| 5824 | // template-ids but excluding non-static class members, |
| 5825 | // expressed as & id-expression where the & is optional if |
| 5826 | // the name refers to a function or array, or if the |
| 5827 | // corresponding template-parameter is a reference; or |
| 5828 | |
| 5829 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 5830 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 5831 | bool ExtraParens = false; |
| 5832 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 5833 | if (!Invalid && !ExtraParens) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5834 | S.Diag(Arg->getBeginLoc(), |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5835 | S.getLangOpts().CPlusPlus11 |
| 5836 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 5837 | : diag::ext_template_arg_extra_parens) |
| 5838 | << Arg->getSourceRange(); |
| 5839 | ExtraParens = true; |
| 5840 | } |
| 5841 | |
| 5842 | Arg = Parens->getSubExpr(); |
| 5843 | } |
| 5844 | |
| 5845 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5846 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5847 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5848 | |
| 5849 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5850 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 5851 | Arg = UnOp->getSubExpr(); |
| 5852 | AddressTaken = true; |
| 5853 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 5854 | } |
| 5855 | } |
| 5856 | |
| 5857 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5858 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5859 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5860 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5861 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5862 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 5863 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 5864 | |
| 5865 | // If our parameter has pointer type, check for a null template value. |
| 5866 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5867 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn, |
| 5868 | Entity)) { |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5869 | case NPV_NullPointer: |
| 5870 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5871 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 5872 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5873 | return false; |
| 5874 | |
| 5875 | case NPV_Error: |
| 5876 | return true; |
| 5877 | |
| 5878 | case NPV_NotNullPointer: |
| 5879 | break; |
| 5880 | } |
| 5881 | } |
| 5882 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5883 | // Stop checking the precise nature of the argument if it is value dependent, |
| 5884 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5885 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5886 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5887 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5888 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5889 | |
| 5890 | if (isa<CXXUuidofExpr>(Arg)) { |
| 5891 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 5892 | ArgIn, Arg, ArgType)) |
| 5893 | return true; |
| 5894 | |
| 5895 | Converted = TemplateArgument(ArgIn); |
| 5896 | return false; |
| 5897 | } |
| 5898 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5899 | if (!DRE) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5900 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 5901 | << Arg->getSourceRange(); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5902 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5903 | return true; |
| 5904 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5905 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5906 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 5907 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5908 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_field) |
| 5909 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5910 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5911 | return true; |
| 5912 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5913 | |
| 5914 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5915 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5916 | if (!Method->isStatic()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5917 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_method) |
| 5918 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5919 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5920 | return true; |
| 5921 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5922 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5923 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5924 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 5925 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5926 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5927 | // A non-type template argument must refer to an object or function. |
| 5928 | if (!Func && !Var) { |
| 5929 | // We found something, but we don't know specifically what it is. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5930 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_object_or_func) |
| 5931 | << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5932 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 5933 | return true; |
| 5934 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5935 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5936 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5937 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5938 | S.Diag(Arg->getBeginLoc(), |
| 5939 | S.getLangOpts().CPlusPlus11 |
| 5940 | ? diag::warn_cxx98_compat_template_arg_object_internal |
| 5941 | : diag::ext_template_arg_object_internal) |
| 5942 | << !Func << Entity << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5943 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5944 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5945 | } else if (!Entity->hasLinkage()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5946 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_object_no_linkage) |
| 5947 | << !Func << Entity << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5948 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5949 | << !Func; |
| 5950 | return true; |
| 5951 | } |
| 5952 | |
| 5953 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5954 | // If the template parameter has pointer type, the function decays. |
| 5955 | if (ParamType->isPointerType() && !AddressTaken) |
| 5956 | ArgType = S.Context.getPointerType(Func->getType()); |
| 5957 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 5958 | // If we originally had an address-of operator, but the |
| 5959 | // parameter has reference type, complain and (if things look |
| 5960 | // like they will work) drop the address-of operator. |
| 5961 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 5962 | ParamType.getNonReferenceType())) { |
| 5963 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5964 | << ParamType; |
| 5965 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5966 | return true; |
| 5967 | } |
| 5968 | |
| 5969 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5970 | << ParamType |
| 5971 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5972 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5973 | |
| 5974 | ArgType = Func->getType(); |
| 5975 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5976 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5977 | // A value of reference type is not an object. |
| 5978 | if (Var->getType()->isReferenceType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5979 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_reference_var) |
| 5980 | << Var->getType() << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5981 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5982 | return true; |
| 5983 | } |
| 5984 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5985 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 5986 | if (Var->getTLSKind()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5987 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_thread_local) |
| 5988 | << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5989 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 5990 | return true; |
| 5991 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5992 | |
| 5993 | // If the template parameter has pointer type, we must have taken |
| 5994 | // the address of this object. |
| 5995 | if (ParamType->isReferenceType()) { |
| 5996 | if (AddressTaken) { |
| 5997 | // If we originally had an address-of operator, but the |
| 5998 | // parameter has reference type, complain and (if things look |
| 5999 | // like they will work) drop the address-of operator. |
| 6000 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 6001 | ParamType.getNonReferenceType())) { |
| 6002 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 6003 | << ParamType; |
| 6004 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6005 | return true; |
| 6006 | } |
| 6007 | |
| 6008 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 6009 | << ParamType |
| 6010 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 6011 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6012 | |
| 6013 | ArgType = Var->getType(); |
| 6014 | } |
| 6015 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 6016 | if (Var->getType()->isArrayType()) { |
| 6017 | // Array-to-pointer decay. |
| 6018 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 6019 | } else { |
| 6020 | // If the template parameter has pointer type but the address of |
| 6021 | // this object was not taken, complain and (possibly) recover by |
| 6022 | // taking the address of the entity. |
| 6023 | ArgType = S.Context.getPointerType(Var->getType()); |
| 6024 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6025 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 6026 | << ParamType; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6027 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6028 | return true; |
| 6029 | } |
| 6030 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6031 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 6032 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), "&"); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6033 | |
| 6034 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6035 | } |
| 6036 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6037 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6038 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 6039 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 6040 | Arg, ArgType)) |
| 6041 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6042 | |
| 6043 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 6044 | Converted = |
| 6045 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6046 | S.MarkAnyDeclReferenced(Arg->getBeginLoc(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6047 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6050 | /// Checks whether the given template argument is a pointer to |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6051 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6052 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 6053 | NonTypeTemplateParmDecl *Param, |
| 6054 | QualType ParamType, |
| 6055 | Expr *&ResultArg, |
| 6056 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6057 | bool Invalid = false; |
| 6058 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6059 | Expr *Arg = ResultArg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6060 | bool ObjCLifetimeConversion; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6061 | |
| 6062 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6063 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6064 | // A template-argument for a non-type, non-template |
| 6065 | // template-parameter shall be one of: [...] |
| 6066 | // |
| 6067 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6068 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6069 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6070 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 6071 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 6072 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6073 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6074 | if (!Invalid && !ExtraParens) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6075 | S.Diag(Arg->getBeginLoc(), |
| 6076 | S.getLangOpts().CPlusPlus11 |
| 6077 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 6078 | : diag::ext_template_arg_extra_parens) |
| 6079 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6080 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6081 | } |
| 6082 | |
| 6083 | Arg = Parens->getSubExpr(); |
| 6084 | } |
| 6085 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6086 | while (SubstNonTypeTemplateParmExpr *subst = |
| 6087 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 6088 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 6089 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6090 | // A pointer-to-member constant written &Class::member. |
| 6091 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6092 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6093 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 6094 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6095 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6096 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6097 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6098 | // A constant of pointer-to-member type. |
| 6099 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6100 | ValueDecl *VD = DRE->getDecl(); |
| 6101 | if (VD->getType()->isMemberPointerType()) { |
| 6102 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
| 6103 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6104 | Converted = TemplateArgument(Arg); |
| 6105 | } else { |
| 6106 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
| 6107 | Converted = TemplateArgument(VD, ParamType); |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6108 | } |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6109 | return Invalid; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6110 | } |
| 6111 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6112 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6113 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6114 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6115 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 6116 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 6117 | |
| 6118 | // Check for a null pointer value. |
| 6119 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ResultArg, |
| 6120 | Entity)) { |
| 6121 | case NPV_Error: |
| 6122 | return true; |
| 6123 | case NPV_NullPointer: |
| 6124 | S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
| 6125 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 6126 | /*isNullPtr*/true); |
| 6127 | return false; |
| 6128 | case NPV_NotNullPointer: |
| 6129 | break; |
| 6130 | } |
| 6131 | |
| 6132 | if (S.IsQualificationConversion(ResultArg->getType(), |
| 6133 | ParamType.getNonReferenceType(), false, |
| 6134 | ObjCLifetimeConversion)) { |
| 6135 | ResultArg = S.ImpCastExprToType(ResultArg, ParamType, CK_NoOp, |
| 6136 | ResultArg->getValueKind()) |
| 6137 | .get(); |
| 6138 | } else if (!S.Context.hasSameUnqualifiedType( |
| 6139 | ResultArg->getType(), ParamType.getNonReferenceType())) { |
| 6140 | // We can't perform this conversion. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6141 | S.Diag(ResultArg->getBeginLoc(), diag::err_template_arg_not_convertible) |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 6142 | << ResultArg->getType() << ParamType << ResultArg->getSourceRange(); |
| 6143 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6144 | return true; |
| 6145 | } |
| 6146 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6147 | if (!DRE) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6148 | return S.Diag(Arg->getBeginLoc(), |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6149 | diag::err_template_arg_not_pointer_to_member_form) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6150 | << Arg->getSourceRange(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6151 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6152 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 6153 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 6154 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6155 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6156 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6157 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 6158 | "Only non-static member pointers can make it here"); |
| 6159 | |
| 6160 | // Okay: this is the address of a non-static member, and therefore |
| 6161 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6162 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 6163 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6164 | } else { |
| 6165 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 6166 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6167 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6168 | return Invalid; |
| 6169 | } |
| 6170 | |
| 6171 | // We found something else, but we don't know specifically what it is. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6172 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_pointer_to_member_form) |
| 6173 | << Arg->getSourceRange(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6174 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6175 | return true; |
| 6176 | } |
| 6177 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6178 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6179 | /// non-type template parameter. |
| 6180 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 6181 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6182 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6183 | /// returns the converted template argument. \p ParamType is the |
| 6184 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6185 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6186 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6187 | TemplateArgument &Converted, |
| 6188 | CheckTemplateArgumentKind CTAK) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6189 | SourceLocation StartLoc = Arg->getBeginLoc(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6190 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6191 | // If the parameter type somehow involves auto, deduce the type now. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6192 | if (getLangOpts().CPlusPlus17 && ParamType->isUndeducedType()) { |
Richard Smith | 4ae5ec8 | 2017-02-22 20:01:55 +0000 | [diff] [blame] | 6193 | // During template argument deduction, we allow 'decltype(auto)' to |
| 6194 | // match an arbitrary dependent argument. |
| 6195 | // FIXME: The language rules don't say what happens in this case. |
| 6196 | // FIXME: We get an opaque dependent type out of decltype(auto) if the |
| 6197 | // expression is merely instantiation-dependent; is this enough? |
| 6198 | if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) { |
| 6199 | auto *AT = dyn_cast<AutoType>(ParamType); |
| 6200 | if (AT && AT->isDecltypeAuto()) { |
| 6201 | Converted = TemplateArgument(Arg); |
| 6202 | return Arg; |
| 6203 | } |
| 6204 | } |
| 6205 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6206 | // When checking a deduced template argument, deduce from its type even if |
| 6207 | // the type is dependent, in order to check the types of non-type template |
| 6208 | // arguments line up properly in partial ordering. |
| 6209 | Optional<unsigned> Depth; |
| 6210 | if (CTAK != CTAK_Specified) |
| 6211 | Depth = Param->getDepth() + 1; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6212 | if (DeduceAutoType( |
| 6213 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6214 | Arg, ParamType, Depth) == DAR_Failed) { |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6215 | Diag(Arg->getExprLoc(), |
| 6216 | diag::err_non_type_template_parm_type_deduction_failure) |
| 6217 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 6218 | << Arg->getSourceRange(); |
| 6219 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6220 | return ExprError(); |
| 6221 | } |
| 6222 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 6223 | // an error. The error message normally references the parameter |
| 6224 | // declaration, but here we'll pass the argument location because that's |
| 6225 | // where the parameter type is deduced. |
| 6226 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 6227 | if (ParamType.isNull()) { |
| 6228 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6229 | return ExprError(); |
| 6230 | } |
| 6231 | } |
| 6232 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6233 | // We should have already dropped all cv-qualifiers by now. |
| 6234 | assert(!ParamType.hasQualifiers() && |
| 6235 | "non-type template parameter type cannot be qualified"); |
| 6236 | |
| 6237 | if (CTAK == CTAK_Deduced && |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 6238 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6239 | Arg->getType())) { |
Richard Smith | 957fbf1 | 2017-01-17 02:14:37 +0000 | [diff] [blame] | 6240 | // FIXME: If either type is dependent, we skip the check. This isn't |
| 6241 | // correct, since during deduction we're supposed to have replaced each |
| 6242 | // template parameter with some unique (non-dependent) placeholder. |
| 6243 | // FIXME: If the argument type contains 'auto', we carry on and fail the |
| 6244 | // type check in order to force specific types to be more specialized than |
| 6245 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to |
| 6246 | // work. |
| 6247 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
| 6248 | !Arg->getType()->getContainedAutoType()) { |
| 6249 | Converted = TemplateArgument(Arg); |
| 6250 | return Arg; |
| 6251 | } |
| 6252 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, |
| 6253 | // we should actually be checking the type of the template argument in P, |
| 6254 | // not the type of the template argument deduced from A, against the |
| 6255 | // template parameter type. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6256 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6257 | << Arg->getType() |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6258 | << ParamType.getUnqualifiedType(); |
| 6259 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6260 | return ExprError(); |
| 6261 | } |
| 6262 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6263 | // If either the parameter has a dependent type or the argument is |
| 6264 | // type-dependent, there's nothing we can check now. |
| 6265 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 6266 | // FIXME: Produce a cloned, canonical expression? |
| 6267 | Converted = TemplateArgument(Arg); |
| 6268 | return Arg; |
| 6269 | } |
| 6270 | |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6271 | // The initialization of the parameter from the argument is |
| 6272 | // a constant-evaluated context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6273 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 6274 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6275 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6276 | if (getLangOpts().CPlusPlus17) { |
| 6277 | // C++17 [temp.arg.nontype]p1: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6278 | // A template-argument for a non-type template parameter shall be |
| 6279 | // a converted constant expression of the type of the template-parameter. |
| 6280 | APValue Value; |
| 6281 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 6282 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 6283 | if (ArgResult.isInvalid()) |
| 6284 | return ExprError(); |
| 6285 | |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6286 | // For a value-dependent argument, CheckConvertedConstantExpression is |
| 6287 | // permitted (and expected) to be unable to determine a value. |
| 6288 | if (ArgResult.get()->isValueDependent()) { |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6289 | Converted = TemplateArgument(ArgResult.get()); |
| 6290 | return ArgResult; |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6291 | } |
| 6292 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6293 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 6294 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6295 | // Convert the APValue to a TemplateArgument. |
| 6296 | switch (Value.getKind()) { |
| 6297 | case APValue::Uninitialized: |
| 6298 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6299 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6300 | break; |
| 6301 | case APValue::Int: |
| 6302 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6303 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6304 | break; |
| 6305 | case APValue::MemberPointer: { |
| 6306 | assert(ParamType->isMemberPointerType()); |
| 6307 | |
| 6308 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 6309 | if (!Value.getMemberPointerPath().empty()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6310 | Diag(Arg->getBeginLoc(), |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6311 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 6312 | << Value.getMemberPointerDecl() << ParamType |
| 6313 | << Arg->getSourceRange(); |
| 6314 | return ExprError(); |
| 6315 | } |
| 6316 | |
| 6317 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6318 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6319 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6320 | break; |
| 6321 | } |
| 6322 | case APValue::LValue: { |
| 6323 | // For a non-type template-parameter of pointer or reference type, |
| 6324 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6325 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 6326 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6327 | // -- a temporary object |
| 6328 | // -- a string literal |
| 6329 | // -- the result of a typeid expression, or |
Eric Christopher | 0d2c56a | 2017-03-31 01:45:39 +0000 | [diff] [blame] | 6330 | // -- a predefined __func__ variable |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6331 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 6332 | if (isa<CXXUuidofExpr>(E)) { |
Bill Wendling | ff57307 | 2019-01-27 07:24:03 +0000 | [diff] [blame] | 6333 | Converted = TemplateArgument(ArgResult.get()->IgnoreImpCasts()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6334 | break; |
| 6335 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6336 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 6337 | << Arg->getSourceRange(); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6338 | return ExprError(); |
| 6339 | } |
| 6340 | auto *VD = const_cast<ValueDecl *>( |
| 6341 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 6342 | // -- a subobject |
| 6343 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 6344 | VD && VD->getType()->isArrayType() && |
| 6345 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 6346 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 6347 | // Per defect report (no number yet): |
| 6348 | // ... other than a pointer to the first element of a complete array |
| 6349 | // object. |
| 6350 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 6351 | Value.isLValueOnePastTheEnd()) { |
| 6352 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 6353 | << Value.getAsString(Context, ParamType); |
| 6354 | return ExprError(); |
| 6355 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6356 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6357 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6358 | assert((!VD || !ParamType->isNullPtrType()) && |
| 6359 | "non-null value of type nullptr_t?"); |
| 6360 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6361 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6362 | break; |
| 6363 | } |
| 6364 | case APValue::AddrLabelDiff: |
| 6365 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
Leonard Chan | 86285d2 | 2019-01-16 18:53:05 +0000 | [diff] [blame] | 6366 | case APValue::FixedPoint: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6367 | case APValue::Float: |
| 6368 | case APValue::ComplexInt: |
| 6369 | case APValue::ComplexFloat: |
| 6370 | case APValue::Vector: |
| 6371 | case APValue::Array: |
| 6372 | case APValue::Struct: |
| 6373 | case APValue::Union: |
| 6374 | llvm_unreachable("invalid kind for template argument"); |
| 6375 | } |
| 6376 | |
| 6377 | return ArgResult.get(); |
| 6378 | } |
| 6379 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6380 | // C++ [temp.arg.nontype]p5: |
| 6381 | // The following conversions are performed on each expression used |
| 6382 | // as a non-type template-argument. If a non-type |
| 6383 | // template-argument cannot be converted to the type of the |
| 6384 | // corresponding template-parameter then the program is |
| 6385 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6386 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6387 | // C++11: |
| 6388 | // -- for a non-type template-parameter of integral or |
| 6389 | // enumeration type, conversions permitted in a converted |
| 6390 | // constant expression are applied. |
| 6391 | // |
| 6392 | // C++98: |
| 6393 | // -- for a non-type template-parameter of integral or |
| 6394 | // enumeration type, integral promotions (4.5) and integral |
| 6395 | // conversions (4.7) are applied. |
| 6396 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6397 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6398 | // C++ [temp.arg.nontype]p1: |
| 6399 | // A template-argument for a non-type, non-template template-parameter |
| 6400 | // shall be one of: |
| 6401 | // |
| 6402 | // -- for a non-type template-parameter of integral or enumeration |
| 6403 | // type, a converted constant expression of the type of the |
| 6404 | // template-parameter; or |
| 6405 | llvm::APSInt Value; |
| 6406 | ExprResult ArgResult = |
| 6407 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 6408 | CCEK_TemplateArg); |
| 6409 | if (ArgResult.isInvalid()) |
| 6410 | return ExprError(); |
| 6411 | |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6412 | // We can't check arbitrary value-dependent arguments. |
| 6413 | if (ArgResult.get()->isValueDependent()) { |
| 6414 | Converted = TemplateArgument(ArgResult.get()); |
| 6415 | return ArgResult; |
| 6416 | } |
| 6417 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6418 | // Widen the argument value to sizeof(parameter type). This is almost |
| 6419 | // always a no-op, except when the parameter type is bool. In |
| 6420 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 6421 | QualType IntegerType = ParamType; |
| 6422 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 6423 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 6424 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 6425 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6426 | Converted = TemplateArgument(Context, Value, |
| 6427 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6428 | return ArgResult; |
| 6429 | } |
| 6430 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6431 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 6432 | if (ArgResult.isInvalid()) |
| 6433 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6434 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6435 | |
| 6436 | QualType ArgType = Arg->getType(); |
| 6437 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6438 | // C++ [temp.arg.nontype]p1: |
| 6439 | // A template-argument for a non-type, non-template |
| 6440 | // template-parameter shall be one of: |
| 6441 | // |
| 6442 | // -- an integral constant-expression of integral or enumeration |
| 6443 | // type; or |
| 6444 | // -- the name of a non-type template-parameter; or |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6445 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6446 | if (!ArgType->isIntegralOrEnumerationType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6447 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_integral_or_enumeral) |
| 6448 | << ArgType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6449 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6450 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6451 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6452 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 6453 | QualType T; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6454 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6455 | public: |
| 6456 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 6457 | |
| 6458 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 6459 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6460 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 6461 | } |
| 6462 | } Diagnoser(ArgType); |
| 6463 | |
| 6464 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6465 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6466 | if (!Arg) |
| 6467 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6468 | } |
| 6469 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6470 | // From here on out, all we care about is the unqualified form |
| 6471 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6472 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6473 | |
| 6474 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 6475 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6476 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6477 | } else if (ParamType->isBooleanType()) { |
| 6478 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6479 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6480 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 6481 | !ParamType->isEnumeralType()) { |
| 6482 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6483 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6484 | } else { |
| 6485 | // We can't perform this conversion. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6486 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 6487 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6488 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6489 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6490 | } |
| 6491 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6492 | // Add the value of this argument to the list of converted |
| 6493 | // arguments. We use the bitwidth and signedness of the template |
| 6494 | // parameter. |
| 6495 | if (Arg->isValueDependent()) { |
| 6496 | // The argument is value-dependent. Create a new |
| 6497 | // TemplateArgument with the converted expression. |
| 6498 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6499 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6500 | } |
| 6501 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6502 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6503 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 6504 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6505 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6506 | if (ParamType->isBooleanType()) { |
| 6507 | // Value must be zero or one. |
| 6508 | Value = Value != 0; |
| 6509 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 6510 | if (Value.getBitWidth() != AllowedBits) |
| 6511 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6512 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6513 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6514 | llvm::APSInt OldValue = Value; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6515 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6516 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6517 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6518 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6519 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6520 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6521 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6522 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6523 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6524 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6525 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6526 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_negative) |
| 6527 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6528 | << Arg->getSourceRange(); |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6529 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6530 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6531 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6532 | // Complain if we overflowed the template parameter's type. |
| 6533 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6534 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6535 | RequiredBits = OldValue.getActiveBits(); |
| 6536 | else if (OldValue.isUnsigned()) |
| 6537 | RequiredBits = OldValue.getActiveBits() + 1; |
| 6538 | else |
| 6539 | RequiredBits = OldValue.getMinSignedBits(); |
| 6540 | if (RequiredBits > AllowedBits) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6541 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_too_large) |
| 6542 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6543 | << Arg->getSourceRange(); |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6544 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6545 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6546 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6547 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6548 | Converted = TemplateArgument(Context, Value, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6549 | ParamType->isEnumeralType() |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 6550 | ? Context.getCanonicalType(ParamType) |
| 6551 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6552 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6553 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6554 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6555 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6556 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 6557 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6558 | // Handle pointer-to-function, reference-to-function, and |
| 6559 | // pointer-to-member-function all in (roughly) the same way. |
| 6560 | if (// -- For a non-type template-parameter of type pointer to |
| 6561 | // function, only the function-to-pointer conversion (4.3) is |
| 6562 | // applied. If the template-argument represents a set of |
| 6563 | // overloaded functions (or a pointer to such), the matching |
| 6564 | // function is selected from the set (13.4). |
| 6565 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6566 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6567 | // -- For a non-type template-parameter of type reference to |
| 6568 | // function, no conversions apply. If the template-argument |
| 6569 | // represents a set of overloaded functions, the matching |
| 6570 | // function is selected from the set (13.4). |
| 6571 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6572 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6573 | // -- For a non-type template-parameter of type pointer to |
| 6574 | // member function, no conversions apply. If the |
| 6575 | // template-argument represents a set of overloaded member |
| 6576 | // functions, the matching member function is selected from |
| 6577 | // the set (13.4). |
| 6578 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6579 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6580 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6581 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6582 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6583 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6584 | true, |
| 6585 | FoundResult)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6586 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6587 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6588 | |
| 6589 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6590 | ArgType = Arg->getType(); |
| 6591 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6592 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6593 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6594 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6595 | if (!ParamType->isMemberPointerType()) { |
| 6596 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6597 | ParamType, |
| 6598 | Arg, Converted)) |
| 6599 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6600 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6601 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6602 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6603 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6604 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6605 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6606 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6607 | } |
| 6608 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 6609 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6610 | // -- for a non-type template-parameter of type pointer to |
| 6611 | // object, qualification conversions (4.4) and the |
| 6612 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6613 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6614 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6615 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6616 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6617 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6618 | ParamType, |
| 6619 | Arg, Converted)) |
| 6620 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6621 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6622 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6623 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6624 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6625 | // -- For a non-type template-parameter of type reference to |
| 6626 | // object, no conversions apply. The type referred to by the |
| 6627 | // reference may be more cv-qualified than the (otherwise |
| 6628 | // identical) type of the template-argument. The |
| 6629 | // template-parameter is bound directly to the |
| 6630 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6631 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6632 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6633 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6634 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6635 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 6636 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6637 | true, |
| 6638 | FoundResult)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6639 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6640 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6641 | |
| 6642 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6643 | ArgType = Arg->getType(); |
| 6644 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6645 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6646 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6647 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6648 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6649 | ParamType, |
| 6650 | Arg, Converted)) |
| 6651 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6652 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6653 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6654 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6655 | // Deal with parameters of type std::nullptr_t. |
| 6656 | if (ParamType->isNullPtrType()) { |
| 6657 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6658 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6659 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6660 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6661 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6662 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 6663 | case NPV_NotNullPointer: |
| 6664 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 6665 | << Arg->getType() << ParamType; |
| 6666 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6667 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6668 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6669 | case NPV_Error: |
| 6670 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6671 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6672 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 6673 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 6674 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 6675 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6676 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6677 | } |
| 6678 | } |
| 6679 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6680 | // -- For a non-type template-parameter of type pointer to data |
| 6681 | // member, qualification conversions (4.4) are applied. |
| 6682 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 6683 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6684 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6685 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6686 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6687 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6688 | } |
| 6689 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6690 | static void DiagnoseTemplateParameterListArityMismatch( |
| 6691 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 6692 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 6693 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6694 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6695 | /// template template parameter. |
| 6696 | /// |
| 6697 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 6698 | /// It returns true if an error occurred, and false otherwise. |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 6699 | bool Sema::CheckTemplateTemplateArgument(TemplateParameterList *Params, |
| 6700 | TemplateArgumentLoc &Arg) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6701 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6702 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 6703 | if (!Template) { |
| 6704 | // Any dependent template name is fine. |
| 6705 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 6706 | return false; |
| 6707 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6708 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6709 | if (Template->isInvalidDecl()) |
| 6710 | return true; |
| 6711 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6712 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6713 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6714 | // the name of a class template or an alias template, expressed as an |
| 6715 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6716 | // primary class templates are considered when matching the |
| 6717 | // template template argument with the corresponding parameter; |
| 6718 | // partial specializations are not considered even if their |
| 6719 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6720 | // |
| 6721 | // Note that we also allow template template parameters here, which |
| 6722 | // will happen when we are dealing with, e.g., class template |
| 6723 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6724 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6725 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6726 | !isa<TypeAliasTemplateDecl>(Template) && |
| 6727 | !isa<BuiltinTemplateDecl>(Template)) { |
| 6728 | assert(isa<FunctionTemplateDecl>(Template) && |
| 6729 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 6730 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6731 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 6732 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6733 | } |
| 6734 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6735 | // C++1z [temp.arg.template]p3: (DR 150) |
| 6736 | // A template-argument matches a template template-parameter P when P |
| 6737 | // is at least as specialized as the template-argument A. |
| 6738 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 6739 | // Quick check for the common case: |
| 6740 | // If P contains a parameter pack, then A [...] matches P if each of A's |
| 6741 | // template parameters matches the corresponding template parameter in |
| 6742 | // the template-parameter-list of P. |
| 6743 | if (TemplateParameterListsAreEqual( |
| 6744 | Template->getTemplateParameters(), Params, false, |
| 6745 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 6746 | return false; |
| 6747 | |
| 6748 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 6749 | Arg.getLocation())) |
| 6750 | return false; |
| 6751 | // FIXME: Produce better diagnostics for deduction failures. |
| 6752 | } |
| 6753 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6754 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 6755 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6756 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6757 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6758 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6761 | /// Given a non-type template argument that refers to a |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6762 | /// declaration and the type of its corresponding non-type template |
| 6763 | /// parameter, produce an expression that properly refers to that |
| 6764 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6765 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6766 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 6767 | QualType ParamType, |
| 6768 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 6769 | // C++ [temp.param]p8: |
| 6770 | // |
| 6771 | // A non-type template-parameter of type "array of T" or |
| 6772 | // "function returning T" is adjusted to be of type "pointer to |
| 6773 | // T" or "pointer to function returning T", respectively. |
| 6774 | if (ParamType->isArrayType()) |
| 6775 | ParamType = Context.getArrayDecayedType(ParamType); |
| 6776 | else if (ParamType->isFunctionType()) |
| 6777 | ParamType = Context.getPointerType(ParamType); |
| 6778 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6779 | // For a NULL non-type template argument, return nullptr casted to the |
| 6780 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6781 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6782 | return ImpCastExprToType( |
| 6783 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 6784 | ParamType, |
| 6785 | ParamType->getAs<MemberPointerType>() |
| 6786 | ? CK_NullToMemberPointer |
| 6787 | : CK_NullToPointer); |
| 6788 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6789 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 6790 | "Only declaration template arguments permitted here"); |
| 6791 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6792 | ValueDecl *VD = Arg.getAsDecl(); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6793 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6794 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 6795 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 6796 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6797 | // If the value is a class member, we might have a pointer-to-member. |
| 6798 | // Determine whether the non-type template template parameter is of |
| 6799 | // pointer-to-member type. If so, we need to build an appropriate |
| 6800 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 6801 | // would refer to the member itself. |
| 6802 | if (ParamType->isMemberPointerType()) { |
| 6803 | QualType ClassType |
| 6804 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 6805 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6806 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6807 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6808 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 6809 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6810 | |
| 6811 | // The actual value-ness of this is unimportant, but for |
| 6812 | // internal consistency's sake, references to instance methods |
| 6813 | // are r-values. |
| 6814 | ExprValueKind VK = VK_LValue; |
| 6815 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 6816 | VK = VK_RValue; |
| 6817 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6818 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6819 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6820 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6821 | Loc, |
| 6822 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6823 | if (RefExpr.isInvalid()) |
| 6824 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6825 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6826 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6827 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6828 | // We might need to perform a trailing qualification conversion, since |
| 6829 | // the element type on the parameter could be more qualified than the |
| 6830 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6831 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6832 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6833 | ParamType.getUnqualifiedType(), false, |
| 6834 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6835 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6836 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6837 | assert(!RefExpr.isInvalid() && |
| 6838 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6839 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6840 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6841 | } |
| 6842 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6843 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6844 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6845 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6846 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6847 | // When the non-type template parameter is a pointer, take the |
| 6848 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6849 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6850 | if (RefExpr.isInvalid()) |
| 6851 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6852 | |
Richard Smith | fc6fca1 | 2017-01-28 00:38:35 +0000 | [diff] [blame] | 6853 | if (!Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
| 6854 | (T->isFunctionType() || T->isArrayType())) { |
| 6855 | // Decay functions and arrays unless we're forming a pointer to array. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6856 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6857 | if (RefExpr.isInvalid()) |
| 6858 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6859 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6860 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6861 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6862 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6863 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6864 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6867 | ExprValueKind VK = VK_RValue; |
| 6868 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6869 | // If the non-type template parameter has reference type, qualify the |
| 6870 | // resulting declaration reference with the extra qualifiers on the |
| 6871 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6872 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 6873 | VK = VK_LValue; |
| 6874 | T = Context.getQualifiedType(T, |
| 6875 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6876 | } else if (isa<FunctionDecl>(VD)) { |
| 6877 | // References to functions are always lvalues. |
| 6878 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6879 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6880 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6881 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6882 | } |
| 6883 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6884 | /// Construct a new expression that refers to the given |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6885 | /// integral template argument with the given source-location |
| 6886 | /// information. |
| 6887 | /// |
| 6888 | /// This routine takes care of the mapping from an integral template |
| 6889 | /// argument (which may have any integral type) to the appropriate |
| 6890 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6891 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6892 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 6893 | SourceLocation Loc) { |
| 6894 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 6895 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6896 | QualType OrigT = Arg.getIntegralType(); |
| 6897 | |
| 6898 | // If this is an enum type that we're instantiating, we need to use an integer |
| 6899 | // type the same size as the enumerator. We don't want to build an |
| 6900 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 6901 | // any integral type with C++11 enum classes, make sure we create the right |
| 6902 | // type of literal for it. |
| 6903 | QualType T = OrigT; |
| 6904 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 6905 | T = ET->getDecl()->getIntegerType(); |
| 6906 | |
| 6907 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6908 | if (T->isAnyCharacterType()) { |
| 6909 | CharacterLiteral::CharacterKind Kind; |
| 6910 | if (T->isWideCharType()) |
| 6911 | Kind = CharacterLiteral::Wide; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 6912 | else if (T->isChar8Type() && getLangOpts().Char8) |
| 6913 | Kind = CharacterLiteral::UTF8; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6914 | else if (T->isChar16Type()) |
| 6915 | Kind = CharacterLiteral::UTF16; |
| 6916 | else if (T->isChar32Type()) |
| 6917 | Kind = CharacterLiteral::UTF32; |
| 6918 | else |
| 6919 | Kind = CharacterLiteral::Ascii; |
| 6920 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6921 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 6922 | Kind, T, Loc); |
| 6923 | } else if (T->isBooleanType()) { |
| 6924 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 6925 | T, Loc); |
| 6926 | } else if (T->isNullPtrType()) { |
| 6927 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 6928 | } else { |
| 6929 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6930 | } |
| 6931 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6932 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6933 | // FIXME: This is a hack. We need a better way to handle substituted |
| 6934 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6935 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 6936 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6937 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6938 | Loc, Loc); |
| 6939 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6940 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6941 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6942 | } |
| 6943 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6944 | /// Match two template parameters within template parameter lists. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6945 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 6946 | bool Complain, |
| 6947 | Sema::TemplateParameterListEqualKind Kind, |
| 6948 | SourceLocation TemplateArgLoc) { |
| 6949 | // Check the actual kind (type, non-type, template). |
| 6950 | if (Old->getKind() != New->getKind()) { |
| 6951 | if (Complain) { |
| 6952 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 6953 | if (TemplateArgLoc.isValid()) { |
| 6954 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6955 | NextDiag = diag::note_template_param_different_kind; |
| 6956 | } |
| 6957 | S.Diag(New->getLocation(), NextDiag) |
| 6958 | << (Kind != Sema::TPL_TemplateMatch); |
| 6959 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 6960 | << (Kind != Sema::TPL_TemplateMatch); |
| 6961 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6962 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6963 | return false; |
| 6964 | } |
| 6965 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6966 | // Check that both are parameter packs or neither are parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6967 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6968 | // template template parameter, the template template parameter can have |
| 6969 | // a parameter pack where the template template argument does not. |
| 6970 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 6971 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 6972 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6973 | if (Complain) { |
| 6974 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 6975 | if (TemplateArgLoc.isValid()) { |
| 6976 | S.Diag(TemplateArgLoc, |
| 6977 | diag::err_template_arg_template_params_mismatch); |
| 6978 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 6979 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6980 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6981 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 6982 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 6983 | : 2; |
| 6984 | S.Diag(New->getLocation(), NextDiag) |
| 6985 | << ParamKind << New->isParameterPack(); |
| 6986 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 6987 | << ParamKind << Old->isParameterPack(); |
| 6988 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6989 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6990 | return false; |
| 6991 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6992 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6993 | // For non-type template parameters, check the type of the parameter. |
| 6994 | if (NonTypeTemplateParmDecl *OldNTTP |
| 6995 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 6996 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6997 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6998 | // If we are matching a template template argument to a template |
| 6999 | // template parameter and one of the non-type template parameter types |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 7000 | // is dependent, then we must wait until template instantiation time |
| 7001 | // to actually compare the arguments. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7002 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 7003 | (OldNTTP->getType()->isDependentType() || |
| 7004 | NewNTTP->getType()->isDependentType())) |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7005 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7006 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7007 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 7008 | if (Complain) { |
| 7009 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 7010 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7011 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7012 | diag::err_template_arg_template_params_mismatch); |
| 7013 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 7014 | } |
| 7015 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 7016 | << NewNTTP->getType() |
| 7017 | << (Kind != Sema::TPL_TemplateMatch); |
| 7018 | S.Diag(OldNTTP->getLocation(), |
| 7019 | diag::note_template_nontype_parm_prev_declaration) |
| 7020 | << OldNTTP->getType(); |
| 7021 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7022 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7023 | return false; |
| 7024 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7025 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7026 | return true; |
| 7027 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7028 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7029 | // For template template parameters, check the template parameter types. |
| 7030 | // The template parameter lists of template template |
| 7031 | // parameters must agree. |
| 7032 | if (TemplateTemplateParmDecl *OldTTP |
| 7033 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7034 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7035 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 7036 | OldTTP->getTemplateParameters(), |
| 7037 | Complain, |
| 7038 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7039 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7040 | : Kind), |
| 7041 | TemplateArgLoc); |
| 7042 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7043 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7044 | return true; |
| 7045 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 7046 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7047 | /// Diagnose a known arity mismatch when comparing template argument |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7048 | /// lists. |
| 7049 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7050 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7051 | TemplateParameterList *New, |
| 7052 | TemplateParameterList *Old, |
| 7053 | Sema::TemplateParameterListEqualKind Kind, |
| 7054 | SourceLocation TemplateArgLoc) { |
| 7055 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 7056 | if (TemplateArgLoc.isValid()) { |
| 7057 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 7058 | NextDiag = diag::note_template_param_list_different_arity; |
| 7059 | } |
| 7060 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 7061 | << (New->size() > Old->size()) |
| 7062 | << (Kind != Sema::TPL_TemplateMatch) |
| 7063 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 7064 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 7065 | << (Kind != Sema::TPL_TemplateMatch) |
| 7066 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 7067 | } |
| 7068 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7069 | /// Determine whether the given template parameter lists are |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7070 | /// equivalent. |
| 7071 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7072 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7073 | /// source code as part of a new template declaration. |
| 7074 | /// |
| 7075 | /// \param Old The old template parameter list, typically found via |
| 7076 | /// name lookup of the template declared with this template parameter |
| 7077 | /// list. |
| 7078 | /// |
| 7079 | /// \param Complain If true, this routine will produce a diagnostic if |
| 7080 | /// the template parameter lists are not equivalent. |
| 7081 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7082 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7083 | /// |
| 7084 | /// \param TemplateArgLoc If this source location is valid, then we |
| 7085 | /// are actually checking the template parameter list of a template |
| 7086 | /// argument (New) against the template parameter list of its |
| 7087 | /// corresponding template template parameter (Old). We produce |
| 7088 | /// slightly different diagnostics in this scenario. |
| 7089 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7090 | /// \returns True if the template parameter lists are equal, false |
| 7091 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7092 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7093 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 7094 | TemplateParameterList *Old, |
| 7095 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7096 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7097 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7098 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 7099 | if (Complain) |
| 7100 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7101 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7102 | |
| 7103 | return false; |
| 7104 | } |
| 7105 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7106 | // C++0x [temp.arg.template]p3: |
| 7107 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7108 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7109 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7110 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7111 | // template-parameter-list of P. [...] |
| 7112 | TemplateParameterList::iterator NewParm = New->begin(); |
| 7113 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7114 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7115 | OldParmEnd = Old->end(); |
| 7116 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 7117 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 7118 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7119 | if (NewParm == NewParmEnd) { |
| 7120 | if (Complain) |
| 7121 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7122 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7123 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7124 | return false; |
| 7125 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7126 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7127 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7128 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7129 | return false; |
| 7130 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7131 | ++NewParm; |
| 7132 | continue; |
| 7133 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7134 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7135 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7136 | // [...] When P's template- parameter-list contains a template parameter |
| 7137 | // pack (14.5.3), the template parameter pack will match zero or more |
| 7138 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7139 | // template-parameter-list of A with the same type and form as the |
| 7140 | // template parameter pack in P (ignoring whether those template |
| 7141 | // parameters are template parameter packs). |
| 7142 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 7143 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7144 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7145 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7146 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7147 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7148 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7149 | // Make sure we exhausted all of the arguments. |
| 7150 | if (NewParm != NewParmEnd) { |
| 7151 | if (Complain) |
| 7152 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7153 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7154 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7155 | return false; |
| 7156 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7157 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7158 | return true; |
| 7159 | } |
| 7160 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7161 | /// Check whether a template can be declared within this scope. |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7162 | /// |
| 7163 | /// If the template declaration is valid in this scope, returns |
| 7164 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7165 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7166 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 7167 | if (!S) |
| 7168 | return false; |
| 7169 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7170 | // Find the nearest enclosing declaration scope. |
| 7171 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7172 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7173 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7174 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7175 | // C++ [temp]p4: |
| 7176 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7177 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 7178 | if (Ctx && Ctx->isExternCContext()) { |
| 7179 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 7180 | << TemplateParams->getSourceRange(); |
| 7181 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 7182 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 7183 | return true; |
| 7184 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 7185 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7186 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7187 | // C++ [temp]p2: |
| 7188 | // A template-declaration can appear only as a namespace scope or |
| 7189 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 7190 | if (Ctx) { |
| 7191 | if (Ctx->isFileContext()) |
| 7192 | return false; |
| 7193 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 7194 | // C++ [temp.mem]p2: |
| 7195 | // A local class shall not have member templates. |
| 7196 | if (RD->isLocalClass()) |
| 7197 | return Diag(TemplateParams->getTemplateLoc(), |
| 7198 | diag::err_template_inside_local_class) |
| 7199 | << TemplateParams->getSourceRange(); |
| 7200 | else |
| 7201 | return false; |
| 7202 | } |
| 7203 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7204 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7205 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7206 | diag::err_template_outside_namespace_or_class_scope) |
| 7207 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7208 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7209 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7210 | /// Determine what kind of template specialization the given declaration |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7211 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7212 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7213 | if (!D) |
| 7214 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7215 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7216 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 7217 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7218 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 7219 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7220 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 7221 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7222 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7223 | return TSK_Undeclared; |
| 7224 | } |
| 7225 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7226 | /// Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7227 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7228 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7229 | /// This routine determines whether a template specialization can be declared |
| 7230 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7231 | /// |
| 7232 | /// \param S the semantic analysis object for which this check is being |
| 7233 | /// performed. |
| 7234 | /// |
| 7235 | /// \param Specialized the entity being specialized or instantiated, which |
| 7236 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7237 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7238 | /// member class). |
| 7239 | /// |
| 7240 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 7241 | /// |
| 7242 | /// \param Loc the location of the explicit specialization or instantiation of |
| 7243 | /// this entity. |
| 7244 | /// |
| 7245 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 7246 | /// a class template. |
| 7247 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7248 | /// \returns true if there was an error that we cannot recover from, false |
| 7249 | /// otherwise. |
| 7250 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 7251 | NamedDecl *Specialized, |
| 7252 | NamedDecl *PrevDecl, |
| 7253 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7254 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7255 | // Keep these "kind" numbers in sync with the %select statements in the |
| 7256 | // various diagnostics emitted by this routine. |
| 7257 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7258 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7259 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7260 | else if (isa<VarTemplateDecl>(Specialized)) |
| 7261 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7262 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7263 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7264 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7265 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7266 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7267 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7268 | else if (isa<RecordDecl>(Specialized)) |
| 7269 | EntityKind = 7; |
| 7270 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 7271 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7272 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7273 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7274 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7275 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7276 | return true; |
| 7277 | } |
| 7278 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7279 | // C++ [temp.expl.spec]p2: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7280 | // An explicit specialization may be declared in any scope in which |
| 7281 | // the corresponding primary template may be defined. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7282 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7283 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7284 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7285 | return true; |
| 7286 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 7287 | |
| 7288 | // C++ [temp.class.spec]p6: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7289 | // A class template partial specialization may be declared in any |
| 7290 | // scope in which the primary template may be defined. |
| 7291 | DeclContext *SpecializedContext = |
| 7292 | Specialized->getDeclContext()->getRedeclContext(); |
| 7293 | DeclContext *DC = S.CurContext->getRedeclContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7294 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7295 | // Make sure that this redeclaration (or definition) occurs in the same |
| 7296 | // scope or an enclosing namespace. |
| 7297 | if (!(DC->isFileContext() ? DC->Encloses(SpecializedContext) |
| 7298 | : DC->Equals(SpecializedContext))) { |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7299 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 7300 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 7301 | << EntityKind << Specialized; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7302 | else { |
| 7303 | auto *ND = cast<NamedDecl>(SpecializedContext); |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7304 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7305 | if (S.getLangOpts().MicrosoftExt && !DC->isRecord()) |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7306 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 7307 | S.Diag(Loc, Diag) << EntityKind << Specialized |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7308 | << ND << isa<CXXRecordDecl>(ND); |
| 7309 | } |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7310 | |
| 7311 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7312 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7313 | // Don't allow specializing in the wrong class during error recovery. |
| 7314 | // Otherwise, things can go horribly wrong. |
| 7315 | if (DC->isRecord()) |
| 7316 | return true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7317 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7318 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7319 | return false; |
| 7320 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7321 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7322 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 7323 | if (!E->isTypeDependent()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7324 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7325 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7326 | Checker.TraverseStmt(E); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7327 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7328 | return E->getSourceRange(); |
| 7329 | return Checker.MatchLoc; |
| 7330 | } |
| 7331 | |
| 7332 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 7333 | if (!TL.getType()->isDependentType()) |
| 7334 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7335 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7336 | Checker.TraverseTypeLoc(TL); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7337 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7338 | return TL.getSourceRange(); |
| 7339 | return Checker.MatchLoc; |
| 7340 | } |
| 7341 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7342 | /// Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7343 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7344 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7345 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 7346 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7347 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 7348 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7349 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7350 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 7351 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7352 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7353 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7354 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7355 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7356 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7357 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7358 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7359 | |
| 7360 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7361 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 7362 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7363 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 7364 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7365 | |
| 7366 | // Strip off any implicit casts we added as part of type checking. |
| 7367 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 7368 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7369 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7370 | // C++ [temp.class.spec]p8: |
| 7371 | // A non-type argument is non-specialized if it is the name of a |
| 7372 | // non-type parameter. All other non-type arguments are |
| 7373 | // specialized. |
| 7374 | // |
| 7375 | // Below, we check the two conditions that only apply to |
| 7376 | // specialized non-type arguments, so skip any non-specialized |
| 7377 | // arguments. |
| 7378 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7379 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7380 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7381 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7382 | // C++ [temp.class.spec]p9: |
| 7383 | // Within the argument list of a class template partial |
| 7384 | // specialization, the following restrictions apply: |
| 7385 | // -- A partially specialized non-type argument expression |
| 7386 | // shall not involve a template parameter of the partial |
| 7387 | // specialization except when the argument expression is a |
| 7388 | // simple identifier. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7389 | // -- The type of a template parameter corresponding to a |
| 7390 | // specialized non-type argument shall not be dependent on a |
| 7391 | // parameter of the specialization. |
| 7392 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
| 7393 | // We implement a compromise between the original rules and DR1315: |
| 7394 | // -- A specialized non-type template argument shall not be |
| 7395 | // type-dependent and the corresponding template parameter |
| 7396 | // shall have a non-dependent type. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7397 | SourceRange ParamUseRange = |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7398 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7399 | if (ParamUseRange.isValid()) { |
| 7400 | if (IsDefaultArgument) { |
| 7401 | S.Diag(TemplateNameLoc, |
| 7402 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 7403 | S.Diag(ParamUseRange.getBegin(), |
| 7404 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 7405 | << ParamUseRange; |
| 7406 | } else { |
| 7407 | S.Diag(ParamUseRange.getBegin(), |
| 7408 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 7409 | << ParamUseRange; |
| 7410 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7411 | return true; |
| 7412 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7413 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7414 | ParamUseRange = findTemplateParameter( |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7415 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7416 | if (ParamUseRange.isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7417 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getBeginLoc(), |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7418 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7419 | << Param->getType(); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7420 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7421 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 7422 | << ParamUseRange; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7423 | return true; |
| 7424 | } |
| 7425 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7426 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7427 | return false; |
| 7428 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7429 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7430 | /// Check the non-type template arguments of a class template |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7431 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 7432 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7433 | /// \param TemplateNameLoc the location of the template name. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7434 | /// \param PrimaryTemplate the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7435 | /// template. |
| 7436 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7437 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7438 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7439 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7440 | /// \returns \c true if there was an error, \c false otherwise. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7441 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 7442 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 7443 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 7444 | // We have to be conservative when checking a template in a dependent |
| 7445 | // context. |
| 7446 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 7447 | return false; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7448 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7449 | TemplateParameterList *TemplateParams = |
| 7450 | PrimaryTemplate->getTemplateParameters(); |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7451 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7452 | NonTypeTemplateParmDecl *Param |
| 7453 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 7454 | if (!Param) |
| 7455 | continue; |
| 7456 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7457 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 7458 | Param, &TemplateArgs[I], |
| 7459 | 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7460 | return true; |
| 7461 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7462 | |
| 7463 | return false; |
| 7464 | } |
| 7465 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7466 | DeclResult Sema::ActOnClassTemplateSpecialization( |
| 7467 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
| 7468 | SourceLocation ModulePrivateLoc, TemplateIdAnnotation &TemplateId, |
| 7469 | const ParsedAttributesView &Attr, |
| 7470 | MultiTemplateParamsArg TemplateParameterLists, SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7471 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 7472 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7473 | CXXScopeSpec &SS = TemplateId.SS; |
| 7474 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7475 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 7476 | // store the location of the outermost template keyword in the declaration. |
| 7477 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7478 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 7479 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 7480 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 7481 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7482 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7483 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7484 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7485 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7486 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 7487 | |
| 7488 | if (!ClassTemplate) { |
| 7489 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7490 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7491 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 7492 | return true; |
| 7493 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7494 | |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7495 | bool isMemberSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7496 | bool isPartialSpecialization = false; |
| 7497 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7498 | // Check the validity of the template headers that introduce this |
| 7499 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7500 | // FIXME: We probably shouldn't complain about these headers for |
| 7501 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7502 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 7503 | TemplateParameterList *TemplateParams = |
| 7504 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7505 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7506 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7507 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7508 | if (Invalid) |
| 7509 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7510 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7511 | if (TemplateParams && TemplateParams->size() > 0) { |
| 7512 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7513 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 7514 | if (TUK == TUK_Friend) { |
| 7515 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 7516 | << SourceRange(LAngleLoc, RAngleLoc); |
| 7517 | return true; |
| 7518 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7519 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7520 | // C++ [temp.class.spec]p10: |
| 7521 | // The template parameter list of a specialization shall not |
| 7522 | // contain default template argument values. |
| 7523 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7524 | Decl *Param = TemplateParams->getParam(I); |
| 7525 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 7526 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7527 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7528 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 7529 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7530 | } |
| 7531 | } else if (NonTypeTemplateParmDecl *NTTP |
| 7532 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 7533 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7534 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7535 | diag::err_default_arg_in_partial_spec) |
| 7536 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7537 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7538 | } |
| 7539 | } else { |
| 7540 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7541 | if (TTP->hasDefaultArgument()) { |
| 7542 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7543 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7544 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7545 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7546 | } |
| 7547 | } |
| 7548 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7549 | } else if (TemplateParams) { |
| 7550 | if (TUK == TUK_Friend) |
| 7551 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7552 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7553 | SourceRange(TemplateParams->getTemplateLoc(), |
| 7554 | TemplateParams->getRAngleLoc())) |
| 7555 | << SourceRange(LAngleLoc, RAngleLoc); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7556 | } else { |
| 7557 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7558 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7559 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7560 | // Check that the specialization uses the same tag kind as the |
| 7561 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7562 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7563 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7564 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7565 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7566 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7567 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7568 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7569 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7570 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7571 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7572 | diag::note_previous_use); |
| 7573 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7574 | } |
| 7575 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7576 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7577 | TemplateArgumentListInfo TemplateArgs = |
| 7578 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7579 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7580 | // Check for unexpanded parameter packs in any of the template arguments. |
| 7581 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7582 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7583 | UPPC_PartialSpecialization)) |
| 7584 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7585 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7586 | // Check that the template argument list is well-formed for this |
| 7587 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7588 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7589 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7590 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7591 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7592 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7593 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7594 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7595 | if (isPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7596 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 7597 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7598 | return true; |
| 7599 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7600 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
| 7601 | // also do it during instantiation. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 7602 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7603 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7604 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7605 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7606 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 7607 | << ClassTemplate->getDeclName(); |
| 7608 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7609 | } |
| 7610 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7611 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7612 | void *InsertPos = nullptr; |
| 7613 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7614 | |
| 7615 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7616 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7617 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7618 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7619 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7620 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7621 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7622 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7623 | // Check whether we can declare a class template specialization in |
| 7624 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7625 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7626 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 7627 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7628 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7629 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7630 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7631 | // The canonical type |
| 7632 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 7633 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7634 | // Build the canonical type that describes the converted template |
| 7635 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7636 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7637 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7638 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7639 | |
| 7640 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7641 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 7642 | // C++ [temp.class.spec]p9b3: |
| 7643 | // |
| 7644 | // -- The argument list of the specialization shall not be identical |
| 7645 | // to the implicit argument list of the primary template. |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 7646 | // |
| 7647 | // This rule has since been removed, because it's redundant given DR1495, |
| 7648 | // but we keep it because it produces better diagnostics and recovery. |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7649 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 7650 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 7651 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7652 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 7653 | ClassTemplate->getIdentifier(), |
| 7654 | TemplateNameLoc, |
| 7655 | Attr, |
| 7656 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7657 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 7658 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7659 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7660 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7661 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7662 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7663 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7664 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 7665 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7666 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7667 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7668 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7669 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 7670 | TemplateParams, |
| 7671 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7672 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7673 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7674 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 7675 | PrevPartial); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 7676 | SetNestedNameSpecifier(*this, Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7677 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7678 | Partial->setTemplateParameterListsInfo( |
| 7679 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7680 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7681 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7682 | if (!PrevPartial) |
| 7683 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7684 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 7685 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7686 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 7687 | // template specialization, make a note of that. |
| 7688 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 7689 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7690 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7691 | CheckTemplatePartialSpecialization(Partial); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7692 | } else { |
| 7693 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7694 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7695 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7696 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7697 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7698 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7699 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7700 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7701 | PrevDecl); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 7702 | SetNestedNameSpecifier(*this, Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7703 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 7704 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7705 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7706 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7707 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7708 | if (!PrevDecl) |
| 7709 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7710 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7711 | if (CurContext->isDependentContext()) { |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7712 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7713 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7714 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7715 | } else { |
| 7716 | CanonType = Context.getTypeDeclType(Specialization); |
| 7717 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7718 | } |
| 7719 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7720 | // C++ [temp.expl.spec]p6: |
| 7721 | // 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] | 7722 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7723 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7724 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7725 | // use occurs; no diagnostic is required. |
| 7726 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7727 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7728 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7729 | // Is there any previous explicit specialization declaration? |
| 7730 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7731 | Okay = true; |
| 7732 | break; |
| 7733 | } |
| 7734 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7735 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7736 | if (!Okay) { |
| 7737 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 7738 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 7739 | << Context.getTypeDeclType(Specialization) << Range; |
| 7740 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7741 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7742 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7743 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7744 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7745 | return true; |
| 7746 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7747 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7748 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7749 | // If this is not a friend, note that this is an explicit specialization. |
| 7750 | if (TUK != TUK_Friend) |
| 7751 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7752 | |
| 7753 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7754 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7755 | RecordDecl *Def = Specialization->getDefinition(); |
| 7756 | NamedDecl *Hidden = nullptr; |
| 7757 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 7758 | SkipBody->ShouldSkip = true; |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7759 | SkipBody->Previous = Def; |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 7760 | makeMergedDefinitionVisible(Hidden); |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7761 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7762 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 7763 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7764 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 7765 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7766 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7767 | } |
| 7768 | } |
| 7769 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7770 | ProcessDeclAttributeList(S, Specialization, Attr); |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 7771 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 7772 | // Add alignment attributes if necessary; these attributes are checked when |
| 7773 | // the ASTContext lays out the structure. |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7774 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 7775 | AddAlignmentAttributesForRecord(Specialization); |
| 7776 | AddMsStructLayoutForRecord(Specialization); |
| 7777 | } |
| 7778 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 7779 | if (ModulePrivateLoc.isValid()) |
| 7780 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 7781 | << (isPartialSpecialization? 1 : 0) |
| 7782 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7783 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 7784 | // Build the fully-sugared type for this class template |
| 7785 | // specialization as the user wrote in the specialization |
| 7786 | // itself. This means that we'll pretty-print the type retrieved |
| 7787 | // from the specialization's declaration the way that the user |
| 7788 | // actually wrote the specialization, rather than formatting the |
| 7789 | // name based on the "canonical" representation used to store the |
| 7790 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7791 | TypeSourceInfo *WrittenTy |
| 7792 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7793 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7794 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7795 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7796 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7797 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7798 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 7799 | // C++ [temp.expl.spec]p9: |
| 7800 | // A template explicit specialization is in the scope of the |
| 7801 | // namespace in which the template was defined. |
| 7802 | // |
| 7803 | // We actually implement this paragraph where we set the semantic |
| 7804 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 7805 | // but we also maintain the lexical context where the actual |
| 7806 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7807 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7808 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7809 | // We may be starting the definition of this specialization. |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7810 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7811 | Specialization->startDefinition(); |
| 7812 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7813 | if (TUK == TUK_Friend) { |
| 7814 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 7815 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 7816 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7817 | /*FIXME:*/KWLoc); |
| 7818 | Friend->setAccess(AS_public); |
| 7819 | CurContext->addDecl(Friend); |
| 7820 | } else { |
| 7821 | // Add the specialization into its lexical context, so that it can |
| 7822 | // be seen when iterating through the list of declarations in that |
| 7823 | // context. However, specializations are not found by name lookup. |
| 7824 | CurContext->addDecl(Specialization); |
| 7825 | } |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7826 | |
| 7827 | if (SkipBody && SkipBody->ShouldSkip) |
| 7828 | return SkipBody->Previous; |
| 7829 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7830 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7831 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7832 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7833 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7834 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7835 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7836 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 7837 | ActOnDocumentableDecl(NewDecl); |
| 7838 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7839 | } |
| 7840 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7841 | /// Strips various properties off an implicit instantiation |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7842 | /// that has just been explicitly specialized. |
| 7843 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7844 | D->dropAttr<DLLImportAttr>(); |
| 7845 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7846 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7847 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7848 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7851 | /// Compute the diagnostic location for an explicit instantiation |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7852 | // declaration or definition. |
| 7853 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7854 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7855 | // Explicit instantiations following a specialization have no effect and |
| 7856 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 7857 | // until a valid name loc is found. |
| 7858 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7859 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 7860 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7861 | PrevDiagLoc = Prev->getLocation(); |
| 7862 | } |
| 7863 | assert(PrevDiagLoc.isValid() && |
| 7864 | "Explicit instantiation without point of instantiation?"); |
| 7865 | return PrevDiagLoc; |
| 7866 | } |
| 7867 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7868 | /// Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7869 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7870 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7871 | /// new specialization/instantiation will have any effect. |
| 7872 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7873 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7874 | /// instantiation. |
| 7875 | /// |
| 7876 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 7877 | /// |
| 7878 | /// \param PrevDecl the previous declaration of the entity. |
| 7879 | /// |
| 7880 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 7881 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7882 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7883 | /// declaration was instantiated (either implicitly or explicitly). |
| 7884 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7885 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7886 | /// specialization or instantiation has no effect and should be ignored. |
| 7887 | /// |
| 7888 | /// \returns true if there was an error that should prevent the introduction of |
| 7889 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7890 | bool |
| 7891 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 7892 | TemplateSpecializationKind NewTSK, |
| 7893 | NamedDecl *PrevDecl, |
| 7894 | TemplateSpecializationKind PrevTSK, |
| 7895 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7896 | bool &HasNoEffect) { |
| 7897 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7898 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7899 | switch (NewTSK) { |
| 7900 | case TSK_Undeclared: |
| 7901 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 7902 | assert( |
| 7903 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 7904 | "previous declaration must be implicit!"); |
| 7905 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7906 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7907 | case TSK_ExplicitSpecialization: |
| 7908 | switch (PrevTSK) { |
| 7909 | case TSK_Undeclared: |
| 7910 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7911 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7912 | // explicitly specialized or has merely been mentioned without any |
| 7913 | // instantiation. |
| 7914 | return false; |
| 7915 | |
| 7916 | case TSK_ImplicitInstantiation: |
| 7917 | if (PrevPointOfInstantiation.isInvalid()) { |
| 7918 | // The declaration itself has not actually been instantiated, so it is |
| 7919 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7920 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7921 | return false; |
| 7922 | } |
| 7923 | // Fall through |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 7924 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7925 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7926 | case TSK_ExplicitInstantiationDeclaration: |
| 7927 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7928 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 7929 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7930 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7931 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7932 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7933 | // 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] | 7934 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7935 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7936 | // implicit instantiation to take place, in every translation unit in |
| 7937 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7938 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7939 | // Is there any previous explicit specialization declaration? |
| 7940 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 7941 | return false; |
| 7942 | } |
| 7943 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7944 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7945 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7946 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7947 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7948 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7949 | return true; |
| 7950 | } |
Galina Kistanova | 1d36e83 | 2017-06-08 18:20:32 +0000 | [diff] [blame] | 7951 | llvm_unreachable("The switch over PrevTSK must be exhaustive."); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7952 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7953 | case TSK_ExplicitInstantiationDeclaration: |
| 7954 | switch (PrevTSK) { |
| 7955 | case TSK_ExplicitInstantiationDeclaration: |
| 7956 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7957 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7958 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7959 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7960 | case TSK_Undeclared: |
| 7961 | case TSK_ImplicitInstantiation: |
| 7962 | // We're explicitly instantiating something that may have already been |
| 7963 | // implicitly instantiated; that's fine. |
| 7964 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7965 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7966 | case TSK_ExplicitSpecialization: |
| 7967 | // C++0x [temp.explicit]p4: |
| 7968 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7969 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7970 | // specialization for that template, the explicit instantiation has no |
| 7971 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7972 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7973 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7974 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7975 | case TSK_ExplicitInstantiationDefinition: |
| 7976 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7977 | // If an entity is the subject of both an explicit instantiation |
| 7978 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7979 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7980 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7981 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7982 | |
| 7983 | // Explicit instantiations following a specialization have no effect and |
| 7984 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 7985 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7986 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 7987 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7988 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7989 | return false; |
| 7990 | } |
Bruno Ricci | d8c1767 | 2018-12-21 20:38:06 +0000 | [diff] [blame] | 7991 | llvm_unreachable("Unexpected TemplateSpecializationKind!"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7992 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7993 | case TSK_ExplicitInstantiationDefinition: |
| 7994 | switch (PrevTSK) { |
| 7995 | case TSK_Undeclared: |
| 7996 | case TSK_ImplicitInstantiation: |
| 7997 | // We're explicitly instantiating something that may have already been |
| 7998 | // implicitly instantiated; that's fine. |
| 7999 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8000 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8001 | case TSK_ExplicitSpecialization: |
| 8002 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 8003 | // For a given set of template parameters, if an explicit |
| 8004 | // instantiation of a template appears after a declaration of |
| 8005 | // an explicit specialization for that template, the explicit |
| 8006 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 8007 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8008 | << PrevDecl; |
| 8009 | Diag(PrevDecl->getLocation(), |
| 8010 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8011 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8012 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8013 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8014 | case TSK_ExplicitInstantiationDeclaration: |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 8015 | // We're explicitly instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8016 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 8017 | |
| 8018 | // C++0x [temp.explicit]p4: |
| 8019 | // For a given set of template parameters, if an explicit instantiation |
| 8020 | // of a template appears after a declaration of an explicit |
| 8021 | // specialization for that template, the explicit instantiation has no |
| 8022 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 8023 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 8024 | // Is there any previous explicit specialization declaration? |
| 8025 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 8026 | HasNoEffect = true; |
| 8027 | break; |
| 8028 | } |
| 8029 | } |
| 8030 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8031 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8032 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8033 | case TSK_ExplicitInstantiationDefinition: |
| 8034 | // C++0x [temp.spec]p5: |
| 8035 | // For a given template and a given set of template-arguments, |
| 8036 | // - an explicit instantiation definition shall appear at most once |
| 8037 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 8038 | |
| 8039 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 8040 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 8041 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 8042 | : diag::err_explicit_instantiation_duplicate) |
| 8043 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 8044 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8045 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8046 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8047 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8048 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8049 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8050 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8051 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8052 | } |
| 8053 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8054 | /// Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8055 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8056 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8057 | /// The only possible way to get a dependent function template specialization |
| 8058 | /// is with a friend declaration, like so: |
| 8059 | /// |
| 8060 | /// \code |
| 8061 | /// template \<class T> void foo(T); |
| 8062 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8063 | /// friend void foo<>(T); |
| 8064 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8065 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8066 | /// |
| 8067 | /// There really isn't any useful analysis we can do here, so we |
| 8068 | /// just store the information. |
| 8069 | bool |
| 8070 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 8071 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 8072 | LookupResult &Previous) { |
| 8073 | // Remove anything from Previous that isn't a function template in |
| 8074 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8075 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8076 | LookupResult::Filter F = Previous.makeFilter(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8077 | enum DiscardReason { NotAFunctionTemplate, NotAMemberOfEnclosing }; |
| 8078 | SmallVector<std::pair<DiscardReason, Decl *>, 8> DiscardedCandidates; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8079 | while (F.hasNext()) { |
| 8080 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8081 | if (!isa<FunctionTemplateDecl>(D)) { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8082 | F.erase(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8083 | DiscardedCandidates.push_back(std::make_pair(NotAFunctionTemplate, D)); |
| 8084 | continue; |
| 8085 | } |
| 8086 | |
| 8087 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8088 | D->getDeclContext()->getRedeclContext())) { |
| 8089 | F.erase(); |
| 8090 | DiscardedCandidates.push_back(std::make_pair(NotAMemberOfEnclosing, D)); |
| 8091 | continue; |
| 8092 | } |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8093 | } |
| 8094 | F.done(); |
| 8095 | |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8096 | if (Previous.empty()) { |
| 8097 | Diag(FD->getLocation(), |
| 8098 | diag::err_dependent_function_template_spec_no_match); |
| 8099 | for (auto &P : DiscardedCandidates) |
| 8100 | Diag(P.second->getLocation(), |
| 8101 | diag::note_dependent_function_template_spec_discard_reason) |
| 8102 | << P.first; |
| 8103 | return true; |
| 8104 | } |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8105 | |
| 8106 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 8107 | ExplicitTemplateArgs); |
| 8108 | return false; |
| 8109 | } |
| 8110 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8111 | /// Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8112 | /// specialization. |
| 8113 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8114 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8115 | /// explicit function template specialization. On successful completion, |
| 8116 | /// the function declaration \p FD will become a function template |
| 8117 | /// specialization. |
| 8118 | /// |
| 8119 | /// \param FD the function declaration, which will be updated to become a |
| 8120 | /// function template specialization. |
| 8121 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8122 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 8123 | /// if any. Note that this may be valid info even when 0 arguments are |
| 8124 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 8125 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8126 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8127 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8128 | /// this function specialization. |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8129 | /// |
| 8130 | /// \param QualifiedFriend whether this is a lookup for a qualified friend |
| 8131 | /// declaration with no explicit template argument list that might be |
| 8132 | /// befriending a function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8133 | bool Sema::CheckFunctionTemplateSpecialization( |
| 8134 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8135 | LookupResult &Previous, bool QualifiedFriend) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8136 | // The set of function template specializations that could match this |
| 8137 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8138 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8139 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 8140 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8141 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8142 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 8143 | ConvertedTemplateArgs; |
| 8144 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8145 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8146 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8147 | I != E; ++I) { |
| 8148 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 8149 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8150 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8151 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8152 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8153 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8154 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8155 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8156 | // When matching a constexpr member function template specialization |
| 8157 | // against the primary template, we don't yet know whether the |
| 8158 | // specialization has an implicit 'const' (because we don't know whether |
| 8159 | // it will be a static member function until we know which template it |
| 8160 | // specializes), so adjust it now assuming it specializes this template. |
| 8161 | QualType FT = FD->getType(); |
| 8162 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8163 | CXXMethodDecl *OldMD = |
| 8164 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8165 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8166 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8167 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 8168 | EPI.TypeQuals.addConst(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8169 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8170 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8171 | } |
| 8172 | } |
| 8173 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8174 | TemplateArgumentListInfo Args; |
| 8175 | if (ExplicitTemplateArgs) |
| 8176 | Args = *ExplicitTemplateArgs; |
| 8177 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8178 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8179 | // A trailing template-argument can be left unspecified in the |
| 8180 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8181 | // provided it can be deduced from the function argument type. |
| 8182 | // Perform template argument deduction to determine whether we may be |
| 8183 | // specializing this template. |
| 8184 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8185 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8186 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 8187 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 8188 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8189 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 8190 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8191 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8192 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8193 | FailedCandidates.addCandidate().set( |
| 8194 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8195 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8196 | (void)TDK; |
| 8197 | continue; |
| 8198 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8199 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8200 | // Target attributes are part of the cuda function signature, so |
| 8201 | // the deduced template's cuda target must match that of the |
| 8202 | // specialization. Given that C++ template deduction does not |
| 8203 | // take target attributes into account, we reject candidates |
| 8204 | // here that have a different target. |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8205 | if (LangOpts.CUDA && |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8206 | IdentifyCUDATarget(Specialization, |
| 8207 | /* IgnoreImplicitHDAttributes = */ true) != |
| 8208 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8209 | FailedCandidates.addCandidate().set( |
| 8210 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8211 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8212 | continue; |
| 8213 | } |
| 8214 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8215 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8216 | if (ExplicitTemplateArgs) |
| 8217 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8218 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8219 | } |
| 8220 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8221 | |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8222 | // For a qualified friend declaration (with no explicit marker to indicate |
| 8223 | // that a template specialization was intended), note all (template and |
| 8224 | // non-template) candidates. |
| 8225 | if (QualifiedFriend && Candidates.empty()) { |
| 8226 | Diag(FD->getLocation(), diag::err_qualified_friend_no_match) |
| 8227 | << FD->getDeclName() << FDLookupContext; |
| 8228 | // FIXME: We should form a single candidate list and diagnose all |
| 8229 | // candidates at once, to get proper sorting and limiting. |
| 8230 | for (auto *OldND : Previous) { |
| 8231 | if (auto *OldFD = dyn_cast<FunctionDecl>(OldND->getUnderlyingDecl())) |
| 8232 | NoteOverloadCandidate(OldND, OldFD, FD->getType(), false); |
| 8233 | } |
| 8234 | FailedCandidates.NoteCandidates(*this, FD->getLocation()); |
| 8235 | return true; |
| 8236 | } |
| 8237 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 8238 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8239 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8240 | Candidates.begin(), Candidates.end(), FailedCandidates, FD->getLocation(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8241 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 8242 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8243 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8244 | PDiag(diag::note_function_template_spec_matched)); |
| 8245 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8246 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8247 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8248 | |
| 8249 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 8250 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 8251 | |
| 8252 | FunctionTemplateSpecializationInfo *SpecInfo |
| 8253 | = Specialization->getTemplateSpecializationInfo(); |
| 8254 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8255 | |
| 8256 | // Note: do not overwrite location info if previous template |
| 8257 | // specialization kind was explicit. |
| 8258 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8259 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8260 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8261 | Specialization->setLexicalDeclContext(FD->getLexicalDeclContext()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8262 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 8263 | // function can differ from the template declaration with respect to |
| 8264 | // the constexpr specifier. |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8265 | // FIXME: We need an update record for this AST mutation. |
| 8266 | // FIXME: What if there are multiple such prior declarations (for instance, |
| 8267 | // from different modules)? |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8268 | Specialization->setConstexpr(FD->isConstexpr()); |
| 8269 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8270 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8271 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8272 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8273 | |
| 8274 | // If this is a friend declaration, then we're not really declaring |
| 8275 | // an explicit specialization. |
| 8276 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8277 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8278 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8279 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8280 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8281 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8282 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8283 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8284 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8285 | |
| 8286 | // C++ [temp.expl.spec]p6: |
| 8287 | // 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] | 8288 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8289 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8290 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8291 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8292 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8293 | if (!isFriend && |
| 8294 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8295 | TSK_ExplicitSpecialization, |
| 8296 | Specialization, |
| 8297 | SpecInfo->getTemplateSpecializationKind(), |
| 8298 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8299 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8300 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8301 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8302 | // Mark the prior declaration as an explicit specialization, so that later |
| 8303 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8304 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8305 | // Since explicit specializations do not inherit '=delete' from their |
| 8306 | // primary function template - check if the 'specialization' that was |
| 8307 | // implicitly generated (during template argument deduction for partial |
| 8308 | // ordering) from the most specialized of all the function templates that |
| 8309 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 8310 | // first check that it was implicitly generated during template argument |
| 8311 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 8312 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 8313 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 8314 | !Specialization->getCanonicalDecl()->isReferenced()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8315 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8316 | assert( |
| 8317 | Specialization->getCanonicalDecl() == Specialization && |
| 8318 | "This must be the only existing declaration of this specialization"); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8319 | // FIXME: We need an update record for this AST mutation. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8320 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8321 | } |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8322 | // FIXME: We need an update record for this AST mutation. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8323 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8324 | MarkUnusedFileScopedDecl(Specialization); |
| 8325 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8326 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8327 | // Turn the given function declaration into a function template |
| 8328 | // specialization, with the template arguments from the previous |
| 8329 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8330 | // Take copies of (semantic and syntactic) template argument lists. |
| 8331 | const TemplateArgumentList* TemplArgs = new (Context) |
| 8332 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8333 | FD->setFunctionTemplateSpecialization( |
| 8334 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 8335 | SpecInfo->getTemplateSpecializationKind(), |
| 8336 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8337 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8338 | // A function template specialization inherits the target attributes |
| 8339 | // of its template. (We require the attributes explicitly in the |
| 8340 | // code to match, but a template may have implicit attributes by |
| 8341 | // virtue e.g. of being constexpr, and it passes these implicit |
| 8342 | // attributes on to its specializations.) |
| 8343 | if (LangOpts.CUDA) |
| 8344 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 8345 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8346 | // The "previous declaration" for this function template specialization is |
| 8347 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8348 | Previous.clear(); |
| 8349 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8350 | return false; |
| 8351 | } |
| 8352 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8353 | /// Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8354 | /// specialization. |
| 8355 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8356 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8357 | /// explicit member function specialization. On successful completion, |
| 8358 | /// the function declaration \p FD will become a member function |
| 8359 | /// specialization. |
| 8360 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8361 | /// \param Member the member declaration, which will be updated to become a |
| 8362 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8363 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8364 | /// \param Previous the set of declarations, one of which may be specialized |
| 8365 | /// by this function specialization; the set will be modified to contain the |
| 8366 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8367 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8368 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8369 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8370 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8371 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8372 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8373 | NamedDecl *Instantiation = nullptr; |
| 8374 | NamedDecl *InstantiatedFrom = nullptr; |
| 8375 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8376 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8377 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8378 | // Nowhere to look anyway. |
| 8379 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8380 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8381 | I != E; ++I) { |
| 8382 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 8383 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 8384 | QualType Adjusted = Function->getType(); |
| 8385 | if (!hasExplicitCallingConv(Adjusted)) |
| 8386 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
Richard Smith | 4576a77 | 2018-09-10 06:35:32 +0000 | [diff] [blame] | 8387 | // This doesn't handle deduced return types, but both function |
| 8388 | // declarations should be undeduced at this point. |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 8389 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8390 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8391 | Instantiation = Method; |
| 8392 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8393 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8394 | break; |
| 8395 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8396 | } |
| 8397 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8398 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8399 | VarDecl *PrevVar; |
| 8400 | if (Previous.isSingleResult() && |
| 8401 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8402 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8403 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8404 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8405 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8406 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8407 | } |
| 8408 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8409 | CXXRecordDecl *PrevRecord; |
| 8410 | if (Previous.isSingleResult() && |
| 8411 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8412 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8413 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8414 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8415 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8416 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8417 | } else if (isa<EnumDecl>(Member)) { |
| 8418 | EnumDecl *PrevEnum; |
| 8419 | if (Previous.isSingleResult() && |
| 8420 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8421 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8422 | Instantiation = PrevEnum; |
| 8423 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 8424 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 8425 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8426 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8427 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8428 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8429 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8430 | // specializations are always out-of-line, the caller will complain about |
| 8431 | // this mismatch later. |
| 8432 | return false; |
| 8433 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8434 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8435 | // A member specialization in a friend declaration isn't really declaring |
| 8436 | // an explicit specialization, just identifying a specific (possibly implicit) |
| 8437 | // specialization. Don't change the template specialization kind. |
| 8438 | // |
| 8439 | // FIXME: Is this really valid? Other compilers reject. |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8440 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 8441 | // Preserve instantiation information. |
| 8442 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 8443 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 8444 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 8445 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8446 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 8447 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 8448 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 8449 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8450 | } |
| 8451 | |
| 8452 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8453 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8454 | return false; |
| 8455 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8456 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8457 | // Make sure that this is a specialization of a member. |
| 8458 | if (!InstantiatedFrom) { |
| 8459 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 8460 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8461 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 8462 | return true; |
| 8463 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8464 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8465 | // C++ [temp.expl.spec]p6: |
| 8466 | // 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] | 8467 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8468 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8469 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8470 | // use occurs; no diagnostic is required. |
| 8471 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8472 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8473 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8474 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 8475 | TSK_ExplicitSpecialization, |
| 8476 | Instantiation, |
| 8477 | MSInfo->getTemplateSpecializationKind(), |
| 8478 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8479 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8480 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8481 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8482 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8483 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8484 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8485 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8486 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8487 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 8488 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8489 | // Note that this member specialization is an "instantiation of" the |
| 8490 | // corresponding member of the original template. |
| 8491 | if (auto *MemberFunction = dyn_cast<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8492 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 8493 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 8494 | TSK_ImplicitInstantiation) { |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8495 | // Explicit specializations of member functions of class templates do not |
| 8496 | // inherit '=delete' from the member function they are specializing. |
| 8497 | if (InstantiationFunction->isDeleted()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8498 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8499 | assert(InstantiationFunction->getCanonicalDecl() == |
| 8500 | InstantiationFunction); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8501 | // FIXME: We need an update record for this AST mutation. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 8502 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8503 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8504 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8505 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8506 | MemberFunction->setInstantiationOfMemberFunction( |
| 8507 | cast<CXXMethodDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8508 | } else if (auto *MemberVar = dyn_cast<VarDecl>(Member)) { |
| 8509 | MemberVar->setInstantiationOfStaticDataMember( |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8510 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8511 | } else if (auto *MemberClass = dyn_cast<CXXRecordDecl>(Member)) { |
| 8512 | MemberClass->setInstantiationOfMemberClass( |
| 8513 | cast<CXXRecordDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8514 | } else if (auto *MemberEnum = dyn_cast<EnumDecl>(Member)) { |
| 8515 | MemberEnum->setInstantiationOfMemberEnum( |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8516 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8517 | } else { |
| 8518 | llvm_unreachable("unknown member specialization kind"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8519 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8520 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8521 | // Save the caller the trouble of having to figure out which declaration |
| 8522 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8523 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8524 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8525 | return false; |
| 8526 | } |
| 8527 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8528 | /// Complete the explicit specialization of a member of a class template by |
| 8529 | /// updating the instantiated member to be marked as an explicit specialization. |
| 8530 | /// |
| 8531 | /// \param OrigD The member declaration instantiated from the template. |
| 8532 | /// \param Loc The location of the explicit specialization of the member. |
| 8533 | template<typename DeclT> |
| 8534 | static void completeMemberSpecializationImpl(Sema &S, DeclT *OrigD, |
| 8535 | SourceLocation Loc) { |
| 8536 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 8537 | return; |
| 8538 | |
| 8539 | // FIXME: Inform AST mutation listeners of this AST mutation. |
| 8540 | // FIXME: If there are multiple in-class declarations of the member (from |
| 8541 | // multiple modules, or a declaration and later definition of a member type), |
| 8542 | // should we update all of them? |
| 8543 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
| 8544 | OrigD->setLocation(Loc); |
| 8545 | } |
| 8546 | |
| 8547 | void Sema::CompleteMemberSpecialization(NamedDecl *Member, |
| 8548 | LookupResult &Previous) { |
| 8549 | NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl()); |
| 8550 | if (Instantiation == Member) |
| 8551 | return; |
| 8552 | |
| 8553 | if (auto *Function = dyn_cast<CXXMethodDecl>(Instantiation)) |
| 8554 | completeMemberSpecializationImpl(*this, Function, Member->getLocation()); |
| 8555 | else if (auto *Var = dyn_cast<VarDecl>(Instantiation)) |
| 8556 | completeMemberSpecializationImpl(*this, Var, Member->getLocation()); |
| 8557 | else if (auto *Record = dyn_cast<CXXRecordDecl>(Instantiation)) |
| 8558 | completeMemberSpecializationImpl(*this, Record, Member->getLocation()); |
| 8559 | else if (auto *Enum = dyn_cast<EnumDecl>(Instantiation)) |
| 8560 | completeMemberSpecializationImpl(*this, Enum, Member->getLocation()); |
| 8561 | else |
| 8562 | llvm_unreachable("unknown member specialization kind"); |
| 8563 | } |
| 8564 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8565 | /// Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8566 | /// |
| 8567 | /// \returns true if a serious error occurs, false otherwise. |
| 8568 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8569 | SourceLocation InstLoc, |
| 8570 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8571 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 8572 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8573 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8574 | if (CurContext->isRecord()) { |
| 8575 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 8576 | << D; |
| 8577 | return true; |
| 8578 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8579 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8580 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8581 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8582 | // template. If the name declared in the explicit instantiation is an |
| 8583 | // unqualified name, the explicit instantiation shall appear in the |
| 8584 | // namespace where its template is declared or, if that namespace is inline |
| 8585 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8586 | // |
| 8587 | // 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] | 8588 | if (WasQualifiedName) { |
| 8589 | if (CurContext->Encloses(OrigContext)) |
| 8590 | return false; |
| 8591 | } else { |
| 8592 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 8593 | return false; |
| 8594 | } |
| 8595 | |
| 8596 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 8597 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8598 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8599 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8600 | diag::err_explicit_instantiation_out_of_scope : |
| 8601 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8602 | << D << NS; |
| 8603 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8604 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8605 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8606 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 8607 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 8608 | << D << NS; |
| 8609 | } else |
| 8610 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8611 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8612 | diag::err_explicit_instantiation_must_be_global : |
| 8613 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 8614 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8615 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8616 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8617 | } |
| 8618 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8619 | /// Determine whether the given scope specifier has a template-id in it. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8620 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 8621 | if (!SS.isSet()) |
| 8622 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8623 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8624 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8625 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8626 | // or a static data member of a class template specialization, the name of |
| 8627 | // the class template specialization in the qualified-id for the member |
| 8628 | // name shall be a simple-template-id. |
| 8629 | // |
| 8630 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8631 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 8632 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8633 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8634 | if (isa<TemplateSpecializationType>(T)) |
| 8635 | return true; |
| 8636 | |
| 8637 | return false; |
| 8638 | } |
| 8639 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8640 | /// Make a dllexport or dllimport attr on a class template specialization take |
| 8641 | /// effect. |
| 8642 | static void dllExportImportClassTemplateSpecialization( |
| 8643 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 8644 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 8645 | assert(A && "dllExportImportClassTemplateSpecialization called " |
| 8646 | "on Def without dllexport or dllimport"); |
| 8647 | |
| 8648 | // We reject explicit instantiations in class scope, so there should |
| 8649 | // never be any delayed exported classes to worry about. |
| 8650 | assert(S.DelayedDllExportClasses.empty() && |
| 8651 | "delayed exports present at explicit instantiation"); |
| 8652 | S.checkClassLevelDLLAttribute(Def); |
| 8653 | |
| 8654 | // Propagate attribute to base class templates. |
| 8655 | for (auto &B : Def->bases()) { |
| 8656 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 8657 | B.getType()->getAsCXXRecordDecl())) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8658 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getBeginLoc()); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8659 | } |
| 8660 | |
| 8661 | S.referenceDLLExportedClassMethods(); |
| 8662 | } |
| 8663 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8664 | // Explicit instantiation of a class template specialization |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8665 | DeclResult Sema::ActOnExplicitInstantiation( |
| 8666 | Scope *S, SourceLocation ExternLoc, SourceLocation TemplateLoc, |
| 8667 | unsigned TagSpec, SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 8668 | TemplateTy TemplateD, SourceLocation TemplateNameLoc, |
| 8669 | SourceLocation LAngleLoc, ASTTemplateArgsPtr TemplateArgsIn, |
| 8670 | SourceLocation RAngleLoc, const ParsedAttributesView &Attr) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8671 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 8672 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8673 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8674 | // Check that the specialization uses the same tag kind as the |
| 8675 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8676 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 8677 | assert(Kind != TTK_Enum && |
| 8678 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8679 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8680 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 8681 | |
| 8682 | if (!ClassTemplate) { |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 8683 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 8684 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8685 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8686 | return true; |
| 8687 | } |
| 8688 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 8689 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 8690 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 8691 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8692 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8693 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8694 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8695 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8696 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8697 | diag::note_previous_use); |
| 8698 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 8699 | } |
| 8700 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8701 | // C++0x [temp.explicit]p2: |
| 8702 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8703 | // definition and an explicit instantiation declaration. An explicit |
| 8704 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8705 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 8706 | ? TSK_ExplicitInstantiationDefinition |
| 8707 | : TSK_ExplicitInstantiationDeclaration; |
| 8708 | |
| 8709 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 8710 | // Check for dllexport class template instantiation declarations. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8711 | for (const ParsedAttr &AL : Attr) { |
| 8712 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8713 | Diag(ExternLoc, |
| 8714 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8715 | Diag(AL.getLoc(), diag::note_attribute); |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8716 | break; |
| 8717 | } |
| 8718 | } |
| 8719 | |
| 8720 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 8721 | Diag(ExternLoc, |
| 8722 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8723 | Diag(A->getLocation(), diag::note_attribute); |
| 8724 | } |
| 8725 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8726 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8727 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 8728 | // instantiation declarations for most purposes. |
| 8729 | bool DLLImportExplicitInstantiationDef = false; |
| 8730 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 8731 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 8732 | // Check for dllimport class template instantiation definitions. |
| 8733 | bool DLLImport = |
| 8734 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8735 | for (const ParsedAttr &AL : Attr) { |
| 8736 | if (AL.getKind() == ParsedAttr::AT_DLLImport) |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8737 | DLLImport = true; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8738 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8739 | // dllexport trumps dllimport here. |
| 8740 | DLLImport = false; |
| 8741 | break; |
| 8742 | } |
| 8743 | } |
| 8744 | if (DLLImport) { |
| 8745 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 8746 | DLLImportExplicitInstantiationDef = true; |
| 8747 | } |
| 8748 | } |
| 8749 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8750 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8751 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 8752 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8753 | |
| 8754 | // Check that the template argument list is well-formed for this |
| 8755 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8756 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8757 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 8758 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8759 | return true; |
| 8760 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8761 | // Find the class template specialization declaration that |
| 8762 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8763 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8764 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 8765 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8766 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8767 | TemplateSpecializationKind PrevDecl_TSK |
| 8768 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 8769 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8770 | // C++0x [temp.explicit]p2: |
| 8771 | // [...] An explicit instantiation shall appear in an enclosing |
| 8772 | // namespace of its template. [...] |
| 8773 | // |
| 8774 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8775 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 8776 | SS.isSet())) |
| 8777 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8778 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8779 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8780 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8781 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8782 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8783 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8784 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8785 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8786 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8787 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8788 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8789 | // Even though HasNoEffect == true means that this explicit instantiation |
| 8790 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 8791 | |
| 8792 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 8793 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8794 | // Since the only prior class template specialization with these |
| 8795 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8796 | // declaration node as our own, updating the source location |
| 8797 | // for the template name to reflect our new declaration. |
| 8798 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8799 | Specialization = PrevDecl; |
| 8800 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8801 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8802 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8803 | |
| 8804 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 8805 | DLLImportExplicitInstantiationDef) { |
| 8806 | // The new specialization might add a dllimport attribute. |
| 8807 | HasNoEffect = false; |
| 8808 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8809 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8810 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8811 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8812 | // Create a new class template specialization declaration node for |
| 8813 | // this explicit specialization. |
| 8814 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 8815 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8816 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 8817 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8818 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 8819 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8820 | PrevDecl); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 8821 | SetNestedNameSpecifier(*this, Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8822 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8823 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8824 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8825 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8826 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8827 | } |
| 8828 | |
| 8829 | // Build the fully-sugared type for this explicit instantiation as |
| 8830 | // the user wrote in the explicit instantiation itself. This means |
| 8831 | // that we'll pretty-print the type retrieved from the |
| 8832 | // specialization's declaration the way that the user actually wrote |
| 8833 | // the explicit instantiation, rather than formatting the name based |
| 8834 | // on the "canonical" representation used to store the template |
| 8835 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 8836 | TypeSourceInfo *WrittenTy |
| 8837 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 8838 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8839 | Context.getTypeDeclType(Specialization)); |
| 8840 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8841 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8842 | // Set source locations for keywords. |
| 8843 | Specialization->setExternLoc(ExternLoc); |
| 8844 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 8845 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8846 | |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8847 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8848 | ProcessDeclAttributeList(S, Specialization, Attr); |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 8849 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8850 | // Add the explicit instantiation into its lexical context. However, |
| 8851 | // since explicit instantiations are never found by name lookup, we |
| 8852 | // just put it into the declaration context directly. |
| 8853 | Specialization->setLexicalDeclContext(CurContext); |
| 8854 | CurContext->addDecl(Specialization); |
| 8855 | |
| 8856 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 8857 | if (HasNoEffect) { |
| 8858 | // Set the template specialization kind. |
| 8859 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8860 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 8861 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8862 | |
| 8863 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8864 | // A definition of a class template or class member template |
| 8865 | // shall be in scope at the point of the explicit instantiation of |
| 8866 | // the class template or class member template. |
| 8867 | // |
| 8868 | // This check comes when we actually try to perform the |
| 8869 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8870 | ClassTemplateSpecializationDecl *Def |
| 8871 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8872 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8873 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8874 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8875 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8876 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8877 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 8878 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8879 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8880 | // Instantiate the members of this class template specialization. |
| 8881 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8882 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8883 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8884 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8885 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 8886 | // TSK_ExplicitInstantiationDefinition |
| 8887 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8888 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 8889 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8890 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8891 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8892 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 8893 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
Shoaib Meenai | ab3f96c | 2016-11-09 23:52:20 +0000 | [diff] [blame] | 8894 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8895 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 8896 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 8897 | // attribute to a template with a previous instantiation declaration. |
| 8898 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8899 | auto *A = cast<InheritableAttr>( |
| 8900 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 8901 | A->setInherited(true); |
| 8902 | Def->addAttr(A); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8903 | dllExportImportClassTemplateSpecialization(*this, Def); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8904 | } |
| 8905 | } |
| 8906 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8907 | // Fix a TSK_ImplicitInstantiation followed by a |
| 8908 | // TSK_ExplicitInstantiationDefinition |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8909 | bool NewlyDLLExported = |
| 8910 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
| 8911 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8912 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8913 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 8914 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 8915 | // attribute to a template with a previous implicit instantiation. |
| 8916 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to |
| 8917 | // avoid potentially strange codegen behavior. For example, if we extend |
| 8918 | // this conditional to dllimport, and we have a source file calling a |
| 8919 | // method on an implicitly instantiated template class instance and then |
| 8920 | // declaring a dllimport explicit instantiation definition for the same |
| 8921 | // template class, the codegen for the method call will not respect the |
| 8922 | // dllimport, while it will with cl. The Def will already have the DLL |
| 8923 | // attribute, since the Def and Specialization will be the same in the |
| 8924 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the |
| 8925 | // attribute to the Specialization; we just need to make it take effect. |
| 8926 | assert(Def == Specialization && |
| 8927 | "Def and Specialization should match for implicit instantiation"); |
| 8928 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 8929 | } |
| 8930 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8931 | // Set the template specialization kind. Make sure it is set before |
| 8932 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 8933 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8934 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8935 | } else { |
| 8936 | |
| 8937 | // Set the template specialization kind. |
| 8938 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8939 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8940 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8941 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8942 | } |
| 8943 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8944 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8945 | DeclResult |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8946 | Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation ExternLoc, |
| 8947 | SourceLocation TemplateLoc, unsigned TagSpec, |
| 8948 | SourceLocation KWLoc, CXXScopeSpec &SS, |
| 8949 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 8950 | const ParsedAttributesView &Attr) { |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8951 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 8952 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8953 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8954 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8955 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 8956 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8957 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 8958 | SourceLocation(), false, TypeResult(), |
Akira Hatanaka | 12ddcee | 2017-06-26 18:46:12 +0000 | [diff] [blame] | 8959 | /*IsTypeSpecifier*/false, |
| 8960 | /*IsTemplateParamOrArg*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8961 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 8962 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8963 | if (!TagD) |
| 8964 | return true; |
| 8965 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8966 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8967 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8968 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 8969 | if (Tag->isInvalidDecl()) |
| 8970 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8971 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8972 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 8973 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 8974 | if (!Pattern) { |
| 8975 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 8976 | << Context.getTypeDeclType(Record); |
| 8977 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 8978 | return true; |
| 8979 | } |
| 8980 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8981 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8982 | // If the explicit instantiation is for a class or member class, the |
| 8983 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8984 | // simple-template-id. |
| 8985 | // |
| 8986 | // C++98 has the same restriction, just worded differently. |
| 8987 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8988 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8989 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8990 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8991 | // C++0x [temp.explicit]p2: |
| 8992 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8993 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8994 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 8995 | TemplateSpecializationKind TSK |
| 8996 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8997 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8998 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8999 | // C++0x [temp.explicit]p2: |
| 9000 | // [...] An explicit instantiation shall appear in an enclosing |
| 9001 | // namespace of its template. [...] |
| 9002 | // |
| 9003 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9004 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9005 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9006 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9007 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9008 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9009 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 9010 | PrevDecl = Record; |
| 9011 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9012 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9013 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9014 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9015 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9016 | PrevDecl, |
| 9017 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9018 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9019 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9020 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9021 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9022 | return TagD; |
| 9023 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9024 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 9025 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9026 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 9027 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 9028 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9029 | // 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] | 9030 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9031 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9032 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 9033 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 9034 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 9035 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 9036 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 9037 | << Pattern; |
| 9038 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9039 | } else { |
| 9040 | if (InstantiateClass(NameLoc, Record, Def, |
| 9041 | getTemplateInstantiationArgs(Record), |
| 9042 | TSK)) |
| 9043 | return true; |
| 9044 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9045 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9046 | if (!RecordDef) |
| 9047 | return true; |
| 9048 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9049 | } |
| 9050 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9051 | // Instantiate all of the members of the class. |
| 9052 | InstantiateClassMembers(NameLoc, RecordDef, |
| 9053 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9054 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9055 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9056 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 9057 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 9058 | // FIXME: We don't have any representation for explicit instantiations of |
| 9059 | // member classes. Such a representation is not needed for compilation, but it |
| 9060 | // should be available for clients that want to see all of the declarations in |
| 9061 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9062 | return TagD; |
| 9063 | } |
| 9064 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9065 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 9066 | SourceLocation ExternLoc, |
| 9067 | SourceLocation TemplateLoc, |
| 9068 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9069 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9070 | // TODO: check if/when DNInfo should replace Name. |
| 9071 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 9072 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9073 | if (!Name) { |
| 9074 | if (!D.isInvalidType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9075 | Diag(D.getDeclSpec().getBeginLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9076 | diag::err_explicit_instantiation_requires_name) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9077 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9078 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9079 | return true; |
| 9080 | } |
| 9081 | |
| 9082 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 9083 | // we find one that is. |
| 9084 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 9085 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 9086 | S = S->getParent(); |
| 9087 | |
| 9088 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9089 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 9090 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9091 | if (R.isNull()) |
| 9092 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9093 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9094 | // C++ [dcl.stc]p1: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9095 | // A storage-class-specifier shall not be specified in [...] an explicit |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9096 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9097 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9098 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 9099 | << Name; |
| 9100 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9101 | } else if (D.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9102 | != DeclSpec::SCS_unspecified) { |
| 9103 | // Complain about then remove the storage class specifier. |
| 9104 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 9105 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9106 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9107 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9108 | } |
| 9109 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 9110 | // C++0x [temp.explicit]p1: |
| 9111 | // [...] An explicit instantiation of a function template shall not use the |
| 9112 | // inline or constexpr specifiers. |
| 9113 | // Presumably, this also applies to member functions of class templates as |
| 9114 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9115 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9116 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9117 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9118 | diag::err_explicit_instantiation_inline : |
| 9119 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9120 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9121 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9122 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 9123 | // not already specified. |
| 9124 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 9125 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9126 | |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 9127 | // A deduction guide is not on the list of entities that can be explicitly |
| 9128 | // instantiated. |
| 9129 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9130 | Diag(D.getDeclSpec().getBeginLoc(), diag::err_deduction_guide_specialized) |
| 9131 | << /*explicit instantiation*/ 0; |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 9132 | return true; |
| 9133 | } |
| 9134 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9135 | // C++0x [temp.explicit]p2: |
| 9136 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9137 | // definition and an explicit instantiation declaration. An explicit |
| 9138 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9139 | TemplateSpecializationKind TSK |
| 9140 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 9141 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9142 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9143 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9144 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9145 | |
| 9146 | if (!R->isFunctionType()) { |
| 9147 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9148 | // A [...] static data member of a class template can be explicitly |
| 9149 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9150 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9151 | // C++1y [temp.explicit]p1: |
| 9152 | // A [...] variable [...] template specialization can be explicitly |
| 9153 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9154 | if (Previous.isAmbiguous()) |
| 9155 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9156 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 9157 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9158 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9159 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9160 | if (!PrevTemplate) { |
| 9161 | if (!Prev || !Prev->isStaticDataMember()) { |
| 9162 | // We expect to see a data data member here. |
| 9163 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 9164 | << Name; |
| 9165 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9166 | P != PEnd; ++P) |
| 9167 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 9168 | return true; |
| 9169 | } |
| 9170 | |
| 9171 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 9172 | // FIXME: Check for explicit specialization? |
| 9173 | Diag(D.getIdentifierLoc(), |
| 9174 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 9175 | << Prev; |
| 9176 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 9177 | // FIXME: Can we provide a note showing where this was declared? |
| 9178 | return true; |
| 9179 | } |
| 9180 | } else { |
| 9181 | // Explicitly instantiate a variable template. |
| 9182 | |
| 9183 | // C++1y [dcl.spec.auto]p6: |
| 9184 | // ... A program that uses auto or decltype(auto) in a context not |
| 9185 | // explicitly allowed in this section is ill-formed. |
| 9186 | // |
| 9187 | // This includes auto-typed variable template instantiations. |
| 9188 | if (R->isUndeducedType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9189 | Diag(T->getTypeLoc().getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9190 | diag::err_auto_not_allowed_var_inst); |
| 9191 | return true; |
| 9192 | } |
| 9193 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9194 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9195 | // C++1y [temp.explicit]p3: |
| 9196 | // If the explicit instantiation is for a variable, the unqualified-id |
| 9197 | // in the declaration shall be a template-id. |
| 9198 | Diag(D.getIdentifierLoc(), |
| 9199 | diag::err_explicit_instantiation_without_template_id) |
| 9200 | << PrevTemplate; |
| 9201 | Diag(PrevTemplate->getLocation(), |
| 9202 | diag::note_explicit_instantiation_here); |
| 9203 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9204 | } |
| 9205 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9206 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9207 | TemplateArgumentListInfo TemplateArgs = |
| 9208 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9209 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9210 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 9211 | D.getIdentifierLoc(), TemplateArgs); |
| 9212 | if (Res.isInvalid()) |
| 9213 | return true; |
| 9214 | |
| 9215 | // Ignore access control bits, we don't need them for redeclaration |
| 9216 | // checking. |
| 9217 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9218 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9219 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9220 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9221 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9222 | // or a static data member of a class template specialization, the name of |
| 9223 | // the class template specialization in the qualified-id for the member |
| 9224 | // name shall be a simple-template-id. |
| 9225 | // |
| 9226 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9227 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 9228 | // This does not apply to variable template specializations, where the |
| 9229 | // template-id is in the unqualified-id instead. |
| 9230 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9231 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9232 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9233 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9234 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9235 | // Check the scope of this explicit instantiation. |
| 9236 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9237 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9238 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 9239 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 9240 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9241 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9242 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9243 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9244 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9245 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9246 | if (!HasNoEffect) { |
| 9247 | // Instantiate static data member or variable template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9248 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Louis Dionne | e6e8175 | 2018-10-10 15:32:29 +0000 | [diff] [blame] | 9249 | // Merge attributes. |
| 9250 | ProcessDeclAttributeList(S, Prev, D.getDeclSpec().getAttributes()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9251 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9252 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 9253 | } |
| 9254 | |
| 9255 | // Check the new variable specialization against the parsed input. |
| 9256 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9257 | Diag(T->getTypeLoc().getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9258 | diag::err_invalid_var_template_spec_type) |
| 9259 | << 0 << PrevTemplate << R << Prev->getType(); |
| 9260 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 9261 | << 2 << PrevTemplate->getDeclName(); |
| 9262 | return true; |
| 9263 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9264 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9265 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9266 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9267 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9268 | |
| 9269 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 9270 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9271 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9272 | TemplateArgumentListInfo TemplateArgs; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9273 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9274 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9275 | HasExplicitTemplateArgs = true; |
| 9276 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9277 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9278 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9279 | // A [...] function [...] can be explicitly instantiated from its template. |
| 9280 | // A member function [...] of a class template can be explicitly |
| 9281 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9282 | // template. |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9283 | UnresolvedSet<8> TemplateMatches; |
| 9284 | FunctionDecl *NonTemplateMatch = nullptr; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9285 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9286 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9287 | P != PEnd; ++P) { |
| 9288 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9289 | if (!HasExplicitTemplateArgs) { |
| 9290 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 9291 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 9292 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 9293 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9294 | if (Method->getPrimaryTemplate()) { |
| 9295 | TemplateMatches.addDecl(Method, P.getAccess()); |
| 9296 | } else { |
| 9297 | // FIXME: Can this assert ever happen? Needs a test. |
| 9298 | assert(!NonTemplateMatch && "Multiple NonTemplateMatches"); |
| 9299 | NonTemplateMatch = Method; |
| 9300 | } |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9301 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9302 | } |
| 9303 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9304 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9305 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 9306 | if (!FunTmpl) |
| 9307 | continue; |
| 9308 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9309 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9310 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9311 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9312 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9313 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 9314 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9315 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9316 | // Keep track of almost-matches. |
| 9317 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9318 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9319 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9320 | (void)TDK; |
| 9321 | continue; |
| 9322 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9323 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 9324 | // Target attributes are part of the cuda function signature, so |
| 9325 | // the cuda target of the instantiated function must match that of its |
| 9326 | // template. Given that C++ template deduction does not take |
| 9327 | // target attributes into account, we reject candidates here that |
| 9328 | // have a different target. |
| 9329 | if (LangOpts.CUDA && |
| 9330 | IdentifyCUDATarget(Specialization, |
| 9331 | /* IgnoreImplicitHDAttributes = */ true) != |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9332 | IdentifyCUDATarget(D.getDeclSpec().getAttributes())) { |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 9333 | FailedCandidates.addCandidate().set( |
| 9334 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 9335 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 9336 | continue; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 9337 | } |
| 9338 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9339 | TemplateMatches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9340 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9341 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9342 | FunctionDecl *Specialization = NonTemplateMatch; |
| 9343 | if (!Specialization) { |
| 9344 | // Find the most specialized function template specialization. |
| 9345 | UnresolvedSetIterator Result = getMostSpecialized( |
| 9346 | TemplateMatches.begin(), TemplateMatches.end(), FailedCandidates, |
| 9347 | D.getIdentifierLoc(), |
| 9348 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 9349 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 9350 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9351 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9352 | if (Result == TemplateMatches.end()) |
| 9353 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9354 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9355 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 9356 | Specialization = cast<FunctionDecl>(*Result); |
| 9357 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9358 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 9359 | // C++11 [except.spec]p4 |
| 9360 | // In an explicit instantiation an exception-specification may be specified, |
| 9361 | // but is not required. |
| 9362 | // If an exception-specification is specified in an explicit instantiation |
| 9363 | // directive, it shall be compatible with the exception-specifications of |
| 9364 | // other declarations of that function. |
| 9365 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 9366 | if (FPT->hasExceptionSpec()) { |
| 9367 | unsigned DiagID = |
| 9368 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 9369 | if (getLangOpts().MicrosoftExt) |
| 9370 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 9371 | bool Result = CheckEquivalentExceptionSpec( |
| 9372 | PDiag(DiagID) << Specialization->getType(), |
| 9373 | PDiag(diag::note_explicit_instantiation_here), |
| 9374 | Specialization->getType()->getAs<FunctionProtoType>(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9375 | Specialization->getLocation(), FPT, D.getBeginLoc()); |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 9376 | // In Microsoft mode, mismatching exception specifications just cause a |
| 9377 | // warning. |
| 9378 | if (!getLangOpts().MicrosoftExt && Result) |
| 9379 | return true; |
| 9380 | } |
| 9381 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9382 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9383 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9384 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 9385 | << Specialization |
| 9386 | << (Specialization->getTemplateSpecializationKind() == |
| 9387 | TSK_ExplicitSpecialization); |
| 9388 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 9389 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9390 | } |
| 9391 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9392 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 9393 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 9394 | PrevDecl = Specialization; |
| 9395 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9396 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9397 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9398 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9399 | PrevDecl, |
| 9400 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9401 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9402 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9403 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9404 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9405 | // FIXME: We may still want to build some representation of this |
| 9406 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9407 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9408 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9409 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 9410 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9411 | ProcessDeclAttributeList(S, Specialization, D.getDeclSpec().getAttributes()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9412 | |
Hans Wennborg | b8304a6 | 2017-11-29 23:44:11 +0000 | [diff] [blame] | 9413 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 9414 | // instantiation declarations. |
| 9415 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 9416 | Specialization->hasAttr<DLLImportAttr>() && |
| 9417 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 9418 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 9419 | |
| 9420 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 9421 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 9422 | if (Specialization->isDefined()) { |
| 9423 | // Let the ASTConsumer know that this function has been explicitly |
| 9424 | // instantiated now, and its linkage might have changed. |
| 9425 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 9426 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 9427 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9428 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9429 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9430 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9431 | // or a static data member of a class template specialization, the name of |
| 9432 | // the class template specialization in the qualified-id for the member |
| 9433 | // name shall be a simple-template-id. |
| 9434 | // |
| 9435 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9436 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9437 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9438 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9439 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9440 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9441 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9442 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9443 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9444 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9445 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9446 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9447 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9448 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9449 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9450 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9451 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9452 | } |
| 9453 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9454 | TypeResult |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 9455 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9456 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 9457 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 9458 | // This has to hold, because SS is expected to be defined. |
| 9459 | assert(Name && "Expected a name in a dependent tag"); |
| 9460 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9461 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9462 | if (!NNS) |
| 9463 | return true; |
| 9464 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9465 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 9466 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9467 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 9468 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9469 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9470 | return true; |
| 9471 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9472 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9473 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9474 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9475 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9476 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9477 | // Create type-source location information for this type. |
| 9478 | TypeLocBuilder TLB; |
| 9479 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9480 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9481 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 9482 | TL.setNameLoc(NameLoc); |
| 9483 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9484 | } |
| 9485 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9486 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9487 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 9488 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 9489 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9490 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9491 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9492 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9493 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9494 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9495 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9496 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9497 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9498 | << FixItHint::CreateRemoval(TypenameLoc); |
| 9499 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9500 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9501 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 9502 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 9503 | if (T.isNull()) |
| 9504 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9505 | |
| 9506 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9507 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9508 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9509 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9510 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9511 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9512 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9513 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9514 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9515 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9516 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9518 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9519 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9520 | } |
| 9521 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9522 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9523 | Sema::ActOnTypenameType(Scope *S, |
| 9524 | SourceLocation TypenameLoc, |
| 9525 | const CXXScopeSpec &SS, |
| 9526 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9527 | TemplateTy TemplateIn, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9528 | IdentifierInfo *TemplateII, |
| 9529 | SourceLocation TemplateIILoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9530 | SourceLocation LAngleLoc, |
| 9531 | ASTTemplateArgsPtr TemplateArgsIn, |
| 9532 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9533 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9534 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9535 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9536 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9537 | diag::ext_typename_outside_of_template) |
| 9538 | << FixItHint::CreateRemoval(TypenameLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9539 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9540 | // Strangely, non-type results are not ignored by this lookup, so the |
| 9541 | // program is ill-formed if it finds an injected-class-name. |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 9542 | if (TypenameLoc.isValid()) { |
| 9543 | auto *LookupRD = |
| 9544 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
| 9545 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 9546 | Diag(TemplateIILoc, |
| 9547 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9548 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 9549 | << (TemplateKWLoc.isValid() ? 1 : 0 /*'template'/'typename' keyword*/); |
| 9550 | } |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9551 | } |
| 9552 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9553 | // Translate the parser's template argument list in our AST format. |
| 9554 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 9555 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9556 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9557 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9558 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 9559 | // Construct a dependent template specialization type. |
| 9560 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9561 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9562 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 9563 | DTN->getQualifier(), |
| 9564 | DTN->getIdentifier(), |
| 9565 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9566 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9567 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9568 | TypeLocBuilder Builder; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9569 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9570 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9571 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 9572 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 9573 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9574 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9575 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9576 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9577 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9578 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9579 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 9580 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9581 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9582 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9583 | if (T.isNull()) |
| 9584 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9585 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9586 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9587 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9588 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9589 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9590 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9591 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9592 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9593 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9594 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9595 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9596 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9597 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 9598 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9599 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9600 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9601 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9602 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 9603 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 9604 | } |
| 9605 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9606 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9607 | /// Determine whether this failed name lookup should be treated as being |
| 9608 | /// disabled by a usage of std::enable_if. |
| 9609 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9610 | SourceRange &CondRange, Expr *&Cond) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9611 | // We must be looking for a ::type... |
| 9612 | if (!II.isStr("type")) |
| 9613 | return false; |
| 9614 | |
| 9615 | // ... within an explicitly-written template specialization... |
| 9616 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 9617 | return false; |
| 9618 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9619 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 9620 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 9621 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9622 | return false; |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 9623 | const TemplateSpecializationType *EnableIfTST = EnableIfTSTLoc.getTypePtr(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9624 | |
| 9625 | // ... which names a complete class template declaration... |
| 9626 | const TemplateDecl *EnableIfDecl = |
| 9627 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 9628 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 9629 | return false; |
| 9630 | |
| 9631 | // ... called "enable_if". |
| 9632 | const IdentifierInfo *EnableIfII = |
| 9633 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 9634 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 9635 | return false; |
| 9636 | |
| 9637 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9638 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9639 | |
| 9640 | // Dig out the condition. |
| 9641 | Cond = nullptr; |
| 9642 | if (EnableIfTSTLoc.getArgLoc(0).getArgument().getKind() |
| 9643 | != TemplateArgument::Expression) |
| 9644 | return true; |
| 9645 | |
| 9646 | Cond = EnableIfTSTLoc.getArgLoc(0).getSourceExpression(); |
| 9647 | |
| 9648 | // Ignore Boolean literals; they add no value. |
| 9649 | if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts())) |
| 9650 | Cond = nullptr; |
| 9651 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9652 | return true; |
| 9653 | } |
| 9654 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9655 | /// Build the type that describes a C++ typename specifier, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9656 | /// e.g., "typename T::type". |
| 9657 | QualType |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9658 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9659 | SourceLocation KeywordLoc, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9660 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9661 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9662 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9663 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9664 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9665 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9666 | DeclContext *Ctx = computeDeclContext(SS); |
| 9667 | if (!Ctx) { |
| 9668 | // If the nested-name-specifier is dependent and couldn't be |
| 9669 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9670 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9671 | return Context.getDependentNameType(Keyword, |
| 9672 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9673 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 9674 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9675 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9676 | // If the nested-name-specifier refers to the current instantiation, |
| 9677 | // the "typename" keyword itself is superfluous. In C++03, the |
| 9678 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 9679 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 9680 | // 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] | 9681 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9682 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 9683 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9684 | |
| 9685 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9686 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 9687 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9688 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9689 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9690 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9691 | case LookupResult::NotFound: { |
| 9692 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 9693 | // a more specific diagnostic. |
| 9694 | SourceRange CondRange; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9695 | Expr *Cond = nullptr; |
| 9696 | if (isEnableIf(QualifierLoc, II, CondRange, Cond)) { |
| 9697 | // If we have a condition, narrow it down to the specific failed |
| 9698 | // condition. |
| 9699 | if (Cond) { |
| 9700 | Expr *FailedCond; |
| 9701 | std::string FailedDescription; |
| 9702 | std::tie(FailedCond, FailedDescription) = |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 9703 | findFailedBooleanCondition(Cond); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9704 | |
| 9705 | Diag(FailedCond->getExprLoc(), |
| 9706 | diag::err_typename_nested_not_found_requirement) |
| 9707 | << FailedDescription |
| 9708 | << FailedCond->getSourceRange(); |
| 9709 | return QualType(); |
| 9710 | } |
| 9711 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9712 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9713 | << Ctx << CondRange; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9714 | return QualType(); |
| 9715 | } |
| 9716 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 9717 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9718 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9719 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9720 | |
| 9721 | case LookupResult::FoundUnresolvedValue: { |
| 9722 | // We found a using declaration that is a value. Most likely, the using |
| 9723 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9724 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9725 | IILoc); |
| 9726 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 9727 | << Name << Ctx << FullRange; |
| 9728 | if (UnresolvedUsingValueDecl *Using |
| 9729 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 9730 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9731 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 9732 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 9733 | } |
| 9734 | } |
| 9735 | // Fall through to create a dependent typename type, from which we can recover |
| 9736 | // better. |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 9737 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9738 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 9739 | case LookupResult::NotFoundInCurrentInstantiation: |
| 9740 | // Okay, it's a member of an unknown instantiation. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9741 | return Context.getDependentNameType(Keyword, |
| 9742 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9743 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9744 | |
| 9745 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9746 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9747 | // C++ [class.qual]p2: |
| 9748 | // In a lookup in which function names are not ignored and the |
| 9749 | // nested-name-specifier nominates a class C, if the name specified |
| 9750 | // after the nested-name-specifier, when looked up in C, is the |
| 9751 | // injected-class-name of C [...] then the name is instead considered |
| 9752 | // to name the constructor of class C. |
| 9753 | // |
| 9754 | // Unlike in an elaborated-type-specifier, function names are not ignored |
| 9755 | // in typename-specifier lookup. However, they are ignored in all the |
| 9756 | // contexts where we form a typename type with no keyword (that is, in |
| 9757 | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). |
| 9758 | // |
| 9759 | // FIXME: That's not strictly true: mem-initializer-id lookup does not |
| 9760 | // ignore functions, but that appears to be an oversight. |
| 9761 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
| 9762 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
| 9763 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
| 9764 | FoundRD->isInjectedClassName() && |
| 9765 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
| 9766 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9767 | << &II << 1 << 0 /*'typename' keyword used*/; |
| 9768 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9769 | // We found a type. Build an ElaboratedType, since the |
| 9770 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 9771 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9772 | return Context.getElaboratedType(Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9773 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9774 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9775 | } |
| 9776 | |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9777 | // C++ [dcl.type.simple]p2: |
| 9778 | // A type-specifier of the form |
| 9779 | // typename[opt] nested-name-specifier[opt] template-name |
| 9780 | // is a placeholder for a deduced class type [...]. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 9781 | if (getLangOpts().CPlusPlus17) { |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9782 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
| 9783 | return Context.getElaboratedType( |
| 9784 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
| 9785 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
| 9786 | QualType(), false)); |
| 9787 | } |
| 9788 | } |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 9789 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9790 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 9791 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9792 | break; |
| 9793 | |
| 9794 | case LookupResult::FoundOverloaded: |
| 9795 | DiagID = diag::err_typename_nested_not_type; |
| 9796 | Referenced = *Result.begin(); |
| 9797 | break; |
| 9798 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 9799 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9800 | return QualType(); |
| 9801 | } |
| 9802 | |
| 9803 | // If we get here, it's because name lookup did not find a |
| 9804 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9805 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9806 | IILoc); |
| 9807 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9808 | if (Referenced) |
| 9809 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 9810 | << Name; |
| 9811 | return QualType(); |
| 9812 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9813 | |
| 9814 | namespace { |
| 9815 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 9816 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9817 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9818 | SourceLocation Loc; |
| 9819 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9820 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9821 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 9822 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9823 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9824 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9825 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9826 | DeclarationName Entity) |
| 9827 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9828 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9829 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9830 | /// Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9831 | /// transformed. |
| 9832 | /// |
| 9833 | /// For the purposes of type reconstruction, a type has already been |
| 9834 | /// transformed if it is NULL or if it is not dependent. |
| 9835 | bool AlreadyTransformed(QualType T) { |
| 9836 | return T.isNull() || !T->isDependentType(); |
| 9837 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9838 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9839 | /// Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9840 | /// rebuilt. |
| 9841 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9842 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9843 | /// Returns the name of the entity whose type is being rebuilt. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9844 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9845 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9846 | /// Sets the "base" location and entity when that |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 9847 | /// information is known based on another transformation. |
| 9848 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 9849 | this->Loc = Loc; |
| 9850 | this->Entity = Entity; |
| 9851 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9852 | |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 9853 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 9854 | // Lambdas never need to be transformed. |
| 9855 | return E; |
| 9856 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9857 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 9858 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9859 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9860 | /// Rebuilds a type within the context of the current instantiation. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9861 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9862 | /// 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] | 9863 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9864 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9865 | /// partial specialization thereof). This routine will rebuild that type now |
| 9866 | /// that we have entered the declarator's scope, which may produce different |
| 9867 | /// canonical types, e.g., |
| 9868 | /// |
| 9869 | /// \code |
| 9870 | /// template<typename T> |
| 9871 | /// struct X { |
| 9872 | /// typedef T* pointer; |
| 9873 | /// pointer data(); |
| 9874 | /// }; |
| 9875 | /// |
| 9876 | /// template<typename T> |
| 9877 | /// typename X<T>::pointer X<T>::data() { ... } |
| 9878 | /// \endcode |
| 9879 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 9880 | /// 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] | 9881 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 9882 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9883 | /// 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] | 9884 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 9885 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9886 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 9887 | SourceLocation Loc, |
| 9888 | DeclarationName Name) { |
| 9889 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9890 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9891 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9892 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 9893 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 9894 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9895 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9896 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9897 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 9898 | DeclarationName()); |
| 9899 | return Rebuilder.TransformExpr(E); |
| 9900 | } |
| 9901 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9902 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9903 | if (SS.isInvalid()) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9904 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9905 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9906 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9907 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 9908 | DeclarationName()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9909 | NestedNameSpecifierLoc Rebuilt |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9910 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9911 | if (!Rebuilt) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9912 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9913 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9914 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9915 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9916 | } |
| 9917 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9918 | /// Rebuild the template parameters now that we know we're in a current |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9919 | /// instantiation. |
| 9920 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 9921 | TemplateParameterList *Params) { |
| 9922 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 9923 | Decl *Param = Params->getParam(I); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9924 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9925 | // There is nothing to rebuild in a type parameter. |
| 9926 | if (isa<TemplateTypeParmDecl>(Param)) |
| 9927 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9928 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9929 | // Rebuild the template parameter list of a template template parameter. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9930 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9931 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 9932 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 9933 | TTP->getTemplateParameters())) |
| 9934 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9935 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9936 | continue; |
| 9937 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9938 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9939 | // Rebuild the type of a non-type template parameter. |
| 9940 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9941 | TypeSourceInfo *NewTSI |
| 9942 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 9943 | NTTP->getLocation(), |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9944 | NTTP->getDeclName()); |
| 9945 | if (!NewTSI) |
| 9946 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9947 | |
Erik Pilkington | 9f9462a | 2018-08-07 22:59:02 +0000 | [diff] [blame] | 9948 | if (NewTSI->getType()->isUndeducedType()) { |
| 9949 | // C++17 [temp.dep.expr]p3: |
| 9950 | // An id-expression is type-dependent if it contains |
| 9951 | // - an identifier associated by name lookup with a non-type |
| 9952 | // template-parameter declared with a type that contains a |
| 9953 | // placeholder type (7.1.7.4), |
| 9954 | NewTSI = SubstAutoTypeSourceInfo(NewTSI, Context.DependentTy); |
| 9955 | } |
| 9956 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9957 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 9958 | NTTP->setTypeSourceInfo(NewTSI); |
| 9959 | NTTP->setType(NewTSI->getType()); |
| 9960 | } |
| 9961 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9962 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9963 | return false; |
| 9964 | } |
| 9965 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9966 | /// Produces a formatted string that describes the binding of |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9967 | /// template parameters to template arguments. |
| 9968 | std::string |
| 9969 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9970 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9971 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9972 | } |
| 9973 | |
| 9974 | std::string |
| 9975 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9976 | const TemplateArgument *Args, |
| 9977 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9978 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9979 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9980 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9981 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9982 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9983 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9984 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9985 | if (I >= NumArgs) |
| 9986 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9987 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9988 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9989 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9990 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9991 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9992 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9993 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9994 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9995 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9996 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9997 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9998 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9999 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 10000 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10001 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 10002 | |
| 10003 | Out << ']'; |
| 10004 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10005 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 10006 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 10007 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 10008 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 10009 | if (!FD) |
| 10010 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 10011 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 10012 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 10013 | |
| 10014 | // Take tokens to avoid allocations |
| 10015 | LPT->Toks.swap(Toks); |
| 10016 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 10017 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 10018 | |
| 10019 | FD->setLateTemplateParsed(true); |
| 10020 | } |
| 10021 | |
| 10022 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 10023 | if (!FD) |
| 10024 | return; |
| 10025 | FD->setLateTemplateParsed(false); |
| 10026 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 10027 | |
| 10028 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 10029 | DeclContext *DC = CurContext; |
| 10030 | |
| 10031 | while (DC) { |
| 10032 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 10033 | const FunctionDecl *FD = RD->isLocalClass(); |
| 10034 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 10035 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 10036 | return false; |
| 10037 | |
| 10038 | DC = DC->getParent(); |
| 10039 | } |
| 10040 | return false; |
| 10041 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10042 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 10043 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10044 | /// Walk the path from which a declaration was instantiated, and check |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10045 | /// that every explicit specialization along that path is visible. This enforces |
| 10046 | /// C++ [temp.expl.spec]/6: |
| 10047 | /// |
| 10048 | /// If a template, a member template or a member of a class template is |
| 10049 | /// explicitly specialized then that specialization shall be declared before |
| 10050 | /// the first use of that specialization that would cause an implicit |
| 10051 | /// instantiation to take place, in every translation unit in which such a |
| 10052 | /// use occurs; no diagnostic is required. |
| 10053 | /// |
| 10054 | /// and also C++ [temp.class.spec]/1: |
| 10055 | /// |
| 10056 | /// A partial specialization shall be declared before the first use of a |
| 10057 | /// class template specialization that would make use of the partial |
| 10058 | /// specialization as the result of an implicit or explicit instantiation |
| 10059 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 10060 | /// required. |
| 10061 | class ExplicitSpecializationVisibilityChecker { |
| 10062 | Sema &S; |
| 10063 | SourceLocation Loc; |
| 10064 | llvm::SmallVector<Module *, 8> Modules; |
| 10065 | |
| 10066 | public: |
| 10067 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 10068 | : S(S), Loc(Loc) {} |
| 10069 | |
| 10070 | void check(NamedDecl *ND) { |
| 10071 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 10072 | return checkImpl(FD); |
| 10073 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 10074 | return checkImpl(RD); |
| 10075 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 10076 | return checkImpl(VD); |
| 10077 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 10078 | return checkImpl(ED); |
| 10079 | } |
| 10080 | |
| 10081 | private: |
| 10082 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 10083 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 10084 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 10085 | const bool Recover = true; |
| 10086 | |
| 10087 | // If we got a custom set of modules (because only a subset of the |
| 10088 | // declarations are interesting), use them, otherwise let |
| 10089 | // diagnoseMissingImport intelligently pick some. |
| 10090 | if (Modules.empty()) |
| 10091 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 10092 | else |
| 10093 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 10094 | } |
| 10095 | |
| 10096 | // Check a specific declaration. There are three problematic cases: |
| 10097 | // |
| 10098 | // 1) The declaration is an explicit specialization of a template |
| 10099 | // specialization. |
| 10100 | // 2) The declaration is an explicit specialization of a member of an |
| 10101 | // templated class. |
| 10102 | // 3) The declaration is an instantiation of a template, and that template |
| 10103 | // is an explicit specialization of a member of a templated class. |
| 10104 | // |
| 10105 | // We don't need to go any deeper than that, as the instantiation of the |
| 10106 | // surrounding class / etc is not triggered by whatever triggered this |
| 10107 | // instantiation, and thus should be checked elsewhere. |
| 10108 | template<typename SpecDecl> |
| 10109 | void checkImpl(SpecDecl *Spec) { |
| 10110 | bool IsHiddenExplicitSpecialization = false; |
| 10111 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 10112 | IsHiddenExplicitSpecialization = |
| 10113 | Spec->getMemberSpecializationInfo() |
| 10114 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 10115 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10116 | } else { |
| 10117 | checkInstantiated(Spec); |
| 10118 | } |
| 10119 | |
| 10120 | if (IsHiddenExplicitSpecialization) |
| 10121 | diagnose(Spec->getMostRecentDecl(), false); |
| 10122 | } |
| 10123 | |
| 10124 | void checkInstantiated(FunctionDecl *FD) { |
| 10125 | if (auto *TD = FD->getPrimaryTemplate()) |
| 10126 | checkTemplate(TD); |
| 10127 | } |
| 10128 | |
| 10129 | void checkInstantiated(CXXRecordDecl *RD) { |
| 10130 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 10131 | if (!SD) |
| 10132 | return; |
| 10133 | |
| 10134 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10135 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 10136 | checkTemplate(TD); |
| 10137 | else if (auto *TD = |
| 10138 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 10139 | if (!S.hasVisibleDeclaration(TD)) |
| 10140 | diagnose(TD, true); |
| 10141 | checkTemplate(TD); |
| 10142 | } |
| 10143 | } |
| 10144 | |
| 10145 | void checkInstantiated(VarDecl *RD) { |
| 10146 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 10147 | if (!SD) |
| 10148 | return; |
| 10149 | |
| 10150 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10151 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 10152 | checkTemplate(TD); |
| 10153 | else if (auto *TD = |
| 10154 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 10155 | if (!S.hasVisibleDeclaration(TD)) |
| 10156 | diagnose(TD, true); |
| 10157 | checkTemplate(TD); |
| 10158 | } |
| 10159 | } |
| 10160 | |
| 10161 | void checkInstantiated(EnumDecl *FD) {} |
| 10162 | |
| 10163 | template<typename TemplDecl> |
| 10164 | void checkTemplate(TemplDecl *TD) { |
| 10165 | if (TD->isMemberSpecialization()) { |
| 10166 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 10167 | diagnose(TD->getMostRecentDecl(), false); |
| 10168 | } |
| 10169 | } |
| 10170 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 10171 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10172 | |
| 10173 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 10174 | if (!getLangOpts().Modules) |
| 10175 | return; |
| 10176 | |
| 10177 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 10178 | } |
| 10179 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10180 | /// Check whether a template partial specialization that we've discovered |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10181 | /// is hidden, and produce suitable diagnostics if so. |
| 10182 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 10183 | NamedDecl *Spec) { |
| 10184 | llvm::SmallVector<Module *, 8> Modules; |
| 10185 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 10186 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 10187 | MissingImportKind::PartialSpecialization, |
| 10188 | /*Recover*/true); |
| 10189 | } |