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, |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 133 | bool AllowDependent, |
| 134 | bool AllowNonTemplateFunctions) { |
| 135 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) { |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 136 | if (getAsTemplateNameDecl(*I, AllowFunctionTemplates, AllowDependent)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 137 | return true; |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 138 | if (AllowNonTemplateFunctions && |
| 139 | isa<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 140 | return true; |
| 141 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 143 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 146 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 147 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 148 | bool hasTemplateKeyword, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 149 | const UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 150 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 151 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 152 | TemplateTy &TemplateResult, |
| 153 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 154 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 155 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 156 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 157 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 158 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 159 | switch (Name.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 160 | case UnqualifiedIdKind::IK_Identifier: |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 161 | TName = DeclarationName(Name.Identifier); |
| 162 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 163 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 164 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 165 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 166 | Name.OperatorFunctionId.Operator); |
| 167 | break; |
| 168 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 169 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 170 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 171 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 172 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 173 | default: |
| 174 | return TNK_Non_template; |
| 175 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 177 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 179 | AssumedTemplateKind AssumedTemplate; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 180 | LookupResult R(*this, TName, Name.getBeginLoc(), LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 181 | if (LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 182 | MemberOfUnknownSpecialization, SourceLocation(), |
| 183 | &AssumedTemplate)) |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 184 | return TNK_Non_template; |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 185 | |
| 186 | if (AssumedTemplate != AssumedTemplateKind::None) { |
| 187 | TemplateResult = TemplateTy::make(Context.getAssumedTemplateName(TName)); |
| 188 | // Let the parser know whether we found nothing or found functions; if we |
| 189 | // found nothing, we want to more carefully check whether this is actually |
| 190 | // a function template name versus some other kind of undeclared identifier. |
| 191 | return AssumedTemplate == AssumedTemplateKind::FoundNothing |
| 192 | ? TNK_Undeclared_template |
| 193 | : TNK_Function_template; |
| 194 | } |
| 195 | |
| 196 | if (R.empty()) |
| 197 | return TNK_Non_template; |
Richard Smith | 40bd10b | 2019-02-15 00:29:04 +0000 | [diff] [blame] | 198 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 199 | NamedDecl *D = nullptr; |
| 200 | if (R.isAmbiguous()) { |
| 201 | // If we got an ambiguity involving a non-function template, treat this |
| 202 | // as a template name, and pick an arbitrary template for error recovery. |
| 203 | bool AnyFunctionTemplates = false; |
| 204 | for (NamedDecl *FoundD : R) { |
| 205 | if (NamedDecl *FoundTemplate = getAsTemplateNameDecl(FoundD)) { |
| 206 | if (isa<FunctionTemplateDecl>(FoundTemplate)) |
| 207 | AnyFunctionTemplates = true; |
| 208 | else { |
| 209 | D = FoundTemplate; |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // If we didn't find any templates at all, this isn't a template name. |
| 216 | // Leave the ambiguity for a later lookup to diagnose. |
| 217 | if (!D && !AnyFunctionTemplates) { |
| 218 | R.suppressDiagnostics(); |
| 219 | return TNK_Non_template; |
| 220 | } |
| 221 | |
| 222 | // If the only templates were function templates, filter out the rest. |
| 223 | // We'll diagnose the ambiguity later. |
| 224 | if (!D) |
| 225 | FilterAcceptableTemplateNames(R); |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 226 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 227 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 228 | // At this point, we have either picked a single template name declaration D |
| 229 | // or we have a non-empty set of results R containing either one template name |
| 230 | // declaration or a set of function templates. |
| 231 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 232 | TemplateName Template; |
| 233 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 235 | unsigned ResultCount = R.end() - R.begin(); |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 236 | if (!D && ResultCount > 1) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 237 | // We assume that we'll preserve the qualifier from a function |
| 238 | // template name in other ways. |
| 239 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 240 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 241 | |
| 242 | // We'll do this lookup again later. |
| 243 | R.suppressDiagnostics(); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 244 | } else { |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 245 | if (!D) { |
| 246 | D = getAsTemplateNameDecl(*R.begin()); |
| 247 | assert(D && "unambiguous result is not a template name"); |
| 248 | } |
| 249 | |
| 250 | if (isa<UnresolvedUsingValueDecl>(D)) { |
| 251 | // We don't yet know whether this is a template-name or not. |
| 252 | MemberOfUnknownSpecialization = true; |
| 253 | return TNK_Non_template; |
| 254 | } |
| 255 | |
| 256 | TemplateDecl *TD = cast<TemplateDecl>(D); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 257 | |
| 258 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 259 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 260 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 261 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 262 | } else { |
| 263 | Template = TemplateName(TD); |
| 264 | } |
| 265 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 266 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 267 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 268 | |
| 269 | // We'll do this lookup again later. |
| 270 | R.suppressDiagnostics(); |
| 271 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 272 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 273 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 274 | isa<BuiltinTemplateDecl>(TD)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 275 | TemplateKind = |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 276 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 277 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 278 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 280 | TemplateResult = TemplateTy::make(Template); |
| 281 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Richard Smith | 278890f | 2017-02-10 20:39:58 +0000 | [diff] [blame] | 284 | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, |
| 285 | SourceLocation NameLoc, |
| 286 | ParsedTemplateTy *Template) { |
| 287 | CXXScopeSpec SS; |
| 288 | bool MemberOfUnknownSpecialization = false; |
| 289 | |
| 290 | // We could use redeclaration lookup here, but we don't need to: the |
| 291 | // syntactic form of a deduction guide is enough to identify it even |
| 292 | // if we can't look up the template name at all. |
| 293 | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 294 | if (LookupTemplateName(R, S, SS, /*ObjectType*/ QualType(), |
| 295 | /*EnteringContext*/ false, |
| 296 | MemberOfUnknownSpecialization)) |
| 297 | return false; |
Richard Smith | 278890f | 2017-02-10 20:39:58 +0000 | [diff] [blame] | 298 | |
| 299 | if (R.empty()) return false; |
| 300 | if (R.isAmbiguous()) { |
| 301 | // FIXME: Diagnose an ambiguity if we find at least one template. |
| 302 | R.suppressDiagnostics(); |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | // We only treat template-names that name type templates as valid deduction |
| 307 | // guide names. |
| 308 | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); |
| 309 | if (!TD || !getAsTypeTemplateDecl(TD)) |
| 310 | return false; |
| 311 | |
| 312 | if (Template) |
| 313 | *Template = TemplateTy::make(TemplateName(TD)); |
| 314 | return true; |
| 315 | } |
| 316 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 317 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 318 | SourceLocation IILoc, |
| 319 | Scope *S, |
| 320 | const CXXScopeSpec *SS, |
| 321 | TemplateTy &SuggestedTemplate, |
| 322 | TemplateNameKind &SuggestedKind) { |
| 323 | // We can't recover unless there's a dependent scope specifier preceding the |
| 324 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 325 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 326 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 327 | computeDeclContext(*SS)) |
| 328 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 330 | // The code is missing a 'template' keyword prior to the dependent template |
| 331 | // name. |
| 332 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 333 | Diag(IILoc, diag::err_template_kw_missing) |
| 334 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 335 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 336 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 337 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 338 | SuggestedKind = TNK_Dependent_template_name; |
| 339 | return true; |
| 340 | } |
| 341 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 342 | bool Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 343 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 344 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 345 | bool EnteringContext, |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 346 | bool &MemberOfUnknownSpecialization, |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 347 | SourceLocation TemplateKWLoc, |
| 348 | AssumedTemplateKind *ATK) { |
| 349 | if (ATK) |
| 350 | *ATK = AssumedTemplateKind::None; |
| 351 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 352 | Found.setTemplateNameLookup(true); |
| 353 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 354 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 355 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 356 | DeclContext *LookupCtx = nullptr; |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 357 | bool IsDependent = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 358 | if (!ObjectType.isNull()) { |
| 359 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 360 | // x->B::f, and we are looking into the type of the object. |
| 361 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 362 | LookupCtx = computeDeclContext(ObjectType); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 363 | IsDependent = !LookupCtx; |
| 364 | assert((IsDependent || !ObjectType->isIncompleteType() || |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 365 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 366 | "Caller should have completed object type"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 368 | // Template names cannot appear inside an Objective-C class or object type. |
| 369 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 370 | Found.clear(); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 371 | return false; |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 372 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 373 | } else if (SS.isSet()) { |
| 374 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 375 | // so long into the context associated with the prior nested-name-specifier. |
| 376 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 377 | IsDependent = !LookupCtx; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 378 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 379 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 380 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 381 | return true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 385 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 386 | if (LookupCtx) { |
| 387 | // Perform "qualified" name lookup into the declaration context we |
| 388 | // computed, which is either the type of the base of a member access |
| 389 | // expression or the declaration context associated with a prior |
| 390 | // nested-name-specifier. |
| 391 | LookupQualifiedName(Found, LookupCtx); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 392 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 393 | // FIXME: The C++ standard does not clearly specify what happens in the |
| 394 | // case where the object type is dependent, and implementations vary. In |
| 395 | // Clang, we treat a name after a . or -> as a template-name if lookup |
| 396 | // finds a non-dependent member or member of the current instantiation that |
| 397 | // is a type template, or finds no such members and lookup in the context |
| 398 | // of the postfix-expression finds a type template. In the latter case, the |
| 399 | // name is nonetheless dependent, and we may resolve it to a member of an |
| 400 | // unknown specialization when we come to instantiate the template. |
| 401 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 404 | if (!SS.isSet() && (ObjectType.isNull() || Found.empty())) { |
| 405 | // C++ [basic.lookup.classref]p1: |
| 406 | // In a class member access expression (5.2.5), if the . or -> token is |
| 407 | // immediately followed by an identifier followed by a <, the |
| 408 | // identifier must be looked up to determine whether the < is the |
| 409 | // beginning of a template argument list (14.2) or a less-than operator. |
| 410 | // The identifier is first looked up in the class of the object |
| 411 | // expression. If the identifier is not found, it is then looked up in |
| 412 | // the context of the entire postfix-expression and shall name a class |
| 413 | // template. |
| 414 | if (S) |
| 415 | LookupName(Found, S); |
| 416 | |
| 417 | if (!ObjectType.isNull()) { |
| 418 | // FIXME: We should filter out all non-type templates here, particularly |
| 419 | // variable templates and concepts. But the exclusion of alias templates |
| 420 | // and template template parameters is a wording defect. |
| 421 | AllowFunctionTemplatesInLookup = false; |
| 422 | ObjectTypeSearchedInScope = true; |
| 423 | } |
| 424 | |
| 425 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
| 426 | } |
| 427 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 428 | if (Found.isAmbiguous()) |
| 429 | return false; |
| 430 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 431 | if (ATK && !SS.isSet() && ObjectType.isNull() && TemplateKWLoc.isInvalid()) { |
| 432 | // C++2a [temp.names]p2: |
| 433 | // A name is also considered to refer to a template if it is an |
| 434 | // unqualified-id followed by a < and name lookup finds either one or more |
| 435 | // functions or finds nothing. |
| 436 | // |
| 437 | // To keep our behavior consistent, we apply the "finds nothing" part in |
| 438 | // all language modes, and diagnose the empty lookup in ActOnCallExpr if we |
| 439 | // successfully form a call to an undeclared template-id. |
| 440 | bool AllFunctions = |
| 441 | getLangOpts().CPlusPlus2a && |
| 442 | std::all_of(Found.begin(), Found.end(), [](NamedDecl *ND) { |
| 443 | return isa<FunctionDecl>(ND->getUnderlyingDecl()); |
| 444 | }); |
| 445 | if (AllFunctions || (Found.empty() && !IsDependent)) { |
| 446 | // If lookup found any functions, or if this is a name that can only be |
| 447 | // used for a function, then strongly assume this is a function |
| 448 | // template-id. |
| 449 | *ATK = (Found.empty() && Found.getLookupName().isIdentifier()) |
| 450 | ? AssumedTemplateKind::FoundNothing |
| 451 | : AssumedTemplateKind::FoundFunctions; |
| 452 | Found.clear(); |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 457 | if (Found.empty() && !IsDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 458 | // If we did not find any names, attempt to correct any typos. |
| 459 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 460 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 461 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 462 | DefaultFilterCCC FilterCCC{}; |
| 463 | FilterCCC.WantTypeSpecifiers = false; |
| 464 | FilterCCC.WantExpressionKeywords = false; |
| 465 | FilterCCC.WantRemainingKeywords = false; |
| 466 | FilterCCC.WantCXXNamedCasts = true; |
| 467 | if (TypoCorrection Corrected = |
| 468 | CorrectTypo(Found.getLookupNameInfo(), Found.getLookupKind(), S, |
| 469 | &SS, FilterCCC, CTK_ErrorRecovery, LookupCtx)) { |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 470 | if (auto *ND = Corrected.getFoundDecl()) |
| 471 | Found.addDecl(ND); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 472 | FilterAcceptableTemplateNames(Found); |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 473 | if (Found.isAmbiguous()) { |
| 474 | Found.clear(); |
| 475 | } else if (!Found.empty()) { |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 476 | Found.setLookupName(Corrected.getCorrection()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 477 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 478 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 479 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 480 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 481 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 482 | << Name << LookupCtx << DroppedSpecifier |
| 483 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 484 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 485 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 486 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 487 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 491 | NamedDecl *ExampleLookupResult = |
| 492 | Found.empty() ? nullptr : Found.getRepresentativeDecl(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 493 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 494 | if (Found.empty()) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 495 | if (IsDependent) { |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 496 | MemberOfUnknownSpecialization = true; |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 497 | return false; |
| 498 | } |
| 499 | |
| 500 | // If a 'template' keyword was used, a lookup that finds only non-template |
| 501 | // names is an error. |
| 502 | if (ExampleLookupResult && TemplateKWLoc.isValid()) { |
| 503 | Diag(Found.getNameLoc(), diag::err_template_kw_refers_to_non_template) |
| 504 | << Found.getLookupName() << SS.getRange(); |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 505 | Diag(ExampleLookupResult->getUnderlyingDecl()->getLocation(), |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 506 | diag::note_template_kw_refers_to_non_template) |
| 507 | << Found.getLookupName(); |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | return false; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 512 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 513 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 514 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 515 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 516 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 517 | // [...] If the lookup in the class of the object expression finds a |
| 518 | // template, the name is also looked up in the context of the entire |
| 519 | // postfix-expression and [...] |
| 520 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 521 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 522 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 523 | LookupOrdinaryName); |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 524 | FoundOuter.setTemplateNameLookup(true); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 525 | LookupName(FoundOuter, S); |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 526 | // FIXME: We silently accept an ambiguous lookup here, in violation of |
| 527 | // [basic.lookup]/1. |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 528 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 529 | |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 530 | NamedDecl *OuterTemplate; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 531 | if (FoundOuter.empty()) { |
| 532 | // - if the name is not found, the name found in the class of the |
| 533 | // object expression is used, otherwise |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 534 | } else if (FoundOuter.isAmbiguous() || !FoundOuter.isSingleResult() || |
| 535 | !(OuterTemplate = |
| 536 | getAsTemplateNameDecl(FoundOuter.getFoundDecl()))) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 537 | // - if the name is found in the context of the entire |
| 538 | // postfix-expression and does not name a class template, the name |
| 539 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 540 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 541 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 542 | // - if the name found is a class template, it must refer to the same |
| 543 | // entity as the one found in the class of the object expression, |
| 544 | // otherwise the program is ill-formed. |
| 545 | if (!Found.isSingleResult() || |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 546 | getAsTemplateNameDecl(Found.getFoundDecl())->getCanonicalDecl() != |
| 547 | OuterTemplate->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 548 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 549 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 550 | << Found.getLookupName() |
| 551 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 552 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 553 | diag::note_ambig_member_ref_object_type) |
| 554 | << ObjectType; |
| 555 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 556 | diag::note_ambig_member_ref_scope); |
| 557 | |
| 558 | // Recover by taking the template that we found in the object |
| 559 | // expression's type. |
| 560 | } |
| 561 | } |
| 562 | } |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 563 | |
| 564 | return false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 567 | void Sema::diagnoseExprIntendedAsTemplateName(Scope *S, ExprResult TemplateName, |
| 568 | SourceLocation Less, |
| 569 | SourceLocation Greater) { |
| 570 | if (TemplateName.isInvalid()) |
| 571 | return; |
| 572 | |
| 573 | DeclarationNameInfo NameInfo; |
| 574 | CXXScopeSpec SS; |
| 575 | LookupNameKind LookupKind; |
| 576 | |
| 577 | DeclContext *LookupCtx = nullptr; |
| 578 | NamedDecl *Found = nullptr; |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 579 | bool MissingTemplateKeyword = false; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 580 | |
| 581 | // Figure out what name we looked up. |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 582 | if (auto *DRE = dyn_cast<DeclRefExpr>(TemplateName.get())) { |
| 583 | NameInfo = DRE->getNameInfo(); |
| 584 | SS.Adopt(DRE->getQualifierLoc()); |
| 585 | LookupKind = LookupOrdinaryName; |
| 586 | Found = DRE->getFoundDecl(); |
| 587 | } else if (auto *ME = dyn_cast<MemberExpr>(TemplateName.get())) { |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 588 | NameInfo = ME->getMemberNameInfo(); |
| 589 | SS.Adopt(ME->getQualifierLoc()); |
| 590 | LookupKind = LookupMemberName; |
| 591 | LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl(); |
| 592 | Found = ME->getMemberDecl(); |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 593 | } else if (auto *DSDRE = |
| 594 | dyn_cast<DependentScopeDeclRefExpr>(TemplateName.get())) { |
| 595 | NameInfo = DSDRE->getNameInfo(); |
| 596 | SS.Adopt(DSDRE->getQualifierLoc()); |
| 597 | MissingTemplateKeyword = true; |
| 598 | } else if (auto *DSME = |
| 599 | dyn_cast<CXXDependentScopeMemberExpr>(TemplateName.get())) { |
| 600 | NameInfo = DSME->getMemberNameInfo(); |
| 601 | SS.Adopt(DSME->getQualifierLoc()); |
| 602 | MissingTemplateKeyword = true; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 603 | } else { |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 604 | llvm_unreachable("unexpected kind of potential template name"); |
| 605 | } |
| 606 | |
| 607 | // If this is a dependent-scope lookup, diagnose that the 'template' keyword |
| 608 | // was missing. |
| 609 | if (MissingTemplateKeyword) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 610 | Diag(NameInfo.getBeginLoc(), diag::err_template_kw_missing) |
| 611 | << "" << NameInfo.getName().getAsString() << SourceRange(Less, Greater); |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 612 | return; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | // Try to correct the name by looking for templates and C++ named casts. |
| 616 | struct TemplateCandidateFilter : CorrectionCandidateCallback { |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 617 | Sema &S; |
| 618 | TemplateCandidateFilter(Sema &S) : S(S) { |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 619 | WantTypeSpecifiers = false; |
| 620 | WantExpressionKeywords = false; |
| 621 | WantRemainingKeywords = false; |
| 622 | WantCXXNamedCasts = true; |
| 623 | }; |
| 624 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| 625 | if (auto *ND = Candidate.getCorrectionDecl()) |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 626 | return S.getAsTemplateNameDecl(ND); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 627 | return Candidate.isKeyword(); |
| 628 | } |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 629 | |
| 630 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 631 | return llvm::make_unique<TemplateCandidateFilter>(*this); |
| 632 | } |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 633 | }; |
| 634 | |
| 635 | DeclarationName Name = NameInfo.getName(); |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 636 | TemplateCandidateFilter CCC(*this); |
| 637 | if (TypoCorrection Corrected = CorrectTypo(NameInfo, LookupKind, S, &SS, CCC, |
| 638 | CTK_ErrorRecovery, LookupCtx)) { |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 639 | auto *ND = Corrected.getFoundDecl(); |
| 640 | if (ND) |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 641 | ND = getAsTemplateNameDecl(ND); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 642 | if (ND || Corrected.isKeyword()) { |
| 643 | if (LookupCtx) { |
| 644 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 645 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| 646 | Name.getAsString() == CorrectedStr; |
| 647 | diagnoseTypo(Corrected, |
| 648 | PDiag(diag::err_non_template_in_member_template_id_suggest) |
| 649 | << Name << LookupCtx << DroppedSpecifier |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 650 | << SS.getRange(), false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 651 | } else { |
| 652 | diagnoseTypo(Corrected, |
| 653 | PDiag(diag::err_non_template_in_template_id_suggest) |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 654 | << Name, false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 655 | } |
| 656 | if (Found) |
| 657 | Diag(Found->getLocation(), |
| 658 | diag::note_non_template_in_template_id_found); |
| 659 | return; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) |
| 664 | << Name << SourceRange(Less, Greater); |
| 665 | if (Found) |
| 666 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); |
| 667 | } |
| 668 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 669 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 670 | /// was just parsed. This is only possible with an explicit scope |
| 671 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 672 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 673 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 674 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 675 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 676 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 677 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 678 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 679 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 680 | // C++11 [expr.prim.general]p12: |
| 681 | // An id-expression that denotes a non-static data member or non-static |
| 682 | // member function of a class can only be used: |
| 683 | // (...) |
| 684 | // - if that id-expression denotes a non-static data member and it |
| 685 | // appears in an unevaluated operand. |
| 686 | // |
| 687 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 688 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 689 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 690 | // always a MemberExpr. |
| 691 | bool MightBeCxx11UnevalField = |
| 692 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 693 | |
Akira Hatanaka | d644e02 | 2016-12-16 03:19:41 +0000 | [diff] [blame] | 694 | // Check if the nested name specifier is an enum type. |
| 695 | bool IsEnum = false; |
| 696 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
| 697 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
| 698 | |
| 699 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 700 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 701 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 702 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 703 | // Since the 'this' expression is synthesized, we don't need to |
| 704 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 705 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 706 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 707 | return CXXDependentScopeMemberExpr::Create( |
| 708 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 709 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 710 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 713 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 714 | } |
| 715 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 716 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 717 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 718 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 719 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 720 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 721 | return DependentScopeDeclRefExpr::Create( |
| 722 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 723 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 726 | |
| 727 | /// Determine whether we would be unable to instantiate this template (because |
| 728 | /// it either has no definition, or is in the process of being instantiated). |
| 729 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 730 | NamedDecl *Instantiation, |
| 731 | bool InstantiatedFromMember, |
| 732 | const NamedDecl *Pattern, |
| 733 | const NamedDecl *PatternDef, |
| 734 | TemplateSpecializationKind TSK, |
| 735 | bool Complain /*= true*/) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 736 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 737 | isa<VarDecl>(Instantiation)); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 738 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 739 | bool IsEntityBeingDefined = false; |
| 740 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 741 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 742 | |
| 743 | if (PatternDef && !IsEntityBeingDefined) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 744 | NamedDecl *SuggestedDef = nullptr; |
| 745 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 746 | /*OnlyNeedComplete*/false)) { |
| 747 | // If we're allowed to diagnose this and recover, do so. |
| 748 | bool Recover = Complain && !isSFINAEContext(); |
| 749 | if (Complain) |
| 750 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 751 | Sema::MissingImportKind::Definition, Recover); |
| 752 | return !Recover; |
| 753 | } |
| 754 | return false; |
| 755 | } |
| 756 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 757 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 758 | return true; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 759 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 760 | llvm::Optional<unsigned> Note; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 761 | QualType InstantiationTy; |
| 762 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 763 | InstantiationTy = Context.getTypeDeclType(TD); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 764 | if (PatternDef) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 765 | Diag(PointOfInstantiation, |
| 766 | diag::err_template_instantiate_within_definition) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 767 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 768 | << InstantiationTy; |
| 769 | // Not much point in noting the template declaration here, since |
| 770 | // we're lexically inside it. |
| 771 | Instantiation->setInvalidDecl(); |
| 772 | } else if (InstantiatedFromMember) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 773 | if (isa<FunctionDecl>(Instantiation)) { |
| 774 | Diag(PointOfInstantiation, |
| 775 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 776 | << /*member function*/ 1 << Instantiation->getDeclName() |
| 777 | << Instantiation->getDeclContext(); |
| 778 | Note = diag::note_explicit_instantiation_here; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 779 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 780 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 781 | Diag(PointOfInstantiation, |
| 782 | diag::err_implicit_instantiate_member_undefined) |
| 783 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 784 | Note = diag::note_member_declared_at; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 785 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 786 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 787 | if (isa<FunctionDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 788 | Diag(PointOfInstantiation, |
| 789 | diag::err_explicit_instantiation_undefined_func_template) |
| 790 | << Pattern; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 791 | Note = diag::note_explicit_instantiation_here; |
| 792 | } else if (isa<TagDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 793 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 794 | << (TSK != TSK_ImplicitInstantiation) |
| 795 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 796 | Note = diag::note_template_decl_here; |
| 797 | } else { |
| 798 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 799 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 800 | Diag(PointOfInstantiation, |
| 801 | diag::err_explicit_instantiation_undefined_var_template) |
| 802 | << Instantiation; |
| 803 | Instantiation->setInvalidDecl(); |
| 804 | } else |
| 805 | Diag(PointOfInstantiation, |
| 806 | diag::err_explicit_instantiation_undefined_member) |
| 807 | << /*static data member*/ 2 << Instantiation->getDeclName() |
| 808 | << Instantiation->getDeclContext(); |
| 809 | Note = diag::note_explicit_instantiation_here; |
| 810 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 811 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 812 | if (Note) // Diagnostics were emitted. |
| 813 | Diag(Pattern->getLocation(), Note.getValue()); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 814 | |
| 815 | // In general, Instantiation isn't marked invalid to get more than one |
| 816 | // error for multiple undefined instantiations. But the code that does |
| 817 | // explicit declaration -> explicit definition conversion can't handle |
| 818 | // invalid declarations, so mark as invalid in that case. |
| 819 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 820 | Instantiation->setInvalidDecl(); |
| 821 | return true; |
| 822 | } |
| 823 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 824 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 825 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 826 | /// declaration at location Loc. Returns true to indicate that this is |
| 827 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 828 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 829 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 830 | |
| 831 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 832 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 833 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 834 | |
| 835 | // C++ [temp.local]p4: |
| 836 | // A template-parameter shall not be redeclared within its |
| 837 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 839 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 840 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 843 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 844 | /// the parameter D to reference the templated declaration and return a pointer |
| 845 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 846 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 847 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 848 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 849 | return Temp; |
| 850 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 851 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 854 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 855 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 856 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 857 | "Only template template arguments can be pack expansions here"); |
| 858 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 859 | "Template template argument pack expansion without packs"); |
| 860 | ParsedTemplateArgument Result(*this); |
| 861 | Result.EllipsisLoc = EllipsisLoc; |
| 862 | return Result; |
| 863 | } |
| 864 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 865 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 866 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 868 | switch (Arg.getKind()) { |
| 869 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 870 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 871 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 872 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 873 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 874 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 875 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 877 | case ParsedTemplateArgument::NonType: { |
| 878 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 879 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 880 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 882 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 883 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 884 | TemplateArgument TArg; |
| 885 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 886 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 887 | else |
| 888 | TArg = Template; |
| 889 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 890 | Arg.getScopeSpec().getWithLocInContext( |
| 891 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 892 | Arg.getLocation(), |
| 893 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 896 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 897 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 898 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 899 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 900 | /// Translates template arguments as provided by the parser |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 901 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 902 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 903 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 904 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 905 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 906 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 907 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 908 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 909 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 910 | SourceLocation Loc, |
| 911 | IdentifierInfo *Name) { |
| 912 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 913 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 914 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 915 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 916 | } |
| 917 | |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 918 | /// Convert a parsed type into a parsed template argument. This is mostly |
| 919 | /// trivial, except that we may have parsed a C++17 deduced class template |
| 920 | /// specialization type, in which case we should form a template template |
| 921 | /// argument instead of a type template argument. |
| 922 | ParsedTemplateArgument Sema::ActOnTemplateTypeArgument(TypeResult ParsedType) { |
| 923 | TypeSourceInfo *TInfo; |
| 924 | QualType T = GetTypeFromParser(ParsedType.get(), &TInfo); |
| 925 | if (T.isNull()) |
| 926 | return ParsedTemplateArgument(); |
| 927 | assert(TInfo && "template argument with no location"); |
| 928 | |
| 929 | // If we might have formed a deduced template specialization type, convert |
| 930 | // it to a template template argument. |
| 931 | if (getLangOpts().CPlusPlus17) { |
| 932 | TypeLoc TL = TInfo->getTypeLoc(); |
| 933 | SourceLocation EllipsisLoc; |
| 934 | if (auto PET = TL.getAs<PackExpansionTypeLoc>()) { |
| 935 | EllipsisLoc = PET.getEllipsisLoc(); |
| 936 | TL = PET.getPatternLoc(); |
| 937 | } |
| 938 | |
| 939 | CXXScopeSpec SS; |
| 940 | if (auto ET = TL.getAs<ElaboratedTypeLoc>()) { |
| 941 | SS.Adopt(ET.getQualifierLoc()); |
| 942 | TL = ET.getNamedTypeLoc(); |
| 943 | } |
| 944 | |
| 945 | if (auto DTST = TL.getAs<DeducedTemplateSpecializationTypeLoc>()) { |
| 946 | TemplateName Name = DTST.getTypePtr()->getTemplateName(); |
| 947 | if (SS.isSet()) |
| 948 | Name = Context.getQualifiedTemplateName(SS.getScopeRep(), |
| 949 | /*HasTemplateKeyword*/ false, |
| 950 | Name.getAsTemplateDecl()); |
| 951 | ParsedTemplateArgument Result(SS, TemplateTy::make(Name), |
| 952 | DTST.getTemplateNameLoc()); |
| 953 | if (EllipsisLoc.isValid()) |
| 954 | Result = Result.getTemplatePackExpansion(EllipsisLoc); |
| 955 | return Result; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | // This is a normal type template argument. Note, if the type template |
| 960 | // 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] | 961 | // 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] | 962 | // convertTypeTemplateArgumentToTemplate. |
| 963 | return ParsedTemplateArgument(ParsedTemplateArgument::Type, |
| 964 | ParsedType.get().getAsOpaquePtr(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 965 | TInfo->getTypeLoc().getBeginLoc()); |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 968 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 969 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 970 | /// the keyword "typename" was used to declare the type parameter |
| 971 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 972 | /// "class" or "typename" keyword. ParamName is the name of the |
| 973 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 974 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 975 | /// If the type parameter has a default argument, it will be added |
| 976 | /// later via ActOnTypeParameterDefault. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 977 | NamedDecl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 978 | SourceLocation EllipsisLoc, |
| 979 | SourceLocation KeyLoc, |
| 980 | IdentifierInfo *ParamName, |
| 981 | SourceLocation ParamNameLoc, |
| 982 | unsigned Depth, unsigned Position, |
| 983 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 984 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | assert(S->isTemplateParamScope() && |
| 986 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 987 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 988 | SourceLocation Loc = ParamNameLoc; |
| 989 | if (!ParamName) |
| 990 | Loc = KeyLoc; |
| 991 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 992 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 993 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 994 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 995 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 996 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 997 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 998 | |
| 999 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1000 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 1001 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1002 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1003 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1004 | IdResolver.AddDecl(Param); |
| 1005 | } |
| 1006 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1007 | // C++0x [temp.param]p9: |
| 1008 | // A default template-argument may be specified for any kind of |
| 1009 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 1010 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1011 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 1012 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1015 | // Handle the default argument, if provided. |
| 1016 | if (DefaultArg) { |
| 1017 | TypeSourceInfo *DefaultTInfo; |
| 1018 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1019 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1020 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1021 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1022 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1023 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1024 | UPPC_DefaultArgument)) |
| 1025 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1026 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1027 | // Check the template argument itself. |
| 1028 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 1029 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1030 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1031 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1032 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1033 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1034 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1035 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1036 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1039 | /// Check that the type of a non-type template parameter is |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1040 | /// well-formed. |
| 1041 | /// |
| 1042 | /// \returns the (possibly-promoted) parameter type if valid; |
| 1043 | /// otherwise, produces a diagnostic and returns a NULL type. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1044 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
| 1045 | SourceLocation Loc) { |
| 1046 | if (TSI->getType()->isUndeducedType()) { |
Erik Pilkington | 9f9462a | 2018-08-07 22:59:02 +0000 | [diff] [blame] | 1047 | // C++17 [temp.dep.expr]p3: |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1048 | // An id-expression is type-dependent if it contains |
| 1049 | // - an identifier associated by name lookup with a non-type |
| 1050 | // template-parameter declared with a type that contains a |
| 1051 | // placeholder type (7.1.7.4), |
| 1052 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
| 1053 | } |
| 1054 | |
| 1055 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
| 1056 | } |
| 1057 | |
| 1058 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
| 1059 | SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 1060 | // We don't allow variably-modified types as the type of non-type template |
| 1061 | // parameters. |
| 1062 | if (T->isVariablyModifiedType()) { |
| 1063 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 1064 | << T; |
| 1065 | return QualType(); |
| 1066 | } |
| 1067 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1068 | // C++ [temp.param]p4: |
| 1069 | // |
| 1070 | // A non-type template-parameter shall have one of the following |
| 1071 | // (optionally cv-qualified) types: |
| 1072 | // |
| 1073 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1074 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1076 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1078 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1079 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1080 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1081 | // -- std::nullptr_t. |
| 1082 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1083 | // If T is a dependent type, we can't do the check now, so we |
| 1084 | // assume that it is well-formed. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1085 | T->isDependentType() || |
| 1086 | // Allow use of auto in template parameter declarations. |
| 1087 | T->isUndeducedType()) { |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 1088 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 1089 | // are ignored when determining its type. |
| 1090 | return T.getUnqualifiedType(); |
| 1091 | } |
| 1092 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1093 | // C++ [temp.param]p8: |
| 1094 | // |
| 1095 | // A non-type template-parameter of type "array of T" or |
| 1096 | // "function returning T" is adjusted to be of type "pointer to |
| 1097 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1098 | else if (T->isArrayType() || T->isFunctionType()) |
| 1099 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1101 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 1102 | << T; |
| 1103 | |
| 1104 | return QualType(); |
| 1105 | } |
| 1106 | |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1107 | NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1108 | unsigned Depth, |
| 1109 | unsigned Position, |
| 1110 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1111 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1112 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1113 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1114 | // Check that we have valid decl-specifiers specified. |
| 1115 | auto CheckValidDeclSpecifiers = [this, &D] { |
| 1116 | // C++ [temp.param] |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1117 | // p1 |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 1118 | // template-parameter: |
| 1119 | // ... |
| 1120 | // parameter-declaration |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1121 | // p2 |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1122 | // ... A storage class shall not be specified in a template-parameter |
| 1123 | // declaration. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1124 | // [dcl.typedef]p1: |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1125 | // The typedef specifier [...] shall not be used in the decl-specifier-seq |
| 1126 | // of a parameter-declaration |
| 1127 | const DeclSpec &DS = D.getDeclSpec(); |
| 1128 | auto EmitDiag = [this](SourceLocation Loc) { |
| 1129 | Diag(Loc, diag::err_invalid_decl_specifier_in_nontype_parm) |
| 1130 | << FixItHint::CreateRemoval(Loc); |
| 1131 | }; |
| 1132 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) |
| 1133 | EmitDiag(DS.getStorageClassSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1134 | |
Sam McCall | 1371cba | 2017-12-22 07:09:51 +0000 | [diff] [blame] | 1135 | if (DS.getThreadStorageClassSpec() != TSCS_unspecified) |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1136 | EmitDiag(DS.getThreadStorageClassSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1137 | |
| 1138 | // [dcl.inline]p1: |
| 1139 | // The inline specifier can be applied only to the declaration or |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1140 | // definition of a variable or function. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1141 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1142 | if (DS.isInlineSpecified()) |
| 1143 | EmitDiag(DS.getInlineSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1144 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1145 | // [dcl.constexpr]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1146 | // The constexpr specifier shall be applied only to the definition of a |
| 1147 | // variable or variable template or the declaration of a function or |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1148 | // function template. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1149 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1150 | if (DS.isConstexprSpecified()) |
| 1151 | EmitDiag(DS.getConstexprSpecLoc()); |
| 1152 | |
| 1153 | // [dcl.fct.spec]p1: |
| 1154 | // Function-specifiers can be used only in function declarations. |
| 1155 | |
| 1156 | if (DS.isVirtualSpecified()) |
| 1157 | EmitDiag(DS.getVirtualSpecLoc()); |
| 1158 | |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 1159 | if (DS.hasExplicitSpecifier()) |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1160 | EmitDiag(DS.getExplicitSpecLoc()); |
| 1161 | |
| 1162 | if (DS.isNoreturnSpecified()) |
| 1163 | EmitDiag(DS.getNoreturnSpecLoc()); |
| 1164 | }; |
| 1165 | |
| 1166 | CheckValidDeclSpecifiers(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1167 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1168 | if (TInfo->getType()->isUndeducedType()) { |
| 1169 | Diag(D.getIdentifierLoc(), |
| 1170 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 1171 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
| 1172 | } |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1173 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1174 | assert(S->isTemplateParamScope() && |
| 1175 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1176 | bool Invalid = false; |
| 1177 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1178 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1179 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1180 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 1181 | Invalid = true; |
| 1182 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1183 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1184 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1185 | bool IsParameterPack = D.hasEllipsis(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1186 | NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create( |
| 1187 | Context, Context.getTranslationUnitDecl(), D.getBeginLoc(), |
| 1188 | D.getIdentifierLoc(), Depth, Position, ParamName, T, IsParameterPack, |
| 1189 | TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1190 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1191 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1192 | if (Invalid) |
| 1193 | Param->setInvalidDecl(); |
| 1194 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1195 | if (ParamName) { |
| 1196 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 1197 | ParamName); |
| 1198 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1199 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1200 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1201 | IdResolver.AddDecl(Param); |
| 1202 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1203 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1204 | // C++0x [temp.param]p9: |
| 1205 | // A default template-argument may be specified for any kind of |
| 1206 | // template-parameter that is not a template parameter pack. |
| 1207 | if (Default && IsParameterPack) { |
| 1208 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1209 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1212 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1213 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1214 | // Check for unexpanded parameter packs. |
| 1215 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 1216 | return Param; |
| 1217 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1218 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1219 | ExprResult DefaultRes = |
| 1220 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1221 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1222 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1223 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1224 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1225 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1226 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1227 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1228 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1229 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1230 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1231 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1232 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1233 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1234 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1235 | /// has been parsed. S is the current scope. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1236 | NamedDecl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1237 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1238 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1239 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1240 | IdentifierInfo *Name, |
| 1241 | SourceLocation NameLoc, |
| 1242 | unsigned Depth, |
| 1243 | unsigned Position, |
| 1244 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1245 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1246 | assert(S->isTemplateParamScope() && |
| 1247 | "Template template parameter not in template parameter scope!"); |
| 1248 | |
| 1249 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1250 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1251 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 1252 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1253 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 1254 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1255 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1256 | Param->setAccess(AS_public); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1257 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1258 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1259 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1260 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1261 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 1262 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1263 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1264 | IdResolver.AddDecl(Param); |
| 1265 | } |
| 1266 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1267 | if (Params->size() == 0) { |
| 1268 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 1269 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 1270 | Param->setInvalidDecl(); |
| 1271 | } |
| 1272 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1273 | // C++0x [temp.param]p9: |
| 1274 | // A default template-argument may be specified for any kind of |
| 1275 | // template-parameter that is not a template parameter pack. |
| 1276 | if (IsParameterPack && !Default.isInvalid()) { |
| 1277 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 1278 | Default = ParsedTemplateArgument(); |
| 1279 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1280 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1281 | if (!Default.isInvalid()) { |
| 1282 | // Check only that we have a template template argument. We don't want to |
| 1283 | // try to check well-formedness now, because our template template parameter |
| 1284 | // might have dependent types in its template parameters, which we wouldn't |
| 1285 | // be able to match now. |
| 1286 | // |
| 1287 | // If none of the template template parameter's template arguments mention |
| 1288 | // other template parameters, we could actually perform more checking here. |
| 1289 | // However, it isn't worth doing. |
| 1290 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 1291 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 1292 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1293 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1294 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1295 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1296 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1297 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1298 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1299 | DefaultArg.getArgument().getAsTemplate(), |
| 1300 | UPPC_DefaultArgument)) |
| 1301 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1302 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1303 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1304 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1305 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1306 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1309 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 1310 | /// constrained by RequiresClause, that contains the template parameters in |
| 1311 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1312 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1313 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 1314 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1316 | SourceLocation LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1317 | ArrayRef<NamedDecl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1318 | SourceLocation RAngleLoc, |
| 1319 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1320 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1321 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1322 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1323 | return TemplateParameterList::Create( |
| 1324 | Context, TemplateLoc, LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1325 | llvm::makeArrayRef(Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 1326 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1327 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1328 | |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 1329 | static void SetNestedNameSpecifier(Sema &S, TagDecl *T, |
| 1330 | const CXXScopeSpec &SS) { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1331 | if (SS.isSet()) |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 1332 | T->setQualifierInfo(SS.getWithLocInContext(S.Context)); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1335 | DeclResult Sema::CheckClassTemplate( |
| 1336 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
| 1337 | CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc, |
| 1338 | const ParsedAttributesView &Attr, TemplateParameterList *TemplateParams, |
| 1339 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
| 1340 | SourceLocation FriendLoc, unsigned NumOuterTemplateParamLists, |
| 1341 | TemplateParameterList **OuterTemplateParamLists, SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1343 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1344 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1345 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1346 | |
| 1347 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1348 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1349 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1350 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1351 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 1352 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1353 | |
| 1354 | // There is no such thing as an unnamed class template. |
| 1355 | if (!Name) { |
| 1356 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1357 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1360 | // Find any previous declaration with this name. For a friend with no |
| 1361 | // scope explicitly specified, we only look for tag declarations (per |
| 1362 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1363 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1364 | LookupResult Previous(*this, Name, NameLoc, |
| 1365 | (SS.isEmpty() && TUK == TUK_Friend) |
| 1366 | ? LookupTagName : LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1367 | forRedeclarationInCurContext()); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1368 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 1369 | SemanticContext = computeDeclContext(SS, true); |
| 1370 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1371 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 1372 | // in the AST, and historically we have just ignored such friend |
| 1373 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1374 | Diag(NameLoc, TUK == TUK_Friend |
| 1375 | ? diag::warn_template_qualified_friend_ignored |
| 1376 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1377 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1378 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1379 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1381 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 1382 | return true; |
| 1383 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1384 | // If we're adding a template to a dependent context, we may need to |
| 1385 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 1386 | // now that we know what the current instantiation is. |
| 1387 | if (SemanticContext->isDependentContext()) { |
| 1388 | ContextRAII SavedContext(*this, SemanticContext); |
| 1389 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 1390 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1391 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 1392 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc, false); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1393 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1394 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1395 | } else { |
| 1396 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 1397 | |
| 1398 | // C++14 [class.mem]p14: |
| 1399 | // If T is the name of a class, then each of the following shall have a |
| 1400 | // name different from T: |
| 1401 | // -- every member template of class T |
| 1402 | if (TUK != TUK_Friend && |
| 1403 | DiagnoseClassNameShadow(SemanticContext, |
| 1404 | DeclarationNameInfo(Name, NameLoc))) |
| 1405 | return true; |
| 1406 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1407 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1408 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1410 | if (Previous.isAmbiguous()) |
| 1411 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1412 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1413 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1414 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1415 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1416 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1417 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1418 | // Maybe we will complain about the shadowed template parameter. |
| 1419 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1420 | // Just pretend that we didn't see the previous declaration. |
| 1421 | PrevDecl = nullptr; |
| 1422 | } |
| 1423 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1424 | // If there is a previous declaration with the same name, check |
| 1425 | // whether this is a valid redeclaration. |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1426 | ClassTemplateDecl *PrevClassTemplate = |
| 1427 | dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1428 | |
| 1429 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1430 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1431 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1432 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1433 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1434 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1435 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1436 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1437 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1438 | PrevClassTemplate |
| 1439 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1440 | ->getSpecializedTemplate(); |
| 1441 | } |
| 1442 | } |
| 1443 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1444 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1445 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1446 | // [...] When looking for a prior declaration of a class or a function |
| 1447 | // 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] | 1448 | // function is neither a qualified name nor a template-id, scopes outside |
| 1449 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1450 | if (!SS.isSet()) { |
| 1451 | DeclContext *OutermostContext = CurContext; |
| 1452 | while (!OutermostContext->isFileContext()) |
| 1453 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1454 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 1455 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1456 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1457 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1458 | SemanticContext = PrevDecl->getDeclContext(); |
| 1459 | } else { |
| 1460 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1461 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1462 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1463 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1464 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1465 | |
| 1466 | // Check that the chosen semantic context doesn't already contain a |
| 1467 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1468 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1469 | DeclContext *LookupContext = SemanticContext; |
| 1470 | while (LookupContext->isTransparentContext()) |
| 1471 | LookupContext = LookupContext->getLookupParent(); |
| 1472 | LookupQualifiedName(Previous, LookupContext); |
| 1473 | |
| 1474 | if (Previous.isAmbiguous()) |
| 1475 | return true; |
| 1476 | |
| 1477 | if (Previous.begin() != Previous.end()) |
| 1478 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1479 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1480 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1481 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1482 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1483 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1484 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1485 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1486 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1487 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1488 | if (SS.isEmpty() && |
| 1489 | !(PrevClassTemplate && |
| 1490 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1491 | SemanticContext->getRedeclContext()))) { |
| 1492 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1493 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1494 | diag::note_using_decl_target); |
| 1495 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1496 | // Recover by ignoring the old declaration. |
| 1497 | PrevDecl = PrevClassTemplate = nullptr; |
| 1498 | } |
| 1499 | } |
| 1500 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1501 | // TODO Memory management; associated constraints are not always stored. |
| 1502 | Expr *const CurAC = formAssociatedConstraints(TemplateParams, nullptr); |
| 1503 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1504 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1505 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1506 | // for a friend in a dependent context: the template parameter list itself |
| 1507 | // could be dependent. |
| 1508 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1509 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1510 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1511 | /*Complain=*/true, |
| 1512 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1513 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1514 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1515 | // Check for matching associated constraints on redeclarations. |
| 1516 | const Expr *const PrevAC = PrevClassTemplate->getAssociatedConstraints(); |
| 1517 | const bool RedeclACMismatch = [&] { |
| 1518 | if (!(CurAC || PrevAC)) |
| 1519 | return false; // Nothing to check; no mismatch. |
| 1520 | if (CurAC && PrevAC) { |
| 1521 | llvm::FoldingSetNodeID CurACInfo, PrevACInfo; |
| 1522 | CurAC->Profile(CurACInfo, Context, /*Canonical=*/true); |
| 1523 | PrevAC->Profile(PrevACInfo, Context, /*Canonical=*/true); |
| 1524 | if (CurACInfo == PrevACInfo) |
| 1525 | return false; // All good; no mismatch. |
| 1526 | } |
| 1527 | return true; |
| 1528 | }(); |
| 1529 | |
| 1530 | if (RedeclACMismatch) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1531 | Diag(CurAC ? CurAC->getBeginLoc() : NameLoc, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1532 | diag::err_template_different_associated_constraints); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1533 | Diag(PrevAC ? PrevAC->getBeginLoc() : PrevClassTemplate->getLocation(), |
| 1534 | diag::note_template_prev_declaration) |
| 1535 | << /*declaration*/ 0; |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1536 | return true; |
| 1537 | } |
| 1538 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1539 | // C++ [temp.class]p4: |
| 1540 | // In a redeclaration, partial specialization, explicit |
| 1541 | // specialization or explicit instantiation of a class template, |
| 1542 | // the class-key shall agree in kind with the original class |
| 1543 | // template declaration (7.1.5.3). |
| 1544 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1545 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1546 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1548 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1549 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1550 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1551 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1554 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1555 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1556 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1557 | // If we have a prior definition that is not visible, treat this as |
| 1558 | // simply making that previous definition visible. |
| 1559 | NamedDecl *Hidden = nullptr; |
| 1560 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1561 | SkipBody->ShouldSkip = true; |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1562 | SkipBody->Previous = Def; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1563 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1564 | assert(Tmpl && "original definition of a class template is not a " |
| 1565 | "class template?"); |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 1566 | makeMergedDefinitionVisible(Hidden); |
| 1567 | makeMergedDefinitionVisible(Tmpl); |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1568 | } else { |
| 1569 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1570 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1571 | // FIXME: Would it make sense to try to "forget" the previous |
| 1572 | // definition, as part of error recovery? |
| 1573 | return true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1574 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1575 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1576 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1577 | } else if (PrevDecl) { |
| 1578 | // C++ [temp]p5: |
| 1579 | // A class template shall not have the same name as any other |
| 1580 | // template, class, function, object, enumeration, enumerator, |
| 1581 | // namespace, or type in the same scope (3.3), except as specified |
| 1582 | // in (14.5.4). |
| 1583 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1584 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1585 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1588 | // Check the template parameter list of this declaration, possibly |
| 1589 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1590 | // template declaration. Skip this check for a friend in a dependent |
| 1591 | // context, because the template parameter list might be dependent. |
| 1592 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1593 | CheckTemplateParameterList( |
| 1594 | TemplateParams, |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1595 | PrevClassTemplate |
| 1596 | ? PrevClassTemplate->getMostRecentDecl()->getTemplateParameters() |
| 1597 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1598 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1599 | SemanticContext->isDependentContext()) |
| 1600 | ? TPC_ClassTemplateMember |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1601 | : TUK == TUK_Friend ? TPC_FriendClassTemplate : TPC_ClassTemplate, |
| 1602 | SkipBody)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1603 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1605 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1606 | // 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] | 1607 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1608 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1609 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1610 | : diag::err_member_decl_does_not_match) |
| 1611 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1612 | Invalid = true; |
| 1613 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1616 | // If this is a templated friend in a dependent context we should not put it |
| 1617 | // on the redecl chain. In some cases, the templated friend can be the most |
| 1618 | // recent declaration tricking the template instantiator to make substitutions |
| 1619 | // there. |
| 1620 | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
| 1621 | bool ShouldAddRedecl |
| 1622 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
| 1623 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1625 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1626 | PrevClassTemplate && ShouldAddRedecl ? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1627 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1628 | /*DelayTypeCreation=*/true); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 1629 | SetNestedNameSpecifier(*this, NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1630 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1631 | NewClass->setTemplateParameterListsInfo( |
| 1632 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1633 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1634 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1635 | // Add alignment attributes if necessary; these attributes are checked when |
| 1636 | // the ASTContext lays out the structure. |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1637 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1638 | AddAlignmentAttributesForRecord(NewClass); |
| 1639 | AddMsStructLayoutForRecord(NewClass); |
| 1640 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1641 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1642 | // Attach the associated constraints when the declaration will not be part of |
| 1643 | // a decl chain. |
| 1644 | Expr *const ACtoAttach = |
| 1645 | PrevClassTemplate && ShouldAddRedecl ? nullptr : CurAC; |
| 1646 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1647 | ClassTemplateDecl *NewTemplate |
| 1648 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1649 | DeclarationName(Name), TemplateParams, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1650 | NewClass, ACtoAttach); |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1651 | |
| 1652 | if (ShouldAddRedecl) |
| 1653 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
| 1654 | |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1655 | NewClass->setDescribedClassTemplate(NewTemplate); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1657 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1658 | NewTemplate->setModulePrivate(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1659 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1660 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1661 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1662 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1663 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1664 | (void)T; |
| 1665 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1666 | // 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] | 1667 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1668 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1669 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1670 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1671 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1672 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1673 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1674 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1676 | // Set the lexical context of these templates |
| 1677 | NewClass->setLexicalDeclContext(CurContext); |
| 1678 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1679 | |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1680 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1681 | NewClass->startDefinition(); |
| 1682 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1683 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1684 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1685 | if (PrevClassTemplate) |
| 1686 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1687 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1688 | AddPushedVisibilityAttribute(NewClass); |
| 1689 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1690 | if (TUK != TUK_Friend) { |
| 1691 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1692 | Scope *Outer = S; |
| 1693 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1694 | Outer = Outer->getParent(); |
| 1695 | PushOnScopeChains(NewTemplate, Outer); |
| 1696 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1697 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1698 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1699 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1700 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1701 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1702 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1703 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1704 | // Friend templates are visible in fairly strange ways. |
| 1705 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1706 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1707 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1708 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1709 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1710 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1711 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1712 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1713 | FriendDecl *Friend = FriendDecl::Create( |
| 1714 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1715 | Friend->setAccess(AS_public); |
| 1716 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1717 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1718 | |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1719 | if (PrevClassTemplate) |
| 1720 | CheckRedeclarationModuleOwnership(NewTemplate, PrevClassTemplate); |
| 1721 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1722 | if (Invalid) { |
| 1723 | NewTemplate->setInvalidDecl(); |
| 1724 | NewClass->setInvalidDecl(); |
| 1725 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1726 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1727 | ActOnDocumentableDecl(NewTemplate); |
| 1728 | |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1729 | if (SkipBody && SkipBody->ShouldSkip) |
| 1730 | return SkipBody->Previous; |
| 1731 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1732 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1735 | namespace { |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1736 | /// Tree transform to "extract" a transformed type from a class template's |
| 1737 | /// constructor to a deduction guide. |
| 1738 | class ExtractTypeForDeductionGuide |
| 1739 | : public TreeTransform<ExtractTypeForDeductionGuide> { |
| 1740 | public: |
| 1741 | typedef TreeTransform<ExtractTypeForDeductionGuide> Base; |
| 1742 | ExtractTypeForDeductionGuide(Sema &SemaRef) : Base(SemaRef) {} |
| 1743 | |
| 1744 | TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); } |
| 1745 | |
| 1746 | QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) { |
| 1747 | return TransformType( |
| 1748 | TLB, |
| 1749 | TL.getTypedefNameDecl()->getTypeSourceInfo()->getTypeLoc()); |
| 1750 | } |
| 1751 | }; |
| 1752 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1753 | /// Transform to convert portions of a constructor declaration into the |
| 1754 | /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1. |
| 1755 | struct ConvertConstructorToDeductionGuideTransform { |
| 1756 | ConvertConstructorToDeductionGuideTransform(Sema &S, |
| 1757 | ClassTemplateDecl *Template) |
| 1758 | : SemaRef(S), Template(Template) {} |
| 1759 | |
| 1760 | Sema &SemaRef; |
| 1761 | ClassTemplateDecl *Template; |
| 1762 | |
| 1763 | DeclContext *DC = Template->getDeclContext(); |
| 1764 | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
| 1765 | DeclarationName DeductionGuideName = |
| 1766 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
| 1767 | |
| 1768 | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
| 1769 | |
| 1770 | // Index adjustment to apply to convert depth-1 template parameters into |
| 1771 | // depth-0 template parameters. |
| 1772 | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
| 1773 | |
| 1774 | /// Transform a constructor declaration into a deduction guide. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1775 | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
| 1776 | CXXConstructorDecl *CD) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1777 | SmallVector<TemplateArgument, 16> SubstArgs; |
| 1778 | |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1779 | LocalInstantiationScope Scope(SemaRef); |
| 1780 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1781 | // C++ [over.match.class.deduct]p1: |
| 1782 | // -- For each constructor of the class template designated by the |
| 1783 | // template-name, a function template with the following properties: |
| 1784 | |
| 1785 | // -- The template parameters are the template parameters of the class |
| 1786 | // template followed by the template parameters (including default |
| 1787 | // template arguments) of the constructor, if any. |
| 1788 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 1789 | if (FTD) { |
| 1790 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
| 1791 | SmallVector<NamedDecl *, 16> AllParams; |
| 1792 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
| 1793 | AllParams.insert(AllParams.begin(), |
| 1794 | TemplateParams->begin(), TemplateParams->end()); |
| 1795 | SubstArgs.reserve(InnerParams->size()); |
| 1796 | |
| 1797 | // Later template parameters could refer to earlier ones, so build up |
| 1798 | // a list of substituted template arguments as we go. |
| 1799 | for (NamedDecl *Param : *InnerParams) { |
| 1800 | MultiLevelTemplateArgumentList Args; |
| 1801 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1802 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1803 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
| 1804 | if (!NewParam) |
| 1805 | return nullptr; |
| 1806 | AllParams.push_back(NewParam); |
| 1807 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
| 1808 | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
| 1809 | } |
| 1810 | TemplateParams = TemplateParameterList::Create( |
| 1811 | SemaRef.Context, InnerParams->getTemplateLoc(), |
| 1812 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
| 1813 | /*FIXME: RequiresClause*/ nullptr); |
| 1814 | } |
| 1815 | |
| 1816 | // If we built a new template-parameter-list, track that we need to |
| 1817 | // substitute references to the old parameters into references to the |
| 1818 | // new ones. |
| 1819 | MultiLevelTemplateArgumentList Args; |
| 1820 | if (FTD) { |
| 1821 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1822 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1825 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1826 | .getAsAdjusted<FunctionProtoTypeLoc>(); |
| 1827 | assert(FPTL && "no prototype for constructor declaration"); |
| 1828 | |
| 1829 | // Transform the type of the function, adjusting the return type and |
| 1830 | // replacing references to the old parameters with references to the |
| 1831 | // new ones. |
| 1832 | TypeLocBuilder TLB; |
| 1833 | SmallVector<ParmVarDecl*, 8> Params; |
| 1834 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args); |
| 1835 | if (NewType.isNull()) |
| 1836 | return nullptr; |
| 1837 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
| 1838 | |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 1839 | return buildDeductionGuide(TemplateParams, CD->getExplicitSpecifier(), |
| 1840 | NewTInfo, CD->getBeginLoc(), CD->getLocation(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1841 | CD->getEndLoc()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | /// Build a deduction guide with the specified parameter types. |
| 1845 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
| 1846 | SourceLocation Loc = Template->getLocation(); |
| 1847 | |
| 1848 | // Build the requested type. |
| 1849 | FunctionProtoType::ExtProtoInfo EPI; |
| 1850 | EPI.HasTrailingReturn = true; |
| 1851 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
| 1852 | DeductionGuideName, EPI); |
| 1853 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
| 1854 | |
| 1855 | FunctionProtoTypeLoc FPTL = |
| 1856 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
| 1857 | |
| 1858 | // Build the parameters, needed during deduction / substitution. |
| 1859 | SmallVector<ParmVarDecl*, 4> Params; |
| 1860 | for (auto T : ParamTypes) { |
| 1861 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
| 1862 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
| 1863 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
| 1864 | NewParam->setScopeInfo(0, Params.size()); |
| 1865 | FPTL.setParam(Params.size(), NewParam); |
| 1866 | Params.push_back(NewParam); |
| 1867 | } |
| 1868 | |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 1869 | return buildDeductionGuide(Template->getTemplateParameters(), |
| 1870 | ExplicitSpecifier(), TSI, Loc, Loc, Loc); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | private: |
| 1874 | /// Transform a constructor template parameter into a deduction guide template |
| 1875 | /// parameter, rebuilding any internal references to earlier parameters and |
| 1876 | /// renumbering as we go. |
| 1877 | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
| 1878 | MultiLevelTemplateArgumentList &Args) { |
| 1879 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
| 1880 | // TemplateTypeParmDecl's index cannot be changed after creation, so |
| 1881 | // substitute it directly. |
| 1882 | auto *NewTTP = TemplateTypeParmDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1883 | SemaRef.Context, DC, TTP->getBeginLoc(), TTP->getLocation(), |
| 1884 | /*Depth*/ 0, Depth1IndexAdjustment + TTP->getIndex(), |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1885 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
| 1886 | TTP->isParameterPack()); |
| 1887 | if (TTP->hasDefaultArgument()) { |
| 1888 | TypeSourceInfo *InstantiatedDefaultArg = |
| 1889 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
| 1890 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
| 1891 | if (InstantiatedDefaultArg) |
| 1892 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
| 1893 | } |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1894 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
| 1895 | NewTTP); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1896 | return NewTTP; |
| 1897 | } |
| 1898 | |
| 1899 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
| 1900 | return transformTemplateParameterImpl(TTP, Args); |
| 1901 | |
| 1902 | return transformTemplateParameterImpl( |
| 1903 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
| 1904 | } |
| 1905 | template<typename TemplateParmDecl> |
| 1906 | TemplateParmDecl * |
| 1907 | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
| 1908 | MultiLevelTemplateArgumentList &Args) { |
| 1909 | // Ask the template instantiator to do the heavy lifting for us, then adjust |
| 1910 | // the index of the parameter once it's done. |
| 1911 | auto *NewParam = |
| 1912 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
| 1913 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
| 1914 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
| 1915 | return NewParam; |
| 1916 | } |
| 1917 | |
| 1918 | QualType transformFunctionProtoType(TypeLocBuilder &TLB, |
| 1919 | FunctionProtoTypeLoc TL, |
| 1920 | SmallVectorImpl<ParmVarDecl*> &Params, |
| 1921 | MultiLevelTemplateArgumentList &Args) { |
| 1922 | SmallVector<QualType, 4> ParamTypes; |
| 1923 | const FunctionProtoType *T = TL.getTypePtr(); |
| 1924 | |
| 1925 | // -- The types of the function parameters are those of the constructor. |
| 1926 | for (auto *OldParam : TL.getParams()) { |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1927 | ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1928 | if (!NewParam) |
| 1929 | return QualType(); |
| 1930 | ParamTypes.push_back(NewParam->getType()); |
| 1931 | Params.push_back(NewParam); |
| 1932 | } |
| 1933 | |
| 1934 | // -- The return type is the class template specialization designated by |
| 1935 | // the template-name and template arguments corresponding to the |
| 1936 | // template parameters obtained from the class template. |
| 1937 | // |
| 1938 | // We use the injected-class-name type of the primary template instead. |
| 1939 | // This has the convenient property that it is different from any type that |
| 1940 | // the user can write in a deduction-guide (because they cannot enter the |
| 1941 | // context of the template), so implicit deduction guides can never collide |
| 1942 | // with explicit ones. |
| 1943 | QualType ReturnType = DeducedType; |
| 1944 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
| 1945 | |
| 1946 | // Resolving a wording defect, we also inherit the variadicness of the |
| 1947 | // constructor. |
| 1948 | FunctionProtoType::ExtProtoInfo EPI; |
| 1949 | EPI.Variadic = T->isVariadic(); |
| 1950 | EPI.HasTrailingReturn = true; |
| 1951 | |
| 1952 | QualType Result = SemaRef.BuildFunctionType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1953 | ReturnType, ParamTypes, TL.getBeginLoc(), DeductionGuideName, EPI); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1954 | if (Result.isNull()) |
| 1955 | return QualType(); |
| 1956 | |
| 1957 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
| 1958 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
| 1959 | NewTL.setLParenLoc(TL.getLParenLoc()); |
| 1960 | NewTL.setRParenLoc(TL.getRParenLoc()); |
| 1961 | NewTL.setExceptionSpecRange(SourceRange()); |
| 1962 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
| 1963 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I) |
| 1964 | NewTL.setParam(I, Params[I]); |
| 1965 | |
| 1966 | return Result; |
| 1967 | } |
| 1968 | |
| 1969 | ParmVarDecl * |
| 1970 | transformFunctionTypeParam(ParmVarDecl *OldParam, |
| 1971 | MultiLevelTemplateArgumentList &Args) { |
| 1972 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
Richard Smith | 479ba8e | 2017-04-20 01:15:31 +0000 | [diff] [blame] | 1973 | TypeSourceInfo *NewDI; |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1974 | if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { |
Richard Smith | 479ba8e | 2017-04-20 01:15:31 +0000 | [diff] [blame] | 1975 | // Expand out the one and only element in each inner pack. |
| 1976 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); |
| 1977 | NewDI = |
| 1978 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, |
| 1979 | OldParam->getLocation(), OldParam->getDeclName()); |
| 1980 | if (!NewDI) return nullptr; |
| 1981 | NewDI = |
| 1982 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), |
| 1983 | PackTL.getTypePtr()->getNumExpansions()); |
| 1984 | } else |
| 1985 | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
| 1986 | OldParam->getDeclName()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1987 | if (!NewDI) |
| 1988 | return nullptr; |
| 1989 | |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1990 | // Extract the type. This (for instance) replaces references to typedef |
| 1991 | // members of the current instantiations with the definitions of those |
| 1992 | // typedefs, avoiding triggering instantiation of the deduced type during |
| 1993 | // deduction. |
| 1994 | NewDI = ExtractTypeForDeductionGuide(SemaRef).transform(NewDI); |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1995 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1996 | // Resolving a wording defect, we also inherit default arguments from the |
| 1997 | // constructor. |
| 1998 | ExprResult NewDefArg; |
| 1999 | if (OldParam->hasDefaultArg()) { |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 2000 | NewDefArg = SemaRef.SubstExpr(OldParam->getDefaultArg(), Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2001 | if (NewDefArg.isInvalid()) |
| 2002 | return nullptr; |
| 2003 | } |
| 2004 | |
| 2005 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
| 2006 | OldParam->getInnerLocStart(), |
| 2007 | OldParam->getLocation(), |
| 2008 | OldParam->getIdentifier(), |
| 2009 | NewDI->getType(), |
| 2010 | NewDI, |
| 2011 | OldParam->getStorageClass(), |
| 2012 | NewDefArg.get()); |
| 2013 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
| 2014 | OldParam->getFunctionScopeIndex()); |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 2015 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2016 | return NewParam; |
| 2017 | } |
| 2018 | |
| 2019 | NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams, |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 2020 | ExplicitSpecifier ES, TypeSourceInfo *TInfo, |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2021 | SourceLocation LocStart, SourceLocation Loc, |
| 2022 | SourceLocation LocEnd) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2023 | DeclarationNameInfo Name(DeductionGuideName, Loc); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 2024 | ArrayRef<ParmVarDecl *> Params = |
| 2025 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
| 2026 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2027 | // Build the implicit deduction guide template. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2028 | auto *Guide = |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 2029 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, ES, Name, |
| 2030 | TInfo->getType(), TInfo, LocEnd); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2031 | Guide->setImplicit(); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 2032 | Guide->setParams(Params); |
| 2033 | |
| 2034 | for (auto *Param : Params) |
| 2035 | Param->setDeclContext(Guide); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2036 | |
| 2037 | auto *GuideTemplate = FunctionTemplateDecl::Create( |
| 2038 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
| 2039 | GuideTemplate->setImplicit(); |
| 2040 | Guide->setDescribedFunctionTemplate(GuideTemplate); |
| 2041 | |
| 2042 | if (isa<CXXRecordDecl>(DC)) { |
| 2043 | Guide->setAccess(AS_public); |
| 2044 | GuideTemplate->setAccess(AS_public); |
| 2045 | } |
| 2046 | |
| 2047 | DC->addDecl(GuideTemplate); |
| 2048 | return GuideTemplate; |
| 2049 | } |
| 2050 | }; |
| 2051 | } |
| 2052 | |
| 2053 | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
| 2054 | SourceLocation Loc) { |
Gauthier Harnisch | b63e577 | 2019-06-14 08:40:04 +0000 | [diff] [blame^] | 2055 | if (CXXRecordDecl *DefRecord = |
| 2056 | cast<CXXRecordDecl>(Template->getTemplatedDecl())->getDefinition()) { |
| 2057 | TemplateDecl *DescribedTemplate = DefRecord->getDescribedClassTemplate(); |
| 2058 | Template = DescribedTemplate ? DescribedTemplate : Template; |
| 2059 | } |
| 2060 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2061 | DeclContext *DC = Template->getDeclContext(); |
| 2062 | if (DC->isDependentContext()) |
| 2063 | return; |
| 2064 | |
| 2065 | ConvertConstructorToDeductionGuideTransform Transform( |
| 2066 | *this, cast<ClassTemplateDecl>(Template)); |
| 2067 | if (!isCompleteType(Loc, Transform.DeducedType)) |
| 2068 | return; |
| 2069 | |
| 2070 | // Check whether we've already declared deduction guides for this template. |
| 2071 | // FIXME: Consider storing a flag on the template to indicate this. |
| 2072 | auto Existing = DC->lookup(Transform.DeductionGuideName); |
| 2073 | for (auto *D : Existing) |
| 2074 | if (D->isImplicit()) |
| 2075 | return; |
| 2076 | |
| 2077 | // In case we were expanding a pack when we attempted to declare deduction |
| 2078 | // guides, turn off pack expansion for everything we're about to do. |
| 2079 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
| 2080 | // Create a template instantiation record to track the "instantiation" of |
| 2081 | // constructors into deduction guides. |
| 2082 | // FIXME: Add a kind for this to give more meaningful diagnostics. But can |
| 2083 | // this substitution process actually fail? |
| 2084 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
Volodymyr Sapsai | 2f649f3 | 2018-05-14 22:49:44 +0000 | [diff] [blame] | 2085 | if (BuildingDeductionGuides.isInvalid()) |
| 2086 | return; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2087 | |
| 2088 | // Convert declared constructors into deduction guide templates. |
| 2089 | // FIXME: Skip constructors for which deduction must necessarily fail (those |
| 2090 | // for which some class template parameter without a default argument never |
| 2091 | // appears in a deduced context). |
| 2092 | bool AddedAny = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2093 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
| 2094 | D = D->getUnderlyingDecl(); |
| 2095 | if (D->isInvalidDecl() || D->isImplicit()) |
| 2096 | continue; |
| 2097 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 2098 | |
| 2099 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2100 | auto *CD = |
| 2101 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2102 | // Class-scope explicit specializations (MS extension) do not result in |
| 2103 | // deduction guides. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2104 | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization())) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2105 | continue; |
| 2106 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2107 | Transform.transformConstructor(FTD, CD); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2108 | AddedAny = true; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2111 | // C++17 [over.match.class.deduct] |
| 2112 | // -- If C is not defined or does not declare any constructors, an |
| 2113 | // additional function template derived as above from a hypothetical |
| 2114 | // constructor C(). |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2115 | if (!AddedAny) |
| 2116 | Transform.buildSimpleDeductionGuide(None); |
| 2117 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2118 | // -- An additional function template derived as above from a hypothetical |
| 2119 | // constructor C(C), called the copy deduction candidate. |
| 2120 | cast<CXXDeductionGuideDecl>( |
| 2121 | cast<FunctionTemplateDecl>( |
| 2122 | Transform.buildSimpleDeductionGuide(Transform.DeducedType)) |
| 2123 | ->getTemplatedDecl()) |
| 2124 | ->setIsCopyDeductionCandidate(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2127 | /// Diagnose the presence of a default template argument on a |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2128 | /// template parameter, which is ill-formed in certain contexts. |
| 2129 | /// |
| 2130 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2131 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2132 | Sema::TemplateParamListContext TPC, |
| 2133 | SourceLocation ParamLoc, |
| 2134 | SourceRange DefArgRange) { |
| 2135 | switch (TPC) { |
| 2136 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2137 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2138 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2139 | return false; |
| 2140 | |
| 2141 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2142 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2143 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2144 | // A default template-argument shall not be specified in a |
| 2145 | // function template declaration or a function template |
| 2146 | // definition [...] |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2147 | // If a friend function template declaration specifies a default |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2148 | // template-argument, that declaration shall be a definition and shall be |
| 2149 | // the only declaration of the function template in the translation unit. |
| 2150 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2151 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2152 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 2153 | : diag::ext_template_parameter_default_in_function_template) |
| 2154 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2155 | return false; |
| 2156 | |
| 2157 | case Sema::TPC_ClassTemplateMember: |
| 2158 | // C++0x [temp.param]p9: |
| 2159 | // A default template-argument shall not be specified in the |
| 2160 | // template-parameter-lists of the definition of a member of a |
| 2161 | // class template that appears outside of the member's class. |
| 2162 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 2163 | << DefArgRange; |
| 2164 | return true; |
| 2165 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 2166 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2167 | case Sema::TPC_FriendFunctionTemplate: |
| 2168 | // C++ [temp.param]p9: |
| 2169 | // A default template-argument shall not be specified in a |
| 2170 | // friend template declaration. |
| 2171 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 2172 | << DefArgRange; |
| 2173 | return true; |
| 2174 | |
| 2175 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 2176 | // for friend function templates if there is only a single |
| 2177 | // declaration (and it is a definition). Strange! |
| 2178 | } |
| 2179 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2180 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2183 | /// Check for unexpanded parameter packs within the template parameters |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2184 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 2185 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 2186 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2187 | // A template template parameter which is a parameter pack is also a pack |
| 2188 | // expansion. |
| 2189 | if (TTP->isParameterPack()) |
| 2190 | return false; |
| 2191 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2192 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 2193 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 2194 | NamedDecl *P = Params->getParam(I); |
| 2195 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2196 | if (!NTTP->isParameterPack() && |
| 2197 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2198 | NTTP->getTypeSourceInfo(), |
| 2199 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 2200 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2201 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2202 | continue; |
| 2203 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2204 | |
| 2205 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2206 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 2207 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 2208 | return true; |
| 2209 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2211 | return false; |
| 2212 | } |
| 2213 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2214 | /// Checks the validity of a template parameter list, possibly |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2215 | /// considering the template parameter list from a previous |
| 2216 | /// declaration. |
| 2217 | /// |
| 2218 | /// If an "old" template parameter list is provided, it must be |
| 2219 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 2220 | /// template parameter list. |
| 2221 | /// |
| 2222 | /// \param NewParams Template parameter list for a new template |
| 2223 | /// declaration. This template parameter list will be updated with any |
| 2224 | /// default arguments that are carried through from the previous |
| 2225 | /// template parameter list. |
| 2226 | /// |
| 2227 | /// \param OldParams If provided, template parameter list from a |
| 2228 | /// previous declaration of the same template. Default template |
| 2229 | /// arguments will be merged from the old template parameter list to |
| 2230 | /// the new template parameter list. |
| 2231 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2232 | /// \param TPC Describes the context in which we are checking the given |
| 2233 | /// template parameter list. |
| 2234 | /// |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2235 | /// \param SkipBody If we might have already made a prior merged definition |
| 2236 | /// of this template visible, the corresponding body-skipping information. |
| 2237 | /// Default argument redefinition is not an error when skipping such a body, |
| 2238 | /// because (under the ODR) we can assume the default arguments are the same |
| 2239 | /// as the prior merged definition. |
| 2240 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2241 | /// \returns true if an error occurred, false otherwise. |
| 2242 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2243 | TemplateParameterList *OldParams, |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2244 | TemplateParamListContext TPC, |
| 2245 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2246 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2248 | // C++ [temp.param]p10: |
| 2249 | // The set of default template-arguments available for use with a |
| 2250 | // template declaration or definition is obtained by merging the |
| 2251 | // default arguments from the definition (if in scope) and all |
| 2252 | // declarations in scope in the same way default function |
| 2253 | // arguments are (8.3.6). |
| 2254 | bool SawDefaultArgument = false; |
| 2255 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2256 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 2257 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 2258 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2259 | if (OldParams) |
| 2260 | OldParam = OldParams->begin(); |
| 2261 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2262 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2263 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2264 | NewParamEnd = NewParams->end(); |
| 2265 | NewParam != NewParamEnd; ++NewParam) { |
| 2266 | // Variables used to diagnose redundant default arguments |
| 2267 | bool RedundantDefaultArg = false; |
| 2268 | SourceLocation OldDefaultLoc; |
| 2269 | SourceLocation NewDefaultLoc; |
| 2270 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2271 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2272 | bool MissingDefaultArg = false; |
| 2273 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2274 | // Variable used to diagnose non-final parameter packs |
| 2275 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2276 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2277 | if (TemplateTypeParmDecl *NewTypeParm |
| 2278 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2279 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2280 | if (NewTypeParm->hasDefaultArgument() && |
| 2281 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2282 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2283 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2284 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2285 | NewTypeParm->removeDefaultArgument(); |
| 2286 | |
| 2287 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2289 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2290 | if (NewTypeParm->isParameterPack()) { |
| 2291 | assert(!NewTypeParm->hasDefaultArgument() && |
| 2292 | "Parameter packs can't have a default argument!"); |
| 2293 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2294 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2295 | NewTypeParm->hasDefaultArgument() && |
| 2296 | (!SkipBody || !SkipBody->ShouldSkip)) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2297 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2298 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2299 | SawDefaultArgument = true; |
| 2300 | RedundantDefaultArg = true; |
| 2301 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2302 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 2303 | // Merge the default argument from the old declaration to the |
| 2304 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2305 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2306 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2307 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 2308 | SawDefaultArgument = true; |
| 2309 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2310 | } else if (SawDefaultArgument) |
| 2311 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2312 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2313 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2314 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2315 | if (!NewNonTypeParm->isParameterPack() && |
| 2316 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2317 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2318 | UPPC_NonTypeTemplateParameterType)) { |
| 2319 | Invalid = true; |
| 2320 | continue; |
| 2321 | } |
| 2322 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2323 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2324 | if (NewNonTypeParm->hasDefaultArgument() && |
| 2325 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2326 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2327 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2328 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2331 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2332 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2333 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2334 | if (NewNonTypeParm->isParameterPack()) { |
| 2335 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 2336 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2337 | if (!NewNonTypeParm->isPackExpansion()) |
| 2338 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2339 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2340 | NewNonTypeParm->hasDefaultArgument() && |
| 2341 | (!SkipBody || !SkipBody->ShouldSkip)) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2342 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2343 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2344 | SawDefaultArgument = true; |
| 2345 | RedundantDefaultArg = true; |
| 2346 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2347 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 2348 | // Merge the default argument from the old declaration to the |
| 2349 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2350 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2351 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2352 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 2353 | SawDefaultArgument = true; |
| 2354 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2355 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2357 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2358 | TemplateTemplateParmDecl *NewTemplateParm |
| 2359 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2360 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2361 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 2362 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2363 | Invalid = true; |
| 2364 | continue; |
| 2365 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2366 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2367 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2368 | if (NewTemplateParm->hasDefaultArgument() && |
| 2369 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2370 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2371 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2372 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2373 | |
| 2374 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2375 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2376 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2377 | if (NewTemplateParm->isParameterPack()) { |
| 2378 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 2379 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2380 | if (!NewTemplateParm->isPackExpansion()) |
| 2381 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2382 | } else if (OldTemplateParm && |
| 2383 | hasVisibleDefaultArgument(OldTemplateParm) && |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2384 | NewTemplateParm->hasDefaultArgument() && |
| 2385 | (!SkipBody || !SkipBody->ShouldSkip)) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2386 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 2387 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2388 | SawDefaultArgument = true; |
| 2389 | RedundantDefaultArg = true; |
| 2390 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2391 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 2392 | // Merge the default argument from the old declaration to the |
| 2393 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2394 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2395 | PreviousDefaultArgLoc |
| 2396 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2397 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 2398 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2399 | PreviousDefaultArgLoc |
| 2400 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2401 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2405 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2406 | // If a template parameter of a primary class template or alias template |
| 2407 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2408 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2409 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 2410 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2411 | Diag((*NewParam)->getLocation(), |
| 2412 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 2413 | Invalid = true; |
| 2414 | } |
| 2415 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2416 | if (RedundantDefaultArg) { |
| 2417 | // C++ [temp.param]p12: |
| 2418 | // A template-parameter shall not be given default arguments |
| 2419 | // by two different declarations in the same scope. |
| 2420 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 2421 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 2422 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 2423 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2424 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2425 | // If a template-parameter of a class template has a default |
| 2426 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 2427 | // have a default template-argument supplied or be a template parameter |
| 2428 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2429 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2430 | diag::err_template_param_default_arg_missing); |
| 2431 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 2432 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2433 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | // If we have an old template parameter list that we're merging |
| 2437 | // in, move on to the next parameter. |
| 2438 | if (OldParams) |
| 2439 | ++OldParam; |
| 2440 | } |
| 2441 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2442 | // We were missing some default arguments at the end of the list, so remove |
| 2443 | // all of the default arguments. |
| 2444 | if (RemoveDefaultArguments) { |
| 2445 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2446 | NewParamEnd = NewParams->end(); |
| 2447 | NewParam != NewParamEnd; ++NewParam) { |
| 2448 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 2449 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2450 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2451 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 2452 | NTTP->removeDefaultArgument(); |
| 2453 | else |
| 2454 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 2455 | } |
| 2456 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2457 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2458 | return Invalid; |
| 2459 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2460 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2461 | namespace { |
| 2462 | |
| 2463 | /// A class which looks for a use of a certain level of template |
| 2464 | /// parameter. |
| 2465 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 2466 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 2467 | |
| 2468 | unsigned Depth; |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2469 | |
| 2470 | // Whether we're looking for a use of a template parameter that makes the |
| 2471 | // overall construct type-dependent / a dependent type. This is strictly |
| 2472 | // best-effort for now; we may fail to match at all for a dependent type |
| 2473 | // in some cases if this is set. |
| 2474 | bool IgnoreNonTypeDependent; |
| 2475 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2476 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2477 | SourceLocation MatchLoc; |
| 2478 | |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2479 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
| 2480 | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
| 2481 | Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2482 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2483 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2484 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
| 2485 | NamedDecl *ND = Params->getParam(0); |
| 2486 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 2487 | Depth = PD->getDepth(); |
| 2488 | } else if (NonTypeTemplateParmDecl *PD = |
| 2489 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 2490 | Depth = PD->getDepth(); |
| 2491 | } else { |
| 2492 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 2493 | } |
| 2494 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2495 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2496 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2497 | if (ParmDepth >= Depth) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2498 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2499 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2500 | return true; |
| 2501 | } |
| 2502 | return false; |
| 2503 | } |
| 2504 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2505 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 2506 | // Prune out non-type-dependent expressions if requested. This can |
| 2507 | // sometimes result in us failing to find a template parameter reference |
| 2508 | // (if a value-dependent expression creates a dependent type), but this |
| 2509 | // mode is best-effort only. |
| 2510 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 2511 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 2512 | return true; |
| 2513 | return super::TraverseStmt(S, Q); |
| 2514 | } |
| 2515 | |
| 2516 | bool TraverseTypeLoc(TypeLoc TL) { |
| 2517 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 2518 | !TL.getType()->isDependentType()) |
| 2519 | return true; |
| 2520 | return super::TraverseTypeLoc(TL); |
| 2521 | } |
| 2522 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2523 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2524 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 2525 | } |
| 2526 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2527 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2528 | // For a best-effort search, keep looking until we find a location. |
| 2529 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | bool TraverseTemplateName(TemplateName N) { |
| 2533 | if (TemplateTemplateParmDecl *PD = |
| 2534 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2535 | if (Matches(PD->getDepth())) |
| 2536 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2537 | return super::TraverseTemplateName(N); |
| 2538 | } |
| 2539 | |
| 2540 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 2541 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2542 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 2543 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2544 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2545 | return super::VisitDeclRefExpr(E); |
| 2546 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2547 | |
| 2548 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 2549 | return TraverseType(T->getReplacementType()); |
| 2550 | } |
| 2551 | |
| 2552 | bool |
| 2553 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 2554 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 2555 | } |
| 2556 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 2557 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 2558 | return TraverseType(T->getInjectedSpecializationType()); |
| 2559 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2560 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2561 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2562 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2563 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2564 | /// list. |
| 2565 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2566 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2567 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2568 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2569 | return Checker.Match; |
| 2570 | } |
| 2571 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2572 | // Find the source range corresponding to the named type in the given |
| 2573 | // nested-name-specifier, if any. |
| 2574 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 2575 | QualType T, |
| 2576 | const CXXScopeSpec &SS) { |
| 2577 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 2578 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 2579 | if (const Type *CurType = NNS->getAsType()) { |
| 2580 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 2581 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 2582 | } else |
| 2583 | break; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2584 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2585 | NNSLoc = NNSLoc.getPrefix(); |
| 2586 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2587 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2588 | return SourceRange(); |
| 2589 | } |
| 2590 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2591 | /// Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2592 | /// specifier, returning the template parameter list that applies to the |
| 2593 | /// name. |
| 2594 | /// |
| 2595 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 2596 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2597 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2598 | /// \param DeclLoc The location of the declaration itself. |
| 2599 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2600 | /// \param SS the scope specifier that will be matched to the given template |
| 2601 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 2602 | /// being declared. |
| 2603 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2604 | /// \param TemplateId The template-id following the scope specifier, if there |
| 2605 | /// is one. Used to check for a missing 'template<>'. |
| 2606 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2607 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 2608 | /// innermost template parameter lists. |
| 2609 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2610 | /// \param IsFriend Whether to apply the slightly different rules for |
| 2611 | /// matching template parameters to scope specifiers in friend |
| 2612 | /// declarations. |
| 2613 | /// |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2614 | /// \param IsMemberSpecialization will be set true if the scope specifier |
| 2615 | /// denotes a fully-specialized type, and therefore this is a declaration of |
| 2616 | /// a member specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2617 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2619 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2620 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2622 | /// 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] | 2623 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2624 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 2625 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2626 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2627 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2628 | bool &IsMemberSpecialization, bool &Invalid) { |
| 2629 | IsMemberSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2630 | Invalid = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2631 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2632 | // The sequence of nested types to which we will match up the template |
| 2633 | // parameter lists. We first build this list by starting with the type named |
| 2634 | // 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] | 2635 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2636 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2637 | if (SS.getScopeRep()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2638 | if (CXXRecordDecl *Record |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2639 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 2640 | T = Context.getTypeDeclType(Record); |
| 2641 | else |
| 2642 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 2643 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2644 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2645 | // If we found an explicit specialization that prevents us from needing |
| 2646 | // 'template<>' headers, this will be set to the location of that |
| 2647 | // explicit specialization. |
| 2648 | SourceLocation ExplicitSpecLoc; |
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 | while (!T.isNull()) { |
| 2651 | NestedTypes.push_back(T); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2652 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2653 | // Retrieve the parent of a record type. |
| 2654 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2655 | // If this type is an explicit specialization, we're done. |
| 2656 | if (ClassTemplateSpecializationDecl *Spec |
| 2657 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2658 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2659 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 2660 | ExplicitSpecLoc = Spec->getLocation(); |
| 2661 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 2662 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2663 | } else if (Record->getTemplateSpecializationKind() |
| 2664 | == TSK_ExplicitSpecialization) { |
| 2665 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2666 | break; |
| 2667 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2668 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2669 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 2670 | T = Context.getTypeDeclType(Parent); |
| 2671 | else |
| 2672 | T = QualType(); |
| 2673 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2676 | if (const TemplateSpecializationType *TST |
| 2677 | = T->getAs<TemplateSpecializationType>()) { |
| 2678 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 2679 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 2680 | T = Context.getTypeDeclType(Parent); |
| 2681 | else |
| 2682 | T = QualType(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2683 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2684 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2685 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2686 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2687 | // Look one step prior in a dependent template specialization type. |
| 2688 | if (const DependentTemplateSpecializationType *DependentTST |
| 2689 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2690 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 2691 | T = QualType(NNS->getAsType(), 0); |
| 2692 | else |
| 2693 | T = QualType(); |
| 2694 | continue; |
| 2695 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2696 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2697 | // Look one step prior in a dependent name type. |
| 2698 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 2699 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 2700 | T = QualType(NNS->getAsType(), 0); |
| 2701 | else |
| 2702 | T = QualType(); |
| 2703 | continue; |
| 2704 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2705 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2706 | // Retrieve the parent of an enumeration type. |
| 2707 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 2708 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 2709 | // check here. |
| 2710 | EnumDecl *Enum = EnumT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2711 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2712 | // Get to the parent type. |
| 2713 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 2714 | T = Context.getTypeDeclType(Parent); |
| 2715 | else |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2716 | T = QualType(); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2717 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2718 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2719 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2720 | T = QualType(); |
| 2721 | } |
| 2722 | // Reverse the nested types list, since we want to traverse from the outermost |
| 2723 | // to the innermost while checking template-parameter-lists. |
| 2724 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2725 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2726 | // C++0x [temp.expl.spec]p17: |
| 2727 | // A member or a member template may be nested within many |
| 2728 | // enclosing class templates. In an explicit specialization for |
| 2729 | // such a member, the member declaration shall be preceded by a |
| 2730 | // template<> for each enclosing class template that is |
| 2731 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2732 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2733 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2734 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2735 | if (SawNonEmptyTemplateParameterList) { |
| 2736 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 2737 | << !Recovery << Range; |
| 2738 | Invalid = true; |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2739 | IsMemberSpecialization = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2740 | return true; |
| 2741 | } |
| 2742 | |
| 2743 | return false; |
| 2744 | }; |
| 2745 | |
| 2746 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 2747 | // Check that we can have an explicit specialization here. |
| 2748 | if (CheckExplicitSpecialization(Range, true)) |
| 2749 | return true; |
| 2750 | |
| 2751 | // We don't have a template header, but we should. |
| 2752 | SourceLocation ExpectedTemplateLoc; |
| 2753 | if (!ParamLists.empty()) |
| 2754 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 2755 | else |
| 2756 | ExpectedTemplateLoc = DeclStartLoc; |
| 2757 | |
| 2758 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 2759 | << Range |
| 2760 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 2761 | return false; |
| 2762 | }; |
| 2763 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2764 | unsigned ParamIdx = 0; |
| 2765 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 2766 | ++TypeIdx) { |
| 2767 | T = NestedTypes[TypeIdx]; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2769 | // Whether we expect a 'template<>' header. |
| 2770 | bool NeedEmptyTemplateHeader = false; |
| 2771 | |
| 2772 | // Whether we expect a template header with parameters. |
| 2773 | bool NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2774 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2775 | // For a dependent type, the set of template parameters that we |
| 2776 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2777 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2778 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2779 | // C++0x [temp.expl.spec]p15: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2780 | // A member or a member template may be nested within many enclosing |
| 2781 | // class templates. In an explicit specialization for such a member, the |
| 2782 | // member declaration shall be preceded by a template<> for each |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2783 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2784 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2785 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 2786 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 2787 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 2788 | NeedNonemptyTemplateHeader = true; |
| 2789 | } else if (Record->isDependentType()) { |
| 2790 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2791 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2792 | ->getTemplateParameters(); |
| 2793 | NeedNonemptyTemplateHeader = true; |
| 2794 | } |
| 2795 | } else if (ClassTemplateSpecializationDecl *Spec |
| 2796 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 2797 | // C++0x [temp.expl.spec]p4: |
| 2798 | // Members of an explicitly specialized class template are defined |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2799 | // in the same manner as members of normal classes, and not using |
| 2800 | // the template<> syntax. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2801 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 2802 | NeedEmptyTemplateHeader = true; |
| 2803 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 2804 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2805 | } else if (Record->getTemplateSpecializationKind()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2806 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2807 | != TSK_ExplicitSpecialization && |
| 2808 | TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2809 | IsMemberSpecialization = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2810 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2811 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2812 | } |
| 2813 | } else if (const TemplateSpecializationType *TST |
| 2814 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 2815 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2816 | ExpectedTemplateParams = Template->getTemplateParameters(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2817 | NeedNonemptyTemplateHeader = true; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2818 | } |
| 2819 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2820 | // FIXME: We actually could/should check the template arguments here |
| 2821 | // against the corresponding template parameter list. |
| 2822 | NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2825 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2826 | // In an explicit specialization declaration for a member of a class |
| 2827 | // template or a member template that ap- pears in namespace scope, the |
| 2828 | // member template and some of its enclosing class templates may remain |
| 2829 | // unspecialized, except that the declaration shall not explicitly |
| 2830 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2831 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2832 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2833 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2834 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2835 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2836 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2837 | } else |
| 2838 | SawNonEmptyTemplateParameterList = true; |
| 2839 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2840 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2841 | if (NeedEmptyTemplateHeader) { |
| 2842 | // 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] | 2843 | // here, then it's a member specialization. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2844 | if (TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2845 | IsMemberSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2846 | |
| 2847 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2848 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2849 | // The header has template parameters when it shouldn't. Complain. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2850 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2851 | diag::err_template_param_list_matches_nontemplate) |
| 2852 | << T |
| 2853 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2854 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2855 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2856 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2857 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2858 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2859 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2860 | // Consume this template header. |
| 2861 | ++ParamIdx; |
| 2862 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2863 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2864 | |
| 2865 | if (!IsFriend) |
| 2866 | if (DiagnoseMissingExplicitSpecialization( |
| 2867 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2868 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2869 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2870 | continue; |
| 2871 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2872 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2873 | if (NeedNonemptyTemplateHeader) { |
| 2874 | // In friend declarations we can have template-ids which don't |
| 2875 | // depend on the corresponding template parameter lists. But |
| 2876 | // assume that empty parameter lists are supposed to match this |
| 2877 | // template-id. |
| 2878 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2879 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2880 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2881 | ExpectedTemplateParams = nullptr; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2882 | else |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2883 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2885 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2886 | if (ParamIdx < ParamLists.size()) { |
| 2887 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2888 | if (ExpectedTemplateParams && |
| 2889 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2890 | ExpectedTemplateParams, |
| 2891 | true, TPL_TemplateMatch)) |
| 2892 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2893 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2894 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2895 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2896 | TPC_ClassTemplateMember)) |
| 2897 | Invalid = true; |
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 | ++ParamIdx; |
| 2900 | continue; |
| 2901 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2902 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2903 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2904 | << T |
| 2905 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2906 | Invalid = true; |
| 2907 | continue; |
| 2908 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2909 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2910 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2911 | // If there were at least as many template-ids as there were template |
| 2912 | // parameter lists, then there are no template parameter lists remaining for |
| 2913 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2914 | if (ParamIdx >= ParamLists.size()) { |
| 2915 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2916 | // We don't have a template header for the declaration itself, but we |
| 2917 | // should. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2918 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2919 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2920 | |
| 2921 | // Fabricate an empty template parameter list for the invented header. |
| 2922 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2923 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2924 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2927 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2928 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2929 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2930 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2931 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2932 | bool HasAnyExplicitSpecHeader = false; |
| 2933 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2934 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2935 | if (ParamLists[I]->size() == 0) |
| 2936 | HasAnyExplicitSpecHeader = true; |
| 2937 | else |
| 2938 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2939 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2940 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2941 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2942 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2943 | : diag::err_template_spec_extra_headers) |
| 2944 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2945 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2946 | |
| 2947 | // If there was a specialization somewhere, such that 'template<>' is |
| 2948 | // not required, and there were any 'template<>' headers, note where the |
| 2949 | // specialization occurred. |
| 2950 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2951 | Diag(ExplicitSpecLoc, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2952 | diag::note_explicit_template_spec_does_not_need_header) |
| 2953 | << NestedTypes.back(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2954 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2955 | // We have a template parameter list with no corresponding scope, which |
| 2956 | // means that the resulting template declaration can't be instantiated |
| 2957 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2958 | if (!AllExplicitSpecHeaders) |
| 2959 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2960 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2961 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2962 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2963 | // In an explicit specialization declaration for a member of a class |
| 2964 | // template or a member template that ap- pears in namespace scope, the |
| 2965 | // member template and some of its enclosing class templates may remain |
| 2966 | // unspecialized, except that the declaration shall not explicitly |
| 2967 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2968 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2969 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2970 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2971 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2972 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2973 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2974 | // Return the last template parameter list, which corresponds to the |
| 2975 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2976 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2979 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2980 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2981 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2982 | << (isa<FunctionTemplateDecl>(Template) |
| 2983 | ? 0 |
| 2984 | : isa<ClassTemplateDecl>(Template) |
| 2985 | ? 1 |
| 2986 | : isa<VarTemplateDecl>(Template) |
| 2987 | ? 2 |
| 2988 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2989 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2990 | return; |
| 2991 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2992 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2993 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2994 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2995 | IEnd = OST->end(); |
| 2996 | I != IEnd; ++I) |
| 2997 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2998 | << 0 << (*I)->getDeclName(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3000 | return; |
| 3001 | } |
| 3002 | } |
| 3003 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3004 | static QualType |
| 3005 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 3006 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 3007 | SourceLocation TemplateLoc, |
| 3008 | TemplateArgumentListInfo &TemplateArgs) { |
| 3009 | ASTContext &Context = SemaRef.getASTContext(); |
| 3010 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 3011 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3012 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 3013 | // S<T, 0, ..., N-1>. |
| 3014 | |
| 3015 | // C++14 [inteseq.intseq]p1: |
| 3016 | // T shall be an integer type. |
| 3017 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 3018 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 3019 | diag::err_integer_sequence_integral_element_type); |
| 3020 | return QualType(); |
| 3021 | } |
| 3022 | |
| 3023 | // C++14 [inteseq.make]p1: |
| 3024 | // If N is negative the program is ill-formed. |
| 3025 | TemplateArgument NumArgsArg = Converted[2]; |
| 3026 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 3027 | if (NumArgs < 0) { |
| 3028 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 3029 | diag::err_integer_sequence_negative_length); |
| 3030 | return QualType(); |
| 3031 | } |
| 3032 | |
| 3033 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 3034 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 3035 | // The type argument gets reused as the first template argument in the |
| 3036 | // synthetic template argument list. |
| 3037 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 3038 | // Expand N into 0 ... N-1. |
| 3039 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 3040 | I < NumArgs; ++I) { |
| 3041 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 3042 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 3043 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3044 | } |
| 3045 | // The first template argument will be reused as the template decl that |
| 3046 | // our synthetic template arguments will be applied to. |
| 3047 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 3048 | TemplateLoc, SyntheticTemplateArgs); |
| 3049 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 3050 | |
| 3051 | case BTK__type_pack_element: |
| 3052 | // Specializations of |
| 3053 | // __type_pack_element<Index, T_1, ..., T_N> |
| 3054 | // are treated like T_Index. |
| 3055 | assert(Converted.size() == 2 && |
| 3056 | "__type_pack_element should be given an index and a parameter pack"); |
| 3057 | |
| 3058 | // If the Index is out of bounds, the program is ill-formed. |
| 3059 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 3060 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 3061 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 3062 | "type std::size_t, and hence be non-negative"); |
| 3063 | if (Index >= Ts.pack_size()) { |
| 3064 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 3065 | diag::err_type_pack_element_out_of_bounds); |
| 3066 | return QualType(); |
| 3067 | } |
| 3068 | |
| 3069 | // We simply return the type at index `Index`. |
| 3070 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 3071 | return Nth->getAsType(); |
| 3072 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3073 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 3074 | } |
| 3075 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3076 | /// Determine whether this alias template is "enable_if_t". |
| 3077 | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { |
| 3078 | return AliasTemplate->getName().equals("enable_if_t"); |
| 3079 | } |
| 3080 | |
| 3081 | /// Collect all of the separable terms in the given condition, which |
| 3082 | /// might be a conjunction. |
| 3083 | /// |
| 3084 | /// FIXME: The right answer is to convert the logical expression into |
| 3085 | /// disjunctive normal form, so we can find the first failed term |
| 3086 | /// within each possible clause. |
| 3087 | static void collectConjunctionTerms(Expr *Clause, |
| 3088 | SmallVectorImpl<Expr *> &Terms) { |
| 3089 | if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { |
| 3090 | if (BinOp->getOpcode() == BO_LAnd) { |
| 3091 | collectConjunctionTerms(BinOp->getLHS(), Terms); |
| 3092 | collectConjunctionTerms(BinOp->getRHS(), Terms); |
| 3093 | } |
| 3094 | |
| 3095 | return; |
| 3096 | } |
| 3097 | |
| 3098 | Terms.push_back(Clause); |
| 3099 | } |
| 3100 | |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3101 | // The ranges-v3 library uses an odd pattern of a top-level "||" with |
| 3102 | // a left-hand side that is value-dependent but never true. Identify |
| 3103 | // the idiom and ignore that term. |
| 3104 | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { |
| 3105 | // Top-level '||'. |
| 3106 | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); |
| 3107 | if (!BinOp) return Cond; |
| 3108 | |
| 3109 | if (BinOp->getOpcode() != BO_LOr) return Cond; |
| 3110 | |
| 3111 | // With an inner '==' that has a literal on the right-hand side. |
| 3112 | Expr *LHS = BinOp->getLHS(); |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3113 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3114 | if (!InnerBinOp) return Cond; |
| 3115 | |
| 3116 | if (InnerBinOp->getOpcode() != BO_EQ || |
| 3117 | !isa<IntegerLiteral>(InnerBinOp->getRHS())) |
| 3118 | return Cond; |
| 3119 | |
| 3120 | // If the inner binary operation came from a macro expansion named |
| 3121 | // CONCEPT_REQUIRES or CONCEPT_REQUIRES_, return the right-hand side |
| 3122 | // of the '||', which is the real, user-provided condition. |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3123 | SourceLocation Loc = InnerBinOp->getExprLoc(); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3124 | if (!Loc.isMacroID()) return Cond; |
| 3125 | |
| 3126 | StringRef MacroName = PP.getImmediateMacroName(Loc); |
| 3127 | if (MacroName == "CONCEPT_REQUIRES" || MacroName == "CONCEPT_REQUIRES_") |
| 3128 | return BinOp->getRHS(); |
| 3129 | |
| 3130 | return Cond; |
| 3131 | } |
| 3132 | |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3133 | namespace { |
| 3134 | |
| 3135 | // A PrinterHelper that prints more helpful diagnostics for some sub-expressions |
| 3136 | // within failing boolean expression, such as substituting template parameters |
| 3137 | // for actual types. |
| 3138 | class FailedBooleanConditionPrinterHelper : public PrinterHelper { |
| 3139 | public: |
| 3140 | explicit FailedBooleanConditionPrinterHelper(const PrintingPolicy &P) |
| 3141 | : Policy(P) {} |
| 3142 | |
| 3143 | bool handledStmt(Stmt *E, raw_ostream &OS) override { |
| 3144 | const auto *DR = dyn_cast<DeclRefExpr>(E); |
| 3145 | if (DR && DR->getQualifier()) { |
| 3146 | // If this is a qualified name, expand the template arguments in nested |
| 3147 | // qualifiers. |
| 3148 | DR->getQualifier()->print(OS, Policy, true); |
| 3149 | // Then print the decl itself. |
| 3150 | const ValueDecl *VD = DR->getDecl(); |
| 3151 | OS << VD->getName(); |
| 3152 | if (const auto *IV = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
| 3153 | // This is a template variable, print the expanded template arguments. |
| 3154 | printTemplateArgumentList(OS, IV->getTemplateArgs().asArray(), Policy); |
| 3155 | } |
| 3156 | return true; |
Clement Courbet | 9d432e0 | 2018-12-04 07:59:57 +0000 | [diff] [blame] | 3157 | } |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3158 | return false; |
Clement Courbet | 9d432e0 | 2018-12-04 07:59:57 +0000 | [diff] [blame] | 3159 | } |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3160 | |
| 3161 | private: |
| 3162 | const PrintingPolicy Policy; |
| 3163 | }; |
| 3164 | |
| 3165 | } // end anonymous namespace |
Clement Courbet | 9d432e0 | 2018-12-04 07:59:57 +0000 | [diff] [blame] | 3166 | |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3167 | std::pair<Expr *, std::string> |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3168 | Sema::findFailedBooleanCondition(Expr *Cond) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3169 | Cond = lookThroughRangesV3Condition(PP, Cond); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3170 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3171 | // Separate out all of the terms in a conjunction. |
| 3172 | SmallVector<Expr *, 4> Terms; |
| 3173 | collectConjunctionTerms(Cond, Terms); |
| 3174 | |
| 3175 | // Determine which term failed. |
| 3176 | Expr *FailedCond = nullptr; |
| 3177 | for (Expr *Term : Terms) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3178 | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); |
| 3179 | |
Clement Courbet | d872041 | 2018-12-10 08:53:17 +0000 | [diff] [blame] | 3180 | // Literals are uninteresting. |
| 3181 | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || |
| 3182 | isa<IntegerLiteral>(TermAsWritten)) |
| 3183 | continue; |
| 3184 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3185 | // The initialization of the parameter from the argument is |
| 3186 | // a constant-evaluated context. |
| 3187 | EnterExpressionEvaluationContext ConstantEvaluated( |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3188 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3189 | |
| 3190 | bool Succeeded; |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3191 | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3192 | !Succeeded) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3193 | FailedCond = TermAsWritten; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3194 | break; |
| 3195 | } |
| 3196 | } |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3197 | if (!FailedCond) |
Clement Courbet | d872041 | 2018-12-10 08:53:17 +0000 | [diff] [blame] | 3198 | FailedCond = Cond->IgnoreParenImpCasts(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3199 | |
| 3200 | std::string Description; |
| 3201 | { |
| 3202 | llvm::raw_string_ostream Out(Description); |
Clement Courbet | fb2c74d | 2018-12-20 09:05:15 +0000 | [diff] [blame] | 3203 | PrintingPolicy Policy = getPrintingPolicy(); |
| 3204 | Policy.PrintCanonicalTypes = true; |
| 3205 | FailedBooleanConditionPrinterHelper Helper(Policy); |
| 3206 | FailedCond->printPretty(Out, &Helper, Policy, 0, "\n", nullptr); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3207 | } |
| 3208 | return { FailedCond, Description }; |
| 3209 | } |
| 3210 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3211 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 3212 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3213 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3214 | DependentTemplateName *DTN |
| 3215 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3216 | if (DTN && DTN->isIdentifier()) |
| 3217 | // When building a template-id where the template-name is dependent, |
| 3218 | // assume the template is a type template. Either our assumption is |
| 3219 | // correct, or the code is ill-formed and will be diagnosed when the |
| 3220 | // dependent name is substituted. |
| 3221 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 3222 | DTN->getQualifier(), |
| 3223 | DTN->getIdentifier(), |
| 3224 | TemplateArgs); |
| 3225 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3226 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 3227 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 3228 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3229 | // We might have a substituted template template parameter pack. If so, |
| 3230 | // build a template specialization type for it. |
| 3231 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 3232 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3233 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3234 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 3235 | << Name; |
| 3236 | NoteAllFoundTemplates(Name); |
| 3237 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3238 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3239 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3240 | // Check that the template argument list is well-formed for this |
| 3241 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3242 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3243 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3244 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3245 | return QualType(); |
| 3246 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3247 | QualType CanonType; |
| 3248 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3249 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3250 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 3251 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3252 | // Find the canonical type for this type alias template specialization. |
| 3253 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 3254 | if (Pattern->isInvalidDecl()) |
| 3255 | return QualType(); |
| 3256 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3257 | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, |
| 3258 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3259 | |
| 3260 | // Only substitute for the innermost template argument list. |
| 3261 | MultiLevelTemplateArgumentList TemplateArgLists; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3262 | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 3263 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 3264 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 3265 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3266 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3267 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3268 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3269 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3270 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3271 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3272 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 3273 | TemplateArgLists, AliasTemplate->getLocation(), |
| 3274 | AliasTemplate->getDeclName()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3275 | if (CanonType.isNull()) { |
| 3276 | // If this was enable_if and we failed to find the nested type |
| 3277 | // within enable_if in a SFINAE context, dig out the specific |
| 3278 | // enable_if condition that failed and present that instead. |
| 3279 | if (isEnableIfAliasTemplate(AliasTemplate)) { |
| 3280 | if (auto DeductionInfo = isSFINAEContext()) { |
| 3281 | if (*DeductionInfo && |
| 3282 | (*DeductionInfo)->hasSFINAEDiagnostic() && |
| 3283 | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == |
| 3284 | diag::err_typename_nested_not_found_enable_if && |
| 3285 | TemplateArgs[0].getArgument().getKind() |
| 3286 | == TemplateArgument::Expression) { |
| 3287 | Expr *FailedCond; |
| 3288 | std::string FailedDescription; |
| 3289 | std::tie(FailedCond, FailedDescription) = |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3290 | findFailedBooleanCondition(TemplateArgs[0].getSourceExpression()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3291 | |
| 3292 | // Remove the old SFINAE diagnostic. |
| 3293 | PartialDiagnosticAt OldDiag = |
| 3294 | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; |
| 3295 | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); |
| 3296 | |
| 3297 | // Add a new SFINAE diagnostic specifying which condition |
| 3298 | // failed. |
| 3299 | (*DeductionInfo)->addSFINAEDiagnostic( |
| 3300 | OldDiag.first, |
| 3301 | PDiag(diag::err_typename_nested_not_found_requirement) |
| 3302 | << FailedDescription |
| 3303 | << FailedCond->getSourceRange()); |
| 3304 | } |
| 3305 | } |
| 3306 | } |
| 3307 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3308 | return QualType(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3309 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3310 | } else if (Name.isDependent() || |
| 3311 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3312 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3313 | // This class template specialization is a dependent |
| 3314 | // type. Therefore, its canonical type is another class template |
| 3315 | // specialization type that contains all of the converted |
| 3316 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 3317 | // A<T, T> have identical types when A is declared as: |
| 3318 | // |
| 3319 | // template<typename T, typename U = T> struct A; |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 3320 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3321 | |
| 3322 | // This might work out to be a current instantiation, in which |
| 3323 | // case the canonical type needs to be the InjectedClassNameType. |
| 3324 | // |
| 3325 | // TODO: in theory this could be a simple hashtable lookup; most |
| 3326 | // changes to CurContext don't change the set of current |
| 3327 | // instantiations. |
| 3328 | if (isa<ClassTemplateDecl>(Template)) { |
| 3329 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 3330 | // If we get out to a namespace, we're done. |
| 3331 | if (Ctx->isFileContext()) break; |
| 3332 | |
| 3333 | // If this isn't a record, keep looking. |
| 3334 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 3335 | if (!Record) continue; |
| 3336 | |
| 3337 | // Look for one of the two cases with InjectedClassNameTypes |
| 3338 | // and check whether it's the same template. |
| 3339 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 3340 | !Record->getDescribedClassTemplate()) |
| 3341 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3342 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3343 | // Fetch the injected class name type and check whether its |
| 3344 | // injected type is equal to the type we just built. |
| 3345 | QualType ICNT = Context.getTypeDeclType(Record); |
| 3346 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 3347 | ->getInjectedSpecializationType(); |
| 3348 | |
| 3349 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 3350 | continue; |
| 3351 | |
| 3352 | // If so, the canonical type of this TST is the injected |
| 3353 | // class name type of the record we just found. |
| 3354 | assert(ICNT.isCanonical()); |
| 3355 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3356 | break; |
| 3357 | } |
| 3358 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3359 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3360 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3361 | // Find the class template specialization declaration that |
| 3362 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3363 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3364 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3365 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3366 | if (!Decl) { |
| 3367 | // This is the first time we have referenced this class template |
| 3368 | // specialization. Create the canonical declaration and add it to |
| 3369 | // the set of specializations. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3370 | Decl = ClassTemplateSpecializationDecl::Create( |
| 3371 | Context, ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 3372 | ClassTemplate->getDeclContext(), |
| 3373 | ClassTemplate->getTemplatedDecl()->getBeginLoc(), |
| 3374 | ClassTemplate->getLocation(), ClassTemplate, Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 3375 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 3376 | if (ClassTemplate->isOutOfLine()) |
| 3377 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 3380 | if (Decl->getSpecializationKind() == TSK_Undeclared) { |
| 3381 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3382 | TemplateArgLists.addOuterTemplateArguments(Converted); |
| 3383 | InstantiateAttrsForDecl(TemplateArgLists, ClassTemplate->getTemplatedDecl(), |
| 3384 | Decl); |
| 3385 | } |
| 3386 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 3387 | // Diagnose uses of this specialization. |
| 3388 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 3389 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3390 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3391 | assert(isa<RecordType>(CanonType) && |
| 3392 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3393 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 3394 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 3395 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3396 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3398 | // Build the fully-sugared type for this class template |
| 3399 | // specialization, which refers back to the class template |
| 3400 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 3401 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 3404 | void Sema::ActOnUndeclaredTypeTemplateName(Scope *S, TemplateTy &ParsedName, |
| 3405 | TemplateNameKind &TNK, |
| 3406 | SourceLocation NameLoc, |
| 3407 | IdentifierInfo *&II) { |
| 3408 | assert(TNK == TNK_Undeclared_template && "not an undeclared template name"); |
| 3409 | |
| 3410 | TemplateName Name = ParsedName.get(); |
| 3411 | auto *ATN = Name.getAsAssumedTemplateName(); |
| 3412 | assert(ATN && "not an assumed template name"); |
| 3413 | II = ATN->getDeclName().getAsIdentifierInfo(); |
| 3414 | |
| 3415 | if (!resolveAssumedTemplateNameAsType(S, Name, NameLoc, /*Diagnose*/false)) { |
| 3416 | // Resolved to a type template name. |
| 3417 | ParsedName = TemplateTy::make(Name); |
| 3418 | TNK = TNK_Type_template; |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | bool Sema::resolveAssumedTemplateNameAsType(Scope *S, TemplateName &Name, |
| 3423 | SourceLocation NameLoc, |
| 3424 | bool Diagnose) { |
| 3425 | // We assumed this undeclared identifier to be an (ADL-only) function |
| 3426 | // template name, but it was used in a context where a type was required. |
| 3427 | // Try to typo-correct it now. |
| 3428 | AssumedTemplateStorage *ATN = Name.getAsAssumedTemplateName(); |
| 3429 | assert(ATN && "not an assumed template name"); |
| 3430 | |
| 3431 | LookupResult R(*this, ATN->getDeclName(), NameLoc, LookupOrdinaryName); |
| 3432 | struct CandidateCallback : CorrectionCandidateCallback { |
| 3433 | bool ValidateCandidate(const TypoCorrection &TC) override { |
| 3434 | return TC.getCorrectionDecl() && |
| 3435 | getAsTypeTemplateDecl(TC.getCorrectionDecl()); |
| 3436 | } |
| 3437 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 3438 | return llvm::make_unique<CandidateCallback>(*this); |
| 3439 | } |
| 3440 | } FilterCCC; |
| 3441 | |
| 3442 | TypoCorrection Corrected = |
| 3443 | CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, nullptr, |
| 3444 | FilterCCC, CTK_ErrorRecovery); |
| 3445 | if (Corrected && Corrected.getFoundDecl()) { |
| 3446 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) |
| 3447 | << ATN->getDeclName()); |
| 3448 | Name = TemplateName(Corrected.getCorrectionDeclAs<TemplateDecl>()); |
| 3449 | return false; |
| 3450 | } |
| 3451 | |
| 3452 | if (Diagnose) |
| 3453 | Diag(R.getNameLoc(), diag::err_no_template) << R.getLookupName(); |
| 3454 | return true; |
| 3455 | } |
| 3456 | |
| 3457 | TypeResult Sema::ActOnTemplateIdType( |
| 3458 | Scope *S, CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
| 3459 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
| 3460 | SourceLocation TemplateIILoc, SourceLocation LAngleLoc, |
| 3461 | ASTTemplateArgsPtr TemplateArgsIn, SourceLocation RAngleLoc, |
| 3462 | bool IsCtorOrDtorName, bool IsClassName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3463 | if (SS.isInvalid()) |
| 3464 | return true; |
| 3465 | |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3466 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { |
| 3467 | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); |
| 3468 | |
| 3469 | // C++ [temp.res]p3: |
| 3470 | // A qualified-id that refers to a type and in which the |
| 3471 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 3472 | // shall be prefixed by the keyword typename to indicate that the |
| 3473 | // qualified-id denotes a type, forming an |
| 3474 | // elaborated-type-specifier (7.1.5.3). |
| 3475 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { |
Richard Smith | 3411fbf | 2017-02-01 21:41:18 +0000 | [diff] [blame] | 3476 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3477 | << SS.getScopeRep() << TemplateII->getName(); |
| 3478 | // Recover as if 'typename' were specified. |
| 3479 | // FIXME: This is not quite correct recovery as we don't transform SS |
| 3480 | // into the corresponding dependent form (and we don't diagnose missing |
| 3481 | // 'template' keywords within SS as a result). |
| 3482 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
| 3483 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
| 3484 | TemplateArgsIn, RAngleLoc); |
| 3485 | } |
| 3486 | |
| 3487 | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
| 3488 | // it's not actually allowed to be used as a type in most cases. Because |
| 3489 | // we annotate it before we know whether it's valid, we have to check for |
| 3490 | // this case here. |
| 3491 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3492 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 3493 | Diag(TemplateIILoc, |
| 3494 | TemplateKWLoc.isInvalid() |
| 3495 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
| 3496 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 3497 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 3498 | << 1 /*if any keyword was present, it was 'template'*/; |
| 3499 | } |
| 3500 | } |
| 3501 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3502 | TemplateName Template = TemplateD.get(); |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 3503 | if (Template.getAsAssumedTemplateName() && |
| 3504 | resolveAssumedTemplateNameAsType(S, Template, TemplateIILoc)) |
| 3505 | return true; |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3507 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3508 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3509 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3510 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3511 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3512 | QualType T |
| 3513 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 3514 | DTN->getQualifier(), |
| 3515 | DTN->getIdentifier(), |
| 3516 | TemplateArgs); |
| 3517 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3518 | TypeLocBuilder TLB; |
| 3519 | DependentTemplateSpecializationTypeLoc SpecTL |
| 3520 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3521 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 3522 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3523 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3524 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3525 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3526 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3527 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3528 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3529 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3530 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3531 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3532 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3533 | if (Result.isNull()) |
| 3534 | return true; |
| 3535 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3536 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3537 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3538 | TemplateSpecializationTypeLoc SpecTL |
| 3539 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3540 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3541 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3542 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3543 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3544 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3545 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3546 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3547 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 3548 | // constructor or destructor name (in such a case, the scope specifier |
| 3549 | // will be attached to the enclosing Decl or Expr node). |
| 3550 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3551 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 3552 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 3553 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3554 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3555 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3556 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3557 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3558 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3559 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3560 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3561 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3562 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3563 | SourceLocation TagLoc, |
| 3564 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3565 | SourceLocation TemplateKWLoc, |
| 3566 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3567 | SourceLocation TemplateLoc, |
| 3568 | SourceLocation LAngleLoc, |
| 3569 | ASTTemplateArgsPtr TemplateArgsIn, |
| 3570 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3571 | TemplateName Template = TemplateD.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3572 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3573 | // Translate the parser's template argument list in our AST format. |
| 3574 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 3575 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3576 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3577 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3578 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3579 | ElaboratedTypeKeyword Keyword |
| 3580 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3581 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3582 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 3583 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3584 | DTN->getQualifier(), |
| 3585 | DTN->getIdentifier(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3586 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3587 | |
| 3588 | // Build type-source information. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3589 | TypeLocBuilder TLB; |
| 3590 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3591 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 3592 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 3593 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3594 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3595 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3596 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3597 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3598 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3599 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3600 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3601 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3602 | |
| 3603 | if (TypeAliasTemplateDecl *TAT = |
| 3604 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 3605 | // C++0x [dcl.type.elab]p2: |
| 3606 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 3607 | // resolves to an alias template specialization, the |
| 3608 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 3609 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 3610 | << TAT << NTK_TypeAliasTemplate << TagKind; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3611 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 3612 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3614 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 3615 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 3616 | return TypeResult(true); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3617 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3618 | // Check the tag kind |
| 3619 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3620 | RecordDecl *D = RT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3621 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3622 | IdentifierInfo *Id = D->getIdentifier(); |
| 3623 | assert(Id && "templated class must have an identifier"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3624 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 3625 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 3626 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3627 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3628 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3629 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3630 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3631 | } |
| 3632 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3633 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3634 | // Provide source-location information for the template specialization. |
| 3635 | TypeLocBuilder TLB; |
| 3636 | TemplateSpecializationTypeLoc SpecTL |
| 3637 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3638 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3639 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 3640 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3641 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3642 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3643 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3644 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3645 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3646 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3647 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 3648 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3649 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3650 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3651 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3654 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 3655 | NamedDecl *PrevDecl, |
| 3656 | SourceLocation Loc, |
| 3657 | bool IsPartialSpecialization); |
| 3658 | |
| 3659 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3660 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3661 | static bool isTemplateArgumentTemplateParameter( |
| 3662 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 3663 | switch (Arg.getKind()) { |
| 3664 | case TemplateArgument::Null: |
| 3665 | case TemplateArgument::NullPtr: |
| 3666 | case TemplateArgument::Integral: |
| 3667 | case TemplateArgument::Declaration: |
| 3668 | case TemplateArgument::Pack: |
| 3669 | case TemplateArgument::TemplateExpansion: |
| 3670 | return false; |
| 3671 | |
| 3672 | case TemplateArgument::Type: { |
| 3673 | QualType Type = Arg.getAsType(); |
| 3674 | const TemplateTypeParmType *TPT = |
| 3675 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 3676 | return TPT && !Type.hasQualifiers() && |
| 3677 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 3678 | } |
| 3679 | |
| 3680 | case TemplateArgument::Expression: { |
| 3681 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 3682 | if (!DRE || !DRE->getDecl()) |
| 3683 | return false; |
| 3684 | const NonTypeTemplateParmDecl *NTTP = |
| 3685 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 3686 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 3687 | } |
| 3688 | |
| 3689 | case TemplateArgument::Template: |
| 3690 | const TemplateTemplateParmDecl *TTP = |
| 3691 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 3692 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 3693 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 3694 | } |
| 3695 | llvm_unreachable("unexpected kind of template argument"); |
| 3696 | } |
| 3697 | |
| 3698 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 3699 | ArrayRef<TemplateArgument> Args) { |
| 3700 | if (Params->size() != Args.size()) |
| 3701 | return false; |
| 3702 | |
| 3703 | unsigned Depth = Params->getDepth(); |
| 3704 | |
| 3705 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3706 | TemplateArgument Arg = Args[I]; |
| 3707 | |
| 3708 | // If the parameter is a pack expansion, the argument must be a pack |
| 3709 | // whose only element is a pack expansion. |
| 3710 | if (Params->getParam(I)->isParameterPack()) { |
| 3711 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 3712 | !Arg.pack_begin()->isPackExpansion()) |
| 3713 | return false; |
| 3714 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 3715 | } |
| 3716 | |
| 3717 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 3718 | return false; |
| 3719 | } |
| 3720 | |
| 3721 | return true; |
| 3722 | } |
| 3723 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3724 | /// Convert the parser's template argument list representation into our form. |
| 3725 | static TemplateArgumentListInfo |
| 3726 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 3727 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 3728 | TemplateId.RAngleLoc); |
| 3729 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 3730 | TemplateId.NumArgs); |
| 3731 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 3732 | return TemplateArgs; |
| 3733 | } |
| 3734 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3735 | template<typename PartialSpecDecl> |
| 3736 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 3737 | if (Partial->getDeclContext()->isDependentContext()) |
| 3738 | return; |
| 3739 | |
| 3740 | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
| 3741 | // for non-substitution-failure issues? |
| 3742 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 3743 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 3744 | return; |
| 3745 | |
| 3746 | auto *Template = Partial->getSpecializedTemplate(); |
| 3747 | S.Diag(Partial->getLocation(), |
Richard Smith | fa4a09d | 2016-12-27 20:03:09 +0000 | [diff] [blame] | 3748 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 3749 | << isa<VarTemplateDecl>(Template); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3750 | |
| 3751 | if (Info.hasSFINAEDiagnostic()) { |
| 3752 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 3753 | PartialDiagnostic::NullDiagnostic()}; |
| 3754 | Info.takeSFINAEDiagnostic(Diag); |
| 3755 | SmallString<128> SFINAEArgString; |
| 3756 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 3757 | S.Diag(Diag.first, |
| 3758 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 3759 | << SFINAEArgString; |
| 3760 | } |
| 3761 | |
| 3762 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 3763 | } |
| 3764 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3765 | static void |
| 3766 | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
| 3767 | const llvm::SmallBitVector &DeducibleParams) { |
| 3768 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3769 | if (!DeducibleParams[I]) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 3770 | NamedDecl *Param = TemplateParams->getParam(I); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3771 | if (Param->getDeclName()) |
| 3772 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3773 | << Param->getDeclName(); |
| 3774 | else |
| 3775 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3776 | << "(anonymous)"; |
| 3777 | } |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3782 | template<typename PartialSpecDecl> |
| 3783 | static void checkTemplatePartialSpecialization(Sema &S, |
| 3784 | PartialSpecDecl *Partial) { |
| 3785 | // C++1z [temp.class.spec]p8: (DR1495) |
| 3786 | // - The specialization shall be more specialized than the primary |
| 3787 | // template (14.5.5.2). |
| 3788 | checkMoreSpecializedThanPrimary(S, Partial); |
| 3789 | |
| 3790 | // C++ [temp.class.spec]p8: (DR1315) |
| 3791 | // - Each template-parameter shall appear at least once in the |
| 3792 | // template-id outside a non-deduced context. |
| 3793 | // C++1z [temp.class.spec.match]p3 (P0127R2) |
| 3794 | // If the template arguments of a partial specialization cannot be |
| 3795 | // deduced because of the structure of its template-parameter-list |
| 3796 | // and the template-id, the program is ill-formed. |
| 3797 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 3798 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3799 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 3800 | TemplateParams->getDepth(), DeducibleParams); |
| 3801 | |
| 3802 | if (!DeducibleParams.all()) { |
| 3803 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3804 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 3805 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 3806 | << (NumNonDeducible > 1) |
| 3807 | << SourceRange(Partial->getLocation(), |
| 3808 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3809 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } |
| 3812 | |
| 3813 | void Sema::CheckTemplatePartialSpecialization( |
| 3814 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 3815 | checkTemplatePartialSpecialization(*this, Partial); |
| 3816 | } |
| 3817 | |
| 3818 | void Sema::CheckTemplatePartialSpecialization( |
| 3819 | VarTemplatePartialSpecializationDecl *Partial) { |
| 3820 | checkTemplatePartialSpecialization(*this, Partial); |
| 3821 | } |
| 3822 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3823 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
| 3824 | // C++1z [temp.param]p11: |
| 3825 | // A template parameter of a deduction guide template that does not have a |
| 3826 | // default-argument shall be deducible from the parameter-type-list of the |
| 3827 | // deduction guide template. |
| 3828 | auto *TemplateParams = TD->getTemplateParameters(); |
| 3829 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3830 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
| 3831 | for (unsigned I = 0; I != TemplateParams->size(); ++I) { |
| 3832 | // A parameter pack is deducible (to an empty pack). |
| 3833 | auto *Param = TemplateParams->getParam(I); |
| 3834 | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)) |
| 3835 | DeducibleParams[I] = true; |
| 3836 | } |
| 3837 | |
| 3838 | if (!DeducibleParams.all()) { |
| 3839 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3840 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
| 3841 | << (NumNonDeducible > 1); |
| 3842 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
| 3843 | } |
| 3844 | } |
| 3845 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3846 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3847 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 3848 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3849 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3850 | // D must be variable template id. |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 3851 | assert(D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3852 | "Variable template specialization is declared with a template it."); |
| 3853 | |
| 3854 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3855 | TemplateArgumentListInfo TemplateArgs = |
| 3856 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3857 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 3858 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 3859 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3860 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3861 | TemplateName Name = TemplateId->Template.get(); |
| 3862 | |
| 3863 | // The template-id must name a variable template. |
| 3864 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3865 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 3866 | if (!VarTemplate) { |
| 3867 | NamedDecl *FnTemplate; |
| 3868 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 3869 | FnTemplate = *OTS->begin(); |
| 3870 | else |
| 3871 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 3872 | if (FnTemplate) |
| 3873 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 3874 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3875 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 3876 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3877 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3878 | |
| 3879 | // Check for unexpanded parameter packs in any of the template arguments. |
| 3880 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 3881 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 3882 | UPPC_PartialSpecialization)) |
| 3883 | return true; |
| 3884 | |
| 3885 | // Check that the template argument list is well-formed for this |
| 3886 | // template. |
| 3887 | SmallVector<TemplateArgument, 4> Converted; |
| 3888 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 3889 | false, Converted)) |
| 3890 | return true; |
| 3891 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3892 | // Find the variable template (partial) specialization declaration that |
| 3893 | // corresponds to these arguments. |
| 3894 | if (IsPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3895 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 3896 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3897 | return true; |
| 3898 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3899 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
| 3900 | // also do them during instantiation. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3901 | bool InstantiationDependent; |
| 3902 | if (!Name.isDependent() && |
| 3903 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3904 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3905 | InstantiationDependent)) { |
| 3906 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3907 | << VarTemplate->getDeclName(); |
| 3908 | IsPartialSpecialization = false; |
| 3909 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3910 | |
| 3911 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 3912 | Converted)) { |
| 3913 | // C++ [temp.class.spec]p9b3: |
| 3914 | // |
| 3915 | // -- The argument list of the specialization shall not be identical |
| 3916 | // to the implicit argument list of the primary template. |
| 3917 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 3918 | << /*variable template*/ 1 |
| 3919 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 3920 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 3921 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 3922 | // of the primary template. |
| 3923 | return true; |
| 3924 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3927 | void *InsertPos = nullptr; |
| 3928 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3929 | |
| 3930 | if (IsPartialSpecialization) |
| 3931 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3932 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3933 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3934 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3935 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3936 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3937 | |
| 3938 | // Check whether we can declare a variable template specialization in |
| 3939 | // the current scope. |
| 3940 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 3941 | TemplateNameLoc, |
| 3942 | IsPartialSpecialization)) |
| 3943 | return true; |
| 3944 | |
| 3945 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 3946 | // Since the only prior variable template specialization with these |
| 3947 | // arguments was referenced but not declared, reuse that |
| 3948 | // declaration node as our own, updating its source location and |
| 3949 | // the list of outer template parameters to reflect our new declaration. |
| 3950 | Specialization = PrevDecl; |
| 3951 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3952 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3953 | } else if (IsPartialSpecialization) { |
| 3954 | // Create a new class template partial specialization declaration node. |
| 3955 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 3956 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3957 | VarTemplatePartialSpecializationDecl *Partial = |
| 3958 | VarTemplatePartialSpecializationDecl::Create( |
| 3959 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 3960 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3961 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3962 | |
| 3963 | if (!PrevPartial) |
| 3964 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 3965 | Specialization = Partial; |
| 3966 | |
| 3967 | // If we are providing an explicit specialization of a member variable |
| 3968 | // template specialization, make a note of that. |
| 3969 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3970 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3971 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3972 | CheckTemplatePartialSpecialization(Partial); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3973 | } else { |
| 3974 | // Create a new class template specialization declaration node for |
| 3975 | // this explicit specialization or friend declaration. |
| 3976 | Specialization = VarTemplateSpecializationDecl::Create( |
| 3977 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3978 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3979 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 3980 | |
| 3981 | if (!PrevDecl) |
| 3982 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 3983 | } |
| 3984 | |
| 3985 | // C++ [temp.expl.spec]p6: |
| 3986 | // If a template, a member template or the member of a class template is |
| 3987 | // explicitly specialized then that specialization shall be declared |
| 3988 | // before the first use of that specialization that would cause an implicit |
| 3989 | // instantiation to take place, in every translation unit in which such a |
| 3990 | // use occurs; no diagnostic is required. |
| 3991 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3992 | bool Okay = false; |
| 3993 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 3994 | // Is there any previous explicit specialization declaration? |
| 3995 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3996 | Okay = true; |
| 3997 | break; |
| 3998 | } |
| 3999 | } |
| 4000 | |
| 4001 | if (!Okay) { |
| 4002 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4003 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4004 | << Name << Range; |
| 4005 | |
| 4006 | Diag(PrevDecl->getPointOfInstantiation(), |
| 4007 | diag::note_instantiation_required_here) |
| 4008 | << (PrevDecl->getTemplateSpecializationKind() != |
| 4009 | TSK_ImplicitInstantiation); |
| 4010 | return true; |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 4015 | Specialization->setLexicalDeclContext(CurContext); |
| 4016 | |
| 4017 | // Add the specialization into its lexical context, so that it can |
| 4018 | // be seen when iterating through the list of declarations in that |
| 4019 | // context. However, specializations are not found by name lookup. |
| 4020 | CurContext->addDecl(Specialization); |
| 4021 | |
| 4022 | // Note that this is an explicit specialization. |
| 4023 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 4024 | |
| 4025 | if (PrevDecl) { |
| 4026 | // Check that this isn't a redefinition of this specialization, |
| 4027 | // merging with previous declarations. |
| 4028 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4029 | forRedeclarationInCurContext()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4030 | PrevSpec.addDecl(PrevDecl); |
| 4031 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 4032 | } else if (Specialization->isStaticDataMember() && |
| 4033 | Specialization->isOutOfLine()) { |
| 4034 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4037 | return Specialization; |
| 4038 | } |
| 4039 | |
| 4040 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4041 | /// A partial specialization whose template arguments have matched |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4042 | /// a given template-id. |
| 4043 | struct PartialSpecMatchResult { |
| 4044 | VarTemplatePartialSpecializationDecl *Partial; |
| 4045 | TemplateArgumentList *Args; |
| 4046 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4047 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4048 | |
| 4049 | DeclResult |
| 4050 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 4051 | SourceLocation TemplateNameLoc, |
| 4052 | const TemplateArgumentListInfo &TemplateArgs) { |
| 4053 | assert(Template && "A variable template id without template?"); |
| 4054 | |
| 4055 | // Check that the template argument list is well-formed for this template. |
| 4056 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4057 | if (CheckTemplateArgumentList( |
| 4058 | Template, TemplateNameLoc, |
| 4059 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4060 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4061 | return true; |
| 4062 | |
| 4063 | // Find the variable template specialization declaration that |
| 4064 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4065 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4066 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4067 | Converted, InsertPos)) { |
| 4068 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4069 | // If we already have a variable template specialization, return it. |
| 4070 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4071 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4072 | |
| 4073 | // This is the first time we have referenced this variable template |
| 4074 | // specialization. Create the canonical declaration and add it to |
| 4075 | // the set of specializations, based on the closest partial specialization |
| 4076 | // that it represents. That is, |
| 4077 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 4078 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4079 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4080 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 4081 | bool AmbiguousPartialSpec = false; |
| 4082 | typedef PartialSpecMatchResult MatchResult; |
| 4083 | SmallVector<MatchResult, 4> Matched; |
| 4084 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 4085 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 4086 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4087 | |
| 4088 | // 1. Attempt to find the closest partial specialization that this |
| 4089 | // specializes, if any. |
| 4090 | // If any of the template arguments is dependent, then this is probably |
| 4091 | // a placeholder for an incomplete declarative context; which must be |
| 4092 | // complete by instantiation time. Thus, do not search through the partial |
| 4093 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 4094 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 4095 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 4096 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4097 | bool InstantiationDependent = false; |
| 4098 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 4099 | TemplateArgs, InstantiationDependent)) { |
| 4100 | |
| 4101 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 4102 | Template->getPartialSpecializations(PartialSpecs); |
| 4103 | |
| 4104 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 4105 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 4106 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 4107 | |
| 4108 | if (TemplateDeductionResult Result = |
| 4109 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 4110 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 4111 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4112 | FailedCandidates.addCandidate().set( |
| 4113 | DeclAccessPair::make(Template, AS_public), Partial, |
| 4114 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4115 | (void)Result; |
| 4116 | } else { |
| 4117 | Matched.push_back(PartialSpecMatchResult()); |
| 4118 | Matched.back().Partial = Partial; |
| 4119 | Matched.back().Args = Info.take(); |
| 4120 | } |
| 4121 | } |
| 4122 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4123 | if (Matched.size() >= 1) { |
| 4124 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 4125 | if (Matched.size() == 1) { |
| 4126 | // -- If exactly one matching specialization is found, the |
| 4127 | // instantiation is generated from that specialization. |
| 4128 | // We don't need to do anything for this. |
| 4129 | } else { |
| 4130 | // -- If more than one matching specialization is found, the |
| 4131 | // partial order rules (14.5.4.2) are used to determine |
| 4132 | // whether one of the specializations is more specialized |
| 4133 | // than the others. If none of the specializations is more |
| 4134 | // specialized than all of the other matching |
| 4135 | // specializations, then the use of the variable template is |
| 4136 | // ambiguous and the program is ill-formed. |
| 4137 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 4138 | PEnd = Matched.end(); |
| 4139 | P != PEnd; ++P) { |
| 4140 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 4141 | PointOfInstantiation) == |
| 4142 | P->Partial) |
| 4143 | Best = P; |
| 4144 | } |
| 4145 | |
| 4146 | // Determine if the best partial specialization is more specialized than |
| 4147 | // the others. |
| 4148 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 4149 | PEnd = Matched.end(); |
| 4150 | P != PEnd; ++P) { |
| 4151 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 4152 | P->Partial, Best->Partial, |
| 4153 | PointOfInstantiation) != Best->Partial) { |
| 4154 | AmbiguousPartialSpec = true; |
| 4155 | break; |
| 4156 | } |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | // Instantiate using the best variable template partial specialization. |
| 4161 | InstantiationPattern = Best->Partial; |
| 4162 | InstantiationArgs = Best->Args; |
| 4163 | } else { |
| 4164 | // -- If no match is found, the instantiation is generated |
| 4165 | // from the primary template. |
| 4166 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 4167 | } |
| 4168 | } |
| 4169 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4170 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4171 | // Note that we do not instantiate a definition until we see an odr-use |
| 4172 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4173 | // FIXME: LateAttrs et al.? |
| 4174 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 4175 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 4176 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 4177 | if (!Decl) |
| 4178 | return true; |
| 4179 | |
| 4180 | if (AmbiguousPartialSpec) { |
| 4181 | // Partial ordering did not produce a clear winner. Complain. |
| 4182 | Decl->setInvalidDecl(); |
| 4183 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 4184 | << Decl; |
| 4185 | |
| 4186 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 4187 | for (MatchResult P : Matched) |
| 4188 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 4189 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 4190 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4191 | return true; |
| 4192 | } |
| 4193 | |
| 4194 | if (VarTemplatePartialSpecializationDecl *D = |
| 4195 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 4196 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 4197 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4198 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 4199 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4200 | assert(Decl && "No variable template specialization?"); |
| 4201 | return Decl; |
| 4202 | } |
| 4203 | |
| 4204 | ExprResult |
| 4205 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 4206 | const DeclarationNameInfo &NameInfo, |
| 4207 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 4208 | const TemplateArgumentListInfo *TemplateArgs) { |
| 4209 | |
| 4210 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 4211 | *TemplateArgs); |
| 4212 | if (Decl.isInvalid()) |
| 4213 | return ExprError(); |
| 4214 | |
| 4215 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 4216 | if (!Var->getTemplateSpecializationKind()) |
| 4217 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 4218 | NameInfo.getLoc()); |
| 4219 | |
| 4220 | // Build an ordinary singleton decl ref. |
| 4221 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4222 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Richard Smith | ecad88d | 2018-04-26 01:08:00 +0000 | [diff] [blame] | 4225 | void Sema::diagnoseMissingTemplateArguments(TemplateName Name, |
| 4226 | SourceLocation Loc) { |
| 4227 | Diag(Loc, diag::err_template_missing_args) |
| 4228 | << (int)getTemplateNameKindForDiagnostics(Name) << Name; |
| 4229 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) { |
| 4230 | Diag(TD->getLocation(), diag::note_template_decl_here) |
| 4231 | << TD->getTemplateParameters()->getSourceRange(); |
| 4232 | } |
| 4233 | } |
| 4234 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4235 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4236 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4237 | LookupResult &R, |
| 4238 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4239 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4240 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 4241 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | // 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] | 4243 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4244 | // foo<int> could identify a single function unambiguously |
| 4245 | // This approach does NOT work, since f<int>(1); |
| 4246 | // gets resolved prior to resorting to overload resolution |
| 4247 | // i.e., template<class T> void f(double); |
| 4248 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4249 | |
| 4250 | // These should be filtered out by our callers. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4251 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 4252 | |
Richard Smith | 0410094 | 2018-04-26 02:10:22 +0000 | [diff] [blame] | 4253 | // Non-function templates require a template argument list. |
| 4254 | if (auto *TD = R.getAsSingle<TemplateDecl>()) { |
| 4255 | if (!TemplateArgs && !isa<FunctionTemplateDecl>(TD)) { |
| 4256 | diagnoseMissingTemplateArguments(TemplateName(TD), R.getNameLoc()); |
| 4257 | return ExprError(); |
| 4258 | } |
| 4259 | } |
| 4260 | |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4261 | auto AnyDependentArguments = [&]() -> bool { |
| 4262 | bool InstantiationDependent; |
| 4263 | return TemplateArgs && |
| 4264 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 4265 | *TemplateArgs, InstantiationDependent); |
| 4266 | }; |
| 4267 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4268 | // In C++1y, check variable template ids. |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4269 | if (R.getAsSingle<VarTemplateDecl>() && !AnyDependentArguments()) { |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 4270 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 4271 | R.getAsSingle<VarTemplateDecl>(), |
| 4272 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4275 | // We don't want lookup warnings at this point. |
| 4276 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4277 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4278 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4279 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4280 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4281 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4282 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4283 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 4284 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4285 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4286 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4289 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4290 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4291 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4292 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4293 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4294 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4295 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4296 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4297 | DeclContext *DC; |
| 4298 | if (!(DC = computeDeclContext(SS, false)) || |
| 4299 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4300 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 4301 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4303 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4304 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4305 | if (LookupTemplateName(R, (Scope *)nullptr, SS, QualType(), |
| 4306 | /*Entering*/false, MemberOfUnknownSpecialization, |
| 4307 | TemplateKWLoc)) |
| 4308 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4309 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4310 | if (R.isAmbiguous()) |
| 4311 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4312 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4313 | if (R.empty()) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4314 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 4315 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4316 | return ExprError(); |
| 4317 | } |
| 4318 | |
| 4319 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4320 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4321 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 4322 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4323 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 4324 | return ExprError(); |
| 4325 | } |
| 4326 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4327 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4330 | /// Form a dependent template name. |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4331 | /// |
| 4332 | /// This action forms a dependent template name given the template |
| 4333 | /// name and its (presumably dependent) scope specifier. For |
| 4334 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 4335 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 4336 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4337 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4338 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4339 | SourceLocation TemplateKWLoc, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 4340 | const UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4341 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4342 | bool EnteringContext, |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4343 | TemplateTy &Result, |
| 4344 | bool AllowInjectedClassName) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4345 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 4346 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4347 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4348 | diag::warn_cxx98_compat_template_outside_of_template : |
| 4349 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4350 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 4351 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4352 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4353 | if (SS.isSet()) |
| 4354 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 4355 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4356 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4357 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4358 | // C++0x [temp.names]p5: |
| 4359 | // If a name prefixed by the keyword template is not the name of |
| 4360 | // a template, the program is ill-formed. [Note: the keyword |
| 4361 | // template may not be applied to non-template members of class |
| 4362 | // templates. -end note ] [ Note: as is the case with the |
| 4363 | // typename prefix, the template prefix is allowed in cases |
| 4364 | // where it is not strictly necessary; i.e., when the |
| 4365 | // nested-name-specifier or the expression on the left of the -> |
| 4366 | // or . is not dependent on a template-parameter, or the use |
| 4367 | // does not appear in the scope of a template. -end note] |
| 4368 | // |
| 4369 | // Note: C++03 was more strict here, because it banned the use of |
| 4370 | // the "template" keyword prior to a template-name that was not a |
| 4371 | // dependent name. C++ DR468 relaxed this requirement (the |
| 4372 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 4373 | // 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] | 4374 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 4375 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 4376 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4377 | MemberOfUnknownSpecialization); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4378 | if (TNK == TNK_Non_template && MemberOfUnknownSpecialization) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4379 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4380 | } else if (TNK == TNK_Non_template) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4381 | // Do the lookup again to determine if this is a "nothing found" case or |
| 4382 | // a "not a template" case. FIXME: Refactor isTemplateName so we don't |
| 4383 | // need to do this. |
| 4384 | DeclarationNameInfo DNI = GetNameFromUnqualifiedId(Name); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4385 | LookupResult R(*this, DNI.getName(), Name.getBeginLoc(), |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4386 | LookupOrdinaryName); |
| 4387 | bool MOUS; |
| 4388 | if (!LookupTemplateName(R, S, SS, ObjectType.get(), EnteringContext, |
Richard Smith | afcfb6b | 2019-02-15 21:53:07 +0000 | [diff] [blame] | 4389 | MOUS, TemplateKWLoc) && !R.isAmbiguous()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4390 | Diag(Name.getBeginLoc(), diag::err_no_member) |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4391 | << DNI.getName() << LookupCtx << SS.getRange(); |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4392 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4393 | } else { |
| 4394 | // We found something; return it. |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4395 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
| 4396 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4397 | Name.getKind() == UnqualifiedIdKind::IK_Identifier && |
| 4398 | Name.Identifier && LookupRD->getIdentifier() == Name.Identifier) { |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4399 | // C++14 [class.qual]p2: |
| 4400 | // In a lookup in which function names are not ignored and the |
| 4401 | // nested-name-specifier nominates a class C, if the name specified |
| 4402 | // [...] is the injected-class-name of C, [...] the name is instead |
| 4403 | // considered to name the constructor |
| 4404 | // |
| 4405 | // We don't get here if naming the constructor would be valid, so we |
| 4406 | // just reject immediately and recover by treating the |
| 4407 | // injected-class-name as naming the template. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4408 | Diag(Name.getBeginLoc(), |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4409 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4410 | << Name.Identifier |
| 4411 | << 0 /*injected-class-name used as template name*/ |
| 4412 | << 1 /*'template' keyword was used*/; |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4413 | } |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4414 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4415 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4418 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4419 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4420 | switch (Name.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4421 | case UnqualifiedIdKind::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4422 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4423 | Name.Identifier)); |
| 4424 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4425 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4426 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4427 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4428 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 4429 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4430 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4431 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 4432 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4433 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4434 | default: |
| 4435 | break; |
| 4436 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4437 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4438 | Diag(Name.getBeginLoc(), diag::err_template_kw_refers_to_non_template) |
| 4439 | << GetNameFromUnqualifiedId(Name).getName() << Name.getSourceRange() |
| 4440 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4441 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4444 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4445 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4446 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4447 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4448 | QualType ArgType; |
| 4449 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4450 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4451 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4452 | switch(Arg.getKind()) { |
| 4453 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4454 | // C++ [temp.arg.type]p1: |
| 4455 | // A template-argument for a template-parameter which is a |
| 4456 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4457 | ArgType = Arg.getAsType(); |
| 4458 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4459 | break; |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 4460 | case TemplateArgument::Template: |
| 4461 | case TemplateArgument::TemplateExpansion: { |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4462 | // We have a template type parameter but the template argument |
| 4463 | // is a template without any arguments. |
| 4464 | SourceRange SR = AL.getSourceRange(); |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 4465 | TemplateName Name = Arg.getAsTemplateOrTemplatePattern(); |
Richard Smith | ecad88d | 2018-04-26 01:08:00 +0000 | [diff] [blame] | 4466 | diagnoseMissingTemplateArguments(Name, SR.getEnd()); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4467 | return true; |
| 4468 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4469 | case TemplateArgument::Expression: { |
| 4470 | // We have a template type parameter but the template argument is an |
| 4471 | // expression; see if maybe it is missing the "typename" keyword. |
| 4472 | CXXScopeSpec SS; |
| 4473 | DeclarationNameInfo NameInfo; |
| 4474 | |
| 4475 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 4476 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4477 | NameInfo = ArgExpr->getNameInfo(); |
| 4478 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 4479 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 4480 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4481 | NameInfo = ArgExpr->getNameInfo(); |
| 4482 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 4483 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 4484 | if (ArgExpr->isImplicitAccess()) { |
| 4485 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4486 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 4487 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4488 | } |
| 4489 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4490 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4491 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 4492 | LookupParsedName(Result, CurScope, &SS); |
| 4493 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 4494 | if (Result.getAsSingle<TypeDecl>() || |
| 4495 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4496 | LookupResult::NotFoundInCurrentInstantiation) { |
| 4497 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4498 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4499 | Diag(Loc, getLangOpts().MSVCCompat |
| 4500 | ? diag::ext_ms_template_type_arg_missing_typename |
| 4501 | : diag::err_template_arg_must_be_type_suggest) |
| 4502 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4503 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4504 | |
| 4505 | // Recover by synthesizing a type using the location information that we |
| 4506 | // already have. |
| 4507 | ArgType = |
| 4508 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 4509 | TypeLocBuilder TLB; |
| 4510 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 4511 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 4512 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 4513 | TL.setNameLoc(NameInfo.getLoc()); |
| 4514 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 4515 | |
| 4516 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 4517 | // properly. |
| 4518 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 4519 | TemplateArgumentLocInfo(TSI)); |
| 4520 | |
| 4521 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4522 | } |
| 4523 | } |
| 4524 | // fallthrough |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 4525 | LLVM_FALLTHROUGH; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4526 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4527 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4528 | // We have a template type parameter but the template argument |
| 4529 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 4530 | SourceRange SR = AL.getSourceRange(); |
| 4531 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4532 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4534 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4535 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4536 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4537 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4538 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4539 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4540 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4541 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4542 | ArgType = Context.getCanonicalType(ArgType); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4543 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4544 | // Objective-C ARC: |
| 4545 | // If an explicitly-specified template argument type is a lifetime type |
| 4546 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4547 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4548 | ArgType->isObjCLifetimeType() && |
| 4549 | !ArgType.getObjCLifetime()) { |
| 4550 | Qualifiers Qs; |
| 4551 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 4552 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 4553 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4554 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4555 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4556 | return false; |
| 4557 | } |
| 4558 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4559 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4560 | /// the given template type parameter. |
| 4561 | /// |
| 4562 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4563 | /// the substitution. |
| 4564 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4565 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4566 | /// for. |
| 4567 | /// |
| 4568 | /// \param TemplateLoc the location of the template name that started the |
| 4569 | /// template-id we are checking. |
| 4570 | /// |
| 4571 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4572 | /// terminates the template-id. |
| 4573 | /// |
| 4574 | /// \param Param the template template parameter whose default we are |
| 4575 | /// substituting into. |
| 4576 | /// |
| 4577 | /// \param Converted the list of template arguments provided for template |
| 4578 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4579 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4580 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4581 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4582 | TemplateDecl *Template, |
| 4583 | SourceLocation TemplateLoc, |
| 4584 | SourceLocation RAngleLoc, |
| 4585 | TemplateTypeParmDecl *Param, |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 4586 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4587 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4588 | |
| 4589 | // If the argument type is dependent, instantiate it now based |
| 4590 | // on the previously-computed template arguments. |
Erik Pilkington | ba88e21 | 2018-11-12 21:31:06 +0000 | [diff] [blame] | 4591 | if (ArgType->getType()->isInstantiationDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4592 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4593 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4594 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4595 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4596 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4597 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4598 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4599 | |
| 4600 | // Only substitute for the innermost template argument list. |
| 4601 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4602 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4603 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4604 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4605 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4606 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4607 | ArgType = |
| 4608 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 4609 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
| 4612 | return ArgType; |
| 4613 | } |
| 4614 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4615 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4616 | /// the given non-type template parameter. |
| 4617 | /// |
| 4618 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4619 | /// the substitution. |
| 4620 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4621 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4622 | /// for. |
| 4623 | /// |
| 4624 | /// \param TemplateLoc the location of the template name that started the |
| 4625 | /// template-id we are checking. |
| 4626 | /// |
| 4627 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4628 | /// terminates the template-id. |
| 4629 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4630 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4631 | /// substituting into. |
| 4632 | /// |
| 4633 | /// \param Converted the list of template arguments provided for template |
| 4634 | /// parameters that precede \p Param in the template parameter list. |
| 4635 | /// |
| 4636 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4637 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4638 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4639 | TemplateDecl *Template, |
| 4640 | SourceLocation TemplateLoc, |
| 4641 | SourceLocation RAngleLoc, |
| 4642 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4643 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4644 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4645 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4646 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4647 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4648 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4649 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4650 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4651 | |
| 4652 | // Only substitute for the innermost template argument list. |
| 4653 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4654 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4655 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4656 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4657 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4658 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 4659 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4660 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4663 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4664 | /// the given template template parameter. |
| 4665 | /// |
| 4666 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4667 | /// the substitution. |
| 4668 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4669 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4670 | /// for. |
| 4671 | /// |
| 4672 | /// \param TemplateLoc the location of the template name that started the |
| 4673 | /// template-id we are checking. |
| 4674 | /// |
| 4675 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4676 | /// terminates the template-id. |
| 4677 | /// |
| 4678 | /// \param Param the template template parameter whose default we are |
| 4679 | /// substituting into. |
| 4680 | /// |
| 4681 | /// \param Converted the list of template arguments provided for template |
| 4682 | /// parameters that precede \p Param in the template parameter list. |
| 4683 | /// |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4684 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4685 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4686 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4687 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 4688 | static TemplateName |
| 4689 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4690 | TemplateDecl *Template, |
| 4691 | SourceLocation TemplateLoc, |
| 4692 | SourceLocation RAngleLoc, |
| 4693 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4694 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4695 | NestedNameSpecifierLoc &QualifierLoc) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4696 | Sema::InstantiatingTemplate Inst( |
| 4697 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 4698 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4699 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4700 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4701 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4702 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4703 | |
| 4704 | // Only substitute for the innermost template argument list. |
| 4705 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4706 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4707 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4708 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4709 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4710 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4711 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4712 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4713 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4714 | QualifierLoc = |
| 4715 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4716 | if (!QualifierLoc) |
| 4717 | return TemplateName(); |
| 4718 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4719 | |
| 4720 | return SemaRef.SubstTemplateName( |
| 4721 | QualifierLoc, |
| 4722 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 4723 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 4724 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4725 | } |
| 4726 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4727 | /// If the given template parameter has a default template |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4728 | /// argument, substitute into that default template argument and |
| 4729 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4730 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4731 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 4732 | SourceLocation TemplateLoc, |
| 4733 | SourceLocation RAngleLoc, |
| 4734 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4735 | SmallVectorImpl<TemplateArgument> |
| 4736 | &Converted, |
| 4737 | bool &HasDefaultArg) { |
| 4738 | HasDefaultArg = false; |
| 4739 | |
| 4740 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4741 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4742 | return TemplateArgumentLoc(); |
| 4743 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4744 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4745 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4746 | TemplateLoc, |
| 4747 | RAngleLoc, |
| 4748 | TypeParm, |
| 4749 | Converted); |
| 4750 | if (DI) |
| 4751 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 4752 | |
| 4753 | return TemplateArgumentLoc(); |
| 4754 | } |
| 4755 | |
| 4756 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 4757 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4758 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4759 | return TemplateArgumentLoc(); |
| 4760 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4761 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4762 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4763 | TemplateLoc, |
| 4764 | RAngleLoc, |
| 4765 | NonTypeParm, |
| 4766 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4767 | if (Arg.isInvalid()) |
| 4768 | return TemplateArgumentLoc(); |
| 4769 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4770 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4771 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 4772 | } |
| 4773 | |
| 4774 | TemplateTemplateParmDecl *TempTempParm |
| 4775 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4776 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4777 | return TemplateArgumentLoc(); |
| 4778 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4779 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4780 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4781 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4782 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4783 | RAngleLoc, |
| 4784 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4785 | Converted, |
| 4786 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4787 | if (TName.isNull()) |
| 4788 | return TemplateArgumentLoc(); |
| 4789 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4790 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4791 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4792 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 4793 | } |
| 4794 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4795 | /// Convert a template-argument that we parsed as a type into a template, if |
| 4796 | /// possible. C++ permits injected-class-names to perform dual service as |
| 4797 | /// template template arguments and as template type arguments. |
| 4798 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { |
| 4799 | // Extract and step over any surrounding nested-name-specifier. |
| 4800 | NestedNameSpecifierLoc QualLoc; |
| 4801 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { |
| 4802 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
| 4803 | return TemplateArgumentLoc(); |
| 4804 | |
| 4805 | QualLoc = ETLoc.getQualifierLoc(); |
| 4806 | TLoc = ETLoc.getNamedTypeLoc(); |
| 4807 | } |
| 4808 | |
| 4809 | // If this type was written as an injected-class-name, it can be used as a |
| 4810 | // template template argument. |
| 4811 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) |
| 4812 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), |
| 4813 | QualLoc, InjLoc.getNameLoc()); |
| 4814 | |
| 4815 | // If this type was written as an injected-class-name, it may have been |
| 4816 | // converted to a RecordType during instantiation. If the RecordType is |
| 4817 | // *not* wrapped in a TemplateSpecializationType and denotes a class |
| 4818 | // template specialization, it must have come from an injected-class-name. |
| 4819 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) |
| 4820 | if (auto *CTSD = |
| 4821 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
| 4822 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), |
| 4823 | QualLoc, RecLoc.getNameLoc()); |
| 4824 | |
| 4825 | return TemplateArgumentLoc(); |
| 4826 | } |
| 4827 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4828 | /// Check that the given template argument corresponds to the given |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4829 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4830 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4831 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4832 | /// checked. |
| 4833 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4834 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4835 | /// |
| 4836 | /// \param Template The template in which the template argument resides. |
| 4837 | /// |
| 4838 | /// \param TemplateLoc The location of the template name for the template |
| 4839 | /// whose argument list we're matching. |
| 4840 | /// |
| 4841 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 4842 | /// the template argument list. |
| 4843 | /// |
| 4844 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 4845 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 4846 | /// |
| 4847 | /// \param Converted The checked, converted argument will be added to the |
| 4848 | /// end of this small vector. |
| 4849 | /// |
| 4850 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 4851 | /// explicitly written, deduced, etc. |
| 4852 | /// |
| 4853 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4854 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4855 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4856 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4857 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4858 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4859 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4860 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4861 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4862 | // Check template type parameters. |
| 4863 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4864 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4865 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4866 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4867 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4868 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4869 | // with the template arguments we've seen thus far. But if the |
| 4870 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4871 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4872 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 4873 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4874 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4875 | // FIXME: Do we need to substitute into parameters here if they're |
| 4876 | // instantiation-dependent but not dependent? |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4877 | if (NTTPType->isDependentType() && |
| 4878 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4879 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4880 | // Do substitution on the type of the non-type template parameter. |
| 4881 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4882 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4883 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4884 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4885 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4886 | |
| 4887 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4888 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4889 | NTTPType = SubstType(NTTPType, |
| 4890 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 4891 | NTTP->getLocation(), |
| 4892 | NTTP->getDeclName()); |
| 4893 | // If that worked, check the non-type template parameter type |
| 4894 | // for validity. |
| 4895 | if (!NTTPType.isNull()) |
| 4896 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 4897 | NTTP->getLocation()); |
| 4898 | if (NTTPType.isNull()) |
| 4899 | return true; |
| 4900 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4901 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4902 | switch (Arg.getArgument().getKind()) { |
| 4903 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4904 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4905 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4906 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4907 | TemplateArgument Result; |
Erich Keane | c90bb6d | 2018-05-07 17:05:20 +0000 | [diff] [blame] | 4908 | unsigned CurSFINAEErrors = NumSFINAEErrors; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4909 | ExprResult Res = |
| 4910 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 4911 | Result, CTAK); |
| 4912 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4913 | return true; |
Erich Keane | c90bb6d | 2018-05-07 17:05:20 +0000 | [diff] [blame] | 4914 | // If the current template argument causes an error, give up now. |
| 4915 | if (CurSFINAEErrors < NumSFINAEErrors) |
| 4916 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4917 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4918 | // If the resulting expression is new, then use it in place of the |
| 4919 | // old expression in the template argument. |
| 4920 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 4921 | TemplateArgument TA(Res.get()); |
| 4922 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 4923 | } |
| 4924 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4925 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4926 | break; |
| 4927 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4928 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4929 | case TemplateArgument::Declaration: |
| 4930 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4931 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4932 | // We've already checked this template argument, so just copy |
| 4933 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4934 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4935 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4936 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4937 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4938 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4939 | // We were given a template template argument. It may not be ill-formed; |
| 4940 | // see below. |
| 4941 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4942 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 4943 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4944 | // We have a template argument such as \c T::template X, which we |
| 4945 | // parsed as a template template argument. However, since we now |
| 4946 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4947 | // template name into an expression. |
| 4948 | |
| 4949 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 4950 | Arg.getTemplateNameLoc()); |
| 4951 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 4952 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4953 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4954 | // FIXME: the template-template arg was a DependentTemplateName, |
| 4955 | // so it was provided with a template keyword. However, its source |
| 4956 | // location is not stored in the template argument structure. |
| 4957 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4958 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 4959 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 4960 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4961 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4962 | // If we parsed the template argument as a pack expansion, create a |
| 4963 | // pack expansion expression. |
| 4964 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4965 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4966 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4967 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4968 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4969 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4970 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4971 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4972 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4973 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4974 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4975 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4976 | break; |
| 4977 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4978 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4979 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4980 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4981 | // therefore cannot be a non-type template argument. |
| 4982 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 4983 | << Arg.getSourceRange(); |
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 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4986 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4987 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4988 | case TemplateArgument::Type: { |
| 4989 | // We have a non-type template parameter but the template |
| 4990 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4991 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4992 | // C++ [temp.arg]p2: |
| 4993 | // In a template-argument, an ambiguity between a type-id and |
| 4994 | // an expression is resolved to a type-id, regardless of the |
| 4995 | // form of the corresponding template-parameter. |
| 4996 | // |
| 4997 | // We warn specifically about this case, since it can be rather |
| 4998 | // confusing for users. |
| 4999 | QualType T = Arg.getArgument().getAsType(); |
| 5000 | SourceRange SR = Arg.getSourceRange(); |
| 5001 | if (T->isFunctionType()) |
| 5002 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 5003 | else |
| 5004 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 5005 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5006 | return true; |
| 5007 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5008 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5009 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5010 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5011 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5013 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
| 5016 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5017 | // Check template template parameters. |
| 5018 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5019 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 5020 | TemplateParameterList *Params = TempParm->getTemplateParameters(); |
| 5021 | if (TempParm->isExpandedParameterPack()) |
| 5022 | Params = TempParm->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5023 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5024 | // Substitute into the template parameter list of the template |
| 5025 | // template parameter, since previously-supplied template arguments |
| 5026 | // may appear within the template template parameter. |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 5027 | // |
| 5028 | // FIXME: Skip this if the parameters aren't instantiation-dependent. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5029 | { |
| 5030 | // Set up a template instantiation context. |
| 5031 | LocalInstantiationScope Scope(*this); |
| 5032 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 5033 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5034 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 5035 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 5036 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5037 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 5038 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 5039 | Params = SubstTemplateParams(Params, CurContext, |
| 5040 | MultiLevelTemplateArgumentList(TemplateArgs)); |
| 5041 | if (!Params) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5042 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5043 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5044 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5045 | // C++1z [temp.local]p1: (DR1004) |
| 5046 | // When [the injected-class-name] is used [...] as a template-argument for |
| 5047 | // a template template-parameter [...] it refers to the class template |
| 5048 | // itself. |
| 5049 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { |
| 5050 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
| 5051 | Arg.getTypeSourceInfo()->getTypeLoc()); |
| 5052 | if (!ConvertedArg.getArgument().isNull()) |
| 5053 | Arg = ConvertedArg; |
| 5054 | } |
| 5055 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5056 | switch (Arg.getArgument().getKind()) { |
| 5057 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5058 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5059 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5060 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5061 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 5062 | if (CheckTemplateTemplateArgument(Params, Arg)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5063 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5064 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5065 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5066 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5067 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5068 | case TemplateArgument::Expression: |
| 5069 | case TemplateArgument::Type: |
| 5070 | // We have a template template parameter but the template |
| 5071 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5072 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5073 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5074 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5075 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5076 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5077 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5078 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5079 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5080 | case TemplateArgument::NullPtr: |
| 5081 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5083 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5084 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5085 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5086 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5087 | return false; |
| 5088 | } |
| 5089 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5090 | /// Check whether the template parameter is a pack expansion, and if so, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5091 | /// determine the number of parameters produced by that expansion. For instance: |
| 5092 | /// |
| 5093 | /// \code |
| 5094 | /// template<typename ...Ts> struct A { |
| 5095 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 5096 | /// }; |
| 5097 | /// \endcode |
| 5098 | /// |
| 5099 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 5100 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 5101 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5102 | if (NonTypeTemplateParmDecl *NTTP |
| 5103 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 5104 | if (NTTP->isExpandedParameterPack()) |
| 5105 | return NTTP->getNumExpansionTypes(); |
| 5106 | } |
| 5107 | |
| 5108 | if (TemplateTemplateParmDecl *TTP |
| 5109 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 5110 | if (TTP->isExpandedParameterPack()) |
| 5111 | return TTP->getNumExpansionTemplateParameters(); |
| 5112 | } |
| 5113 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 5114 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5117 | /// Diagnose a missing template argument. |
| 5118 | template<typename TemplateParmDecl> |
| 5119 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 5120 | TemplateDecl *TD, |
| 5121 | const TemplateParmDecl *D, |
| 5122 | TemplateArgumentListInfo &Args) { |
| 5123 | // Dig out the most recent declaration of the template parameter; there may be |
| 5124 | // declarations of the template that are more recent than TD. |
| 5125 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 5126 | ->getTemplateParameters() |
| 5127 | ->getParam(D->getIndex())); |
| 5128 | |
| 5129 | // If there's a default argument that's not visible, diagnose that we're |
| 5130 | // missing a module import. |
| 5131 | llvm::SmallVector<Module*, 8> Modules; |
| 5132 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 5133 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 5134 | D->getDefaultArgumentLoc(), Modules, |
| 5135 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 5136 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5137 | return true; |
| 5138 | } |
| 5139 | |
| 5140 | // FIXME: If there's a more recent default argument that *is* visible, |
| 5141 | // diagnose that it was declared too late. |
| 5142 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5143 | TemplateParameterList *Params = TD->getTemplateParameters(); |
| 5144 | |
| 5145 | S.Diag(Loc, diag::err_template_arg_list_different_arity) |
| 5146 | << /*not enough args*/0 |
| 5147 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(TD)) |
| 5148 | << TD; |
| 5149 | S.Diag(TD->getLocation(), diag::note_template_decl_here) |
| 5150 | << Params->getSourceRange(); |
| 5151 | return true; |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5154 | /// Check that the given template argument list is well-formed |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5155 | /// for specializing the given template. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5156 | bool Sema::CheckTemplateArgumentList( |
| 5157 | TemplateDecl *Template, SourceLocation TemplateLoc, |
| 5158 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
| 5159 | SmallVectorImpl<TemplateArgument> &Converted, |
| 5160 | bool UpdateArgsWithConversions) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5161 | // Make a copy of the template arguments for processing. Only make the |
| 5162 | // changes at the end when successful in matching the arguments to the |
| 5163 | // template. |
| 5164 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 5165 | |
Erich Keane | af0795b | 2017-10-24 01:39:56 +0000 | [diff] [blame] | 5166 | // Make sure we get the template parameter list from the most |
| 5167 | // recentdeclaration, since that is the only one that has is guaranteed to |
| 5168 | // have all the default template argument information. |
| 5169 | TemplateParameterList *Params = |
| 5170 | cast<TemplateDecl>(Template->getMostRecentDecl()) |
| 5171 | ->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5172 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5173 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5174 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5175 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5176 | // [...] The type and form of each template-argument specified in |
| 5177 | // a template-id shall match the type and form specified for the |
| 5178 | // corresponding parameter declared by the template in its |
| 5179 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5180 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5181 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5182 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 5183 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5184 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5185 | ParamEnd = Params->end(); |
| 5186 | Param != ParamEnd; /* increment in loop */) { |
| 5187 | // If we have an expanded parameter pack, make sure we don't have too |
| 5188 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 5189 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5190 | if (*Expansions == ArgumentPack.size()) { |
| 5191 | // We're done with this parameter pack. Pack up its arguments and add |
| 5192 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5193 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5194 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5195 | ArgumentPack.clear(); |
| 5196 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5197 | // This argument is assigned to the next parameter. |
| 5198 | ++Param; |
| 5199 | continue; |
| 5200 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 5201 | // Not enough arguments for this parameter pack. |
| 5202 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5203 | << /*not enough args*/0 |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 5204 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5205 | << Template; |
| 5206 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5207 | << Params->getSourceRange(); |
| 5208 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5209 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5210 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5211 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5212 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5213 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5214 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5215 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5216 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5217 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5218 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5219 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5220 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5221 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 5222 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5223 | // 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] | 5224 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5225 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5226 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5227 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5228 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5229 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 5230 | return true; |
| 5231 | } |
| 5232 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5233 | // We're now done with this argument. |
| 5234 | ++ArgIdx; |
| 5235 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5236 | if ((*Param)->isTemplateParameterPack()) { |
| 5237 | // The template parameter was a template parameter pack, so take the |
| 5238 | // deduced argument and place it on the argument pack. Note that we |
| 5239 | // stay on the same template parameter so that we can deduce more |
| 5240 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 5241 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5242 | } else { |
| 5243 | // Move to the next template parameter. |
| 5244 | ++Param; |
| 5245 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5246 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5247 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 5248 | // the remaining arguments, because we don't know what parameters they'll |
| 5249 | // match up with. |
| 5250 | if (PackExpansionIntoNonPack) { |
| 5251 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5252 | // If we were part way through filling in an expanded parameter pack, |
| 5253 | // fall back to just producing individual arguments. |
| 5254 | Converted.insert(Converted.end(), |
| 5255 | ArgumentPack.begin(), ArgumentPack.end()); |
| 5256 | ArgumentPack.clear(); |
| 5257 | } |
| 5258 | |
| 5259 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5260 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5261 | ++ArgIdx; |
| 5262 | } |
| 5263 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5264 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5265 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5266 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5267 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5268 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5269 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5270 | // If we're checking a partial template argument list, we're done. |
| 5271 | if (PartialTemplateArgs) { |
| 5272 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5273 | Converted.push_back( |
| 5274 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 5275 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5276 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5277 | } |
| 5278 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5279 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5280 | // 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] | 5281 | if ((*Param)->isTemplateParameterPack()) { |
| 5282 | assert(!getExpandedPackSize(*Param) && |
| 5283 | "Should have dealt with this already"); |
| 5284 | |
| 5285 | // A non-expanded parameter pack before the end of the parameter list |
| 5286 | // only occurs for an ill-formed template parameter list, unless we've |
| 5287 | // got a partial argument list for a function template, so just bail out. |
| 5288 | if (Param + 1 != ParamEnd) |
| 5289 | return true; |
| 5290 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5291 | Converted.push_back( |
| 5292 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5293 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5294 | |
| 5295 | ++Param; |
| 5296 | continue; |
| 5297 | } |
| 5298 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5299 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5300 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5301 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5302 | // Retrieve the default template argument from the template |
| 5303 | // parameter. For each kind of template parameter, we substitute the |
| 5304 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5305 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5306 | // the default argument. |
| 5307 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5308 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5309 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 5310 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5311 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5312 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5313 | Template, |
| 5314 | TemplateLoc, |
| 5315 | RAngleLoc, |
| 5316 | TTP, |
| 5317 | Converted); |
| 5318 | if (!ArgType) |
| 5319 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5320 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5321 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 5322 | ArgType); |
| 5323 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5324 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5325 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5326 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 5327 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5328 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5329 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5330 | TemplateLoc, |
| 5331 | RAngleLoc, |
| 5332 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5333 | Converted); |
| 5334 | if (E.isInvalid()) |
| 5335 | return true; |
| 5336 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5337 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5338 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 5339 | } else { |
| 5340 | TemplateTemplateParmDecl *TempParm |
| 5341 | = cast<TemplateTemplateParmDecl>(*Param); |
| 5342 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5343 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5344 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 5345 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5346 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 5347 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5348 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5349 | TemplateLoc, |
| 5350 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5351 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5352 | Converted, |
| 5353 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5354 | if (Name.isNull()) |
| 5355 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5356 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5357 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 5358 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5359 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5360 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5361 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 5362 | // the default template argument. We're not actually instantiating a |
| 5363 | // template here, we just create this object to put a note into the |
| 5364 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 5365 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 5366 | SourceRange(TemplateLoc, RAngleLoc)); |
| 5367 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 5368 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5369 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5370 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 5371 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5372 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5373 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5374 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5375 | // Core issue 150 (assumed resolution): if this is a template template |
| 5376 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5377 | // template definition. |
| 5378 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5379 | NewArgs.addArgument(Arg); |
| 5380 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5381 | // Move to the next template parameter and argument. |
| 5382 | ++Param; |
| 5383 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5384 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5385 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5386 | // If we're performing a partial argument substitution, allow any trailing |
| 5387 | // pack expansions; they might be empty. This can happen even if |
| 5388 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 5389 | // still dependent). |
| 5390 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 5391 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5392 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 5393 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5396 | // If we have any leftover arguments, then there were too many arguments. |
| 5397 | // Complain and fail. |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5398 | if (ArgIdx < NumArgs) { |
| 5399 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 5400 | << /*too many args*/1 |
| 5401 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
| 5402 | << Template |
| 5403 | << SourceRange(NewArgs[ArgIdx].getLocation(), NewArgs.getRAngleLoc()); |
| 5404 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5405 | << Params->getSourceRange(); |
| 5406 | return true; |
| 5407 | } |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5408 | |
| 5409 | // No problems found with the new argument list, propagate changes back |
| 5410 | // to caller. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5411 | if (UpdateArgsWithConversions) |
| 5412 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5413 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5414 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5417 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5418 | class UnnamedLocalNoLinkageFinder |
| 5419 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5420 | { |
| 5421 | Sema &S; |
| 5422 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5423 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5424 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5425 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5426 | public: |
| 5427 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 5428 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5429 | bool Visit(QualType T) { |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5430 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5431 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5432 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5433 | #define TYPE(Class, Parent) \ |
| 5434 | bool Visit##Class##Type(const Class##Type *); |
| 5435 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 5436 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5437 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 5438 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5439 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5441 | bool VisitTagDecl(const TagDecl *Tag); |
| 5442 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 5443 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 5444 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5445 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5446 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5447 | return false; |
| 5448 | } |
| 5449 | |
| 5450 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 5451 | return Visit(T->getElementType()); |
| 5452 | } |
| 5453 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5454 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5455 | return Visit(T->getPointeeType()); |
| 5456 | } |
| 5457 | |
| 5458 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5459 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5460 | return Visit(T->getPointeeType()); |
| 5461 | } |
| 5462 | |
| 5463 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5464 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5465 | return Visit(T->getPointeeType()); |
| 5466 | } |
| 5467 | |
| 5468 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5469 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5470 | return Visit(T->getPointeeType()); |
| 5471 | } |
| 5472 | |
| 5473 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5475 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 5476 | } |
| 5477 | |
| 5478 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5479 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5480 | return Visit(T->getElementType()); |
| 5481 | } |
| 5482 | |
| 5483 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5484 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5485 | return Visit(T->getElementType()); |
| 5486 | } |
| 5487 | |
| 5488 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5489 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5490 | return Visit(T->getElementType()); |
| 5491 | } |
| 5492 | |
| 5493 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5494 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5495 | return Visit(T->getElementType()); |
| 5496 | } |
| 5497 | |
| 5498 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5499 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5500 | return Visit(T->getElementType()); |
| 5501 | } |
| 5502 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 5503 | bool UnnamedLocalNoLinkageFinder::VisitDependentAddressSpaceType( |
| 5504 | const DependentAddressSpaceType *T) { |
| 5505 | return Visit(T->getPointeeType()); |
| 5506 | } |
| 5507 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5508 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 5509 | return Visit(T->getElementType()); |
| 5510 | } |
| 5511 | |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 5512 | bool UnnamedLocalNoLinkageFinder::VisitDependentVectorType( |
| 5513 | const DependentVectorType *T) { |
| 5514 | return Visit(T->getElementType()); |
| 5515 | } |
| 5516 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5517 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 5518 | return Visit(T->getElementType()); |
| 5519 | } |
| 5520 | |
| 5521 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 5522 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 5523 | for (const auto &A : T->param_types()) { |
| 5524 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5525 | return true; |
| 5526 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5527 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5528 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 5532 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5533 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 5537 | const UnresolvedUsingType*) { |
| 5538 | return false; |
| 5539 | } |
| 5540 | |
| 5541 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 5542 | return false; |
| 5543 | } |
| 5544 | |
| 5545 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 5546 | return Visit(T->getUnderlyingType()); |
| 5547 | } |
| 5548 | |
| 5549 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 5550 | return false; |
| 5551 | } |
| 5552 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5553 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 5554 | const UnaryTransformType*) { |
| 5555 | return false; |
| 5556 | } |
| 5557 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5558 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 5559 | return Visit(T->getDeducedType()); |
| 5560 | } |
| 5561 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5562 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
| 5563 | const DeducedTemplateSpecializationType *T) { |
| 5564 | return Visit(T->getDeducedType()); |
| 5565 | } |
| 5566 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5567 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 5568 | return VisitTagDecl(T->getDecl()); |
| 5569 | } |
| 5570 | |
| 5571 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 5572 | return VisitTagDecl(T->getDecl()); |
| 5573 | } |
| 5574 | |
| 5575 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 5576 | const TemplateTypeParmType*) { |
| 5577 | return false; |
| 5578 | } |
| 5579 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 5580 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 5581 | const SubstTemplateTypeParmPackType *) { |
| 5582 | return false; |
| 5583 | } |
| 5584 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5585 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 5586 | const TemplateSpecializationType*) { |
| 5587 | return false; |
| 5588 | } |
| 5589 | |
| 5590 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 5591 | const InjectedClassNameType* T) { |
| 5592 | return VisitTagDecl(T->getDecl()); |
| 5593 | } |
| 5594 | |
| 5595 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 5596 | const DependentNameType* T) { |
| 5597 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5598 | } |
| 5599 | |
| 5600 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 5601 | const DependentTemplateSpecializationType* T) { |
| 5602 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5603 | } |
| 5604 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5605 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 5606 | const PackExpansionType* T) { |
| 5607 | return Visit(T->getPattern()); |
| 5608 | } |
| 5609 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5610 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 5611 | return false; |
| 5612 | } |
| 5613 | |
| 5614 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 5615 | const ObjCInterfaceType *) { |
| 5616 | return false; |
| 5617 | } |
| 5618 | |
| 5619 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 5620 | const ObjCObjectPointerType *) { |
| 5621 | return false; |
| 5622 | } |
| 5623 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5624 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 5625 | return Visit(T->getValueType()); |
| 5626 | } |
| 5627 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5628 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 5629 | return false; |
| 5630 | } |
| 5631 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5632 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 5633 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5634 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5635 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5636 | diag::warn_cxx98_compat_template_arg_local_type : |
| 5637 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5638 | << S.Context.getTypeDeclType(Tag) << SR; |
| 5639 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 5642 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5643 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5644 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5645 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 5646 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5647 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 5648 | return true; |
| 5649 | } |
| 5650 | |
| 5651 | return false; |
| 5652 | } |
| 5653 | |
| 5654 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 5655 | NestedNameSpecifier *NNS) { |
| 5656 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 5657 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5658 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5659 | switch (NNS->getKind()) { |
| 5660 | case NestedNameSpecifier::Identifier: |
| 5661 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5662 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5663 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5664 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5665 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5666 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5667 | case NestedNameSpecifier::TypeSpec: |
| 5668 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 5669 | return Visit(QualType(NNS->getAsType(), 0)); |
| 5670 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5671 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5672 | } |
| 5673 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5674 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5675 | /// template type parameter. |
| 5676 | /// |
| 5677 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 5678 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5679 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5680 | TypeSourceInfo *ArgInfo) { |
| 5681 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5682 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 5683 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 5684 | |
| 5685 | if (Arg->isVariablyModifiedType()) { |
| 5686 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5687 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5688 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5689 | } |
| 5690 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5691 | // C++03 [temp.arg.type]p2: |
| 5692 | // A local type, a type with no linkage, an unnamed type or a type |
| 5693 | // compounded from any of these types shall not be used as a |
| 5694 | // template-argument for a template type-parameter. |
| 5695 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5696 | // 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] | 5697 | // a warning. |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5698 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5699 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 5700 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 5701 | } |
| 5702 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5703 | return false; |
| 5704 | } |
| 5705 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5706 | enum NullPointerValueKind { |
| 5707 | NPV_NotNullPointer, |
| 5708 | NPV_NullPointer, |
| 5709 | NPV_Error |
| 5710 | }; |
| 5711 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5712 | /// Determine whether the given template argument is a null pointer |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5713 | /// value of the appropriate type. |
| 5714 | static NullPointerValueKind |
| 5715 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5716 | QualType ParamType, Expr *Arg, |
| 5717 | Decl *Entity = nullptr) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5718 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 5719 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5720 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5721 | // dllimport'd entities aren't constant but are available inside of template |
| 5722 | // arguments. |
| 5723 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 5724 | return NPV_NotNullPointer; |
| 5725 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5726 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 5727 | llvm_unreachable( |
| 5728 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5729 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 5730 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5731 | return NPV_NotNullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5732 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5733 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5734 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 5735 | if (ArgRV.isInvalid()) |
| 5736 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5737 | Arg = ArgRV.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5738 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5739 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5740 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5741 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5742 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5743 | EvalResult.HasSideEffects) { |
| 5744 | SourceLocation DiagLoc = Arg->getExprLoc(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5745 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5746 | // If our only note is the usual "invalid subexpression" note, just point |
| 5747 | // the caret at its location rather than producing an essentially |
| 5748 | // redundant note. |
| 5749 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 5750 | diag::note_invalid_subexpr_in_const_expr) { |
| 5751 | DiagLoc = Notes[0].first; |
| 5752 | Notes.clear(); |
| 5753 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5754 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5755 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 5756 | << Arg->getType() << Arg->getSourceRange(); |
| 5757 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 5758 | S.Diag(Notes[I].first, Notes[I].second); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5759 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5760 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5761 | return NPV_Error; |
| 5762 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5763 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5764 | // C++11 [temp.arg.nontype]p1: |
| 5765 | // - an address constant expression of type std::nullptr_t |
| 5766 | if (Arg->getType()->isNullPtrType()) |
| 5767 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5768 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5769 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 5770 | // - a constant expression that evaluates to a null member pointer value |
| 5771 | // (4.11); or |
| 5772 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 5773 | (EvalResult.Val.isMemberPointer() && |
| 5774 | !EvalResult.Val.getMemberPointerDecl())) { |
| 5775 | // If our expression has an appropriate type, we've succeeded. |
| 5776 | bool ObjCLifetimeConversion; |
| 5777 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 5778 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 5779 | ObjCLifetimeConversion)) |
| 5780 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5781 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5782 | // The types didn't match, but we know we got a null pointer; complain, |
| 5783 | // then recover as if the types were correct. |
| 5784 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 5785 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 5786 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5787 | return NPV_NullPointer; |
| 5788 | } |
| 5789 | |
| 5790 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 5791 | // constant, suggest a cast to the appropriate type. |
| 5792 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 5793 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 5794 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5795 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), Code) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5796 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getEndLoc()), |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5797 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5798 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5799 | return NPV_NullPointer; |
| 5800 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5801 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5802 | // FIXME: If we ever want to support general, address-constant expressions |
| 5803 | // as non-type template arguments, we should return the ExprResult here to |
| 5804 | // be interpreted by the caller. |
| 5805 | return NPV_NotNullPointer; |
| 5806 | } |
| 5807 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5808 | /// Checks whether the given template argument is compatible with its |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5809 | /// template parameter. |
| 5810 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 5811 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 5812 | Expr *Arg, QualType ArgType) { |
| 5813 | bool ObjCLifetimeConversion; |
| 5814 | if (ParamType->isPointerType() && |
| 5815 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 5816 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 5817 | ObjCLifetimeConversion)) { |
| 5818 | // For pointer-to-object types, qualification conversions are |
| 5819 | // permitted. |
| 5820 | } else { |
| 5821 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 5822 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 5823 | // C++ [temp.arg.nontype]p5b3: |
| 5824 | // For a non-type template-parameter of type reference to |
| 5825 | // object, no conversions apply. The type referred to by the |
| 5826 | // reference may be more cv-qualified than the (otherwise |
| 5827 | // identical) type of the template- argument. The |
| 5828 | // template-parameter is bound directly to the |
| 5829 | // template-argument, which shall be an lvalue. |
| 5830 | |
| 5831 | // FIXME: Other qualifiers? |
| 5832 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 5833 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 5834 | |
| 5835 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5836 | S.Diag(Arg->getBeginLoc(), |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5837 | diag::err_template_arg_ref_bind_ignores_quals) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5838 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5839 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5840 | return true; |
| 5841 | } |
| 5842 | } |
| 5843 | } |
| 5844 | |
| 5845 | // At this point, the template argument refers to an object or |
| 5846 | // function with external linkage. We now need to check whether the |
| 5847 | // argument and parameter types are compatible. |
| 5848 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 5849 | ParamType.getNonReferenceType())) { |
| 5850 | // We can't perform this conversion or binding. |
| 5851 | if (ParamType->isReferenceType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5852 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_no_ref_bind) |
| 5853 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5854 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5855 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 5856 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5857 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5858 | return true; |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | return false; |
| 5863 | } |
| 5864 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5865 | /// Checks whether the given template argument is the address |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5866 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5867 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5868 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 5869 | NonTypeTemplateParmDecl *Param, |
| 5870 | QualType ParamType, |
| 5871 | Expr *ArgIn, |
| 5872 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5873 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5874 | Expr *Arg = ArgIn; |
| 5875 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5876 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5877 | bool AddressTaken = false; |
| 5878 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5879 | if (S.getLangOpts().MicrosoftExt) { |
| 5880 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 5881 | // dereference and address-of operators. |
| 5882 | Arg = Arg->IgnoreParenCasts(); |
| 5883 | |
| 5884 | bool ExtWarnMSTemplateArg = false; |
| 5885 | UnaryOperatorKind FirstOpKind; |
| 5886 | SourceLocation FirstOpLoc; |
| 5887 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5888 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 5889 | if (UnOpKind == UO_Deref) |
| 5890 | ExtWarnMSTemplateArg = true; |
| 5891 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 5892 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 5893 | if (!AddrOpLoc.isValid()) { |
| 5894 | FirstOpKind = UnOpKind; |
| 5895 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 5896 | } |
| 5897 | } else |
| 5898 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5899 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5900 | if (FirstOpLoc.isValid()) { |
| 5901 | if (ExtWarnMSTemplateArg) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5902 | S.Diag(ArgIn->getBeginLoc(), diag::ext_ms_deref_template_argument) |
| 5903 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5904 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5905 | if (FirstOpKind == UO_AddrOf) |
| 5906 | AddressTaken = true; |
| 5907 | else if (Arg->getType()->isPointerType()) { |
| 5908 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 5909 | // constant expression. |
| 5910 | assert(FirstOpKind == UO_Deref); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5911 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 5912 | << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5913 | } |
| 5914 | } |
| 5915 | } else { |
| 5916 | // See through any implicit casts we added to fix the type. |
| 5917 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5918 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5919 | // C++ [temp.arg.nontype]p1: |
| 5920 | // |
| 5921 | // A template-argument for a non-type, non-template |
| 5922 | // template-parameter shall be one of: [...] |
| 5923 | // |
| 5924 | // -- the address of an object or function with external |
| 5925 | // linkage, including function templates and function |
| 5926 | // template-ids but excluding non-static class members, |
| 5927 | // expressed as & id-expression where the & is optional if |
| 5928 | // the name refers to a function or array, or if the |
| 5929 | // corresponding template-parameter is a reference; or |
| 5930 | |
| 5931 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 5932 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 5933 | bool ExtraParens = false; |
| 5934 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 5935 | if (!Invalid && !ExtraParens) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5936 | S.Diag(Arg->getBeginLoc(), |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5937 | S.getLangOpts().CPlusPlus11 |
| 5938 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 5939 | : diag::ext_template_arg_extra_parens) |
| 5940 | << Arg->getSourceRange(); |
| 5941 | ExtraParens = true; |
| 5942 | } |
| 5943 | |
| 5944 | Arg = Parens->getSubExpr(); |
| 5945 | } |
| 5946 | |
| 5947 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5948 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5949 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5950 | |
| 5951 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5952 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 5953 | Arg = UnOp->getSubExpr(); |
| 5954 | AddressTaken = true; |
| 5955 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 5956 | } |
| 5957 | } |
| 5958 | |
| 5959 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5960 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5961 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5962 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5963 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5964 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 5965 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 5966 | |
| 5967 | // If our parameter has pointer type, check for a null template value. |
| 5968 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5969 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn, |
| 5970 | Entity)) { |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5971 | case NPV_NullPointer: |
| 5972 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5973 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 5974 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5975 | return false; |
| 5976 | |
| 5977 | case NPV_Error: |
| 5978 | return true; |
| 5979 | |
| 5980 | case NPV_NotNullPointer: |
| 5981 | break; |
| 5982 | } |
| 5983 | } |
| 5984 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5985 | // Stop checking the precise nature of the argument if it is value dependent, |
| 5986 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5987 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5988 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5989 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5990 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5991 | |
| 5992 | if (isa<CXXUuidofExpr>(Arg)) { |
| 5993 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 5994 | ArgIn, Arg, ArgType)) |
| 5995 | return true; |
| 5996 | |
| 5997 | Converted = TemplateArgument(ArgIn); |
| 5998 | return false; |
| 5999 | } |
| 6000 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6001 | if (!DRE) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6002 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 6003 | << Arg->getSourceRange(); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6004 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6005 | return true; |
| 6006 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 6007 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6008 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 6009 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6010 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_field) |
| 6011 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6012 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6013 | return true; |
| 6014 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6015 | |
| 6016 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6017 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6018 | if (!Method->isStatic()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6019 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_method) |
| 6020 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6021 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6022 | return true; |
| 6023 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6024 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6025 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6026 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 6027 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6028 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6029 | // A non-type template argument must refer to an object or function. |
| 6030 | if (!Func && !Var) { |
| 6031 | // We found something, but we don't know specifically what it is. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6032 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_object_or_func) |
| 6033 | << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6034 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 6035 | return true; |
| 6036 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6037 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6038 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 6039 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6040 | S.Diag(Arg->getBeginLoc(), |
| 6041 | S.getLangOpts().CPlusPlus11 |
| 6042 | ? diag::warn_cxx98_compat_template_arg_object_internal |
| 6043 | : diag::ext_template_arg_object_internal) |
| 6044 | << !Func << Entity << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6045 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 6046 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 6047 | } else if (!Entity->hasLinkage()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6048 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_object_no_linkage) |
| 6049 | << !Func << Entity << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6050 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 6051 | << !Func; |
| 6052 | return true; |
| 6053 | } |
| 6054 | |
| 6055 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6056 | // If the template parameter has pointer type, the function decays. |
| 6057 | if (ParamType->isPointerType() && !AddressTaken) |
| 6058 | ArgType = S.Context.getPointerType(Func->getType()); |
| 6059 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 6060 | // If we originally had an address-of operator, but the |
| 6061 | // parameter has reference type, complain and (if things look |
| 6062 | // like they will work) drop the address-of operator. |
| 6063 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 6064 | ParamType.getNonReferenceType())) { |
| 6065 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 6066 | << ParamType; |
| 6067 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6068 | return true; |
| 6069 | } |
| 6070 | |
| 6071 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 6072 | << ParamType |
| 6073 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 6074 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6075 | |
| 6076 | ArgType = Func->getType(); |
| 6077 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6078 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6079 | // A value of reference type is not an object. |
| 6080 | if (Var->getType()->isReferenceType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6081 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_reference_var) |
| 6082 | << Var->getType() << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6083 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6084 | return true; |
| 6085 | } |
| 6086 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6087 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 6088 | if (Var->getTLSKind()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6089 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_thread_local) |
| 6090 | << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 6091 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 6092 | return true; |
| 6093 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6094 | |
| 6095 | // If the template parameter has pointer type, we must have taken |
| 6096 | // the address of this object. |
| 6097 | if (ParamType->isReferenceType()) { |
| 6098 | if (AddressTaken) { |
| 6099 | // If we originally had an address-of operator, but the |
| 6100 | // parameter has reference type, complain and (if things look |
| 6101 | // like they will work) drop the address-of operator. |
| 6102 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 6103 | ParamType.getNonReferenceType())) { |
| 6104 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 6105 | << ParamType; |
| 6106 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6107 | return true; |
| 6108 | } |
| 6109 | |
| 6110 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 6111 | << ParamType |
| 6112 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 6113 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6114 | |
| 6115 | ArgType = Var->getType(); |
| 6116 | } |
| 6117 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 6118 | if (Var->getType()->isArrayType()) { |
| 6119 | // Array-to-pointer decay. |
| 6120 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 6121 | } else { |
| 6122 | // If the template parameter has pointer type but the address of |
| 6123 | // this object was not taken, complain and (possibly) recover by |
| 6124 | // taking the address of the entity. |
| 6125 | ArgType = S.Context.getPointerType(Var->getType()); |
| 6126 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6127 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 6128 | << ParamType; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6129 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6130 | return true; |
| 6131 | } |
| 6132 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6133 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 6134 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), "&"); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6135 | |
| 6136 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6137 | } |
| 6138 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6139 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6140 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 6141 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 6142 | Arg, ArgType)) |
| 6143 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6144 | |
| 6145 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 6146 | Converted = |
| 6147 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6148 | S.MarkAnyDeclReferenced(Arg->getBeginLoc(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6149 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6150 | } |
| 6151 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6152 | /// Checks whether the given template argument is a pointer to |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6153 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6154 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 6155 | NonTypeTemplateParmDecl *Param, |
| 6156 | QualType ParamType, |
| 6157 | Expr *&ResultArg, |
| 6158 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6159 | bool Invalid = false; |
| 6160 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6161 | Expr *Arg = ResultArg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6162 | bool ObjCLifetimeConversion; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6163 | |
| 6164 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6165 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6166 | // A template-argument for a non-type, non-template |
| 6167 | // template-parameter shall be one of: [...] |
| 6168 | // |
| 6169 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6170 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6171 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6172 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 6173 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 6174 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6175 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6176 | if (!Invalid && !ExtraParens) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6177 | S.Diag(Arg->getBeginLoc(), |
| 6178 | S.getLangOpts().CPlusPlus11 |
| 6179 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 6180 | : diag::ext_template_arg_extra_parens) |
| 6181 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6182 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6183 | } |
| 6184 | |
| 6185 | Arg = Parens->getSubExpr(); |
| 6186 | } |
| 6187 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6188 | while (SubstNonTypeTemplateParmExpr *subst = |
| 6189 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 6190 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 6191 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6192 | // A pointer-to-member constant written &Class::member. |
| 6193 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6194 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6195 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 6196 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6197 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6198 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6199 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6200 | // A constant of pointer-to-member type. |
| 6201 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6202 | ValueDecl *VD = DRE->getDecl(); |
| 6203 | if (VD->getType()->isMemberPointerType()) { |
| 6204 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
| 6205 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6206 | Converted = TemplateArgument(Arg); |
| 6207 | } else { |
| 6208 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
| 6209 | Converted = TemplateArgument(VD, ParamType); |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6210 | } |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6211 | return Invalid; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6212 | } |
| 6213 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6214 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6215 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6216 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6217 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 6218 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 6219 | |
| 6220 | // Check for a null pointer value. |
| 6221 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ResultArg, |
| 6222 | Entity)) { |
| 6223 | case NPV_Error: |
| 6224 | return true; |
| 6225 | case NPV_NullPointer: |
| 6226 | S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
| 6227 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 6228 | /*isNullPtr*/true); |
| 6229 | return false; |
| 6230 | case NPV_NotNullPointer: |
| 6231 | break; |
| 6232 | } |
| 6233 | |
| 6234 | if (S.IsQualificationConversion(ResultArg->getType(), |
| 6235 | ParamType.getNonReferenceType(), false, |
| 6236 | ObjCLifetimeConversion)) { |
| 6237 | ResultArg = S.ImpCastExprToType(ResultArg, ParamType, CK_NoOp, |
| 6238 | ResultArg->getValueKind()) |
| 6239 | .get(); |
| 6240 | } else if (!S.Context.hasSameUnqualifiedType( |
| 6241 | ResultArg->getType(), ParamType.getNonReferenceType())) { |
| 6242 | // We can't perform this conversion. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6243 | S.Diag(ResultArg->getBeginLoc(), diag::err_template_arg_not_convertible) |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 6244 | << ResultArg->getType() << ParamType << ResultArg->getSourceRange(); |
| 6245 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6246 | return true; |
| 6247 | } |
| 6248 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6249 | if (!DRE) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6250 | return S.Diag(Arg->getBeginLoc(), |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6251 | diag::err_template_arg_not_pointer_to_member_form) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6252 | << Arg->getSourceRange(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6253 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6254 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 6255 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 6256 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6257 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6258 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6259 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 6260 | "Only non-static member pointers can make it here"); |
| 6261 | |
| 6262 | // Okay: this is the address of a non-static member, and therefore |
| 6263 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6264 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 6265 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6266 | } else { |
| 6267 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 6268 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6269 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6270 | return Invalid; |
| 6271 | } |
| 6272 | |
| 6273 | // 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] | 6274 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_pointer_to_member_form) |
| 6275 | << Arg->getSourceRange(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6276 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6277 | return true; |
| 6278 | } |
| 6279 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6280 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6281 | /// non-type template parameter. |
| 6282 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 6283 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6284 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6285 | /// returns the converted template argument. \p ParamType is the |
| 6286 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6287 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6288 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6289 | TemplateArgument &Converted, |
| 6290 | CheckTemplateArgumentKind CTAK) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6291 | SourceLocation StartLoc = Arg->getBeginLoc(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6292 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6293 | // If the parameter type somehow involves auto, deduce the type now. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6294 | if (getLangOpts().CPlusPlus17 && ParamType->isUndeducedType()) { |
Richard Smith | 4ae5ec8 | 2017-02-22 20:01:55 +0000 | [diff] [blame] | 6295 | // During template argument deduction, we allow 'decltype(auto)' to |
| 6296 | // match an arbitrary dependent argument. |
| 6297 | // FIXME: The language rules don't say what happens in this case. |
| 6298 | // FIXME: We get an opaque dependent type out of decltype(auto) if the |
| 6299 | // expression is merely instantiation-dependent; is this enough? |
| 6300 | if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) { |
| 6301 | auto *AT = dyn_cast<AutoType>(ParamType); |
| 6302 | if (AT && AT->isDecltypeAuto()) { |
| 6303 | Converted = TemplateArgument(Arg); |
| 6304 | return Arg; |
| 6305 | } |
| 6306 | } |
| 6307 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6308 | // When checking a deduced template argument, deduce from its type even if |
| 6309 | // the type is dependent, in order to check the types of non-type template |
| 6310 | // arguments line up properly in partial ordering. |
| 6311 | Optional<unsigned> Depth; |
| 6312 | if (CTAK != CTAK_Specified) |
| 6313 | Depth = Param->getDepth() + 1; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6314 | if (DeduceAutoType( |
| 6315 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6316 | Arg, ParamType, Depth) == DAR_Failed) { |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6317 | Diag(Arg->getExprLoc(), |
| 6318 | diag::err_non_type_template_parm_type_deduction_failure) |
| 6319 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 6320 | << Arg->getSourceRange(); |
| 6321 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6322 | return ExprError(); |
| 6323 | } |
| 6324 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 6325 | // an error. The error message normally references the parameter |
| 6326 | // declaration, but here we'll pass the argument location because that's |
| 6327 | // where the parameter type is deduced. |
| 6328 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 6329 | if (ParamType.isNull()) { |
| 6330 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6331 | return ExprError(); |
| 6332 | } |
| 6333 | } |
| 6334 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6335 | // We should have already dropped all cv-qualifiers by now. |
| 6336 | assert(!ParamType.hasQualifiers() && |
| 6337 | "non-type template parameter type cannot be qualified"); |
| 6338 | |
| 6339 | if (CTAK == CTAK_Deduced && |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 6340 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6341 | Arg->getType())) { |
Richard Smith | 957fbf1 | 2017-01-17 02:14:37 +0000 | [diff] [blame] | 6342 | // FIXME: If either type is dependent, we skip the check. This isn't |
| 6343 | // correct, since during deduction we're supposed to have replaced each |
| 6344 | // template parameter with some unique (non-dependent) placeholder. |
| 6345 | // FIXME: If the argument type contains 'auto', we carry on and fail the |
| 6346 | // type check in order to force specific types to be more specialized than |
| 6347 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to |
| 6348 | // work. |
| 6349 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
| 6350 | !Arg->getType()->getContainedAutoType()) { |
| 6351 | Converted = TemplateArgument(Arg); |
| 6352 | return Arg; |
| 6353 | } |
| 6354 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, |
| 6355 | // we should actually be checking the type of the template argument in P, |
| 6356 | // not the type of the template argument deduced from A, against the |
| 6357 | // template parameter type. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6358 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6359 | << Arg->getType() |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6360 | << ParamType.getUnqualifiedType(); |
| 6361 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6362 | return ExprError(); |
| 6363 | } |
| 6364 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6365 | // If either the parameter has a dependent type or the argument is |
| 6366 | // type-dependent, there's nothing we can check now. |
| 6367 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 6368 | // FIXME: Produce a cloned, canonical expression? |
| 6369 | Converted = TemplateArgument(Arg); |
| 6370 | return Arg; |
| 6371 | } |
| 6372 | |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6373 | // The initialization of the parameter from the argument is |
| 6374 | // a constant-evaluated context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6375 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 6376 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6377 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6378 | if (getLangOpts().CPlusPlus17) { |
| 6379 | // C++17 [temp.arg.nontype]p1: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6380 | // A template-argument for a non-type template parameter shall be |
| 6381 | // a converted constant expression of the type of the template-parameter. |
| 6382 | APValue Value; |
| 6383 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 6384 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 6385 | if (ArgResult.isInvalid()) |
| 6386 | return ExprError(); |
| 6387 | |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6388 | // For a value-dependent argument, CheckConvertedConstantExpression is |
| 6389 | // permitted (and expected) to be unable to determine a value. |
| 6390 | if (ArgResult.get()->isValueDependent()) { |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6391 | Converted = TemplateArgument(ArgResult.get()); |
| 6392 | return ArgResult; |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6393 | } |
| 6394 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6395 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 6396 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6397 | // Convert the APValue to a TemplateArgument. |
| 6398 | switch (Value.getKind()) { |
Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 6399 | case APValue::None: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6400 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6401 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6402 | break; |
Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 6403 | case APValue::Indeterminate: |
| 6404 | llvm_unreachable("result of constant evaluation should be initialized"); |
| 6405 | break; |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6406 | case APValue::Int: |
| 6407 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6408 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6409 | break; |
| 6410 | case APValue::MemberPointer: { |
| 6411 | assert(ParamType->isMemberPointerType()); |
| 6412 | |
| 6413 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 6414 | if (!Value.getMemberPointerPath().empty()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6415 | Diag(Arg->getBeginLoc(), |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6416 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 6417 | << Value.getMemberPointerDecl() << ParamType |
| 6418 | << Arg->getSourceRange(); |
| 6419 | return ExprError(); |
| 6420 | } |
| 6421 | |
| 6422 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6423 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6424 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6425 | break; |
| 6426 | } |
| 6427 | case APValue::LValue: { |
| 6428 | // For a non-type template-parameter of pointer or reference type, |
| 6429 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6430 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 6431 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6432 | // -- a temporary object |
| 6433 | // -- a string literal |
| 6434 | // -- the result of a typeid expression, or |
Eric Christopher | 0d2c56a | 2017-03-31 01:45:39 +0000 | [diff] [blame] | 6435 | // -- a predefined __func__ variable |
Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 6436 | APValue::LValueBase Base = Value.getLValueBase(); |
| 6437 | auto *VD = const_cast<ValueDecl *>(Base.dyn_cast<const ValueDecl *>()); |
| 6438 | if (Base && !VD) { |
| 6439 | auto *E = Base.dyn_cast<const Expr *>(); |
| 6440 | if (E && isa<CXXUuidofExpr>(E)) { |
Bill Wendling | ff57307 | 2019-01-27 07:24:03 +0000 | [diff] [blame] | 6441 | Converted = TemplateArgument(ArgResult.get()->IgnoreImpCasts()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6442 | break; |
| 6443 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6444 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 6445 | << Arg->getSourceRange(); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6446 | return ExprError(); |
| 6447 | } |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6448 | // -- a subobject |
| 6449 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 6450 | VD && VD->getType()->isArrayType() && |
Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 6451 | Value.getLValuePath()[0].getAsArrayIndex() == 0 && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6452 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 6453 | // Per defect report (no number yet): |
| 6454 | // ... other than a pointer to the first element of a complete array |
| 6455 | // object. |
| 6456 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 6457 | Value.isLValueOnePastTheEnd()) { |
| 6458 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 6459 | << Value.getAsString(Context, ParamType); |
| 6460 | return ExprError(); |
| 6461 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6462 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6463 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6464 | assert((!VD || !ParamType->isNullPtrType()) && |
| 6465 | "non-null value of type nullptr_t?"); |
| 6466 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6467 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6468 | break; |
| 6469 | } |
| 6470 | case APValue::AddrLabelDiff: |
| 6471 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
Leonard Chan | 86285d2 | 2019-01-16 18:53:05 +0000 | [diff] [blame] | 6472 | case APValue::FixedPoint: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6473 | case APValue::Float: |
| 6474 | case APValue::ComplexInt: |
| 6475 | case APValue::ComplexFloat: |
| 6476 | case APValue::Vector: |
| 6477 | case APValue::Array: |
| 6478 | case APValue::Struct: |
| 6479 | case APValue::Union: |
| 6480 | llvm_unreachable("invalid kind for template argument"); |
| 6481 | } |
| 6482 | |
| 6483 | return ArgResult.get(); |
| 6484 | } |
| 6485 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6486 | // C++ [temp.arg.nontype]p5: |
| 6487 | // The following conversions are performed on each expression used |
| 6488 | // as a non-type template-argument. If a non-type |
| 6489 | // template-argument cannot be converted to the type of the |
| 6490 | // corresponding template-parameter then the program is |
| 6491 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6492 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6493 | // C++11: |
| 6494 | // -- for a non-type template-parameter of integral or |
| 6495 | // enumeration type, conversions permitted in a converted |
| 6496 | // constant expression are applied. |
| 6497 | // |
| 6498 | // C++98: |
| 6499 | // -- for a non-type template-parameter of integral or |
| 6500 | // enumeration type, integral promotions (4.5) and integral |
| 6501 | // conversions (4.7) are applied. |
| 6502 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6503 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6504 | // C++ [temp.arg.nontype]p1: |
| 6505 | // A template-argument for a non-type, non-template template-parameter |
| 6506 | // shall be one of: |
| 6507 | // |
| 6508 | // -- for a non-type template-parameter of integral or enumeration |
| 6509 | // type, a converted constant expression of the type of the |
| 6510 | // template-parameter; or |
| 6511 | llvm::APSInt Value; |
| 6512 | ExprResult ArgResult = |
| 6513 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 6514 | CCEK_TemplateArg); |
| 6515 | if (ArgResult.isInvalid()) |
| 6516 | return ExprError(); |
| 6517 | |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6518 | // We can't check arbitrary value-dependent arguments. |
| 6519 | if (ArgResult.get()->isValueDependent()) { |
| 6520 | Converted = TemplateArgument(ArgResult.get()); |
| 6521 | return ArgResult; |
| 6522 | } |
| 6523 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6524 | // Widen the argument value to sizeof(parameter type). This is almost |
| 6525 | // always a no-op, except when the parameter type is bool. In |
| 6526 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 6527 | QualType IntegerType = ParamType; |
| 6528 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 6529 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 6530 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 6531 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6532 | Converted = TemplateArgument(Context, Value, |
| 6533 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6534 | return ArgResult; |
| 6535 | } |
| 6536 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6537 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 6538 | if (ArgResult.isInvalid()) |
| 6539 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6540 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6541 | |
| 6542 | QualType ArgType = Arg->getType(); |
| 6543 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6544 | // C++ [temp.arg.nontype]p1: |
| 6545 | // A template-argument for a non-type, non-template |
| 6546 | // template-parameter shall be one of: |
| 6547 | // |
| 6548 | // -- an integral constant-expression of integral or enumeration |
| 6549 | // type; or |
| 6550 | // -- the name of a non-type template-parameter; or |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6551 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6552 | if (!ArgType->isIntegralOrEnumerationType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6553 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_integral_or_enumeral) |
| 6554 | << ArgType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6555 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6556 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6557 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6558 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 6559 | QualType T; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6560 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6561 | public: |
| 6562 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 6563 | |
| 6564 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 6565 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6566 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 6567 | } |
| 6568 | } Diagnoser(ArgType); |
| 6569 | |
| 6570 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6571 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6572 | if (!Arg) |
| 6573 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6574 | } |
| 6575 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6576 | // From here on out, all we care about is the unqualified form |
| 6577 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6578 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6579 | |
| 6580 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 6581 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6582 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6583 | } else if (ParamType->isBooleanType()) { |
| 6584 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6585 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6586 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 6587 | !ParamType->isEnumeralType()) { |
| 6588 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6589 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6590 | } else { |
| 6591 | // We can't perform this conversion. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6592 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 6593 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6594 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6595 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6598 | // Add the value of this argument to the list of converted |
| 6599 | // arguments. We use the bitwidth and signedness of the template |
| 6600 | // parameter. |
| 6601 | if (Arg->isValueDependent()) { |
| 6602 | // The argument is value-dependent. Create a new |
| 6603 | // TemplateArgument with the converted expression. |
| 6604 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6605 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6606 | } |
| 6607 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6608 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6609 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 6610 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6611 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6612 | if (ParamType->isBooleanType()) { |
| 6613 | // Value must be zero or one. |
| 6614 | Value = Value != 0; |
| 6615 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 6616 | if (Value.getBitWidth() != AllowedBits) |
| 6617 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6618 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6619 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6620 | llvm::APSInt OldValue = Value; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6621 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6622 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6623 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6624 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6625 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6626 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6627 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6628 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6629 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6630 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6631 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6632 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_negative) |
| 6633 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6634 | << Arg->getSourceRange(); |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6635 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6636 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6637 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6638 | // Complain if we overflowed the template parameter's type. |
| 6639 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6640 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6641 | RequiredBits = OldValue.getActiveBits(); |
| 6642 | else if (OldValue.isUnsigned()) |
| 6643 | RequiredBits = OldValue.getActiveBits() + 1; |
| 6644 | else |
| 6645 | RequiredBits = OldValue.getMinSignedBits(); |
| 6646 | if (RequiredBits > AllowedBits) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6647 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_too_large) |
| 6648 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6649 | << Arg->getSourceRange(); |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6650 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6651 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6652 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6653 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6654 | Converted = TemplateArgument(Context, Value, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6655 | ParamType->isEnumeralType() |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 6656 | ? Context.getCanonicalType(ParamType) |
| 6657 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6658 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6659 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6660 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6661 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6662 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 6663 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6664 | // Handle pointer-to-function, reference-to-function, and |
| 6665 | // pointer-to-member-function all in (roughly) the same way. |
| 6666 | if (// -- For a non-type template-parameter of type pointer to |
| 6667 | // function, only the function-to-pointer conversion (4.3) is |
| 6668 | // applied. If the template-argument represents a set of |
| 6669 | // overloaded functions (or a pointer to such), the matching |
| 6670 | // function is selected from the set (13.4). |
| 6671 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6672 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6673 | // -- For a non-type template-parameter of type reference to |
| 6674 | // function, no conversions apply. If the template-argument |
| 6675 | // represents a set of overloaded functions, the matching |
| 6676 | // function is selected from the set (13.4). |
| 6677 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6678 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6679 | // -- For a non-type template-parameter of type pointer to |
| 6680 | // member function, no conversions apply. If the |
| 6681 | // template-argument represents a set of overloaded member |
| 6682 | // functions, the matching member function is selected from |
| 6683 | // the set (13.4). |
| 6684 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6685 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6686 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6687 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6688 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6689 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6690 | true, |
| 6691 | FoundResult)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6692 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6693 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6694 | |
| 6695 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6696 | ArgType = Arg->getType(); |
| 6697 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6698 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6699 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6700 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6701 | if (!ParamType->isMemberPointerType()) { |
| 6702 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6703 | ParamType, |
| 6704 | Arg, Converted)) |
| 6705 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6706 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6707 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6708 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6709 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6710 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6711 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6712 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6713 | } |
| 6714 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 6715 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6716 | // -- for a non-type template-parameter of type pointer to |
| 6717 | // object, qualification conversions (4.4) and the |
| 6718 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6719 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6720 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6721 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6722 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6723 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6724 | ParamType, |
| 6725 | Arg, Converted)) |
| 6726 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6727 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6728 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6729 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6730 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6731 | // -- For a non-type template-parameter of type reference to |
| 6732 | // object, no conversions apply. The type referred to by the |
| 6733 | // reference may be more cv-qualified than the (otherwise |
| 6734 | // identical) type of the template-argument. The |
| 6735 | // template-parameter is bound directly to the |
| 6736 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6737 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6738 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6739 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6740 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6741 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 6742 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6743 | true, |
| 6744 | FoundResult)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6745 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6746 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6747 | |
| 6748 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6749 | ArgType = Arg->getType(); |
| 6750 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6751 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6752 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6753 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6754 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6755 | ParamType, |
| 6756 | Arg, Converted)) |
| 6757 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6758 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6759 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6760 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6761 | // Deal with parameters of type std::nullptr_t. |
| 6762 | if (ParamType->isNullPtrType()) { |
| 6763 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6764 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6765 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6766 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6767 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6768 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 6769 | case NPV_NotNullPointer: |
| 6770 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 6771 | << Arg->getType() << ParamType; |
| 6772 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6773 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6774 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6775 | case NPV_Error: |
| 6776 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6777 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6778 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 6779 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 6780 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 6781 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6782 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6783 | } |
| 6784 | } |
| 6785 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6786 | // -- For a non-type template-parameter of type pointer to data |
| 6787 | // member, qualification conversions (4.4) are applied. |
| 6788 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 6789 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6790 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6791 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6792 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6793 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6794 | } |
| 6795 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6796 | static void DiagnoseTemplateParameterListArityMismatch( |
| 6797 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 6798 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 6799 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6800 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6801 | /// template template parameter. |
| 6802 | /// |
| 6803 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 6804 | /// It returns true if an error occurred, and false otherwise. |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 6805 | bool Sema::CheckTemplateTemplateArgument(TemplateParameterList *Params, |
| 6806 | TemplateArgumentLoc &Arg) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6807 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6808 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 6809 | if (!Template) { |
| 6810 | // Any dependent template name is fine. |
| 6811 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 6812 | return false; |
| 6813 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6814 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6815 | if (Template->isInvalidDecl()) |
| 6816 | return true; |
| 6817 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6818 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6819 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6820 | // the name of a class template or an alias template, expressed as an |
| 6821 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6822 | // primary class templates are considered when matching the |
| 6823 | // template template argument with the corresponding parameter; |
| 6824 | // partial specializations are not considered even if their |
| 6825 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6826 | // |
| 6827 | // Note that we also allow template template parameters here, which |
| 6828 | // will happen when we are dealing with, e.g., class template |
| 6829 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6830 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6831 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6832 | !isa<TypeAliasTemplateDecl>(Template) && |
| 6833 | !isa<BuiltinTemplateDecl>(Template)) { |
| 6834 | assert(isa<FunctionTemplateDecl>(Template) && |
| 6835 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 6836 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6837 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 6838 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6839 | } |
| 6840 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6841 | // C++1z [temp.arg.template]p3: (DR 150) |
| 6842 | // A template-argument matches a template template-parameter P when P |
| 6843 | // is at least as specialized as the template-argument A. |
| 6844 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 6845 | // Quick check for the common case: |
| 6846 | // If P contains a parameter pack, then A [...] matches P if each of A's |
| 6847 | // template parameters matches the corresponding template parameter in |
| 6848 | // the template-parameter-list of P. |
| 6849 | if (TemplateParameterListsAreEqual( |
| 6850 | Template->getTemplateParameters(), Params, false, |
| 6851 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 6852 | return false; |
| 6853 | |
| 6854 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 6855 | Arg.getLocation())) |
| 6856 | return false; |
| 6857 | // FIXME: Produce better diagnostics for deduction failures. |
| 6858 | } |
| 6859 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6860 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 6861 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6862 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6863 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6864 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6867 | /// Given a non-type template argument that refers to a |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6868 | /// declaration and the type of its corresponding non-type template |
| 6869 | /// parameter, produce an expression that properly refers to that |
| 6870 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6871 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6872 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 6873 | QualType ParamType, |
| 6874 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 6875 | // C++ [temp.param]p8: |
| 6876 | // |
| 6877 | // A non-type template-parameter of type "array of T" or |
| 6878 | // "function returning T" is adjusted to be of type "pointer to |
| 6879 | // T" or "pointer to function returning T", respectively. |
| 6880 | if (ParamType->isArrayType()) |
| 6881 | ParamType = Context.getArrayDecayedType(ParamType); |
| 6882 | else if (ParamType->isFunctionType()) |
| 6883 | ParamType = Context.getPointerType(ParamType); |
| 6884 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6885 | // For a NULL non-type template argument, return nullptr casted to the |
| 6886 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6887 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6888 | return ImpCastExprToType( |
| 6889 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 6890 | ParamType, |
| 6891 | ParamType->getAs<MemberPointerType>() |
| 6892 | ? CK_NullToMemberPointer |
| 6893 | : CK_NullToPointer); |
| 6894 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6895 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 6896 | "Only declaration template arguments permitted here"); |
| 6897 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6898 | ValueDecl *VD = Arg.getAsDecl(); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6899 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6900 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 6901 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 6902 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6903 | // If the value is a class member, we might have a pointer-to-member. |
| 6904 | // Determine whether the non-type template template parameter is of |
| 6905 | // pointer-to-member type. If so, we need to build an appropriate |
| 6906 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 6907 | // would refer to the member itself. |
| 6908 | if (ParamType->isMemberPointerType()) { |
| 6909 | QualType ClassType |
| 6910 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 6911 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6912 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6913 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6914 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 6915 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6916 | |
| 6917 | // The actual value-ness of this is unimportant, but for |
| 6918 | // internal consistency's sake, references to instance methods |
| 6919 | // are r-values. |
| 6920 | ExprValueKind VK = VK_LValue; |
| 6921 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 6922 | VK = VK_RValue; |
| 6923 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6924 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6925 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6926 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6927 | Loc, |
| 6928 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6929 | if (RefExpr.isInvalid()) |
| 6930 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6931 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6932 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6933 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6934 | // We might need to perform a trailing qualification conversion, since |
| 6935 | // the element type on the parameter could be more qualified than the |
| 6936 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6937 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6938 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6939 | ParamType.getUnqualifiedType(), false, |
| 6940 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6941 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6942 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6943 | assert(!RefExpr.isInvalid() && |
| 6944 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6945 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6946 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6947 | } |
| 6948 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6949 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6950 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6951 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6952 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6953 | // When the non-type template parameter is a pointer, take the |
| 6954 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6955 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6956 | if (RefExpr.isInvalid()) |
| 6957 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6958 | |
Richard Smith | fc6fca1 | 2017-01-28 00:38:35 +0000 | [diff] [blame] | 6959 | if (!Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
| 6960 | (T->isFunctionType() || T->isArrayType())) { |
| 6961 | // Decay functions and arrays unless we're forming a pointer to array. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6962 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6963 | if (RefExpr.isInvalid()) |
| 6964 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6965 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6966 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6967 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6968 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6969 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6970 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6971 | } |
| 6972 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6973 | ExprValueKind VK = VK_RValue; |
| 6974 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6975 | // If the non-type template parameter has reference type, qualify the |
| 6976 | // resulting declaration reference with the extra qualifiers on the |
| 6977 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6978 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 6979 | VK = VK_LValue; |
| 6980 | T = Context.getQualifiedType(T, |
| 6981 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6982 | } else if (isa<FunctionDecl>(VD)) { |
| 6983 | // References to functions are always lvalues. |
| 6984 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6985 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6986 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6987 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6990 | /// Construct a new expression that refers to the given |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6991 | /// integral template argument with the given source-location |
| 6992 | /// information. |
| 6993 | /// |
| 6994 | /// This routine takes care of the mapping from an integral template |
| 6995 | /// argument (which may have any integral type) to the appropriate |
| 6996 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6997 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6998 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 6999 | SourceLocation Loc) { |
| 7000 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 7001 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 7002 | QualType OrigT = Arg.getIntegralType(); |
| 7003 | |
| 7004 | // If this is an enum type that we're instantiating, we need to use an integer |
| 7005 | // type the same size as the enumerator. We don't want to build an |
| 7006 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 7007 | // any integral type with C++11 enum classes, make sure we create the right |
| 7008 | // type of literal for it. |
| 7009 | QualType T = OrigT; |
| 7010 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 7011 | T = ET->getDecl()->getIntegerType(); |
| 7012 | |
| 7013 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 7014 | if (T->isAnyCharacterType()) { |
| 7015 | CharacterLiteral::CharacterKind Kind; |
| 7016 | if (T->isWideCharType()) |
| 7017 | Kind = CharacterLiteral::Wide; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 7018 | else if (T->isChar8Type() && getLangOpts().Char8) |
| 7019 | Kind = CharacterLiteral::UTF8; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 7020 | else if (T->isChar16Type()) |
| 7021 | Kind = CharacterLiteral::UTF16; |
| 7022 | else if (T->isChar32Type()) |
| 7023 | Kind = CharacterLiteral::UTF32; |
| 7024 | else |
| 7025 | Kind = CharacterLiteral::Ascii; |
| 7026 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 7027 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 7028 | Kind, T, Loc); |
| 7029 | } else if (T->isBooleanType()) { |
| 7030 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 7031 | T, Loc); |
| 7032 | } else if (T->isNullPtrType()) { |
| 7033 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 7034 | } else { |
| 7035 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 7036 | } |
| 7037 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 7038 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 7039 | // FIXME: This is a hack. We need a better way to handle substituted |
| 7040 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7041 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 7042 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 7043 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 7044 | Loc, Loc); |
| 7045 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7046 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 7047 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 7048 | } |
| 7049 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7050 | /// Match two template parameters within template parameter lists. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7051 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 7052 | bool Complain, |
| 7053 | Sema::TemplateParameterListEqualKind Kind, |
| 7054 | SourceLocation TemplateArgLoc) { |
| 7055 | // Check the actual kind (type, non-type, template). |
| 7056 | if (Old->getKind() != New->getKind()) { |
| 7057 | if (Complain) { |
| 7058 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 7059 | if (TemplateArgLoc.isValid()) { |
| 7060 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 7061 | NextDiag = diag::note_template_param_different_kind; |
| 7062 | } |
| 7063 | S.Diag(New->getLocation(), NextDiag) |
| 7064 | << (Kind != Sema::TPL_TemplateMatch); |
| 7065 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 7066 | << (Kind != Sema::TPL_TemplateMatch); |
| 7067 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7068 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7069 | return false; |
| 7070 | } |
| 7071 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 7072 | // Check that both are parameter packs or neither are parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7073 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7074 | // template template parameter, the template template parameter can have |
| 7075 | // a parameter pack where the template template argument does not. |
| 7076 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 7077 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 7078 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7079 | if (Complain) { |
| 7080 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 7081 | if (TemplateArgLoc.isValid()) { |
| 7082 | S.Diag(TemplateArgLoc, |
| 7083 | diag::err_template_arg_template_params_mismatch); |
| 7084 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 7085 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7086 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7087 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 7088 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 7089 | : 2; |
| 7090 | S.Diag(New->getLocation(), NextDiag) |
| 7091 | << ParamKind << New->isParameterPack(); |
| 7092 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 7093 | << ParamKind << Old->isParameterPack(); |
| 7094 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7095 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7096 | return false; |
| 7097 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7098 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7099 | // For non-type template parameters, check the type of the parameter. |
| 7100 | if (NonTypeTemplateParmDecl *OldNTTP |
| 7101 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 7102 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7103 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7104 | // If we are matching a template template argument to a template |
| 7105 | // template parameter and one of the non-type template parameter types |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 7106 | // is dependent, then we must wait until template instantiation time |
| 7107 | // to actually compare the arguments. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7108 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 7109 | (OldNTTP->getType()->isDependentType() || |
| 7110 | NewNTTP->getType()->isDependentType())) |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7111 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7112 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7113 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 7114 | if (Complain) { |
| 7115 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 7116 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7117 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7118 | diag::err_template_arg_template_params_mismatch); |
| 7119 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 7120 | } |
| 7121 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 7122 | << NewNTTP->getType() |
| 7123 | << (Kind != Sema::TPL_TemplateMatch); |
| 7124 | S.Diag(OldNTTP->getLocation(), |
| 7125 | diag::note_template_nontype_parm_prev_declaration) |
| 7126 | << OldNTTP->getType(); |
| 7127 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7128 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7129 | return false; |
| 7130 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7131 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7132 | return true; |
| 7133 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7134 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7135 | // For template template parameters, check the template parameter types. |
| 7136 | // The template parameter lists of template template |
| 7137 | // parameters must agree. |
| 7138 | if (TemplateTemplateParmDecl *OldTTP |
| 7139 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7140 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7141 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 7142 | OldTTP->getTemplateParameters(), |
| 7143 | Complain, |
| 7144 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7145 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7146 | : Kind), |
| 7147 | TemplateArgLoc); |
| 7148 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7149 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7150 | return true; |
| 7151 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 7152 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7153 | /// Diagnose a known arity mismatch when comparing template argument |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7154 | /// lists. |
| 7155 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7156 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7157 | TemplateParameterList *New, |
| 7158 | TemplateParameterList *Old, |
| 7159 | Sema::TemplateParameterListEqualKind Kind, |
| 7160 | SourceLocation TemplateArgLoc) { |
| 7161 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 7162 | if (TemplateArgLoc.isValid()) { |
| 7163 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 7164 | NextDiag = diag::note_template_param_list_different_arity; |
| 7165 | } |
| 7166 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 7167 | << (New->size() > Old->size()) |
| 7168 | << (Kind != Sema::TPL_TemplateMatch) |
| 7169 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 7170 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 7171 | << (Kind != Sema::TPL_TemplateMatch) |
| 7172 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 7173 | } |
| 7174 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7175 | /// Determine whether the given template parameter lists are |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7176 | /// equivalent. |
| 7177 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7178 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7179 | /// source code as part of a new template declaration. |
| 7180 | /// |
| 7181 | /// \param Old The old template parameter list, typically found via |
| 7182 | /// name lookup of the template declared with this template parameter |
| 7183 | /// list. |
| 7184 | /// |
| 7185 | /// \param Complain If true, this routine will produce a diagnostic if |
| 7186 | /// the template parameter lists are not equivalent. |
| 7187 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7188 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7189 | /// |
| 7190 | /// \param TemplateArgLoc If this source location is valid, then we |
| 7191 | /// are actually checking the template parameter list of a template |
| 7192 | /// argument (New) against the template parameter list of its |
| 7193 | /// corresponding template template parameter (Old). We produce |
| 7194 | /// slightly different diagnostics in this scenario. |
| 7195 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7196 | /// \returns True if the template parameter lists are equal, false |
| 7197 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7198 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7199 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 7200 | TemplateParameterList *Old, |
| 7201 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7202 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7203 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7204 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 7205 | if (Complain) |
| 7206 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7207 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7208 | |
| 7209 | return false; |
| 7210 | } |
| 7211 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7212 | // C++0x [temp.arg.template]p3: |
| 7213 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7214 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7215 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7216 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7217 | // template-parameter-list of P. [...] |
| 7218 | TemplateParameterList::iterator NewParm = New->begin(); |
| 7219 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7220 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7221 | OldParmEnd = Old->end(); |
| 7222 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 7223 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 7224 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7225 | if (NewParm == NewParmEnd) { |
| 7226 | if (Complain) |
| 7227 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7228 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7229 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7230 | return false; |
| 7231 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7232 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7233 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7234 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7235 | return false; |
| 7236 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7237 | ++NewParm; |
| 7238 | continue; |
| 7239 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7240 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7241 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7242 | // [...] When P's template- parameter-list contains a template parameter |
| 7243 | // pack (14.5.3), the template parameter pack will match zero or more |
| 7244 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7245 | // template-parameter-list of A with the same type and form as the |
| 7246 | // template parameter pack in P (ignoring whether those template |
| 7247 | // parameters are template parameter packs). |
| 7248 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 7249 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7250 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7251 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7252 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7253 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7254 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7255 | // Make sure we exhausted all of the arguments. |
| 7256 | if (NewParm != NewParmEnd) { |
| 7257 | if (Complain) |
| 7258 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7259 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7260 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7261 | return false; |
| 7262 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7263 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7264 | return true; |
| 7265 | } |
| 7266 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7267 | /// Check whether a template can be declared within this scope. |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7268 | /// |
| 7269 | /// If the template declaration is valid in this scope, returns |
| 7270 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7271 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7272 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 7273 | if (!S) |
| 7274 | return false; |
| 7275 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7276 | // Find the nearest enclosing declaration scope. |
| 7277 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7278 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7279 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7280 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7281 | // C++ [temp]p4: |
| 7282 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7283 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 7284 | if (Ctx && Ctx->isExternCContext()) { |
| 7285 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 7286 | << TemplateParams->getSourceRange(); |
| 7287 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 7288 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 7289 | return true; |
| 7290 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 7291 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7292 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7293 | // C++ [temp]p2: |
| 7294 | // A template-declaration can appear only as a namespace scope or |
| 7295 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 7296 | if (Ctx) { |
| 7297 | if (Ctx->isFileContext()) |
| 7298 | return false; |
| 7299 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 7300 | // C++ [temp.mem]p2: |
| 7301 | // A local class shall not have member templates. |
| 7302 | if (RD->isLocalClass()) |
| 7303 | return Diag(TemplateParams->getTemplateLoc(), |
| 7304 | diag::err_template_inside_local_class) |
| 7305 | << TemplateParams->getSourceRange(); |
| 7306 | else |
| 7307 | return false; |
| 7308 | } |
| 7309 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7310 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7311 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7312 | diag::err_template_outside_namespace_or_class_scope) |
| 7313 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7314 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7315 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7316 | /// Determine what kind of template specialization the given declaration |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7317 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7318 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7319 | if (!D) |
| 7320 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7321 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7322 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 7323 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7324 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 7325 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7326 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 7327 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7328 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7329 | return TSK_Undeclared; |
| 7330 | } |
| 7331 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7332 | /// Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7333 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7334 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7335 | /// This routine determines whether a template specialization can be declared |
| 7336 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7337 | /// |
| 7338 | /// \param S the semantic analysis object for which this check is being |
| 7339 | /// performed. |
| 7340 | /// |
| 7341 | /// \param Specialized the entity being specialized or instantiated, which |
| 7342 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7343 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7344 | /// member class). |
| 7345 | /// |
| 7346 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 7347 | /// |
| 7348 | /// \param Loc the location of the explicit specialization or instantiation of |
| 7349 | /// this entity. |
| 7350 | /// |
| 7351 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 7352 | /// a class template. |
| 7353 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7354 | /// \returns true if there was an error that we cannot recover from, false |
| 7355 | /// otherwise. |
| 7356 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 7357 | NamedDecl *Specialized, |
| 7358 | NamedDecl *PrevDecl, |
| 7359 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7360 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7361 | // Keep these "kind" numbers in sync with the %select statements in the |
| 7362 | // various diagnostics emitted by this routine. |
| 7363 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7364 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7365 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7366 | else if (isa<VarTemplateDecl>(Specialized)) |
| 7367 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7368 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7369 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7370 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7371 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7372 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7373 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7374 | else if (isa<RecordDecl>(Specialized)) |
| 7375 | EntityKind = 7; |
| 7376 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 7377 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7378 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7379 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7380 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7381 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7382 | return true; |
| 7383 | } |
| 7384 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7385 | // C++ [temp.expl.spec]p2: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7386 | // An explicit specialization may be declared in any scope in which |
| 7387 | // the corresponding primary template may be defined. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7388 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7389 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7390 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7391 | return true; |
| 7392 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 7393 | |
| 7394 | // C++ [temp.class.spec]p6: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7395 | // A class template partial specialization may be declared in any |
| 7396 | // scope in which the primary template may be defined. |
| 7397 | DeclContext *SpecializedContext = |
| 7398 | Specialized->getDeclContext()->getRedeclContext(); |
| 7399 | DeclContext *DC = S.CurContext->getRedeclContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7400 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7401 | // Make sure that this redeclaration (or definition) occurs in the same |
| 7402 | // scope or an enclosing namespace. |
| 7403 | if (!(DC->isFileContext() ? DC->Encloses(SpecializedContext) |
| 7404 | : DC->Equals(SpecializedContext))) { |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7405 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 7406 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 7407 | << EntityKind << Specialized; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7408 | else { |
| 7409 | auto *ND = cast<NamedDecl>(SpecializedContext); |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7410 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7411 | if (S.getLangOpts().MicrosoftExt && !DC->isRecord()) |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7412 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 7413 | S.Diag(Loc, Diag) << EntityKind << Specialized |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7414 | << ND << isa<CXXRecordDecl>(ND); |
| 7415 | } |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7416 | |
| 7417 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7418 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7419 | // Don't allow specializing in the wrong class during error recovery. |
| 7420 | // Otherwise, things can go horribly wrong. |
| 7421 | if (DC->isRecord()) |
| 7422 | return true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7423 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7424 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7425 | return false; |
| 7426 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7427 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7428 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 7429 | if (!E->isTypeDependent()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7430 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7431 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7432 | Checker.TraverseStmt(E); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7433 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7434 | return E->getSourceRange(); |
| 7435 | return Checker.MatchLoc; |
| 7436 | } |
| 7437 | |
| 7438 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 7439 | if (!TL.getType()->isDependentType()) |
| 7440 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7441 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7442 | Checker.TraverseTypeLoc(TL); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7443 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7444 | return TL.getSourceRange(); |
| 7445 | return Checker.MatchLoc; |
| 7446 | } |
| 7447 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7448 | /// Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7449 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7450 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7451 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 7452 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7453 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 7454 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7455 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7456 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 7457 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7458 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7459 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7460 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7461 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7462 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7463 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7464 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7465 | |
| 7466 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7467 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 7468 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7469 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 7470 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7471 | |
| 7472 | // Strip off any implicit casts we added as part of type checking. |
| 7473 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 7474 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7475 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7476 | // C++ [temp.class.spec]p8: |
| 7477 | // A non-type argument is non-specialized if it is the name of a |
| 7478 | // non-type parameter. All other non-type arguments are |
| 7479 | // specialized. |
| 7480 | // |
| 7481 | // Below, we check the two conditions that only apply to |
| 7482 | // specialized non-type arguments, so skip any non-specialized |
| 7483 | // arguments. |
| 7484 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7485 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7486 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7487 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7488 | // C++ [temp.class.spec]p9: |
| 7489 | // Within the argument list of a class template partial |
| 7490 | // specialization, the following restrictions apply: |
| 7491 | // -- A partially specialized non-type argument expression |
| 7492 | // shall not involve a template parameter of the partial |
| 7493 | // specialization except when the argument expression is a |
| 7494 | // simple identifier. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7495 | // -- The type of a template parameter corresponding to a |
| 7496 | // specialized non-type argument shall not be dependent on a |
| 7497 | // parameter of the specialization. |
| 7498 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
| 7499 | // We implement a compromise between the original rules and DR1315: |
| 7500 | // -- A specialized non-type template argument shall not be |
| 7501 | // type-dependent and the corresponding template parameter |
| 7502 | // shall have a non-dependent type. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7503 | SourceRange ParamUseRange = |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7504 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7505 | if (ParamUseRange.isValid()) { |
| 7506 | if (IsDefaultArgument) { |
| 7507 | S.Diag(TemplateNameLoc, |
| 7508 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 7509 | S.Diag(ParamUseRange.getBegin(), |
| 7510 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 7511 | << ParamUseRange; |
| 7512 | } else { |
| 7513 | S.Diag(ParamUseRange.getBegin(), |
| 7514 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 7515 | << ParamUseRange; |
| 7516 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7517 | return true; |
| 7518 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7519 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7520 | ParamUseRange = findTemplateParameter( |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7521 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7522 | if (ParamUseRange.isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7523 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getBeginLoc(), |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7524 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7525 | << Param->getType(); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7526 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7527 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 7528 | << ParamUseRange; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7529 | return true; |
| 7530 | } |
| 7531 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7532 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7533 | return false; |
| 7534 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7535 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7536 | /// Check the non-type template arguments of a class template |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7537 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 7538 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7539 | /// \param TemplateNameLoc the location of the template name. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7540 | /// \param PrimaryTemplate the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7541 | /// template. |
| 7542 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7543 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7544 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7545 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7546 | /// \returns \c true if there was an error, \c false otherwise. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7547 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 7548 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 7549 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 7550 | // We have to be conservative when checking a template in a dependent |
| 7551 | // context. |
| 7552 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 7553 | return false; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7554 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7555 | TemplateParameterList *TemplateParams = |
| 7556 | PrimaryTemplate->getTemplateParameters(); |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7557 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7558 | NonTypeTemplateParmDecl *Param |
| 7559 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 7560 | if (!Param) |
| 7561 | continue; |
| 7562 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7563 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 7564 | Param, &TemplateArgs[I], |
| 7565 | 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7566 | return true; |
| 7567 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7568 | |
| 7569 | return false; |
| 7570 | } |
| 7571 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7572 | DeclResult Sema::ActOnClassTemplateSpecialization( |
| 7573 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
| 7574 | SourceLocation ModulePrivateLoc, TemplateIdAnnotation &TemplateId, |
| 7575 | const ParsedAttributesView &Attr, |
| 7576 | MultiTemplateParamsArg TemplateParameterLists, SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7577 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 7578 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7579 | CXXScopeSpec &SS = TemplateId.SS; |
| 7580 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7581 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 7582 | // store the location of the outermost template keyword in the declaration. |
| 7583 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7584 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 7585 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 7586 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 7587 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7588 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7589 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7590 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7591 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7592 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 7593 | |
| 7594 | if (!ClassTemplate) { |
| 7595 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7596 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7597 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 7598 | return true; |
| 7599 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7600 | |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7601 | bool isMemberSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7602 | bool isPartialSpecialization = false; |
| 7603 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7604 | // Check the validity of the template headers that introduce this |
| 7605 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7606 | // FIXME: We probably shouldn't complain about these headers for |
| 7607 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7608 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 7609 | TemplateParameterList *TemplateParams = |
| 7610 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7611 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7612 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7613 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7614 | if (Invalid) |
| 7615 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7616 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7617 | if (TemplateParams && TemplateParams->size() > 0) { |
| 7618 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7619 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 7620 | if (TUK == TUK_Friend) { |
| 7621 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 7622 | << SourceRange(LAngleLoc, RAngleLoc); |
| 7623 | return true; |
| 7624 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7625 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7626 | // C++ [temp.class.spec]p10: |
| 7627 | // The template parameter list of a specialization shall not |
| 7628 | // contain default template argument values. |
| 7629 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7630 | Decl *Param = TemplateParams->getParam(I); |
| 7631 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 7632 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7633 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7634 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 7635 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7636 | } |
| 7637 | } else if (NonTypeTemplateParmDecl *NTTP |
| 7638 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 7639 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7640 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7641 | diag::err_default_arg_in_partial_spec) |
| 7642 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7643 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7644 | } |
| 7645 | } else { |
| 7646 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7647 | if (TTP->hasDefaultArgument()) { |
| 7648 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7649 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7650 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7651 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7652 | } |
| 7653 | } |
| 7654 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7655 | } else if (TemplateParams) { |
| 7656 | if (TUK == TUK_Friend) |
| 7657 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7658 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7659 | SourceRange(TemplateParams->getTemplateLoc(), |
| 7660 | TemplateParams->getRAngleLoc())) |
| 7661 | << SourceRange(LAngleLoc, RAngleLoc); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7662 | } else { |
| 7663 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7664 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7665 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7666 | // Check that the specialization uses the same tag kind as the |
| 7667 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7668 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7669 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7670 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7671 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7672 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7673 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7674 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7675 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7676 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7677 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7678 | diag::note_previous_use); |
| 7679 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7680 | } |
| 7681 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7682 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7683 | TemplateArgumentListInfo TemplateArgs = |
| 7684 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7685 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7686 | // Check for unexpanded parameter packs in any of the template arguments. |
| 7687 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7688 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7689 | UPPC_PartialSpecialization)) |
| 7690 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7691 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7692 | // Check that the template argument list is well-formed for this |
| 7693 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7694 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7695 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7696 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7697 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7698 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7699 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7700 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7701 | if (isPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7702 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 7703 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7704 | return true; |
| 7705 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7706 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
| 7707 | // also do it during instantiation. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 7708 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7709 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7710 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7711 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7712 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 7713 | << ClassTemplate->getDeclName(); |
| 7714 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7715 | } |
| 7716 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7717 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7718 | void *InsertPos = nullptr; |
| 7719 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7720 | |
| 7721 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7722 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7723 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7724 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7725 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7726 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7727 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7728 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7729 | // Check whether we can declare a class template specialization in |
| 7730 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7731 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7732 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 7733 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7734 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7735 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7736 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7737 | // The canonical type |
| 7738 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 7739 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7740 | // Build the canonical type that describes the converted template |
| 7741 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7742 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7743 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7744 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7745 | |
| 7746 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7747 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 7748 | // C++ [temp.class.spec]p9b3: |
| 7749 | // |
| 7750 | // -- The argument list of the specialization shall not be identical |
| 7751 | // to the implicit argument list of the primary template. |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 7752 | // |
| 7753 | // This rule has since been removed, because it's redundant given DR1495, |
| 7754 | // but we keep it because it produces better diagnostics and recovery. |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7755 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 7756 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 7757 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7758 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 7759 | ClassTemplate->getIdentifier(), |
| 7760 | TemplateNameLoc, |
| 7761 | Attr, |
| 7762 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7763 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 7764 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7765 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7766 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7767 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7768 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7769 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7770 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 7771 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7772 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7773 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7774 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7775 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 7776 | TemplateParams, |
| 7777 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7778 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7779 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7780 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 7781 | PrevPartial); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 7782 | SetNestedNameSpecifier(*this, Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7783 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7784 | Partial->setTemplateParameterListsInfo( |
| 7785 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7786 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7787 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7788 | if (!PrevPartial) |
| 7789 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7790 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 7791 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7792 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 7793 | // template specialization, make a note of that. |
| 7794 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 7795 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7796 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7797 | CheckTemplatePartialSpecialization(Partial); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7798 | } else { |
| 7799 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7800 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7801 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7802 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7803 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7804 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7805 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7806 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7807 | PrevDecl); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 7808 | SetNestedNameSpecifier(*this, Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7809 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 7810 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7811 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7812 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7813 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7814 | if (!PrevDecl) |
| 7815 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7816 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7817 | if (CurContext->isDependentContext()) { |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7818 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7819 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7820 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7821 | } else { |
| 7822 | CanonType = Context.getTypeDeclType(Specialization); |
| 7823 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7824 | } |
| 7825 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7826 | // C++ [temp.expl.spec]p6: |
| 7827 | // 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] | 7828 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7829 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7830 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7831 | // use occurs; no diagnostic is required. |
| 7832 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7833 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7834 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7835 | // Is there any previous explicit specialization declaration? |
| 7836 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7837 | Okay = true; |
| 7838 | break; |
| 7839 | } |
| 7840 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7841 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7842 | if (!Okay) { |
| 7843 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 7844 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 7845 | << Context.getTypeDeclType(Specialization) << Range; |
| 7846 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7847 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7848 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7849 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7850 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7851 | return true; |
| 7852 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7853 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7854 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7855 | // If this is not a friend, note that this is an explicit specialization. |
| 7856 | if (TUK != TUK_Friend) |
| 7857 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7858 | |
| 7859 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7860 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7861 | RecordDecl *Def = Specialization->getDefinition(); |
| 7862 | NamedDecl *Hidden = nullptr; |
| 7863 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 7864 | SkipBody->ShouldSkip = true; |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7865 | SkipBody->Previous = Def; |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 7866 | makeMergedDefinitionVisible(Hidden); |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7867 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7868 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 7869 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7870 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 7871 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7872 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7873 | } |
| 7874 | } |
| 7875 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7876 | ProcessDeclAttributeList(S, Specialization, Attr); |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 7877 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 7878 | // Add alignment attributes if necessary; these attributes are checked when |
| 7879 | // the ASTContext lays out the structure. |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7880 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 7881 | AddAlignmentAttributesForRecord(Specialization); |
| 7882 | AddMsStructLayoutForRecord(Specialization); |
| 7883 | } |
| 7884 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 7885 | if (ModulePrivateLoc.isValid()) |
| 7886 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 7887 | << (isPartialSpecialization? 1 : 0) |
| 7888 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7889 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 7890 | // Build the fully-sugared type for this class template |
| 7891 | // specialization as the user wrote in the specialization |
| 7892 | // itself. This means that we'll pretty-print the type retrieved |
| 7893 | // from the specialization's declaration the way that the user |
| 7894 | // actually wrote the specialization, rather than formatting the |
| 7895 | // name based on the "canonical" representation used to store the |
| 7896 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7897 | TypeSourceInfo *WrittenTy |
| 7898 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7899 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7900 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7901 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7902 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7903 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7904 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 7905 | // C++ [temp.expl.spec]p9: |
| 7906 | // A template explicit specialization is in the scope of the |
| 7907 | // namespace in which the template was defined. |
| 7908 | // |
| 7909 | // We actually implement this paragraph where we set the semantic |
| 7910 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 7911 | // but we also maintain the lexical context where the actual |
| 7912 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7913 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7914 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7915 | // We may be starting the definition of this specialization. |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7916 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7917 | Specialization->startDefinition(); |
| 7918 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7919 | if (TUK == TUK_Friend) { |
| 7920 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 7921 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 7922 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7923 | /*FIXME:*/KWLoc); |
| 7924 | Friend->setAccess(AS_public); |
| 7925 | CurContext->addDecl(Friend); |
| 7926 | } else { |
| 7927 | // Add the specialization into its lexical context, so that it can |
| 7928 | // be seen when iterating through the list of declarations in that |
| 7929 | // context. However, specializations are not found by name lookup. |
| 7930 | CurContext->addDecl(Specialization); |
| 7931 | } |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7932 | |
| 7933 | if (SkipBody && SkipBody->ShouldSkip) |
| 7934 | return SkipBody->Previous; |
| 7935 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7936 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7937 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7938 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7939 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7940 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7941 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7942 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 7943 | ActOnDocumentableDecl(NewDecl); |
| 7944 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7945 | } |
| 7946 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7947 | /// Strips various properties off an implicit instantiation |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7948 | /// that has just been explicitly specialized. |
| 7949 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7950 | D->dropAttr<DLLImportAttr>(); |
| 7951 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7952 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7953 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7954 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7955 | } |
| 7956 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7957 | /// Compute the diagnostic location for an explicit instantiation |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7958 | // declaration or definition. |
| 7959 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7960 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7961 | // Explicit instantiations following a specialization have no effect and |
| 7962 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 7963 | // until a valid name loc is found. |
| 7964 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7965 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 7966 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7967 | PrevDiagLoc = Prev->getLocation(); |
| 7968 | } |
| 7969 | assert(PrevDiagLoc.isValid() && |
| 7970 | "Explicit instantiation without point of instantiation?"); |
| 7971 | return PrevDiagLoc; |
| 7972 | } |
| 7973 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7974 | /// Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7975 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7976 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7977 | /// new specialization/instantiation will have any effect. |
| 7978 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7979 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7980 | /// instantiation. |
| 7981 | /// |
| 7982 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 7983 | /// |
| 7984 | /// \param PrevDecl the previous declaration of the entity. |
| 7985 | /// |
| 7986 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 7987 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7988 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7989 | /// declaration was instantiated (either implicitly or explicitly). |
| 7990 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7991 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7992 | /// specialization or instantiation has no effect and should be ignored. |
| 7993 | /// |
| 7994 | /// \returns true if there was an error that should prevent the introduction of |
| 7995 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7996 | bool |
| 7997 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 7998 | TemplateSpecializationKind NewTSK, |
| 7999 | NamedDecl *PrevDecl, |
| 8000 | TemplateSpecializationKind PrevTSK, |
| 8001 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8002 | bool &HasNoEffect) { |
| 8003 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8004 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8005 | switch (NewTSK) { |
| 8006 | case TSK_Undeclared: |
| 8007 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 8008 | assert( |
| 8009 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 8010 | "previous declaration must be implicit!"); |
| 8011 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8012 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8013 | case TSK_ExplicitSpecialization: |
| 8014 | switch (PrevTSK) { |
| 8015 | case TSK_Undeclared: |
| 8016 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8017 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8018 | // explicitly specialized or has merely been mentioned without any |
| 8019 | // instantiation. |
| 8020 | return false; |
| 8021 | |
| 8022 | case TSK_ImplicitInstantiation: |
| 8023 | if (PrevPointOfInstantiation.isInvalid()) { |
| 8024 | // The declaration itself has not actually been instantiated, so it is |
| 8025 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8026 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8027 | return false; |
| 8028 | } |
| 8029 | // Fall through |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 8030 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8031 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8032 | case TSK_ExplicitInstantiationDeclaration: |
| 8033 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8034 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 8035 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8036 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8037 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8038 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8039 | // 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] | 8040 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8041 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8042 | // implicit instantiation to take place, in every translation unit in |
| 8043 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 8044 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 8045 | // Is there any previous explicit specialization declaration? |
| 8046 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 8047 | return false; |
| 8048 | } |
| 8049 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8050 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8051 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8052 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8053 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8054 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8055 | return true; |
| 8056 | } |
Galina Kistanova | 1d36e83 | 2017-06-08 18:20:32 +0000 | [diff] [blame] | 8057 | llvm_unreachable("The switch over PrevTSK must be exhaustive."); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8058 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8059 | case TSK_ExplicitInstantiationDeclaration: |
| 8060 | switch (PrevTSK) { |
| 8061 | case TSK_ExplicitInstantiationDeclaration: |
| 8062 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8063 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8064 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8065 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8066 | case TSK_Undeclared: |
| 8067 | case TSK_ImplicitInstantiation: |
| 8068 | // We're explicitly instantiating something that may have already been |
| 8069 | // implicitly instantiated; that's fine. |
| 8070 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8071 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8072 | case TSK_ExplicitSpecialization: |
| 8073 | // C++0x [temp.explicit]p4: |
| 8074 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8075 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8076 | // specialization for that template, the explicit instantiation has no |
| 8077 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8078 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8079 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8080 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8081 | case TSK_ExplicitInstantiationDefinition: |
| 8082 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8083 | // If an entity is the subject of both an explicit instantiation |
| 8084 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8085 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8086 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8087 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 8088 | |
| 8089 | // Explicit instantiations following a specialization have no effect and |
| 8090 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 8091 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 8092 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 8093 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8094 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8095 | return false; |
| 8096 | } |
Bruno Ricci | d8c1767 | 2018-12-21 20:38:06 +0000 | [diff] [blame] | 8097 | llvm_unreachable("Unexpected TemplateSpecializationKind!"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8098 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8099 | case TSK_ExplicitInstantiationDefinition: |
| 8100 | switch (PrevTSK) { |
| 8101 | case TSK_Undeclared: |
| 8102 | case TSK_ImplicitInstantiation: |
| 8103 | // We're explicitly instantiating something that may have already been |
| 8104 | // implicitly instantiated; that's fine. |
| 8105 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8106 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8107 | case TSK_ExplicitSpecialization: |
| 8108 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 8109 | // For a given set of template parameters, if an explicit |
| 8110 | // instantiation of a template appears after a declaration of |
| 8111 | // an explicit specialization for that template, the explicit |
| 8112 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 8113 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8114 | << PrevDecl; |
| 8115 | Diag(PrevDecl->getLocation(), |
| 8116 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8117 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8118 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8119 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8120 | case TSK_ExplicitInstantiationDeclaration: |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 8121 | // We're explicitly instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8122 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 8123 | |
| 8124 | // C++0x [temp.explicit]p4: |
| 8125 | // For a given set of template parameters, if an explicit instantiation |
| 8126 | // of a template appears after a declaration of an explicit |
| 8127 | // specialization for that template, the explicit instantiation has no |
| 8128 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 8129 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 8130 | // Is there any previous explicit specialization declaration? |
| 8131 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 8132 | HasNoEffect = true; |
| 8133 | break; |
| 8134 | } |
| 8135 | } |
| 8136 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8137 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8138 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8139 | case TSK_ExplicitInstantiationDefinition: |
| 8140 | // C++0x [temp.spec]p5: |
| 8141 | // For a given template and a given set of template-arguments, |
| 8142 | // - an explicit instantiation definition shall appear at most once |
| 8143 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 8144 | |
| 8145 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 8146 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 8147 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 8148 | : diag::err_explicit_instantiation_duplicate) |
| 8149 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 8150 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8151 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8152 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8153 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8154 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8155 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8156 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8157 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8158 | } |
| 8159 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8160 | /// Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8161 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8162 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8163 | /// The only possible way to get a dependent function template specialization |
| 8164 | /// is with a friend declaration, like so: |
| 8165 | /// |
| 8166 | /// \code |
| 8167 | /// template \<class T> void foo(T); |
| 8168 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8169 | /// friend void foo<>(T); |
| 8170 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8171 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8172 | /// |
| 8173 | /// There really isn't any useful analysis we can do here, so we |
| 8174 | /// just store the information. |
| 8175 | bool |
| 8176 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 8177 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 8178 | LookupResult &Previous) { |
| 8179 | // Remove anything from Previous that isn't a function template in |
| 8180 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8181 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8182 | LookupResult::Filter F = Previous.makeFilter(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8183 | enum DiscardReason { NotAFunctionTemplate, NotAMemberOfEnclosing }; |
| 8184 | SmallVector<std::pair<DiscardReason, Decl *>, 8> DiscardedCandidates; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8185 | while (F.hasNext()) { |
| 8186 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8187 | if (!isa<FunctionTemplateDecl>(D)) { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8188 | F.erase(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8189 | DiscardedCandidates.push_back(std::make_pair(NotAFunctionTemplate, D)); |
| 8190 | continue; |
| 8191 | } |
| 8192 | |
| 8193 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8194 | D->getDeclContext()->getRedeclContext())) { |
| 8195 | F.erase(); |
| 8196 | DiscardedCandidates.push_back(std::make_pair(NotAMemberOfEnclosing, D)); |
| 8197 | continue; |
| 8198 | } |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8199 | } |
| 8200 | F.done(); |
| 8201 | |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8202 | if (Previous.empty()) { |
| 8203 | Diag(FD->getLocation(), |
| 8204 | diag::err_dependent_function_template_spec_no_match); |
| 8205 | for (auto &P : DiscardedCandidates) |
| 8206 | Diag(P.second->getLocation(), |
| 8207 | diag::note_dependent_function_template_spec_discard_reason) |
| 8208 | << P.first; |
| 8209 | return true; |
| 8210 | } |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8211 | |
| 8212 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 8213 | ExplicitTemplateArgs); |
| 8214 | return false; |
| 8215 | } |
| 8216 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8217 | /// Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8218 | /// specialization. |
| 8219 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8220 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8221 | /// explicit function template specialization. On successful completion, |
| 8222 | /// the function declaration \p FD will become a function template |
| 8223 | /// specialization. |
| 8224 | /// |
| 8225 | /// \param FD the function declaration, which will be updated to become a |
| 8226 | /// function template specialization. |
| 8227 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8228 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 8229 | /// if any. Note that this may be valid info even when 0 arguments are |
| 8230 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 8231 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8232 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8233 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8234 | /// this function specialization. |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8235 | /// |
| 8236 | /// \param QualifiedFriend whether this is a lookup for a qualified friend |
| 8237 | /// declaration with no explicit template argument list that might be |
| 8238 | /// befriending a function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8239 | bool Sema::CheckFunctionTemplateSpecialization( |
| 8240 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8241 | LookupResult &Previous, bool QualifiedFriend) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8242 | // The set of function template specializations that could match this |
| 8243 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8244 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8245 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 8246 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8247 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8248 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 8249 | ConvertedTemplateArgs; |
| 8250 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8251 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8252 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8253 | I != E; ++I) { |
| 8254 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 8255 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8256 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8257 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8258 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8259 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8260 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8261 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8262 | // When matching a constexpr member function template specialization |
| 8263 | // against the primary template, we don't yet know whether the |
| 8264 | // specialization has an implicit 'const' (because we don't know whether |
| 8265 | // it will be a static member function until we know which template it |
| 8266 | // specializes), so adjust it now assuming it specializes this template. |
| 8267 | QualType FT = FD->getType(); |
| 8268 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8269 | CXXMethodDecl *OldMD = |
| 8270 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8271 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8272 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8273 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 8274 | EPI.TypeQuals.addConst(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8275 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8276 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8277 | } |
| 8278 | } |
| 8279 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8280 | TemplateArgumentListInfo Args; |
| 8281 | if (ExplicitTemplateArgs) |
| 8282 | Args = *ExplicitTemplateArgs; |
| 8283 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8284 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8285 | // A trailing template-argument can be left unspecified in the |
| 8286 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8287 | // provided it can be deduced from the function argument type. |
| 8288 | // Perform template argument deduction to determine whether we may be |
| 8289 | // specializing this template. |
| 8290 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8291 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8292 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 8293 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 8294 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8295 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 8296 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8297 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8298 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8299 | FailedCandidates.addCandidate().set( |
| 8300 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8301 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8302 | (void)TDK; |
| 8303 | continue; |
| 8304 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8305 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8306 | // Target attributes are part of the cuda function signature, so |
| 8307 | // the deduced template's cuda target must match that of the |
| 8308 | // specialization. Given that C++ template deduction does not |
| 8309 | // take target attributes into account, we reject candidates |
| 8310 | // here that have a different target. |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8311 | if (LangOpts.CUDA && |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8312 | IdentifyCUDATarget(Specialization, |
| 8313 | /* IgnoreImplicitHDAttributes = */ true) != |
| 8314 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8315 | FailedCandidates.addCandidate().set( |
| 8316 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8317 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8318 | continue; |
| 8319 | } |
| 8320 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8321 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8322 | if (ExplicitTemplateArgs) |
| 8323 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8324 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8325 | } |
| 8326 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8327 | |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8328 | // For a qualified friend declaration (with no explicit marker to indicate |
| 8329 | // that a template specialization was intended), note all (template and |
| 8330 | // non-template) candidates. |
| 8331 | if (QualifiedFriend && Candidates.empty()) { |
| 8332 | Diag(FD->getLocation(), diag::err_qualified_friend_no_match) |
| 8333 | << FD->getDeclName() << FDLookupContext; |
| 8334 | // FIXME: We should form a single candidate list and diagnose all |
| 8335 | // candidates at once, to get proper sorting and limiting. |
| 8336 | for (auto *OldND : Previous) { |
| 8337 | if (auto *OldFD = dyn_cast<FunctionDecl>(OldND->getUnderlyingDecl())) |
| 8338 | NoteOverloadCandidate(OldND, OldFD, FD->getType(), false); |
| 8339 | } |
| 8340 | FailedCandidates.NoteCandidates(*this, FD->getLocation()); |
| 8341 | return true; |
| 8342 | } |
| 8343 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 8344 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8345 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8346 | Candidates.begin(), Candidates.end(), FailedCandidates, FD->getLocation(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8347 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 8348 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8349 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8350 | PDiag(diag::note_function_template_spec_matched)); |
| 8351 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8352 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8353 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8354 | |
| 8355 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 8356 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 8357 | |
| 8358 | FunctionTemplateSpecializationInfo *SpecInfo |
| 8359 | = Specialization->getTemplateSpecializationInfo(); |
| 8360 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8361 | |
| 8362 | // Note: do not overwrite location info if previous template |
| 8363 | // specialization kind was explicit. |
| 8364 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8365 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8366 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8367 | Specialization->setLexicalDeclContext(FD->getLexicalDeclContext()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8368 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 8369 | // function can differ from the template declaration with respect to |
| 8370 | // the constexpr specifier. |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8371 | // FIXME: We need an update record for this AST mutation. |
| 8372 | // FIXME: What if there are multiple such prior declarations (for instance, |
| 8373 | // from different modules)? |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8374 | Specialization->setConstexpr(FD->isConstexpr()); |
| 8375 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8376 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8377 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8378 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8379 | |
| 8380 | // If this is a friend declaration, then we're not really declaring |
| 8381 | // an explicit specialization. |
| 8382 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8383 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8384 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8385 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8386 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8387 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8388 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8389 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8390 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8391 | |
| 8392 | // C++ [temp.expl.spec]p6: |
| 8393 | // 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] | 8394 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8395 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8396 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8397 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8398 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8399 | if (!isFriend && |
| 8400 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8401 | TSK_ExplicitSpecialization, |
| 8402 | Specialization, |
| 8403 | SpecInfo->getTemplateSpecializationKind(), |
| 8404 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8405 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8406 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8407 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8408 | // Mark the prior declaration as an explicit specialization, so that later |
| 8409 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8410 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8411 | // Since explicit specializations do not inherit '=delete' from their |
| 8412 | // primary function template - check if the 'specialization' that was |
| 8413 | // implicitly generated (during template argument deduction for partial |
| 8414 | // ordering) from the most specialized of all the function templates that |
| 8415 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 8416 | // first check that it was implicitly generated during template argument |
| 8417 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 8418 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 8419 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 8420 | !Specialization->getCanonicalDecl()->isReferenced()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8421 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8422 | assert( |
| 8423 | Specialization->getCanonicalDecl() == Specialization && |
| 8424 | "This must be the only existing declaration of this specialization"); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8425 | // FIXME: We need an update record for this AST mutation. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8426 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8427 | } |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8428 | // FIXME: We need an update record for this AST mutation. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8429 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8430 | MarkUnusedFileScopedDecl(Specialization); |
| 8431 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8432 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8433 | // Turn the given function declaration into a function template |
| 8434 | // specialization, with the template arguments from the previous |
| 8435 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8436 | // Take copies of (semantic and syntactic) template argument lists. |
| 8437 | const TemplateArgumentList* TemplArgs = new (Context) |
| 8438 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8439 | FD->setFunctionTemplateSpecialization( |
| 8440 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 8441 | SpecInfo->getTemplateSpecializationKind(), |
| 8442 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8443 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8444 | // A function template specialization inherits the target attributes |
| 8445 | // of its template. (We require the attributes explicitly in the |
| 8446 | // code to match, but a template may have implicit attributes by |
| 8447 | // virtue e.g. of being constexpr, and it passes these implicit |
| 8448 | // attributes on to its specializations.) |
| 8449 | if (LangOpts.CUDA) |
| 8450 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 8451 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8452 | // The "previous declaration" for this function template specialization is |
| 8453 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8454 | Previous.clear(); |
| 8455 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8456 | return false; |
| 8457 | } |
| 8458 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8459 | /// Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8460 | /// specialization. |
| 8461 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8462 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8463 | /// explicit member function specialization. On successful completion, |
| 8464 | /// the function declaration \p FD will become a member function |
| 8465 | /// specialization. |
| 8466 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8467 | /// \param Member the member declaration, which will be updated to become a |
| 8468 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8469 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8470 | /// \param Previous the set of declarations, one of which may be specialized |
| 8471 | /// by this function specialization; the set will be modified to contain the |
| 8472 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8473 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8474 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8475 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8476 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8477 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8478 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8479 | NamedDecl *Instantiation = nullptr; |
| 8480 | NamedDecl *InstantiatedFrom = nullptr; |
| 8481 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8482 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8483 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8484 | // Nowhere to look anyway. |
| 8485 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8486 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8487 | I != E; ++I) { |
| 8488 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 8489 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 8490 | QualType Adjusted = Function->getType(); |
| 8491 | if (!hasExplicitCallingConv(Adjusted)) |
| 8492 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
Richard Smith | 4576a77 | 2018-09-10 06:35:32 +0000 | [diff] [blame] | 8493 | // This doesn't handle deduced return types, but both function |
| 8494 | // declarations should be undeduced at this point. |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 8495 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8496 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8497 | Instantiation = Method; |
| 8498 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8499 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8500 | break; |
| 8501 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8502 | } |
| 8503 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8504 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8505 | VarDecl *PrevVar; |
| 8506 | if (Previous.isSingleResult() && |
| 8507 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8508 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8509 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8510 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8511 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8512 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8513 | } |
| 8514 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8515 | CXXRecordDecl *PrevRecord; |
| 8516 | if (Previous.isSingleResult() && |
| 8517 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8518 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8519 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8520 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8521 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8522 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8523 | } else if (isa<EnumDecl>(Member)) { |
| 8524 | EnumDecl *PrevEnum; |
| 8525 | if (Previous.isSingleResult() && |
| 8526 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8527 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8528 | Instantiation = PrevEnum; |
| 8529 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 8530 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 8531 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8532 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8533 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8534 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8535 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8536 | // specializations are always out-of-line, the caller will complain about |
| 8537 | // this mismatch later. |
| 8538 | return false; |
| 8539 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8540 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8541 | // A member specialization in a friend declaration isn't really declaring |
| 8542 | // an explicit specialization, just identifying a specific (possibly implicit) |
| 8543 | // specialization. Don't change the template specialization kind. |
| 8544 | // |
| 8545 | // FIXME: Is this really valid? Other compilers reject. |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8546 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 8547 | // Preserve instantiation information. |
| 8548 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 8549 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 8550 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 8551 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8552 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 8553 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 8554 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 8555 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8556 | } |
| 8557 | |
| 8558 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8559 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8560 | return false; |
| 8561 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8562 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8563 | // Make sure that this is a specialization of a member. |
| 8564 | if (!InstantiatedFrom) { |
| 8565 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 8566 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8567 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 8568 | return true; |
| 8569 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8570 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8571 | // C++ [temp.expl.spec]p6: |
| 8572 | // 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] | 8573 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8574 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8575 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8576 | // use occurs; no diagnostic is required. |
| 8577 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8578 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8579 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8580 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 8581 | TSK_ExplicitSpecialization, |
| 8582 | Instantiation, |
| 8583 | MSInfo->getTemplateSpecializationKind(), |
| 8584 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8585 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8586 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8587 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8588 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8589 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8590 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8591 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8592 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8593 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 8594 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8595 | // Note that this member specialization is an "instantiation of" the |
| 8596 | // corresponding member of the original template. |
| 8597 | if (auto *MemberFunction = dyn_cast<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8598 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 8599 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 8600 | TSK_ImplicitInstantiation) { |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8601 | // Explicit specializations of member functions of class templates do not |
| 8602 | // inherit '=delete' from the member function they are specializing. |
| 8603 | if (InstantiationFunction->isDeleted()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8604 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8605 | assert(InstantiationFunction->getCanonicalDecl() == |
| 8606 | InstantiationFunction); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8607 | // FIXME: We need an update record for this AST mutation. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 8608 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8609 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8610 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8611 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8612 | MemberFunction->setInstantiationOfMemberFunction( |
| 8613 | cast<CXXMethodDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8614 | } else if (auto *MemberVar = dyn_cast<VarDecl>(Member)) { |
| 8615 | MemberVar->setInstantiationOfStaticDataMember( |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8616 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8617 | } else if (auto *MemberClass = dyn_cast<CXXRecordDecl>(Member)) { |
| 8618 | MemberClass->setInstantiationOfMemberClass( |
| 8619 | cast<CXXRecordDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8620 | } else if (auto *MemberEnum = dyn_cast<EnumDecl>(Member)) { |
| 8621 | MemberEnum->setInstantiationOfMemberEnum( |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8622 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8623 | } else { |
| 8624 | llvm_unreachable("unknown member specialization kind"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8625 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8626 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8627 | // Save the caller the trouble of having to figure out which declaration |
| 8628 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8629 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8630 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8631 | return false; |
| 8632 | } |
| 8633 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8634 | /// Complete the explicit specialization of a member of a class template by |
| 8635 | /// updating the instantiated member to be marked as an explicit specialization. |
| 8636 | /// |
| 8637 | /// \param OrigD The member declaration instantiated from the template. |
| 8638 | /// \param Loc The location of the explicit specialization of the member. |
| 8639 | template<typename DeclT> |
| 8640 | static void completeMemberSpecializationImpl(Sema &S, DeclT *OrigD, |
| 8641 | SourceLocation Loc) { |
| 8642 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 8643 | return; |
| 8644 | |
| 8645 | // FIXME: Inform AST mutation listeners of this AST mutation. |
| 8646 | // FIXME: If there are multiple in-class declarations of the member (from |
| 8647 | // multiple modules, or a declaration and later definition of a member type), |
| 8648 | // should we update all of them? |
| 8649 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
| 8650 | OrigD->setLocation(Loc); |
| 8651 | } |
| 8652 | |
| 8653 | void Sema::CompleteMemberSpecialization(NamedDecl *Member, |
| 8654 | LookupResult &Previous) { |
| 8655 | NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl()); |
| 8656 | if (Instantiation == Member) |
| 8657 | return; |
| 8658 | |
| 8659 | if (auto *Function = dyn_cast<CXXMethodDecl>(Instantiation)) |
| 8660 | completeMemberSpecializationImpl(*this, Function, Member->getLocation()); |
| 8661 | else if (auto *Var = dyn_cast<VarDecl>(Instantiation)) |
| 8662 | completeMemberSpecializationImpl(*this, Var, Member->getLocation()); |
| 8663 | else if (auto *Record = dyn_cast<CXXRecordDecl>(Instantiation)) |
| 8664 | completeMemberSpecializationImpl(*this, Record, Member->getLocation()); |
| 8665 | else if (auto *Enum = dyn_cast<EnumDecl>(Instantiation)) |
| 8666 | completeMemberSpecializationImpl(*this, Enum, Member->getLocation()); |
| 8667 | else |
| 8668 | llvm_unreachable("unknown member specialization kind"); |
| 8669 | } |
| 8670 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8671 | /// Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8672 | /// |
| 8673 | /// \returns true if a serious error occurs, false otherwise. |
| 8674 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8675 | SourceLocation InstLoc, |
| 8676 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8677 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 8678 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8679 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8680 | if (CurContext->isRecord()) { |
| 8681 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 8682 | << D; |
| 8683 | return true; |
| 8684 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8685 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8686 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8687 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8688 | // template. If the name declared in the explicit instantiation is an |
| 8689 | // unqualified name, the explicit instantiation shall appear in the |
| 8690 | // namespace where its template is declared or, if that namespace is inline |
| 8691 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8692 | // |
| 8693 | // 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] | 8694 | if (WasQualifiedName) { |
| 8695 | if (CurContext->Encloses(OrigContext)) |
| 8696 | return false; |
| 8697 | } else { |
| 8698 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 8699 | return false; |
| 8700 | } |
| 8701 | |
| 8702 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 8703 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8704 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8705 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8706 | diag::err_explicit_instantiation_out_of_scope : |
| 8707 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8708 | << D << NS; |
| 8709 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8710 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8711 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8712 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 8713 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 8714 | << D << NS; |
| 8715 | } else |
| 8716 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8717 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8718 | diag::err_explicit_instantiation_must_be_global : |
| 8719 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 8720 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8721 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8722 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8723 | } |
| 8724 | |
Richard Smith | 0d923af | 2019-04-26 01:51:07 +0000 | [diff] [blame] | 8725 | /// Common checks for whether an explicit instantiation of \p D is valid. |
| 8726 | static bool CheckExplicitInstantiation(Sema &S, NamedDecl *D, |
| 8727 | SourceLocation InstLoc, |
| 8728 | bool WasQualifiedName, |
| 8729 | TemplateSpecializationKind TSK) { |
| 8730 | // C++ [temp.explicit]p13: |
| 8731 | // An explicit instantiation declaration shall not name a specialization of |
| 8732 | // a template with internal linkage. |
| 8733 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
| 8734 | D->getFormalLinkage() == InternalLinkage) { |
| 8735 | S.Diag(InstLoc, diag::err_explicit_instantiation_internal_linkage) << D; |
| 8736 | return true; |
| 8737 | } |
| 8738 | |
| 8739 | // C++11 [temp.explicit]p3: [DR 275] |
| 8740 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 8741 | // template. |
| 8742 | if (CheckExplicitInstantiationScope(S, D, InstLoc, WasQualifiedName)) |
| 8743 | return true; |
| 8744 | |
| 8745 | return false; |
| 8746 | } |
| 8747 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8748 | /// Determine whether the given scope specifier has a template-id in it. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8749 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 8750 | if (!SS.isSet()) |
| 8751 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8752 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8753 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8754 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8755 | // or a static data member of a class template specialization, the name of |
| 8756 | // the class template specialization in the qualified-id for the member |
| 8757 | // name shall be a simple-template-id. |
| 8758 | // |
| 8759 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8760 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 8761 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8762 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8763 | if (isa<TemplateSpecializationType>(T)) |
| 8764 | return true; |
| 8765 | |
| 8766 | return false; |
| 8767 | } |
| 8768 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8769 | /// Make a dllexport or dllimport attr on a class template specialization take |
| 8770 | /// effect. |
| 8771 | static void dllExportImportClassTemplateSpecialization( |
| 8772 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 8773 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 8774 | assert(A && "dllExportImportClassTemplateSpecialization called " |
| 8775 | "on Def without dllexport or dllimport"); |
| 8776 | |
| 8777 | // We reject explicit instantiations in class scope, so there should |
| 8778 | // never be any delayed exported classes to worry about. |
| 8779 | assert(S.DelayedDllExportClasses.empty() && |
| 8780 | "delayed exports present at explicit instantiation"); |
| 8781 | S.checkClassLevelDLLAttribute(Def); |
| 8782 | |
| 8783 | // Propagate attribute to base class templates. |
| 8784 | for (auto &B : Def->bases()) { |
| 8785 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 8786 | B.getType()->getAsCXXRecordDecl())) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8787 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getBeginLoc()); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8788 | } |
| 8789 | |
| 8790 | S.referenceDLLExportedClassMethods(); |
| 8791 | } |
| 8792 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8793 | // Explicit instantiation of a class template specialization |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8794 | DeclResult Sema::ActOnExplicitInstantiation( |
| 8795 | Scope *S, SourceLocation ExternLoc, SourceLocation TemplateLoc, |
| 8796 | unsigned TagSpec, SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 8797 | TemplateTy TemplateD, SourceLocation TemplateNameLoc, |
| 8798 | SourceLocation LAngleLoc, ASTTemplateArgsPtr TemplateArgsIn, |
| 8799 | SourceLocation RAngleLoc, const ParsedAttributesView &Attr) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8800 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 8801 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8802 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8803 | // Check that the specialization uses the same tag kind as the |
| 8804 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8805 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 8806 | assert(Kind != TTK_Enum && |
| 8807 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8808 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8809 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 8810 | |
| 8811 | if (!ClassTemplate) { |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 8812 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 8813 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8814 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8815 | return true; |
| 8816 | } |
| 8817 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 8818 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 8819 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 8820 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8821 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8822 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8823 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8824 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8825 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8826 | diag::note_previous_use); |
| 8827 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 8828 | } |
| 8829 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8830 | // C++0x [temp.explicit]p2: |
| 8831 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8832 | // definition and an explicit instantiation declaration. An explicit |
| 8833 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8834 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 8835 | ? TSK_ExplicitInstantiationDefinition |
| 8836 | : TSK_ExplicitInstantiationDeclaration; |
| 8837 | |
Martin Storsjo | 5be69bc | 2019-04-26 08:09:51 +0000 | [diff] [blame] | 8838 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
| 8839 | !Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) { |
| 8840 | // Check for dllexport class template instantiation declarations, |
| 8841 | // except for MinGW mode. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8842 | for (const ParsedAttr &AL : Attr) { |
| 8843 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8844 | Diag(ExternLoc, |
| 8845 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8846 | Diag(AL.getLoc(), diag::note_attribute); |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8847 | break; |
| 8848 | } |
| 8849 | } |
| 8850 | |
| 8851 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 8852 | Diag(ExternLoc, |
| 8853 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8854 | Diag(A->getLocation(), diag::note_attribute); |
| 8855 | } |
| 8856 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8857 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8858 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 8859 | // instantiation declarations for most purposes. |
| 8860 | bool DLLImportExplicitInstantiationDef = false; |
| 8861 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 8862 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 8863 | // Check for dllimport class template instantiation definitions. |
| 8864 | bool DLLImport = |
| 8865 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8866 | for (const ParsedAttr &AL : Attr) { |
| 8867 | if (AL.getKind() == ParsedAttr::AT_DLLImport) |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8868 | DLLImport = true; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8869 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8870 | // dllexport trumps dllimport here. |
| 8871 | DLLImport = false; |
| 8872 | break; |
| 8873 | } |
| 8874 | } |
| 8875 | if (DLLImport) { |
| 8876 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 8877 | DLLImportExplicitInstantiationDef = true; |
| 8878 | } |
| 8879 | } |
| 8880 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8881 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8882 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 8883 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8884 | |
| 8885 | // Check that the template argument list is well-formed for this |
| 8886 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8887 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8888 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 8889 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8890 | return true; |
| 8891 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8892 | // Find the class template specialization declaration that |
| 8893 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8894 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8895 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 8896 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8897 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8898 | TemplateSpecializationKind PrevDecl_TSK |
| 8899 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 8900 | |
Martin Storsjo | 5be69bc | 2019-04-26 08:09:51 +0000 | [diff] [blame] | 8901 | if (TSK == TSK_ExplicitInstantiationDefinition && PrevDecl != nullptr && |
| 8902 | Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) { |
| 8903 | // Check for dllexport class template instantiation definitions in MinGW |
| 8904 | // mode, if a previous declaration of the instantiation was seen. |
| 8905 | for (const ParsedAttr &AL : Attr) { |
| 8906 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
| 8907 | Diag(AL.getLoc(), |
| 8908 | diag::warn_attribute_dllexport_explicit_instantiation_def); |
| 8909 | break; |
| 8910 | } |
| 8911 | } |
| 8912 | } |
| 8913 | |
Richard Smith | 0d923af | 2019-04-26 01:51:07 +0000 | [diff] [blame] | 8914 | if (CheckExplicitInstantiation(*this, ClassTemplate, TemplateNameLoc, |
| 8915 | SS.isSet(), TSK)) |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8916 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8917 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8918 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8919 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8920 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8921 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8922 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8923 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8924 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8925 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8926 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8927 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8928 | // Even though HasNoEffect == true means that this explicit instantiation |
| 8929 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 8930 | |
| 8931 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 8932 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8933 | // Since the only prior class template specialization with these |
| 8934 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8935 | // declaration node as our own, updating the source location |
| 8936 | // for the template name to reflect our new declaration. |
| 8937 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8938 | Specialization = PrevDecl; |
| 8939 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8940 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8941 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8942 | |
| 8943 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 8944 | DLLImportExplicitInstantiationDef) { |
| 8945 | // The new specialization might add a dllimport attribute. |
| 8946 | HasNoEffect = false; |
| 8947 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8948 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8949 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8950 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8951 | // Create a new class template specialization declaration node for |
| 8952 | // this explicit specialization. |
| 8953 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 8954 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8955 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 8956 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8957 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 8958 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8959 | PrevDecl); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 8960 | SetNestedNameSpecifier(*this, Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8961 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8962 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8963 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8964 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8965 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8966 | } |
| 8967 | |
| 8968 | // Build the fully-sugared type for this explicit instantiation as |
| 8969 | // the user wrote in the explicit instantiation itself. This means |
| 8970 | // that we'll pretty-print the type retrieved from the |
| 8971 | // specialization's declaration the way that the user actually wrote |
| 8972 | // the explicit instantiation, rather than formatting the name based |
| 8973 | // on the "canonical" representation used to store the template |
| 8974 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 8975 | TypeSourceInfo *WrittenTy |
| 8976 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 8977 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8978 | Context.getTypeDeclType(Specialization)); |
| 8979 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8980 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8981 | // Set source locations for keywords. |
| 8982 | Specialization->setExternLoc(ExternLoc); |
| 8983 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 8984 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8985 | |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8986 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8987 | ProcessDeclAttributeList(S, Specialization, Attr); |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 8988 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8989 | // Add the explicit instantiation into its lexical context. However, |
| 8990 | // since explicit instantiations are never found by name lookup, we |
| 8991 | // just put it into the declaration context directly. |
| 8992 | Specialization->setLexicalDeclContext(CurContext); |
| 8993 | CurContext->addDecl(Specialization); |
| 8994 | |
| 8995 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 8996 | if (HasNoEffect) { |
| 8997 | // Set the template specialization kind. |
| 8998 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8999 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 9000 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 9001 | |
| 9002 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 9003 | // A definition of a class template or class member template |
| 9004 | // shall be in scope at the point of the explicit instantiation of |
| 9005 | // the class template or class member template. |
| 9006 | // |
| 9007 | // This check comes when we actually try to perform the |
| 9008 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 9009 | ClassTemplateSpecializationDecl *Def |
| 9010 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9011 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 9012 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 9013 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9014 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9015 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9016 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 9017 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9018 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9019 | // Instantiate the members of this class template specialization. |
| 9020 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9021 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 9022 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 9023 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 9024 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 9025 | // TSK_ExplicitInstantiationDefinition |
| 9026 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 9027 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 9028 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 9029 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 9030 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 9031 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 9032 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
Shoaib Meenai | ab3f96c | 2016-11-09 23:52:20 +0000 | [diff] [blame] | 9033 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 9034 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 9035 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 9036 | // attribute to a template with a previous instantiation declaration. |
| 9037 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 9038 | auto *A = cast<InheritableAttr>( |
| 9039 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 9040 | A->setInherited(true); |
| 9041 | Def->addAttr(A); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 9042 | dllExportImportClassTemplateSpecialization(*this, Def); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 9043 | } |
| 9044 | } |
| 9045 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 9046 | // Fix a TSK_ImplicitInstantiation followed by a |
| 9047 | // TSK_ExplicitInstantiationDefinition |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 9048 | bool NewlyDLLExported = |
| 9049 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
| 9050 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 9051 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 9052 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 9053 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 9054 | // attribute to a template with a previous implicit instantiation. |
| 9055 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to |
| 9056 | // avoid potentially strange codegen behavior. For example, if we extend |
| 9057 | // this conditional to dllimport, and we have a source file calling a |
| 9058 | // method on an implicitly instantiated template class instance and then |
| 9059 | // declaring a dllimport explicit instantiation definition for the same |
| 9060 | // template class, the codegen for the method call will not respect the |
| 9061 | // dllimport, while it will with cl. The Def will already have the DLL |
| 9062 | // attribute, since the Def and Specialization will be the same in the |
| 9063 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the |
| 9064 | // attribute to the Specialization; we just need to make it take effect. |
| 9065 | assert(Def == Specialization && |
| 9066 | "Def and Specialization should match for implicit instantiation"); |
| 9067 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 9068 | } |
| 9069 | |
Martin Storsjo | 5be69bc | 2019-04-26 08:09:51 +0000 | [diff] [blame] | 9070 | // In MinGW mode, export the template instantiation if the declaration |
| 9071 | // was marked dllexport. |
| 9072 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 9073 | Context.getTargetInfo().getTriple().isWindowsGNUEnvironment() && |
| 9074 | PrevDecl->hasAttr<DLLExportAttr>()) { |
| 9075 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 9076 | } |
| 9077 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 9078 | // Set the template specialization kind. Make sure it is set before |
| 9079 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 9080 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 9081 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 9082 | } else { |
| 9083 | |
| 9084 | // Set the template specialization kind. |
| 9085 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 9086 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 9087 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9088 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 9089 | } |
| 9090 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9091 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9092 | DeclResult |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9093 | Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation ExternLoc, |
| 9094 | SourceLocation TemplateLoc, unsigned TagSpec, |
| 9095 | SourceLocation KWLoc, CXXScopeSpec &SS, |
| 9096 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 9097 | const ParsedAttributesView &Attr) { |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9098 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 9099 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9100 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9101 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9102 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 9103 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9104 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 9105 | SourceLocation(), false, TypeResult(), |
Akira Hatanaka | 12ddcee | 2017-06-26 18:46:12 +0000 | [diff] [blame] | 9106 | /*IsTypeSpecifier*/false, |
| 9107 | /*IsTemplateParamOrArg*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9108 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 9109 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9110 | if (!TagD) |
| 9111 | return true; |
| 9112 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9113 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 9114 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9115 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 9116 | if (Tag->isInvalidDecl()) |
| 9117 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9118 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9119 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 9120 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 9121 | if (!Pattern) { |
| 9122 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 9123 | << Context.getTypeDeclType(Record); |
| 9124 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 9125 | return true; |
| 9126 | } |
| 9127 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9128 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9129 | // If the explicit instantiation is for a class or member class, the |
| 9130 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9131 | // simple-template-id. |
| 9132 | // |
| 9133 | // C++98 has the same restriction, just worded differently. |
| 9134 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9135 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9136 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9137 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9138 | // C++0x [temp.explicit]p2: |
| 9139 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9140 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9141 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 9142 | TemplateSpecializationKind TSK |
| 9143 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 9144 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9145 | |
Richard Smith | 0d923af | 2019-04-26 01:51:07 +0000 | [diff] [blame] | 9146 | CheckExplicitInstantiation(*this, Record, NameLoc, true, TSK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9147 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9148 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9149 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9150 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9151 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 9152 | PrevDecl = Record; |
| 9153 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9154 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9155 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9156 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9157 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9158 | PrevDecl, |
| 9159 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9160 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9161 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9162 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9163 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9164 | return TagD; |
| 9165 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9166 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 9167 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9168 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 9169 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 9170 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9171 | // 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] | 9172 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9173 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9174 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 9175 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 9176 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 9177 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 9178 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 9179 | << Pattern; |
| 9180 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9181 | } else { |
| 9182 | if (InstantiateClass(NameLoc, Record, Def, |
| 9183 | getTemplateInstantiationArgs(Record), |
| 9184 | TSK)) |
| 9185 | return true; |
| 9186 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9187 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9188 | if (!RecordDef) |
| 9189 | return true; |
| 9190 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9191 | } |
| 9192 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9193 | // Instantiate all of the members of the class. |
| 9194 | InstantiateClassMembers(NameLoc, RecordDef, |
| 9195 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9196 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9197 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9198 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 9199 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 9200 | // FIXME: We don't have any representation for explicit instantiations of |
| 9201 | // member classes. Such a representation is not needed for compilation, but it |
| 9202 | // should be available for clients that want to see all of the declarations in |
| 9203 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9204 | return TagD; |
| 9205 | } |
| 9206 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9207 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 9208 | SourceLocation ExternLoc, |
| 9209 | SourceLocation TemplateLoc, |
| 9210 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9211 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9212 | // TODO: check if/when DNInfo should replace Name. |
| 9213 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 9214 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9215 | if (!Name) { |
| 9216 | if (!D.isInvalidType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9217 | Diag(D.getDeclSpec().getBeginLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9218 | diag::err_explicit_instantiation_requires_name) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9219 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9220 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9221 | return true; |
| 9222 | } |
| 9223 | |
| 9224 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 9225 | // we find one that is. |
| 9226 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 9227 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 9228 | S = S->getParent(); |
| 9229 | |
| 9230 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9231 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 9232 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9233 | if (R.isNull()) |
| 9234 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9235 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9236 | // C++ [dcl.stc]p1: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9237 | // A storage-class-specifier shall not be specified in [...] an explicit |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9238 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9239 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9240 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 9241 | << Name; |
| 9242 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9243 | } else if (D.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9244 | != DeclSpec::SCS_unspecified) { |
| 9245 | // Complain about then remove the storage class specifier. |
| 9246 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 9247 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9248 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9249 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9250 | } |
| 9251 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 9252 | // C++0x [temp.explicit]p1: |
| 9253 | // [...] An explicit instantiation of a function template shall not use the |
| 9254 | // inline or constexpr specifiers. |
| 9255 | // Presumably, this also applies to member functions of class templates as |
| 9256 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9257 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9258 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9259 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9260 | diag::err_explicit_instantiation_inline : |
| 9261 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9262 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9263 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9264 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 9265 | // not already specified. |
| 9266 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 9267 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9268 | |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 9269 | // A deduction guide is not on the list of entities that can be explicitly |
| 9270 | // instantiated. |
| 9271 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9272 | Diag(D.getDeclSpec().getBeginLoc(), diag::err_deduction_guide_specialized) |
| 9273 | << /*explicit instantiation*/ 0; |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 9274 | return true; |
| 9275 | } |
| 9276 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9277 | // C++0x [temp.explicit]p2: |
| 9278 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9279 | // definition and an explicit instantiation declaration. An explicit |
| 9280 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9281 | TemplateSpecializationKind TSK |
| 9282 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 9283 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9284 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9285 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9286 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9287 | |
| 9288 | if (!R->isFunctionType()) { |
| 9289 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9290 | // A [...] static data member of a class template can be explicitly |
| 9291 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9292 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9293 | // C++1y [temp.explicit]p1: |
| 9294 | // A [...] variable [...] template specialization can be explicitly |
| 9295 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9296 | if (Previous.isAmbiguous()) |
| 9297 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9298 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 9299 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9300 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9301 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9302 | if (!PrevTemplate) { |
| 9303 | if (!Prev || !Prev->isStaticDataMember()) { |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 9304 | // We expect to see a static data member here. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9305 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 9306 | << Name; |
| 9307 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9308 | P != PEnd; ++P) |
| 9309 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 9310 | return true; |
| 9311 | } |
| 9312 | |
| 9313 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 9314 | // FIXME: Check for explicit specialization? |
| 9315 | Diag(D.getIdentifierLoc(), |
| 9316 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 9317 | << Prev; |
| 9318 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 9319 | // FIXME: Can we provide a note showing where this was declared? |
| 9320 | return true; |
| 9321 | } |
| 9322 | } else { |
| 9323 | // Explicitly instantiate a variable template. |
| 9324 | |
| 9325 | // C++1y [dcl.spec.auto]p6: |
| 9326 | // ... A program that uses auto or decltype(auto) in a context not |
| 9327 | // explicitly allowed in this section is ill-formed. |
| 9328 | // |
| 9329 | // This includes auto-typed variable template instantiations. |
| 9330 | if (R->isUndeducedType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9331 | Diag(T->getTypeLoc().getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9332 | diag::err_auto_not_allowed_var_inst); |
| 9333 | return true; |
| 9334 | } |
| 9335 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9336 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9337 | // C++1y [temp.explicit]p3: |
| 9338 | // If the explicit instantiation is for a variable, the unqualified-id |
| 9339 | // in the declaration shall be a template-id. |
| 9340 | Diag(D.getIdentifierLoc(), |
| 9341 | diag::err_explicit_instantiation_without_template_id) |
| 9342 | << PrevTemplate; |
| 9343 | Diag(PrevTemplate->getLocation(), |
| 9344 | diag::note_explicit_instantiation_here); |
| 9345 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9346 | } |
| 9347 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9348 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9349 | TemplateArgumentListInfo TemplateArgs = |
| 9350 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9351 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9352 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 9353 | D.getIdentifierLoc(), TemplateArgs); |
| 9354 | if (Res.isInvalid()) |
| 9355 | return true; |
| 9356 | |
| 9357 | // Ignore access control bits, we don't need them for redeclaration |
| 9358 | // checking. |
| 9359 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9360 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9361 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9362 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9363 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9364 | // or a static data member of a class template specialization, the name of |
| 9365 | // the class template specialization in the qualified-id for the member |
| 9366 | // name shall be a simple-template-id. |
| 9367 | // |
| 9368 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9369 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 9370 | // This does not apply to variable template specializations, where the |
| 9371 | // template-id is in the unqualified-id instead. |
| 9372 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9373 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9374 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9375 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9376 | |
Richard Smith | 0d923af | 2019-04-26 01:51:07 +0000 | [diff] [blame] | 9377 | CheckExplicitInstantiation(*this, Prev, D.getIdentifierLoc(), true, TSK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9378 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9379 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 9380 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 9381 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9382 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9383 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9384 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9385 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9386 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9387 | if (!HasNoEffect) { |
| 9388 | // Instantiate static data member or variable template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9389 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Louis Dionne | e6e8175 | 2018-10-10 15:32:29 +0000 | [diff] [blame] | 9390 | // Merge attributes. |
| 9391 | ProcessDeclAttributeList(S, Prev, D.getDeclSpec().getAttributes()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9392 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9393 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 9394 | } |
| 9395 | |
| 9396 | // Check the new variable specialization against the parsed input. |
| 9397 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9398 | Diag(T->getTypeLoc().getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9399 | diag::err_invalid_var_template_spec_type) |
| 9400 | << 0 << PrevTemplate << R << Prev->getType(); |
| 9401 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 9402 | << 2 << PrevTemplate->getDeclName(); |
| 9403 | return true; |
| 9404 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9405 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9406 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9407 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9408 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9409 | |
| 9410 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 9411 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9412 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9413 | TemplateArgumentListInfo TemplateArgs; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9414 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9415 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9416 | HasExplicitTemplateArgs = true; |
| 9417 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9418 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9419 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9420 | // A [...] function [...] can be explicitly instantiated from its template. |
| 9421 | // A member function [...] of a class template can be explicitly |
| 9422 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9423 | // template. |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9424 | UnresolvedSet<8> TemplateMatches; |
| 9425 | FunctionDecl *NonTemplateMatch = nullptr; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9426 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9427 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9428 | P != PEnd; ++P) { |
| 9429 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9430 | if (!HasExplicitTemplateArgs) { |
| 9431 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 9432 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 9433 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 9434 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9435 | if (Method->getPrimaryTemplate()) { |
| 9436 | TemplateMatches.addDecl(Method, P.getAccess()); |
| 9437 | } else { |
| 9438 | // FIXME: Can this assert ever happen? Needs a test. |
| 9439 | assert(!NonTemplateMatch && "Multiple NonTemplateMatches"); |
| 9440 | NonTemplateMatch = Method; |
| 9441 | } |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9442 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9443 | } |
| 9444 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9445 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9446 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 9447 | if (!FunTmpl) |
| 9448 | continue; |
| 9449 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9450 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9451 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9452 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9453 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9454 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 9455 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9456 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9457 | // Keep track of almost-matches. |
| 9458 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9459 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9460 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9461 | (void)TDK; |
| 9462 | continue; |
| 9463 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9464 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 9465 | // Target attributes are part of the cuda function signature, so |
| 9466 | // the cuda target of the instantiated function must match that of its |
| 9467 | // template. Given that C++ template deduction does not take |
| 9468 | // target attributes into account, we reject candidates here that |
| 9469 | // have a different target. |
| 9470 | if (LangOpts.CUDA && |
| 9471 | IdentifyCUDATarget(Specialization, |
| 9472 | /* IgnoreImplicitHDAttributes = */ true) != |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9473 | IdentifyCUDATarget(D.getDeclSpec().getAttributes())) { |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 9474 | FailedCandidates.addCandidate().set( |
| 9475 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 9476 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 9477 | continue; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 9478 | } |
| 9479 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9480 | TemplateMatches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9481 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9482 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9483 | FunctionDecl *Specialization = NonTemplateMatch; |
| 9484 | if (!Specialization) { |
| 9485 | // Find the most specialized function template specialization. |
| 9486 | UnresolvedSetIterator Result = getMostSpecialized( |
| 9487 | TemplateMatches.begin(), TemplateMatches.end(), FailedCandidates, |
| 9488 | D.getIdentifierLoc(), |
| 9489 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 9490 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 9491 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9492 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9493 | if (Result == TemplateMatches.end()) |
| 9494 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9495 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9496 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 9497 | Specialization = cast<FunctionDecl>(*Result); |
| 9498 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9499 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 9500 | // C++11 [except.spec]p4 |
| 9501 | // In an explicit instantiation an exception-specification may be specified, |
| 9502 | // but is not required. |
| 9503 | // If an exception-specification is specified in an explicit instantiation |
| 9504 | // directive, it shall be compatible with the exception-specifications of |
| 9505 | // other declarations of that function. |
| 9506 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 9507 | if (FPT->hasExceptionSpec()) { |
| 9508 | unsigned DiagID = |
| 9509 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 9510 | if (getLangOpts().MicrosoftExt) |
| 9511 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 9512 | bool Result = CheckEquivalentExceptionSpec( |
| 9513 | PDiag(DiagID) << Specialization->getType(), |
| 9514 | PDiag(diag::note_explicit_instantiation_here), |
| 9515 | Specialization->getType()->getAs<FunctionProtoType>(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9516 | Specialization->getLocation(), FPT, D.getBeginLoc()); |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 9517 | // In Microsoft mode, mismatching exception specifications just cause a |
| 9518 | // warning. |
| 9519 | if (!getLangOpts().MicrosoftExt && Result) |
| 9520 | return true; |
| 9521 | } |
| 9522 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9523 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9524 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9525 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 9526 | << Specialization |
| 9527 | << (Specialization->getTemplateSpecializationKind() == |
| 9528 | TSK_ExplicitSpecialization); |
| 9529 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 9530 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9533 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 9534 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 9535 | PrevDecl = Specialization; |
| 9536 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9537 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9538 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9539 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9540 | PrevDecl, |
| 9541 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9542 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9543 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9544 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9545 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9546 | // FIXME: We may still want to build some representation of this |
| 9547 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9548 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9549 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9550 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 9551 | |
Richard Smith | 0d923af | 2019-04-26 01:51:07 +0000 | [diff] [blame] | 9552 | // HACK: libc++ has a bug where it attempts to explicitly instantiate the |
| 9553 | // functions |
| 9554 | // valarray<size_t>::valarray(size_t) and |
| 9555 | // valarray<size_t>::~valarray() |
| 9556 | // that it declared to have internal linkage with the internal_linkage |
| 9557 | // attribute. Ignore the explicit instantiation declaration in this case. |
| 9558 | if (Specialization->hasAttr<InternalLinkageAttr>() && |
| 9559 | TSK == TSK_ExplicitInstantiationDeclaration) { |
| 9560 | if (auto *RD = dyn_cast<CXXRecordDecl>(Specialization->getDeclContext())) |
| 9561 | if (RD->getIdentifier() && RD->getIdentifier()->isStr("valarray") && |
| 9562 | RD->isInStdNamespace()) |
| 9563 | return (Decl*) nullptr; |
| 9564 | } |
| 9565 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9566 | ProcessDeclAttributeList(S, Specialization, D.getDeclSpec().getAttributes()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9567 | |
Hans Wennborg | b8304a6 | 2017-11-29 23:44:11 +0000 | [diff] [blame] | 9568 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 9569 | // instantiation declarations. |
| 9570 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 9571 | Specialization->hasAttr<DLLImportAttr>() && |
| 9572 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 9573 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 9574 | |
| 9575 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 9576 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 9577 | if (Specialization->isDefined()) { |
| 9578 | // Let the ASTConsumer know that this function has been explicitly |
| 9579 | // instantiated now, and its linkage might have changed. |
| 9580 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 9581 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 9582 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9583 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9584 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9585 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9586 | // or a static data member of a class template specialization, the name of |
| 9587 | // the class template specialization in the qualified-id for the member |
| 9588 | // name shall be a simple-template-id. |
| 9589 | // |
| 9590 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9591 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9592 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9593 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9594 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9595 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9596 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9597 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9598 | |
Richard Smith | 0d923af | 2019-04-26 01:51:07 +0000 | [diff] [blame] | 9599 | CheckExplicitInstantiation( |
| 9600 | *this, |
| 9601 | FunTmpl ? (NamedDecl *)FunTmpl |
| 9602 | : Specialization->getInstantiatedFromMemberFunction(), |
| 9603 | D.getIdentifierLoc(), D.getCXXScopeSpec().isSet(), TSK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9604 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9605 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9606 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9607 | } |
| 9608 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9609 | TypeResult |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 9610 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9611 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 9612 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 9613 | // This has to hold, because SS is expected to be defined. |
| 9614 | assert(Name && "Expected a name in a dependent tag"); |
| 9615 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9616 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9617 | if (!NNS) |
| 9618 | return true; |
| 9619 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9620 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 9621 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9622 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 9623 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9624 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9625 | return true; |
| 9626 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9627 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9628 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9629 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9630 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9631 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9632 | // Create type-source location information for this type. |
| 9633 | TypeLocBuilder TLB; |
| 9634 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9635 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9636 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 9637 | TL.setNameLoc(NameLoc); |
| 9638 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9639 | } |
| 9640 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9641 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9642 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 9643 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 9644 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9645 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9646 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9647 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9648 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9649 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9650 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9651 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9652 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9653 | << FixItHint::CreateRemoval(TypenameLoc); |
| 9654 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9655 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9656 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 9657 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 9658 | if (T.isNull()) |
| 9659 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9660 | |
| 9661 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9662 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9663 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9664 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9665 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9666 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9667 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9668 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9669 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9670 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9671 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9672 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9673 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9674 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9675 | } |
| 9676 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9677 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9678 | Sema::ActOnTypenameType(Scope *S, |
| 9679 | SourceLocation TypenameLoc, |
| 9680 | const CXXScopeSpec &SS, |
| 9681 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9682 | TemplateTy TemplateIn, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9683 | IdentifierInfo *TemplateII, |
| 9684 | SourceLocation TemplateIILoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9685 | SourceLocation LAngleLoc, |
| 9686 | ASTTemplateArgsPtr TemplateArgsIn, |
| 9687 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9688 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9689 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9690 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9691 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9692 | diag::ext_typename_outside_of_template) |
| 9693 | << FixItHint::CreateRemoval(TypenameLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9694 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9695 | // Strangely, non-type results are not ignored by this lookup, so the |
| 9696 | // program is ill-formed if it finds an injected-class-name. |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 9697 | if (TypenameLoc.isValid()) { |
| 9698 | auto *LookupRD = |
| 9699 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
| 9700 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 9701 | Diag(TemplateIILoc, |
| 9702 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9703 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 9704 | << (TemplateKWLoc.isValid() ? 1 : 0 /*'template'/'typename' keyword*/); |
| 9705 | } |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9706 | } |
| 9707 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9708 | // Translate the parser's template argument list in our AST format. |
| 9709 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 9710 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9711 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9712 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9713 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 9714 | // Construct a dependent template specialization type. |
| 9715 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9716 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9717 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 9718 | DTN->getQualifier(), |
| 9719 | DTN->getIdentifier(), |
| 9720 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9721 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9722 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9723 | TypeLocBuilder Builder; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9724 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9725 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9726 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 9727 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 9728 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9729 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9730 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9731 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9732 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9733 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9734 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 9735 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9736 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9737 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9738 | if (T.isNull()) |
| 9739 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9740 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9741 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9742 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9743 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9744 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9745 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9746 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9747 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9748 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9749 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9750 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9751 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9752 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 9753 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9754 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9755 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9756 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9757 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 9758 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 9759 | } |
| 9760 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9761 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9762 | /// Determine whether this failed name lookup should be treated as being |
| 9763 | /// disabled by a usage of std::enable_if. |
| 9764 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9765 | SourceRange &CondRange, Expr *&Cond) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9766 | // We must be looking for a ::type... |
| 9767 | if (!II.isStr("type")) |
| 9768 | return false; |
| 9769 | |
| 9770 | // ... within an explicitly-written template specialization... |
| 9771 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 9772 | return false; |
| 9773 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9774 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 9775 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 9776 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9777 | return false; |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 9778 | const TemplateSpecializationType *EnableIfTST = EnableIfTSTLoc.getTypePtr(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9779 | |
| 9780 | // ... which names a complete class template declaration... |
| 9781 | const TemplateDecl *EnableIfDecl = |
| 9782 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 9783 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 9784 | return false; |
| 9785 | |
| 9786 | // ... called "enable_if". |
| 9787 | const IdentifierInfo *EnableIfII = |
| 9788 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 9789 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 9790 | return false; |
| 9791 | |
| 9792 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9793 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9794 | |
| 9795 | // Dig out the condition. |
| 9796 | Cond = nullptr; |
| 9797 | if (EnableIfTSTLoc.getArgLoc(0).getArgument().getKind() |
| 9798 | != TemplateArgument::Expression) |
| 9799 | return true; |
| 9800 | |
| 9801 | Cond = EnableIfTSTLoc.getArgLoc(0).getSourceExpression(); |
| 9802 | |
| 9803 | // Ignore Boolean literals; they add no value. |
| 9804 | if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts())) |
| 9805 | Cond = nullptr; |
| 9806 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9807 | return true; |
| 9808 | } |
| 9809 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9810 | /// Build the type that describes a C++ typename specifier, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9811 | /// e.g., "typename T::type". |
| 9812 | QualType |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9813 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9814 | SourceLocation KeywordLoc, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9815 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9816 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9817 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9818 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9819 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9820 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9821 | DeclContext *Ctx = computeDeclContext(SS); |
| 9822 | if (!Ctx) { |
| 9823 | // If the nested-name-specifier is dependent and couldn't be |
| 9824 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9825 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9826 | return Context.getDependentNameType(Keyword, |
| 9827 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9828 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 9829 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9830 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9831 | // If the nested-name-specifier refers to the current instantiation, |
| 9832 | // the "typename" keyword itself is superfluous. In C++03, the |
| 9833 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 9834 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 9835 | // 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] | 9836 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9837 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 9838 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9839 | |
| 9840 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9841 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 9842 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9843 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9844 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9845 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9846 | case LookupResult::NotFound: { |
| 9847 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 9848 | // a more specific diagnostic. |
| 9849 | SourceRange CondRange; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9850 | Expr *Cond = nullptr; |
| 9851 | if (isEnableIf(QualifierLoc, II, CondRange, Cond)) { |
| 9852 | // If we have a condition, narrow it down to the specific failed |
| 9853 | // condition. |
| 9854 | if (Cond) { |
| 9855 | Expr *FailedCond; |
| 9856 | std::string FailedDescription; |
| 9857 | std::tie(FailedCond, FailedDescription) = |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 9858 | findFailedBooleanCondition(Cond); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9859 | |
| 9860 | Diag(FailedCond->getExprLoc(), |
| 9861 | diag::err_typename_nested_not_found_requirement) |
| 9862 | << FailedDescription |
| 9863 | << FailedCond->getSourceRange(); |
| 9864 | return QualType(); |
| 9865 | } |
| 9866 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9867 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9868 | << Ctx << CondRange; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9869 | return QualType(); |
| 9870 | } |
| 9871 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 9872 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9873 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9874 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9875 | |
| 9876 | case LookupResult::FoundUnresolvedValue: { |
| 9877 | // We found a using declaration that is a value. Most likely, the using |
| 9878 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9879 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9880 | IILoc); |
| 9881 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 9882 | << Name << Ctx << FullRange; |
| 9883 | if (UnresolvedUsingValueDecl *Using |
| 9884 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 9885 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9886 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 9887 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 9888 | } |
| 9889 | } |
| 9890 | // Fall through to create a dependent typename type, from which we can recover |
| 9891 | // better. |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 9892 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9893 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 9894 | case LookupResult::NotFoundInCurrentInstantiation: |
| 9895 | // Okay, it's a member of an unknown instantiation. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9896 | return Context.getDependentNameType(Keyword, |
| 9897 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9898 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9899 | |
| 9900 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9901 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9902 | // C++ [class.qual]p2: |
| 9903 | // In a lookup in which function names are not ignored and the |
| 9904 | // nested-name-specifier nominates a class C, if the name specified |
| 9905 | // after the nested-name-specifier, when looked up in C, is the |
| 9906 | // injected-class-name of C [...] then the name is instead considered |
| 9907 | // to name the constructor of class C. |
| 9908 | // |
| 9909 | // Unlike in an elaborated-type-specifier, function names are not ignored |
| 9910 | // in typename-specifier lookup. However, they are ignored in all the |
| 9911 | // contexts where we form a typename type with no keyword (that is, in |
| 9912 | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). |
| 9913 | // |
| 9914 | // FIXME: That's not strictly true: mem-initializer-id lookup does not |
| 9915 | // ignore functions, but that appears to be an oversight. |
| 9916 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
| 9917 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
| 9918 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
| 9919 | FoundRD->isInjectedClassName() && |
| 9920 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
| 9921 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9922 | << &II << 1 << 0 /*'typename' keyword used*/; |
| 9923 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9924 | // We found a type. Build an ElaboratedType, since the |
| 9925 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 9926 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9927 | return Context.getElaboratedType(Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9928 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9929 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9930 | } |
| 9931 | |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9932 | // C++ [dcl.type.simple]p2: |
| 9933 | // A type-specifier of the form |
| 9934 | // typename[opt] nested-name-specifier[opt] template-name |
| 9935 | // is a placeholder for a deduced class type [...]. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 9936 | if (getLangOpts().CPlusPlus17) { |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9937 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
| 9938 | return Context.getElaboratedType( |
| 9939 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
| 9940 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
| 9941 | QualType(), false)); |
| 9942 | } |
| 9943 | } |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 9944 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9945 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 9946 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9947 | break; |
| 9948 | |
| 9949 | case LookupResult::FoundOverloaded: |
| 9950 | DiagID = diag::err_typename_nested_not_type; |
| 9951 | Referenced = *Result.begin(); |
| 9952 | break; |
| 9953 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 9954 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9955 | return QualType(); |
| 9956 | } |
| 9957 | |
| 9958 | // If we get here, it's because name lookup did not find a |
| 9959 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9960 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9961 | IILoc); |
| 9962 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9963 | if (Referenced) |
| 9964 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 9965 | << Name; |
| 9966 | return QualType(); |
| 9967 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9968 | |
| 9969 | namespace { |
| 9970 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 9971 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9972 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9973 | SourceLocation Loc; |
| 9974 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9975 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9976 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 9977 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9978 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9979 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9980 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9981 | DeclarationName Entity) |
| 9982 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9983 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9984 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9985 | /// Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9986 | /// transformed. |
| 9987 | /// |
| 9988 | /// For the purposes of type reconstruction, a type has already been |
| 9989 | /// transformed if it is NULL or if it is not dependent. |
| 9990 | bool AlreadyTransformed(QualType T) { |
| 9991 | return T.isNull() || !T->isDependentType(); |
| 9992 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9993 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9994 | /// Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9995 | /// rebuilt. |
| 9996 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9997 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9998 | /// Returns the name of the entity whose type is being rebuilt. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9999 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10000 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10001 | /// Sets the "base" location and entity when that |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 10002 | /// information is known based on another transformation. |
| 10003 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 10004 | this->Loc = Loc; |
| 10005 | this->Entity = Entity; |
| 10006 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10007 | |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 10008 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 10009 | // Lambdas never need to be transformed. |
| 10010 | return E; |
| 10011 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 10012 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 10013 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 10014 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10015 | /// Rebuilds a type within the context of the current instantiation. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 10016 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10017 | /// 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] | 10018 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10019 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 10020 | /// partial specialization thereof). This routine will rebuild that type now |
| 10021 | /// that we have entered the declarator's scope, which may produce different |
| 10022 | /// canonical types, e.g., |
| 10023 | /// |
| 10024 | /// \code |
| 10025 | /// template<typename T> |
| 10026 | /// struct X { |
| 10027 | /// typedef T* pointer; |
| 10028 | /// pointer data(); |
| 10029 | /// }; |
| 10030 | /// |
| 10031 | /// template<typename T> |
| 10032 | /// typename X<T>::pointer X<T>::data() { ... } |
| 10033 | /// \endcode |
| 10034 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 10035 | /// 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] | 10036 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 10037 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 10038 | /// 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] | 10039 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 10040 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 10041 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 10042 | SourceLocation Loc, |
| 10043 | DeclarationName Name) { |
| 10044 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 10045 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10046 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 10047 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 10048 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 10049 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10050 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10051 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 10052 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 10053 | DeclarationName()); |
| 10054 | return Rebuilder.TransformExpr(E); |
| 10055 | } |
| 10056 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 10057 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10058 | if (SS.isInvalid()) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 10059 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 10060 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 10061 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 10062 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 10063 | DeclarationName()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10064 | NestedNameSpecifierLoc Rebuilt |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 10065 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10066 | if (!Rebuilt) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 10067 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 10068 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 10069 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 10070 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 10071 | } |
| 10072 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10073 | /// 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] | 10074 | /// instantiation. |
| 10075 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 10076 | TemplateParameterList *Params) { |
| 10077 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 10078 | Decl *Param = Params->getParam(I); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10079 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 10080 | // There is nothing to rebuild in a type parameter. |
| 10081 | if (isa<TemplateTypeParmDecl>(Param)) |
| 10082 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10083 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 10084 | // Rebuild the template parameter list of a template template parameter. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10085 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 10086 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 10087 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 10088 | TTP->getTemplateParameters())) |
| 10089 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10090 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 10091 | continue; |
| 10092 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10093 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 10094 | // Rebuild the type of a non-type template parameter. |
| 10095 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10096 | TypeSourceInfo *NewTSI |
| 10097 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 10098 | NTTP->getLocation(), |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 10099 | NTTP->getDeclName()); |
| 10100 | if (!NewTSI) |
| 10101 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10102 | |
Erik Pilkington | 9f9462a | 2018-08-07 22:59:02 +0000 | [diff] [blame] | 10103 | if (NewTSI->getType()->isUndeducedType()) { |
| 10104 | // C++17 [temp.dep.expr]p3: |
| 10105 | // An id-expression is type-dependent if it contains |
| 10106 | // - an identifier associated by name lookup with a non-type |
| 10107 | // template-parameter declared with a type that contains a |
| 10108 | // placeholder type (7.1.7.4), |
| 10109 | NewTSI = SubstAutoTypeSourceInfo(NewTSI, Context.DependentTy); |
| 10110 | } |
| 10111 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 10112 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 10113 | NTTP->setTypeSourceInfo(NewTSI); |
| 10114 | NTTP->setType(NewTSI->getType()); |
| 10115 | } |
| 10116 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 10117 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 10118 | return false; |
| 10119 | } |
| 10120 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10121 | /// Produces a formatted string that describes the binding of |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10122 | /// template parameters to template arguments. |
| 10123 | std::string |
| 10124 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 10125 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 10126 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 10127 | } |
| 10128 | |
| 10129 | std::string |
| 10130 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 10131 | const TemplateArgument *Args, |
| 10132 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 10133 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 10134 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10135 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 10136 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 10137 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10138 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10139 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 10140 | if (I >= NumArgs) |
| 10141 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10142 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10143 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 10144 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10145 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 10146 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10147 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10148 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 10149 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10150 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 10151 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10152 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10153 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 10154 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 10155 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10156 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 10157 | |
| 10158 | Out << ']'; |
| 10159 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 10160 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 10161 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 10162 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 10163 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 10164 | if (!FD) |
| 10165 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 10166 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 10167 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 10168 | |
| 10169 | // Take tokens to avoid allocations |
| 10170 | LPT->Toks.swap(Toks); |
| 10171 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 10172 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 10173 | |
| 10174 | FD->setLateTemplateParsed(true); |
| 10175 | } |
| 10176 | |
| 10177 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 10178 | if (!FD) |
| 10179 | return; |
| 10180 | FD->setLateTemplateParsed(false); |
| 10181 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 10182 | |
| 10183 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 10184 | DeclContext *DC = CurContext; |
| 10185 | |
| 10186 | while (DC) { |
| 10187 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 10188 | const FunctionDecl *FD = RD->isLocalClass(); |
| 10189 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 10190 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 10191 | return false; |
| 10192 | |
| 10193 | DC = DC->getParent(); |
| 10194 | } |
| 10195 | return false; |
| 10196 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10197 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 10198 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10199 | /// Walk the path from which a declaration was instantiated, and check |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10200 | /// that every explicit specialization along that path is visible. This enforces |
| 10201 | /// C++ [temp.expl.spec]/6: |
| 10202 | /// |
| 10203 | /// If a template, a member template or a member of a class template is |
| 10204 | /// explicitly specialized then that specialization shall be declared before |
| 10205 | /// the first use of that specialization that would cause an implicit |
| 10206 | /// instantiation to take place, in every translation unit in which such a |
| 10207 | /// use occurs; no diagnostic is required. |
| 10208 | /// |
| 10209 | /// and also C++ [temp.class.spec]/1: |
| 10210 | /// |
| 10211 | /// A partial specialization shall be declared before the first use of a |
| 10212 | /// class template specialization that would make use of the partial |
| 10213 | /// specialization as the result of an implicit or explicit instantiation |
| 10214 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 10215 | /// required. |
| 10216 | class ExplicitSpecializationVisibilityChecker { |
| 10217 | Sema &S; |
| 10218 | SourceLocation Loc; |
| 10219 | llvm::SmallVector<Module *, 8> Modules; |
| 10220 | |
| 10221 | public: |
| 10222 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 10223 | : S(S), Loc(Loc) {} |
| 10224 | |
| 10225 | void check(NamedDecl *ND) { |
| 10226 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 10227 | return checkImpl(FD); |
| 10228 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 10229 | return checkImpl(RD); |
| 10230 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 10231 | return checkImpl(VD); |
| 10232 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 10233 | return checkImpl(ED); |
| 10234 | } |
| 10235 | |
| 10236 | private: |
| 10237 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 10238 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 10239 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 10240 | const bool Recover = true; |
| 10241 | |
| 10242 | // If we got a custom set of modules (because only a subset of the |
| 10243 | // declarations are interesting), use them, otherwise let |
| 10244 | // diagnoseMissingImport intelligently pick some. |
| 10245 | if (Modules.empty()) |
| 10246 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 10247 | else |
| 10248 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 10249 | } |
| 10250 | |
| 10251 | // Check a specific declaration. There are three problematic cases: |
| 10252 | // |
| 10253 | // 1) The declaration is an explicit specialization of a template |
| 10254 | // specialization. |
| 10255 | // 2) The declaration is an explicit specialization of a member of an |
| 10256 | // templated class. |
| 10257 | // 3) The declaration is an instantiation of a template, and that template |
| 10258 | // is an explicit specialization of a member of a templated class. |
| 10259 | // |
| 10260 | // We don't need to go any deeper than that, as the instantiation of the |
| 10261 | // surrounding class / etc is not triggered by whatever triggered this |
| 10262 | // instantiation, and thus should be checked elsewhere. |
| 10263 | template<typename SpecDecl> |
| 10264 | void checkImpl(SpecDecl *Spec) { |
| 10265 | bool IsHiddenExplicitSpecialization = false; |
| 10266 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 10267 | IsHiddenExplicitSpecialization = |
| 10268 | Spec->getMemberSpecializationInfo() |
| 10269 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 10270 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10271 | } else { |
| 10272 | checkInstantiated(Spec); |
| 10273 | } |
| 10274 | |
| 10275 | if (IsHiddenExplicitSpecialization) |
| 10276 | diagnose(Spec->getMostRecentDecl(), false); |
| 10277 | } |
| 10278 | |
| 10279 | void checkInstantiated(FunctionDecl *FD) { |
| 10280 | if (auto *TD = FD->getPrimaryTemplate()) |
| 10281 | checkTemplate(TD); |
| 10282 | } |
| 10283 | |
| 10284 | void checkInstantiated(CXXRecordDecl *RD) { |
| 10285 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 10286 | if (!SD) |
| 10287 | return; |
| 10288 | |
| 10289 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10290 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 10291 | checkTemplate(TD); |
| 10292 | else if (auto *TD = |
| 10293 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 10294 | if (!S.hasVisibleDeclaration(TD)) |
| 10295 | diagnose(TD, true); |
| 10296 | checkTemplate(TD); |
| 10297 | } |
| 10298 | } |
| 10299 | |
| 10300 | void checkInstantiated(VarDecl *RD) { |
| 10301 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 10302 | if (!SD) |
| 10303 | return; |
| 10304 | |
| 10305 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10306 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 10307 | checkTemplate(TD); |
| 10308 | else if (auto *TD = |
| 10309 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 10310 | if (!S.hasVisibleDeclaration(TD)) |
| 10311 | diagnose(TD, true); |
| 10312 | checkTemplate(TD); |
| 10313 | } |
| 10314 | } |
| 10315 | |
| 10316 | void checkInstantiated(EnumDecl *FD) {} |
| 10317 | |
| 10318 | template<typename TemplDecl> |
| 10319 | void checkTemplate(TemplDecl *TD) { |
| 10320 | if (TD->isMemberSpecialization()) { |
| 10321 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 10322 | diagnose(TD->getMostRecentDecl(), false); |
| 10323 | } |
| 10324 | } |
| 10325 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 10326 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10327 | |
| 10328 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 10329 | if (!getLangOpts().Modules) |
| 10330 | return; |
| 10331 | |
| 10332 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 10333 | } |
| 10334 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10335 | /// Check whether a template partial specialization that we've discovered |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10336 | /// is hidden, and produce suitable diagnostics if so. |
| 10337 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 10338 | NamedDecl *Spec) { |
| 10339 | llvm::SmallVector<Module *, 8> Modules; |
| 10340 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 10341 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 10342 | MissingImportKind::PartialSpecialization, |
| 10343 | /*Recover*/true); |
| 10344 | } |