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, |
| 69 | /// returns NULL. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 70 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 71 | NamedDecl *Orig, |
| 72 | bool AllowFunctionTemplates) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 73 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 75 | if (isa<TemplateDecl>(D)) { |
| 76 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 77 | return nullptr; |
| 78 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 79 | return Orig; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 80 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 82 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 83 | // C++ [temp.local]p1: |
| 84 | // Like normal (non-template) classes, class templates have an |
| 85 | // injected-class-name (Clause 9). The injected-class-name |
| 86 | // can be used with or without a template-argument-list. When |
| 87 | // it is used without a template-argument-list, it is |
| 88 | // equivalent to the injected-class-name followed by the |
| 89 | // template-parameters of the class template enclosed in |
| 90 | // <>. When it is used with a template-argument-list, it |
| 91 | // refers to the specified class template specialization, |
| 92 | // which could be the current specialization or another |
| 93 | // specialization. |
| 94 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 95 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 96 | if (Record->getDescribedClassTemplate()) |
| 97 | return Record->getDescribedClassTemplate(); |
| 98 | |
| 99 | if (ClassTemplateSpecializationDecl *Spec |
| 100 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 101 | return Spec->getSpecializedTemplate(); |
| 102 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 104 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 105 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 107 | // 'using Dependent::foo;' can resolve to a template name. |
| 108 | // 'using typename Dependent::foo;' cannot (not even if 'foo' is an |
| 109 | // injected-class-name). |
| 110 | if (isa<UnresolvedUsingValueDecl>(D)) |
| 111 | return D; |
| 112 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 113 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 116 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 117 | bool AllowFunctionTemplates) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 118 | // The set of class templates we've already seen. |
| 119 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 120 | LookupResult::Filter filter = R.makeFilter(); |
| 121 | while (filter.hasNext()) { |
| 122 | NamedDecl *Orig = filter.next(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 123 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 124 | AllowFunctionTemplates); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 125 | if (!Repl) |
| 126 | filter.erase(); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 127 | else if (Repl != Orig) { |
| 128 | |
| 129 | // C++ [temp.local]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 130 | // A lookup that finds an injected-class-name (10.2) can result in an |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 131 | // ambiguity in certain cases (for example, if it is found in more than |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 132 | // one base class). If all of the injected-class-names that are found |
| 133 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 134 | // is used as a template-name, the reference refers to the class |
| 135 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 136 | // ambiguous. |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 137 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 138 | if (!ClassTemplates.insert(ClassTmpl).second) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 139 | filter.erase(); |
| 140 | continue; |
| 141 | } |
John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 142 | |
| 143 | // FIXME: we promote access to public here as a workaround to |
| 144 | // the fact that LookupResult doesn't let us remember that we |
| 145 | // found this template through a particular injected class name, |
| 146 | // which means we end up doing nasty things to the invariants. |
| 147 | // Pretending that access is public is *much* safer. |
| 148 | filter.replace(Repl, AS_public); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 149 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 150 | } |
| 151 | filter.done(); |
| 152 | } |
| 153 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 154 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 155 | bool AllowFunctionTemplates) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 156 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 157 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 158 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 160 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 163 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 164 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 165 | bool hasTemplateKeyword, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 166 | const UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 167 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 168 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 169 | TemplateTy &TemplateResult, |
| 170 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 171 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 172 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 173 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 174 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 176 | switch (Name.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 177 | case UnqualifiedIdKind::IK_Identifier: |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 178 | TName = DeclarationName(Name.Identifier); |
| 179 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 180 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 181 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 182 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 183 | Name.OperatorFunctionId.Operator); |
| 184 | break; |
| 185 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 186 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 187 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 188 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 189 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 190 | default: |
| 191 | return TNK_Non_template; |
| 192 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 194 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 196 | LookupResult R(*this, TName, Name.getBeginLoc(), LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 197 | if (LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 198 | MemberOfUnknownSpecialization)) |
| 199 | return TNK_Non_template; |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 200 | if (R.empty()) return TNK_Non_template; |
| 201 | if (R.isAmbiguous()) { |
| 202 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 203 | R.suppressDiagnostics(); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 204 | |
| 205 | // FIXME: we might have ambiguous templates, in which case we |
| 206 | // should at least parse them properly! |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 207 | return TNK_Non_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 208 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 209 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 210 | TemplateName Template; |
| 211 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 213 | unsigned ResultCount = R.end() - R.begin(); |
| 214 | if (ResultCount > 1) { |
| 215 | // We assume that we'll preserve the qualifier from a function |
| 216 | // template name in other ways. |
| 217 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 218 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 219 | |
| 220 | // We'll do this lookup again later. |
| 221 | R.suppressDiagnostics(); |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 222 | } else if (isa<UnresolvedUsingValueDecl>((*R.begin())->getUnderlyingDecl())) { |
| 223 | // We don't yet know whether this is a template-name or not. |
| 224 | MemberOfUnknownSpecialization = true; |
| 225 | return TNK_Non_template; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 226 | } else { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 227 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 228 | |
| 229 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 230 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 231 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 232 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 233 | } else { |
| 234 | Template = TemplateName(TD); |
| 235 | } |
| 236 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 237 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 238 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 239 | |
| 240 | // We'll do this lookup again later. |
| 241 | R.suppressDiagnostics(); |
| 242 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 243 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 244 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 245 | isa<BuiltinTemplateDecl>(TD)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 246 | TemplateKind = |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 247 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 248 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 249 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 251 | TemplateResult = TemplateTy::make(Template); |
| 252 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Richard Smith | 278890f | 2017-02-10 20:39:58 +0000 | [diff] [blame] | 255 | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, |
| 256 | SourceLocation NameLoc, |
| 257 | ParsedTemplateTy *Template) { |
| 258 | CXXScopeSpec SS; |
| 259 | bool MemberOfUnknownSpecialization = false; |
| 260 | |
| 261 | // We could use redeclaration lookup here, but we don't need to: the |
| 262 | // syntactic form of a deduction guide is enough to identify it even |
| 263 | // if we can't look up the template name at all. |
| 264 | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 265 | if (LookupTemplateName(R, S, SS, /*ObjectType*/ QualType(), |
| 266 | /*EnteringContext*/ false, |
| 267 | MemberOfUnknownSpecialization)) |
| 268 | return false; |
Richard Smith | 278890f | 2017-02-10 20:39:58 +0000 | [diff] [blame] | 269 | |
| 270 | if (R.empty()) return false; |
| 271 | if (R.isAmbiguous()) { |
| 272 | // FIXME: Diagnose an ambiguity if we find at least one template. |
| 273 | R.suppressDiagnostics(); |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | // We only treat template-names that name type templates as valid deduction |
| 278 | // guide names. |
| 279 | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); |
| 280 | if (!TD || !getAsTypeTemplateDecl(TD)) |
| 281 | return false; |
| 282 | |
| 283 | if (Template) |
| 284 | *Template = TemplateTy::make(TemplateName(TD)); |
| 285 | return true; |
| 286 | } |
| 287 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 288 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 289 | SourceLocation IILoc, |
| 290 | Scope *S, |
| 291 | const CXXScopeSpec *SS, |
| 292 | TemplateTy &SuggestedTemplate, |
| 293 | TemplateNameKind &SuggestedKind) { |
| 294 | // We can't recover unless there's a dependent scope specifier preceding the |
| 295 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 296 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 297 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 298 | computeDeclContext(*SS)) |
| 299 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 300 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 301 | // The code is missing a 'template' keyword prior to the dependent template |
| 302 | // name. |
| 303 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 304 | Diag(IILoc, diag::err_template_kw_missing) |
| 305 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 306 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 307 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 308 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 309 | SuggestedKind = TNK_Dependent_template_name; |
| 310 | return true; |
| 311 | } |
| 312 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 313 | bool Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 314 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 315 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 316 | bool EnteringContext, |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 317 | bool &MemberOfUnknownSpecialization, |
| 318 | SourceLocation TemplateKWLoc) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 319 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 320 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 321 | DeclContext *LookupCtx = nullptr; |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 322 | bool IsDependent = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 323 | if (!ObjectType.isNull()) { |
| 324 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 325 | // x->B::f, and we are looking into the type of the object. |
| 326 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 327 | LookupCtx = computeDeclContext(ObjectType); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 328 | IsDependent = !LookupCtx; |
| 329 | assert((IsDependent || !ObjectType->isIncompleteType() || |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 330 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 331 | "Caller should have completed object type"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 332 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 333 | // Template names cannot appear inside an Objective-C class or object type. |
| 334 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 335 | Found.clear(); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 336 | return false; |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 337 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 338 | } else if (SS.isSet()) { |
| 339 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 340 | // so long into the context associated with the prior nested-name-specifier. |
| 341 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 342 | IsDependent = !LookupCtx; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 343 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 344 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 345 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 346 | return true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 350 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 351 | if (LookupCtx) { |
| 352 | // Perform "qualified" name lookup into the declaration context we |
| 353 | // computed, which is either the type of the base of a member access |
| 354 | // expression or the declaration context associated with a prior |
| 355 | // nested-name-specifier. |
| 356 | LookupQualifiedName(Found, LookupCtx); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 357 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 358 | // FIXME: The C++ standard does not clearly specify what happens in the |
| 359 | // case where the object type is dependent, and implementations vary. In |
| 360 | // Clang, we treat a name after a . or -> as a template-name if lookup |
| 361 | // finds a non-dependent member or member of the current instantiation that |
| 362 | // is a type template, or finds no such members and lookup in the context |
| 363 | // of the postfix-expression finds a type template. In the latter case, the |
| 364 | // name is nonetheless dependent, and we may resolve it to a member of an |
| 365 | // unknown specialization when we come to instantiate the template. |
| 366 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 369 | if (!SS.isSet() && (ObjectType.isNull() || Found.empty())) { |
| 370 | // C++ [basic.lookup.classref]p1: |
| 371 | // In a class member access expression (5.2.5), if the . or -> token is |
| 372 | // immediately followed by an identifier followed by a <, the |
| 373 | // identifier must be looked up to determine whether the < is the |
| 374 | // beginning of a template argument list (14.2) or a less-than operator. |
| 375 | // The identifier is first looked up in the class of the object |
| 376 | // expression. If the identifier is not found, it is then looked up in |
| 377 | // the context of the entire postfix-expression and shall name a class |
| 378 | // template. |
| 379 | if (S) |
| 380 | LookupName(Found, S); |
| 381 | |
| 382 | if (!ObjectType.isNull()) { |
| 383 | // FIXME: We should filter out all non-type templates here, particularly |
| 384 | // variable templates and concepts. But the exclusion of alias templates |
| 385 | // and template template parameters is a wording defect. |
| 386 | AllowFunctionTemplatesInLookup = false; |
| 387 | ObjectTypeSearchedInScope = true; |
| 388 | } |
| 389 | |
| 390 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
| 391 | } |
| 392 | |
| 393 | if (Found.empty() && !IsDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 394 | // If we did not find any names, attempt to correct any typos. |
| 395 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 396 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 397 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 398 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 399 | FilterCCC->WantTypeSpecifiers = false; |
| 400 | FilterCCC->WantExpressionKeywords = false; |
| 401 | FilterCCC->WantRemainingKeywords = false; |
| 402 | FilterCCC->WantCXXNamedCasts = true; |
| 403 | if (TypoCorrection Corrected = CorrectTypo( |
| 404 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 405 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 406 | Found.setLookupName(Corrected.getCorrection()); |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 407 | if (auto *ND = Corrected.getFoundDecl()) |
| 408 | Found.addDecl(ND); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 409 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 410 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 411 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 412 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 413 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 414 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 415 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 416 | << Name << LookupCtx << DroppedSpecifier |
| 417 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 418 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 419 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 420 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 421 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 422 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 423 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 427 | NamedDecl *ExampleLookupResult = |
| 428 | Found.empty() ? nullptr : Found.getRepresentativeDecl(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 429 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 430 | if (Found.empty()) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 431 | if (IsDependent) { |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 432 | MemberOfUnknownSpecialization = true; |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
| 436 | // If a 'template' keyword was used, a lookup that finds only non-template |
| 437 | // names is an error. |
| 438 | if (ExampleLookupResult && TemplateKWLoc.isValid()) { |
| 439 | Diag(Found.getNameLoc(), diag::err_template_kw_refers_to_non_template) |
| 440 | << Found.getLookupName() << SS.getRange(); |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 441 | Diag(ExampleLookupResult->getUnderlyingDecl()->getLocation(), |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 442 | diag::note_template_kw_refers_to_non_template) |
| 443 | << Found.getLookupName(); |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | return false; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 448 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 449 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 450 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 451 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 452 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 453 | // [...] If the lookup in the class of the object expression finds a |
| 454 | // template, the name is also looked up in the context of the entire |
| 455 | // postfix-expression and [...] |
| 456 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 457 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 458 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 459 | LookupOrdinaryName); |
| 460 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 461 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 462 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 463 | if (FoundOuter.empty()) { |
| 464 | // - if the name is not found, the name found in the class of the |
| 465 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 466 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 467 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 468 | // - if the name is found in the context of the entire |
| 469 | // postfix-expression and does not name a class template, the name |
| 470 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 471 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 472 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 473 | // - if the name found is a class template, it must refer to the same |
| 474 | // entity as the one found in the class of the object expression, |
| 475 | // otherwise the program is ill-formed. |
| 476 | if (!Found.isSingleResult() || |
| 477 | Found.getFoundDecl()->getCanonicalDecl() |
| 478 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 479 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 480 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 481 | << Found.getLookupName() |
| 482 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 483 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 484 | diag::note_ambig_member_ref_object_type) |
| 485 | << ObjectType; |
| 486 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 487 | diag::note_ambig_member_ref_scope); |
| 488 | |
| 489 | // Recover by taking the template that we found in the object |
| 490 | // expression's type. |
| 491 | } |
| 492 | } |
| 493 | } |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 494 | |
| 495 | return false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 498 | void Sema::diagnoseExprIntendedAsTemplateName(Scope *S, ExprResult TemplateName, |
| 499 | SourceLocation Less, |
| 500 | SourceLocation Greater) { |
| 501 | if (TemplateName.isInvalid()) |
| 502 | return; |
| 503 | |
| 504 | DeclarationNameInfo NameInfo; |
| 505 | CXXScopeSpec SS; |
| 506 | LookupNameKind LookupKind; |
| 507 | |
| 508 | DeclContext *LookupCtx = nullptr; |
| 509 | NamedDecl *Found = nullptr; |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 510 | bool MissingTemplateKeyword = false; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 511 | |
| 512 | // Figure out what name we looked up. |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 513 | if (auto *DRE = dyn_cast<DeclRefExpr>(TemplateName.get())) { |
| 514 | NameInfo = DRE->getNameInfo(); |
| 515 | SS.Adopt(DRE->getQualifierLoc()); |
| 516 | LookupKind = LookupOrdinaryName; |
| 517 | Found = DRE->getFoundDecl(); |
| 518 | } else if (auto *ME = dyn_cast<MemberExpr>(TemplateName.get())) { |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 519 | NameInfo = ME->getMemberNameInfo(); |
| 520 | SS.Adopt(ME->getQualifierLoc()); |
| 521 | LookupKind = LookupMemberName; |
| 522 | LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl(); |
| 523 | Found = ME->getMemberDecl(); |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 524 | } else if (auto *DSDRE = |
| 525 | dyn_cast<DependentScopeDeclRefExpr>(TemplateName.get())) { |
| 526 | NameInfo = DSDRE->getNameInfo(); |
| 527 | SS.Adopt(DSDRE->getQualifierLoc()); |
| 528 | MissingTemplateKeyword = true; |
| 529 | } else if (auto *DSME = |
| 530 | dyn_cast<CXXDependentScopeMemberExpr>(TemplateName.get())) { |
| 531 | NameInfo = DSME->getMemberNameInfo(); |
| 532 | SS.Adopt(DSME->getQualifierLoc()); |
| 533 | MissingTemplateKeyword = true; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 534 | } else { |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 535 | llvm_unreachable("unexpected kind of potential template name"); |
| 536 | } |
| 537 | |
| 538 | // If this is a dependent-scope lookup, diagnose that the 'template' keyword |
| 539 | // was missing. |
| 540 | if (MissingTemplateKeyword) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 541 | Diag(NameInfo.getBeginLoc(), diag::err_template_kw_missing) |
| 542 | << "" << NameInfo.getName().getAsString() << SourceRange(Less, Greater); |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 543 | return; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // Try to correct the name by looking for templates and C++ named casts. |
| 547 | struct TemplateCandidateFilter : CorrectionCandidateCallback { |
| 548 | TemplateCandidateFilter() { |
| 549 | WantTypeSpecifiers = false; |
| 550 | WantExpressionKeywords = false; |
| 551 | WantRemainingKeywords = false; |
| 552 | WantCXXNamedCasts = true; |
| 553 | }; |
| 554 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| 555 | if (auto *ND = Candidate.getCorrectionDecl()) |
| 556 | return isAcceptableTemplateName(ND->getASTContext(), ND, true); |
| 557 | return Candidate.isKeyword(); |
| 558 | } |
| 559 | }; |
| 560 | |
| 561 | DeclarationName Name = NameInfo.getName(); |
| 562 | if (TypoCorrection Corrected = |
| 563 | CorrectTypo(NameInfo, LookupKind, S, &SS, |
| 564 | llvm::make_unique<TemplateCandidateFilter>(), |
| 565 | CTK_ErrorRecovery, LookupCtx)) { |
| 566 | auto *ND = Corrected.getFoundDecl(); |
| 567 | if (ND) |
| 568 | ND = isAcceptableTemplateName(Context, ND, |
| 569 | /*AllowFunctionTemplates*/ true); |
| 570 | if (ND || Corrected.isKeyword()) { |
| 571 | if (LookupCtx) { |
| 572 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 573 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| 574 | Name.getAsString() == CorrectedStr; |
| 575 | diagnoseTypo(Corrected, |
| 576 | PDiag(diag::err_non_template_in_member_template_id_suggest) |
| 577 | << Name << LookupCtx << DroppedSpecifier |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 578 | << SS.getRange(), false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 579 | } else { |
| 580 | diagnoseTypo(Corrected, |
| 581 | PDiag(diag::err_non_template_in_template_id_suggest) |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 582 | << Name, false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 583 | } |
| 584 | if (Found) |
| 585 | Diag(Found->getLocation(), |
| 586 | diag::note_non_template_in_template_id_found); |
| 587 | return; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) |
| 592 | << Name << SourceRange(Less, Greater); |
| 593 | if (Found) |
| 594 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); |
| 595 | } |
| 596 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 597 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 598 | /// was just parsed. This is only possible with an explicit scope |
| 599 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 600 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 601 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 602 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 603 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 604 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 605 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 606 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 608 | // C++11 [expr.prim.general]p12: |
| 609 | // An id-expression that denotes a non-static data member or non-static |
| 610 | // member function of a class can only be used: |
| 611 | // (...) |
| 612 | // - if that id-expression denotes a non-static data member and it |
| 613 | // appears in an unevaluated operand. |
| 614 | // |
| 615 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 616 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 617 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 618 | // always a MemberExpr. |
| 619 | bool MightBeCxx11UnevalField = |
| 620 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 621 | |
Akira Hatanaka | d644e02 | 2016-12-16 03:19:41 +0000 | [diff] [blame] | 622 | // Check if the nested name specifier is an enum type. |
| 623 | bool IsEnum = false; |
| 624 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
| 625 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
| 626 | |
| 627 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 628 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 629 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 630 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 631 | // Since the 'this' expression is synthesized, we don't need to |
| 632 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 633 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 634 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 635 | return CXXDependentScopeMemberExpr::Create( |
| 636 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 637 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 638 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 641 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 642 | } |
| 643 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 644 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 645 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 646 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 647 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 648 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 649 | return DependentScopeDeclRefExpr::Create( |
| 650 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 651 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 654 | |
| 655 | /// Determine whether we would be unable to instantiate this template (because |
| 656 | /// it either has no definition, or is in the process of being instantiated). |
| 657 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 658 | NamedDecl *Instantiation, |
| 659 | bool InstantiatedFromMember, |
| 660 | const NamedDecl *Pattern, |
| 661 | const NamedDecl *PatternDef, |
| 662 | TemplateSpecializationKind TSK, |
| 663 | bool Complain /*= true*/) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 664 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 665 | isa<VarDecl>(Instantiation)); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 666 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 667 | bool IsEntityBeingDefined = false; |
| 668 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 669 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 670 | |
| 671 | if (PatternDef && !IsEntityBeingDefined) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 672 | NamedDecl *SuggestedDef = nullptr; |
| 673 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 674 | /*OnlyNeedComplete*/false)) { |
| 675 | // If we're allowed to diagnose this and recover, do so. |
| 676 | bool Recover = Complain && !isSFINAEContext(); |
| 677 | if (Complain) |
| 678 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 679 | Sema::MissingImportKind::Definition, Recover); |
| 680 | return !Recover; |
| 681 | } |
| 682 | return false; |
| 683 | } |
| 684 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 685 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 686 | return true; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 687 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 688 | llvm::Optional<unsigned> Note; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 689 | QualType InstantiationTy; |
| 690 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 691 | InstantiationTy = Context.getTypeDeclType(TD); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 692 | if (PatternDef) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 693 | Diag(PointOfInstantiation, |
| 694 | diag::err_template_instantiate_within_definition) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 695 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 696 | << InstantiationTy; |
| 697 | // Not much point in noting the template declaration here, since |
| 698 | // we're lexically inside it. |
| 699 | Instantiation->setInvalidDecl(); |
| 700 | } else if (InstantiatedFromMember) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 701 | if (isa<FunctionDecl>(Instantiation)) { |
| 702 | Diag(PointOfInstantiation, |
| 703 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 704 | << /*member function*/ 1 << Instantiation->getDeclName() |
| 705 | << Instantiation->getDeclContext(); |
| 706 | Note = diag::note_explicit_instantiation_here; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 707 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 708 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 709 | Diag(PointOfInstantiation, |
| 710 | diag::err_implicit_instantiate_member_undefined) |
| 711 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 712 | Note = diag::note_member_declared_at; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 713 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 714 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 715 | if (isa<FunctionDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 716 | Diag(PointOfInstantiation, |
| 717 | diag::err_explicit_instantiation_undefined_func_template) |
| 718 | << Pattern; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 719 | Note = diag::note_explicit_instantiation_here; |
| 720 | } else if (isa<TagDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 721 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 722 | << (TSK != TSK_ImplicitInstantiation) |
| 723 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 724 | Note = diag::note_template_decl_here; |
| 725 | } else { |
| 726 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 727 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 728 | Diag(PointOfInstantiation, |
| 729 | diag::err_explicit_instantiation_undefined_var_template) |
| 730 | << Instantiation; |
| 731 | Instantiation->setInvalidDecl(); |
| 732 | } else |
| 733 | Diag(PointOfInstantiation, |
| 734 | diag::err_explicit_instantiation_undefined_member) |
| 735 | << /*static data member*/ 2 << Instantiation->getDeclName() |
| 736 | << Instantiation->getDeclContext(); |
| 737 | Note = diag::note_explicit_instantiation_here; |
| 738 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 739 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 740 | if (Note) // Diagnostics were emitted. |
| 741 | Diag(Pattern->getLocation(), Note.getValue()); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 742 | |
| 743 | // In general, Instantiation isn't marked invalid to get more than one |
| 744 | // error for multiple undefined instantiations. But the code that does |
| 745 | // explicit declaration -> explicit definition conversion can't handle |
| 746 | // invalid declarations, so mark as invalid in that case. |
| 747 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 748 | Instantiation->setInvalidDecl(); |
| 749 | return true; |
| 750 | } |
| 751 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 752 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 753 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 754 | /// declaration at location Loc. Returns true to indicate that this is |
| 755 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 756 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 757 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 758 | |
| 759 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 760 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 761 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 762 | |
| 763 | // C++ [temp.local]p4: |
| 764 | // A template-parameter shall not be redeclared within its |
| 765 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 767 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 768 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 771 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 772 | /// the parameter D to reference the templated declaration and return a pointer |
| 773 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 774 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 775 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 776 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 777 | return Temp; |
| 778 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 779 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 782 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 783 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 784 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 785 | "Only template template arguments can be pack expansions here"); |
| 786 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 787 | "Template template argument pack expansion without packs"); |
| 788 | ParsedTemplateArgument Result(*this); |
| 789 | Result.EllipsisLoc = EllipsisLoc; |
| 790 | return Result; |
| 791 | } |
| 792 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 793 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 794 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 796 | switch (Arg.getKind()) { |
| 797 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 798 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 799 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 800 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 801 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 802 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 803 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 804 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 805 | case ParsedTemplateArgument::NonType: { |
| 806 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 807 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 808 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 809 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 810 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 811 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 812 | TemplateArgument TArg; |
| 813 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 814 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 815 | else |
| 816 | TArg = Template; |
| 817 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 818 | Arg.getScopeSpec().getWithLocInContext( |
| 819 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 820 | Arg.getLocation(), |
| 821 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 824 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 825 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 826 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 827 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 828 | /// Translates template arguments as provided by the parser |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 829 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 830 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 831 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 832 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 833 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 834 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 835 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 836 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 837 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 838 | SourceLocation Loc, |
| 839 | IdentifierInfo *Name) { |
| 840 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 841 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 842 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 843 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 844 | } |
| 845 | |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 846 | /// Convert a parsed type into a parsed template argument. This is mostly |
| 847 | /// trivial, except that we may have parsed a C++17 deduced class template |
| 848 | /// specialization type, in which case we should form a template template |
| 849 | /// argument instead of a type template argument. |
| 850 | ParsedTemplateArgument Sema::ActOnTemplateTypeArgument(TypeResult ParsedType) { |
| 851 | TypeSourceInfo *TInfo; |
| 852 | QualType T = GetTypeFromParser(ParsedType.get(), &TInfo); |
| 853 | if (T.isNull()) |
| 854 | return ParsedTemplateArgument(); |
| 855 | assert(TInfo && "template argument with no location"); |
| 856 | |
| 857 | // If we might have formed a deduced template specialization type, convert |
| 858 | // it to a template template argument. |
| 859 | if (getLangOpts().CPlusPlus17) { |
| 860 | TypeLoc TL = TInfo->getTypeLoc(); |
| 861 | SourceLocation EllipsisLoc; |
| 862 | if (auto PET = TL.getAs<PackExpansionTypeLoc>()) { |
| 863 | EllipsisLoc = PET.getEllipsisLoc(); |
| 864 | TL = PET.getPatternLoc(); |
| 865 | } |
| 866 | |
| 867 | CXXScopeSpec SS; |
| 868 | if (auto ET = TL.getAs<ElaboratedTypeLoc>()) { |
| 869 | SS.Adopt(ET.getQualifierLoc()); |
| 870 | TL = ET.getNamedTypeLoc(); |
| 871 | } |
| 872 | |
| 873 | if (auto DTST = TL.getAs<DeducedTemplateSpecializationTypeLoc>()) { |
| 874 | TemplateName Name = DTST.getTypePtr()->getTemplateName(); |
| 875 | if (SS.isSet()) |
| 876 | Name = Context.getQualifiedTemplateName(SS.getScopeRep(), |
| 877 | /*HasTemplateKeyword*/ false, |
| 878 | Name.getAsTemplateDecl()); |
| 879 | ParsedTemplateArgument Result(SS, TemplateTy::make(Name), |
| 880 | DTST.getTemplateNameLoc()); |
| 881 | if (EllipsisLoc.isValid()) |
| 882 | Result = Result.getTemplatePackExpansion(EllipsisLoc); |
| 883 | return Result; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // This is a normal type template argument. Note, if the type template |
| 888 | // 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] | 889 | // 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] | 890 | // convertTypeTemplateArgumentToTemplate. |
| 891 | return ParsedTemplateArgument(ParsedTemplateArgument::Type, |
| 892 | ParsedType.get().getAsOpaquePtr(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 893 | TInfo->getTypeLoc().getBeginLoc()); |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 896 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 897 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 898 | /// the keyword "typename" was used to declare the type parameter |
| 899 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 900 | /// "class" or "typename" keyword. ParamName is the name of the |
| 901 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 902 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 903 | /// If the type parameter has a default argument, it will be added |
| 904 | /// later via ActOnTypeParameterDefault. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 905 | NamedDecl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 906 | SourceLocation EllipsisLoc, |
| 907 | SourceLocation KeyLoc, |
| 908 | IdentifierInfo *ParamName, |
| 909 | SourceLocation ParamNameLoc, |
| 910 | unsigned Depth, unsigned Position, |
| 911 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 912 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | assert(S->isTemplateParamScope() && |
| 914 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 915 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 916 | SourceLocation Loc = ParamNameLoc; |
| 917 | if (!ParamName) |
| 918 | Loc = KeyLoc; |
| 919 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 920 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 921 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 922 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 923 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 924 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 925 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 926 | |
| 927 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 928 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 929 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 930 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 931 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 932 | IdResolver.AddDecl(Param); |
| 933 | } |
| 934 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 935 | // C++0x [temp.param]p9: |
| 936 | // A default template-argument may be specified for any kind of |
| 937 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 938 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 939 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 940 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 943 | // Handle the default argument, if provided. |
| 944 | if (DefaultArg) { |
| 945 | TypeSourceInfo *DefaultTInfo; |
| 946 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 947 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 948 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 949 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 950 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 951 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 952 | UPPC_DefaultArgument)) |
| 953 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 955 | // Check the template argument itself. |
| 956 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 957 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 958 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 959 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 960 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 961 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 962 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 963 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 964 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 967 | /// Check that the type of a non-type template parameter is |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 968 | /// well-formed. |
| 969 | /// |
| 970 | /// \returns the (possibly-promoted) parameter type if valid; |
| 971 | /// otherwise, produces a diagnostic and returns a NULL type. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 972 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
| 973 | SourceLocation Loc) { |
| 974 | if (TSI->getType()->isUndeducedType()) { |
Erik Pilkington | 9f9462a | 2018-08-07 22:59:02 +0000 | [diff] [blame] | 975 | // C++17 [temp.dep.expr]p3: |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 976 | // An id-expression is type-dependent if it contains |
| 977 | // - an identifier associated by name lookup with a non-type |
| 978 | // template-parameter declared with a type that contains a |
| 979 | // placeholder type (7.1.7.4), |
| 980 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
| 981 | } |
| 982 | |
| 983 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
| 984 | } |
| 985 | |
| 986 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
| 987 | SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 988 | // We don't allow variably-modified types as the type of non-type template |
| 989 | // parameters. |
| 990 | if (T->isVariablyModifiedType()) { |
| 991 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 992 | << T; |
| 993 | return QualType(); |
| 994 | } |
| 995 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 996 | // C++ [temp.param]p4: |
| 997 | // |
| 998 | // A non-type template-parameter shall have one of the following |
| 999 | // (optionally cv-qualified) types: |
| 1000 | // |
| 1001 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1002 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1004 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1006 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1007 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1008 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1009 | // -- std::nullptr_t. |
| 1010 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1011 | // If T is a dependent type, we can't do the check now, so we |
| 1012 | // assume that it is well-formed. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1013 | T->isDependentType() || |
| 1014 | // Allow use of auto in template parameter declarations. |
| 1015 | T->isUndeducedType()) { |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 1016 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 1017 | // are ignored when determining its type. |
| 1018 | return T.getUnqualifiedType(); |
| 1019 | } |
| 1020 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1021 | // C++ [temp.param]p8: |
| 1022 | // |
| 1023 | // A non-type template-parameter of type "array of T" or |
| 1024 | // "function returning T" is adjusted to be of type "pointer to |
| 1025 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1026 | else if (T->isArrayType() || T->isFunctionType()) |
| 1027 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1028 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1029 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 1030 | << T; |
| 1031 | |
| 1032 | return QualType(); |
| 1033 | } |
| 1034 | |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1035 | NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1036 | unsigned Depth, |
| 1037 | unsigned Position, |
| 1038 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1039 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1040 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1041 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1042 | // Check that we have valid decl-specifiers specified. |
| 1043 | auto CheckValidDeclSpecifiers = [this, &D] { |
| 1044 | // C++ [temp.param] |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1045 | // p1 |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 1046 | // template-parameter: |
| 1047 | // ... |
| 1048 | // parameter-declaration |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1049 | // p2 |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1050 | // ... A storage class shall not be specified in a template-parameter |
| 1051 | // declaration. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1052 | // [dcl.typedef]p1: |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1053 | // The typedef specifier [...] shall not be used in the decl-specifier-seq |
| 1054 | // of a parameter-declaration |
| 1055 | const DeclSpec &DS = D.getDeclSpec(); |
| 1056 | auto EmitDiag = [this](SourceLocation Loc) { |
| 1057 | Diag(Loc, diag::err_invalid_decl_specifier_in_nontype_parm) |
| 1058 | << FixItHint::CreateRemoval(Loc); |
| 1059 | }; |
| 1060 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) |
| 1061 | EmitDiag(DS.getStorageClassSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1062 | |
Sam McCall | 1371cba | 2017-12-22 07:09:51 +0000 | [diff] [blame] | 1063 | if (DS.getThreadStorageClassSpec() != TSCS_unspecified) |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1064 | EmitDiag(DS.getThreadStorageClassSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1065 | |
| 1066 | // [dcl.inline]p1: |
| 1067 | // The inline specifier can be applied only to the declaration or |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1068 | // definition of a variable or function. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1069 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1070 | if (DS.isInlineSpecified()) |
| 1071 | EmitDiag(DS.getInlineSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1072 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1073 | // [dcl.constexpr]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1074 | // The constexpr specifier shall be applied only to the definition of a |
| 1075 | // variable or variable template or the declaration of a function or |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1076 | // function template. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1077 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1078 | if (DS.isConstexprSpecified()) |
| 1079 | EmitDiag(DS.getConstexprSpecLoc()); |
| 1080 | |
| 1081 | // [dcl.fct.spec]p1: |
| 1082 | // Function-specifiers can be used only in function declarations. |
| 1083 | |
| 1084 | if (DS.isVirtualSpecified()) |
| 1085 | EmitDiag(DS.getVirtualSpecLoc()); |
| 1086 | |
| 1087 | if (DS.isExplicitSpecified()) |
| 1088 | EmitDiag(DS.getExplicitSpecLoc()); |
| 1089 | |
| 1090 | if (DS.isNoreturnSpecified()) |
| 1091 | EmitDiag(DS.getNoreturnSpecLoc()); |
| 1092 | }; |
| 1093 | |
| 1094 | CheckValidDeclSpecifiers(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1095 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1096 | if (TInfo->getType()->isUndeducedType()) { |
| 1097 | Diag(D.getIdentifierLoc(), |
| 1098 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 1099 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
| 1100 | } |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1101 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1102 | assert(S->isTemplateParamScope() && |
| 1103 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1104 | bool Invalid = false; |
| 1105 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1106 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1107 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1108 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 1109 | Invalid = true; |
| 1110 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1111 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1112 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1113 | bool IsParameterPack = D.hasEllipsis(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1114 | NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create( |
| 1115 | Context, Context.getTranslationUnitDecl(), D.getBeginLoc(), |
| 1116 | D.getIdentifierLoc(), Depth, Position, ParamName, T, IsParameterPack, |
| 1117 | TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1118 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1119 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1120 | if (Invalid) |
| 1121 | Param->setInvalidDecl(); |
| 1122 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1123 | if (ParamName) { |
| 1124 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 1125 | ParamName); |
| 1126 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1127 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1128 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1129 | IdResolver.AddDecl(Param); |
| 1130 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1131 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1132 | // C++0x [temp.param]p9: |
| 1133 | // A default template-argument may be specified for any kind of |
| 1134 | // template-parameter that is not a template parameter pack. |
| 1135 | if (Default && IsParameterPack) { |
| 1136 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1137 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1140 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1141 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1142 | // Check for unexpanded parameter packs. |
| 1143 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 1144 | return Param; |
| 1145 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1146 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1147 | ExprResult DefaultRes = |
| 1148 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1149 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1150 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1151 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1152 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1153 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1154 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1155 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1156 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1157 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1158 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1159 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1161 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1162 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1163 | /// has been parsed. S is the current scope. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1164 | NamedDecl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1165 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1166 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1167 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1168 | IdentifierInfo *Name, |
| 1169 | SourceLocation NameLoc, |
| 1170 | unsigned Depth, |
| 1171 | unsigned Position, |
| 1172 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1173 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1174 | assert(S->isTemplateParamScope() && |
| 1175 | "Template template parameter not in template parameter scope!"); |
| 1176 | |
| 1177 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1178 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1179 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 1180 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1181 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 1182 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1183 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1184 | Param->setAccess(AS_public); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1185 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1186 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1187 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1188 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1189 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 1190 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1191 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1192 | IdResolver.AddDecl(Param); |
| 1193 | } |
| 1194 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1195 | if (Params->size() == 0) { |
| 1196 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 1197 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 1198 | Param->setInvalidDecl(); |
| 1199 | } |
| 1200 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1201 | // C++0x [temp.param]p9: |
| 1202 | // A default template-argument may be specified for any kind of |
| 1203 | // template-parameter that is not a template parameter pack. |
| 1204 | if (IsParameterPack && !Default.isInvalid()) { |
| 1205 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 1206 | Default = ParsedTemplateArgument(); |
| 1207 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1208 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1209 | if (!Default.isInvalid()) { |
| 1210 | // Check only that we have a template template argument. We don't want to |
| 1211 | // try to check well-formedness now, because our template template parameter |
| 1212 | // might have dependent types in its template parameters, which we wouldn't |
| 1213 | // be able to match now. |
| 1214 | // |
| 1215 | // If none of the template template parameter's template arguments mention |
| 1216 | // other template parameters, we could actually perform more checking here. |
| 1217 | // However, it isn't worth doing. |
| 1218 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 1219 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 1220 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1221 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1222 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1223 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1225 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1226 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1227 | DefaultArg.getArgument().getAsTemplate(), |
| 1228 | UPPC_DefaultArgument)) |
| 1229 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1230 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1231 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1232 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1233 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1234 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1237 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 1238 | /// constrained by RequiresClause, that contains the template parameters in |
| 1239 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1240 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1241 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 1242 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1244 | SourceLocation LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1245 | ArrayRef<NamedDecl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1246 | SourceLocation RAngleLoc, |
| 1247 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1248 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1249 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1250 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1251 | return TemplateParameterList::Create( |
| 1252 | Context, TemplateLoc, LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1253 | llvm::makeArrayRef(Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 1254 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1255 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1256 | |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 1257 | static void SetNestedNameSpecifier(Sema &S, TagDecl *T, |
| 1258 | const CXXScopeSpec &SS) { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1259 | if (SS.isSet()) |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 1260 | T->setQualifierInfo(SS.getWithLocInContext(S.Context)); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1263 | DeclResult Sema::CheckClassTemplate( |
| 1264 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
| 1265 | CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc, |
| 1266 | const ParsedAttributesView &Attr, TemplateParameterList *TemplateParams, |
| 1267 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
| 1268 | SourceLocation FriendLoc, unsigned NumOuterTemplateParamLists, |
| 1269 | TemplateParameterList **OuterTemplateParamLists, SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1271 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1272 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1273 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1276 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1277 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1278 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1279 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 1280 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1281 | |
| 1282 | // There is no such thing as an unnamed class template. |
| 1283 | if (!Name) { |
| 1284 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1285 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1288 | // Find any previous declaration with this name. For a friend with no |
| 1289 | // scope explicitly specified, we only look for tag declarations (per |
| 1290 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1291 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1292 | LookupResult Previous(*this, Name, NameLoc, |
| 1293 | (SS.isEmpty() && TUK == TUK_Friend) |
| 1294 | ? LookupTagName : LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1295 | forRedeclarationInCurContext()); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1296 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 1297 | SemanticContext = computeDeclContext(SS, true); |
| 1298 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1299 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 1300 | // in the AST, and historically we have just ignored such friend |
| 1301 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1302 | Diag(NameLoc, TUK == TUK_Friend |
| 1303 | ? diag::warn_template_qualified_friend_ignored |
| 1304 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1305 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1306 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1307 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1309 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 1310 | return true; |
| 1311 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1312 | // If we're adding a template to a dependent context, we may need to |
| 1313 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 1314 | // now that we know what the current instantiation is. |
| 1315 | if (SemanticContext->isDependentContext()) { |
| 1316 | ContextRAII SavedContext(*this, SemanticContext); |
| 1317 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 1318 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1319 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 1320 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc, false); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1321 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1322 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1323 | } else { |
| 1324 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 1325 | |
| 1326 | // C++14 [class.mem]p14: |
| 1327 | // If T is the name of a class, then each of the following shall have a |
| 1328 | // name different from T: |
| 1329 | // -- every member template of class T |
| 1330 | if (TUK != TUK_Friend && |
| 1331 | DiagnoseClassNameShadow(SemanticContext, |
| 1332 | DeclarationNameInfo(Name, NameLoc))) |
| 1333 | return true; |
| 1334 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1335 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1336 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1338 | if (Previous.isAmbiguous()) |
| 1339 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1340 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1341 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1342 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1343 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1344 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1345 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1346 | // Maybe we will complain about the shadowed template parameter. |
| 1347 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1348 | // Just pretend that we didn't see the previous declaration. |
| 1349 | PrevDecl = nullptr; |
| 1350 | } |
| 1351 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1352 | // If there is a previous declaration with the same name, check |
| 1353 | // whether this is a valid redeclaration. |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1354 | ClassTemplateDecl *PrevClassTemplate = |
| 1355 | dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1356 | |
| 1357 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1358 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1359 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1360 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1361 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1362 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1363 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1364 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1365 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1366 | PrevClassTemplate |
| 1367 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1368 | ->getSpecializedTemplate(); |
| 1369 | } |
| 1370 | } |
| 1371 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1372 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1373 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1374 | // [...] When looking for a prior declaration of a class or a function |
| 1375 | // 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] | 1376 | // function is neither a qualified name nor a template-id, scopes outside |
| 1377 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1378 | if (!SS.isSet()) { |
| 1379 | DeclContext *OutermostContext = CurContext; |
| 1380 | while (!OutermostContext->isFileContext()) |
| 1381 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1382 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 1383 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1384 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1385 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1386 | SemanticContext = PrevDecl->getDeclContext(); |
| 1387 | } else { |
| 1388 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1389 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1390 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1391 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1392 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1393 | |
| 1394 | // Check that the chosen semantic context doesn't already contain a |
| 1395 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1396 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1397 | DeclContext *LookupContext = SemanticContext; |
| 1398 | while (LookupContext->isTransparentContext()) |
| 1399 | LookupContext = LookupContext->getLookupParent(); |
| 1400 | LookupQualifiedName(Previous, LookupContext); |
| 1401 | |
| 1402 | if (Previous.isAmbiguous()) |
| 1403 | return true; |
| 1404 | |
| 1405 | if (Previous.begin() != Previous.end()) |
| 1406 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1407 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1408 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1409 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1410 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1411 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1412 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1413 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1414 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1415 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1416 | if (SS.isEmpty() && |
| 1417 | !(PrevClassTemplate && |
| 1418 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1419 | SemanticContext->getRedeclContext()))) { |
| 1420 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1421 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1422 | diag::note_using_decl_target); |
| 1423 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1424 | // Recover by ignoring the old declaration. |
| 1425 | PrevDecl = PrevClassTemplate = nullptr; |
| 1426 | } |
| 1427 | } |
| 1428 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1429 | // TODO Memory management; associated constraints are not always stored. |
| 1430 | Expr *const CurAC = formAssociatedConstraints(TemplateParams, nullptr); |
| 1431 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1432 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1433 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1434 | // for a friend in a dependent context: the template parameter list itself |
| 1435 | // could be dependent. |
| 1436 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1437 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1438 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1439 | /*Complain=*/true, |
| 1440 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1441 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1442 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1443 | // Check for matching associated constraints on redeclarations. |
| 1444 | const Expr *const PrevAC = PrevClassTemplate->getAssociatedConstraints(); |
| 1445 | const bool RedeclACMismatch = [&] { |
| 1446 | if (!(CurAC || PrevAC)) |
| 1447 | return false; // Nothing to check; no mismatch. |
| 1448 | if (CurAC && PrevAC) { |
| 1449 | llvm::FoldingSetNodeID CurACInfo, PrevACInfo; |
| 1450 | CurAC->Profile(CurACInfo, Context, /*Canonical=*/true); |
| 1451 | PrevAC->Profile(PrevACInfo, Context, /*Canonical=*/true); |
| 1452 | if (CurACInfo == PrevACInfo) |
| 1453 | return false; // All good; no mismatch. |
| 1454 | } |
| 1455 | return true; |
| 1456 | }(); |
| 1457 | |
| 1458 | if (RedeclACMismatch) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1459 | Diag(CurAC ? CurAC->getBeginLoc() : NameLoc, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1460 | diag::err_template_different_associated_constraints); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1461 | Diag(PrevAC ? PrevAC->getBeginLoc() : PrevClassTemplate->getLocation(), |
| 1462 | diag::note_template_prev_declaration) |
| 1463 | << /*declaration*/ 0; |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1464 | return true; |
| 1465 | } |
| 1466 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1467 | // C++ [temp.class]p4: |
| 1468 | // In a redeclaration, partial specialization, explicit |
| 1469 | // specialization or explicit instantiation of a class template, |
| 1470 | // the class-key shall agree in kind with the original class |
| 1471 | // template declaration (7.1.5.3). |
| 1472 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1473 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1474 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1476 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1477 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1478 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1479 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1482 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1483 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1484 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1485 | // If we have a prior definition that is not visible, treat this as |
| 1486 | // simply making that previous definition visible. |
| 1487 | NamedDecl *Hidden = nullptr; |
| 1488 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1489 | SkipBody->ShouldSkip = true; |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1490 | SkipBody->Previous = Def; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1491 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1492 | assert(Tmpl && "original definition of a class template is not a " |
| 1493 | "class template?"); |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 1494 | makeMergedDefinitionVisible(Hidden); |
| 1495 | makeMergedDefinitionVisible(Tmpl); |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1496 | } else { |
| 1497 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1498 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1499 | // FIXME: Would it make sense to try to "forget" the previous |
| 1500 | // definition, as part of error recovery? |
| 1501 | return true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1502 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1503 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1504 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1505 | } else if (PrevDecl) { |
| 1506 | // C++ [temp]p5: |
| 1507 | // A class template shall not have the same name as any other |
| 1508 | // template, class, function, object, enumeration, enumerator, |
| 1509 | // namespace, or type in the same scope (3.3), except as specified |
| 1510 | // in (14.5.4). |
| 1511 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1512 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
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 | } |
| 1515 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1516 | // Check the template parameter list of this declaration, possibly |
| 1517 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1518 | // template declaration. Skip this check for a friend in a dependent |
| 1519 | // context, because the template parameter list might be dependent. |
| 1520 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1521 | CheckTemplateParameterList( |
| 1522 | TemplateParams, |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1523 | PrevClassTemplate |
| 1524 | ? PrevClassTemplate->getMostRecentDecl()->getTemplateParameters() |
| 1525 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1526 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1527 | SemanticContext->isDependentContext()) |
| 1528 | ? TPC_ClassTemplateMember |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1529 | : TUK == TUK_Friend ? TPC_FriendClassTemplate : TPC_ClassTemplate, |
| 1530 | SkipBody)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1531 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1533 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1534 | // 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] | 1535 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1536 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1537 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1538 | : diag::err_member_decl_does_not_match) |
| 1539 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1540 | Invalid = true; |
| 1541 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1544 | // If this is a templated friend in a dependent context we should not put it |
| 1545 | // on the redecl chain. In some cases, the templated friend can be the most |
| 1546 | // recent declaration tricking the template instantiator to make substitutions |
| 1547 | // there. |
| 1548 | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
| 1549 | bool ShouldAddRedecl |
| 1550 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
| 1551 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1553 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1554 | PrevClassTemplate && ShouldAddRedecl ? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1555 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1556 | /*DelayTypeCreation=*/true); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 1557 | SetNestedNameSpecifier(*this, NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1558 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1559 | NewClass->setTemplateParameterListsInfo( |
| 1560 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1561 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1562 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1563 | // Add alignment attributes if necessary; these attributes are checked when |
| 1564 | // the ASTContext lays out the structure. |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1565 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1566 | AddAlignmentAttributesForRecord(NewClass); |
| 1567 | AddMsStructLayoutForRecord(NewClass); |
| 1568 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1569 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1570 | // Attach the associated constraints when the declaration will not be part of |
| 1571 | // a decl chain. |
| 1572 | Expr *const ACtoAttach = |
| 1573 | PrevClassTemplate && ShouldAddRedecl ? nullptr : CurAC; |
| 1574 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1575 | ClassTemplateDecl *NewTemplate |
| 1576 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1577 | DeclarationName(Name), TemplateParams, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1578 | NewClass, ACtoAttach); |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1579 | |
| 1580 | if (ShouldAddRedecl) |
| 1581 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
| 1582 | |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1583 | NewClass->setDescribedClassTemplate(NewTemplate); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1584 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1585 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1586 | NewTemplate->setModulePrivate(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1587 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1588 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1589 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1590 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1591 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1592 | (void)T; |
| 1593 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1594 | // 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] | 1595 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1596 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1597 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1598 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1599 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1600 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1601 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1602 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1604 | // Set the lexical context of these templates |
| 1605 | NewClass->setLexicalDeclContext(CurContext); |
| 1606 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1607 | |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1608 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1609 | NewClass->startDefinition(); |
| 1610 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1611 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1612 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1613 | if (PrevClassTemplate) |
| 1614 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1615 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1616 | AddPushedVisibilityAttribute(NewClass); |
| 1617 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1618 | if (TUK != TUK_Friend) { |
| 1619 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1620 | Scope *Outer = S; |
| 1621 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1622 | Outer = Outer->getParent(); |
| 1623 | PushOnScopeChains(NewTemplate, Outer); |
| 1624 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1625 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1626 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1627 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1628 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1629 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1630 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1631 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1632 | // Friend templates are visible in fairly strange ways. |
| 1633 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1634 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1635 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1636 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1637 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1638 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1639 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1640 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1641 | FriendDecl *Friend = FriendDecl::Create( |
| 1642 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1643 | Friend->setAccess(AS_public); |
| 1644 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1645 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1646 | |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1647 | if (PrevClassTemplate) |
| 1648 | CheckRedeclarationModuleOwnership(NewTemplate, PrevClassTemplate); |
| 1649 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1650 | if (Invalid) { |
| 1651 | NewTemplate->setInvalidDecl(); |
| 1652 | NewClass->setInvalidDecl(); |
| 1653 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1654 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1655 | ActOnDocumentableDecl(NewTemplate); |
| 1656 | |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1657 | if (SkipBody && SkipBody->ShouldSkip) |
| 1658 | return SkipBody->Previous; |
| 1659 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1660 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1663 | namespace { |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1664 | /// Tree transform to "extract" a transformed type from a class template's |
| 1665 | /// constructor to a deduction guide. |
| 1666 | class ExtractTypeForDeductionGuide |
| 1667 | : public TreeTransform<ExtractTypeForDeductionGuide> { |
| 1668 | public: |
| 1669 | typedef TreeTransform<ExtractTypeForDeductionGuide> Base; |
| 1670 | ExtractTypeForDeductionGuide(Sema &SemaRef) : Base(SemaRef) {} |
| 1671 | |
| 1672 | TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); } |
| 1673 | |
| 1674 | QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) { |
| 1675 | return TransformType( |
| 1676 | TLB, |
| 1677 | TL.getTypedefNameDecl()->getTypeSourceInfo()->getTypeLoc()); |
| 1678 | } |
| 1679 | }; |
| 1680 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1681 | /// Transform to convert portions of a constructor declaration into the |
| 1682 | /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1. |
| 1683 | struct ConvertConstructorToDeductionGuideTransform { |
| 1684 | ConvertConstructorToDeductionGuideTransform(Sema &S, |
| 1685 | ClassTemplateDecl *Template) |
| 1686 | : SemaRef(S), Template(Template) {} |
| 1687 | |
| 1688 | Sema &SemaRef; |
| 1689 | ClassTemplateDecl *Template; |
| 1690 | |
| 1691 | DeclContext *DC = Template->getDeclContext(); |
| 1692 | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
| 1693 | DeclarationName DeductionGuideName = |
| 1694 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
| 1695 | |
| 1696 | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
| 1697 | |
| 1698 | // Index adjustment to apply to convert depth-1 template parameters into |
| 1699 | // depth-0 template parameters. |
| 1700 | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
| 1701 | |
| 1702 | /// Transform a constructor declaration into a deduction guide. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1703 | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
| 1704 | CXXConstructorDecl *CD) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1705 | SmallVector<TemplateArgument, 16> SubstArgs; |
| 1706 | |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1707 | LocalInstantiationScope Scope(SemaRef); |
| 1708 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1709 | // C++ [over.match.class.deduct]p1: |
| 1710 | // -- For each constructor of the class template designated by the |
| 1711 | // template-name, a function template with the following properties: |
| 1712 | |
| 1713 | // -- The template parameters are the template parameters of the class |
| 1714 | // template followed by the template parameters (including default |
| 1715 | // template arguments) of the constructor, if any. |
| 1716 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 1717 | if (FTD) { |
| 1718 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
| 1719 | SmallVector<NamedDecl *, 16> AllParams; |
| 1720 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
| 1721 | AllParams.insert(AllParams.begin(), |
| 1722 | TemplateParams->begin(), TemplateParams->end()); |
| 1723 | SubstArgs.reserve(InnerParams->size()); |
| 1724 | |
| 1725 | // Later template parameters could refer to earlier ones, so build up |
| 1726 | // a list of substituted template arguments as we go. |
| 1727 | for (NamedDecl *Param : *InnerParams) { |
| 1728 | MultiLevelTemplateArgumentList Args; |
| 1729 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1730 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1731 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
| 1732 | if (!NewParam) |
| 1733 | return nullptr; |
| 1734 | AllParams.push_back(NewParam); |
| 1735 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
| 1736 | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
| 1737 | } |
| 1738 | TemplateParams = TemplateParameterList::Create( |
| 1739 | SemaRef.Context, InnerParams->getTemplateLoc(), |
| 1740 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
| 1741 | /*FIXME: RequiresClause*/ nullptr); |
| 1742 | } |
| 1743 | |
| 1744 | // If we built a new template-parameter-list, track that we need to |
| 1745 | // substitute references to the old parameters into references to the |
| 1746 | // new ones. |
| 1747 | MultiLevelTemplateArgumentList Args; |
| 1748 | if (FTD) { |
| 1749 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1750 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1753 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1754 | .getAsAdjusted<FunctionProtoTypeLoc>(); |
| 1755 | assert(FPTL && "no prototype for constructor declaration"); |
| 1756 | |
| 1757 | // Transform the type of the function, adjusting the return type and |
| 1758 | // replacing references to the old parameters with references to the |
| 1759 | // new ones. |
| 1760 | TypeLocBuilder TLB; |
| 1761 | SmallVector<ParmVarDecl*, 8> Params; |
| 1762 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args); |
| 1763 | if (NewType.isNull()) |
| 1764 | return nullptr; |
| 1765 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
| 1766 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1767 | return buildDeductionGuide(TemplateParams, CD->isExplicit(), NewTInfo, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1768 | CD->getBeginLoc(), CD->getLocation(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1769 | CD->getEndLoc()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | /// Build a deduction guide with the specified parameter types. |
| 1773 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
| 1774 | SourceLocation Loc = Template->getLocation(); |
| 1775 | |
| 1776 | // Build the requested type. |
| 1777 | FunctionProtoType::ExtProtoInfo EPI; |
| 1778 | EPI.HasTrailingReturn = true; |
| 1779 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
| 1780 | DeductionGuideName, EPI); |
| 1781 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
| 1782 | |
| 1783 | FunctionProtoTypeLoc FPTL = |
| 1784 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
| 1785 | |
| 1786 | // Build the parameters, needed during deduction / substitution. |
| 1787 | SmallVector<ParmVarDecl*, 4> Params; |
| 1788 | for (auto T : ParamTypes) { |
| 1789 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
| 1790 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
| 1791 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
| 1792 | NewParam->setScopeInfo(0, Params.size()); |
| 1793 | FPTL.setParam(Params.size(), NewParam); |
| 1794 | Params.push_back(NewParam); |
| 1795 | } |
| 1796 | |
| 1797 | return buildDeductionGuide(Template->getTemplateParameters(), false, TSI, |
| 1798 | Loc, Loc, Loc); |
| 1799 | } |
| 1800 | |
| 1801 | private: |
| 1802 | /// Transform a constructor template parameter into a deduction guide template |
| 1803 | /// parameter, rebuilding any internal references to earlier parameters and |
| 1804 | /// renumbering as we go. |
| 1805 | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
| 1806 | MultiLevelTemplateArgumentList &Args) { |
| 1807 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
| 1808 | // TemplateTypeParmDecl's index cannot be changed after creation, so |
| 1809 | // substitute it directly. |
| 1810 | auto *NewTTP = TemplateTypeParmDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1811 | SemaRef.Context, DC, TTP->getBeginLoc(), TTP->getLocation(), |
| 1812 | /*Depth*/ 0, Depth1IndexAdjustment + TTP->getIndex(), |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1813 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
| 1814 | TTP->isParameterPack()); |
| 1815 | if (TTP->hasDefaultArgument()) { |
| 1816 | TypeSourceInfo *InstantiatedDefaultArg = |
| 1817 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
| 1818 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
| 1819 | if (InstantiatedDefaultArg) |
| 1820 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
| 1821 | } |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1822 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
| 1823 | NewTTP); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1824 | return NewTTP; |
| 1825 | } |
| 1826 | |
| 1827 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
| 1828 | return transformTemplateParameterImpl(TTP, Args); |
| 1829 | |
| 1830 | return transformTemplateParameterImpl( |
| 1831 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
| 1832 | } |
| 1833 | template<typename TemplateParmDecl> |
| 1834 | TemplateParmDecl * |
| 1835 | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
| 1836 | MultiLevelTemplateArgumentList &Args) { |
| 1837 | // Ask the template instantiator to do the heavy lifting for us, then adjust |
| 1838 | // the index of the parameter once it's done. |
| 1839 | auto *NewParam = |
| 1840 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
| 1841 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
| 1842 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
| 1843 | return NewParam; |
| 1844 | } |
| 1845 | |
| 1846 | QualType transformFunctionProtoType(TypeLocBuilder &TLB, |
| 1847 | FunctionProtoTypeLoc TL, |
| 1848 | SmallVectorImpl<ParmVarDecl*> &Params, |
| 1849 | MultiLevelTemplateArgumentList &Args) { |
| 1850 | SmallVector<QualType, 4> ParamTypes; |
| 1851 | const FunctionProtoType *T = TL.getTypePtr(); |
| 1852 | |
| 1853 | // -- The types of the function parameters are those of the constructor. |
| 1854 | for (auto *OldParam : TL.getParams()) { |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1855 | ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1856 | if (!NewParam) |
| 1857 | return QualType(); |
| 1858 | ParamTypes.push_back(NewParam->getType()); |
| 1859 | Params.push_back(NewParam); |
| 1860 | } |
| 1861 | |
| 1862 | // -- The return type is the class template specialization designated by |
| 1863 | // the template-name and template arguments corresponding to the |
| 1864 | // template parameters obtained from the class template. |
| 1865 | // |
| 1866 | // We use the injected-class-name type of the primary template instead. |
| 1867 | // This has the convenient property that it is different from any type that |
| 1868 | // the user can write in a deduction-guide (because they cannot enter the |
| 1869 | // context of the template), so implicit deduction guides can never collide |
| 1870 | // with explicit ones. |
| 1871 | QualType ReturnType = DeducedType; |
| 1872 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
| 1873 | |
| 1874 | // Resolving a wording defect, we also inherit the variadicness of the |
| 1875 | // constructor. |
| 1876 | FunctionProtoType::ExtProtoInfo EPI; |
| 1877 | EPI.Variadic = T->isVariadic(); |
| 1878 | EPI.HasTrailingReturn = true; |
| 1879 | |
| 1880 | QualType Result = SemaRef.BuildFunctionType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1881 | ReturnType, ParamTypes, TL.getBeginLoc(), DeductionGuideName, EPI); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1882 | if (Result.isNull()) |
| 1883 | return QualType(); |
| 1884 | |
| 1885 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
| 1886 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
| 1887 | NewTL.setLParenLoc(TL.getLParenLoc()); |
| 1888 | NewTL.setRParenLoc(TL.getRParenLoc()); |
| 1889 | NewTL.setExceptionSpecRange(SourceRange()); |
| 1890 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
| 1891 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I) |
| 1892 | NewTL.setParam(I, Params[I]); |
| 1893 | |
| 1894 | return Result; |
| 1895 | } |
| 1896 | |
| 1897 | ParmVarDecl * |
| 1898 | transformFunctionTypeParam(ParmVarDecl *OldParam, |
| 1899 | MultiLevelTemplateArgumentList &Args) { |
| 1900 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
Richard Smith | 479ba8e | 2017-04-20 01:15:31 +0000 | [diff] [blame] | 1901 | TypeSourceInfo *NewDI; |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1902 | if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { |
Richard Smith | 479ba8e | 2017-04-20 01:15:31 +0000 | [diff] [blame] | 1903 | // Expand out the one and only element in each inner pack. |
| 1904 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); |
| 1905 | NewDI = |
| 1906 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, |
| 1907 | OldParam->getLocation(), OldParam->getDeclName()); |
| 1908 | if (!NewDI) return nullptr; |
| 1909 | NewDI = |
| 1910 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), |
| 1911 | PackTL.getTypePtr()->getNumExpansions()); |
| 1912 | } else |
| 1913 | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
| 1914 | OldParam->getDeclName()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1915 | if (!NewDI) |
| 1916 | return nullptr; |
| 1917 | |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1918 | // Extract the type. This (for instance) replaces references to typedef |
| 1919 | // members of the current instantiations with the definitions of those |
| 1920 | // typedefs, avoiding triggering instantiation of the deduced type during |
| 1921 | // deduction. |
| 1922 | NewDI = ExtractTypeForDeductionGuide(SemaRef).transform(NewDI); |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1923 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1924 | // Resolving a wording defect, we also inherit default arguments from the |
| 1925 | // constructor. |
| 1926 | ExprResult NewDefArg; |
| 1927 | if (OldParam->hasDefaultArg()) { |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1928 | NewDefArg = SemaRef.SubstExpr(OldParam->getDefaultArg(), Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1929 | if (NewDefArg.isInvalid()) |
| 1930 | return nullptr; |
| 1931 | } |
| 1932 | |
| 1933 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
| 1934 | OldParam->getInnerLocStart(), |
| 1935 | OldParam->getLocation(), |
| 1936 | OldParam->getIdentifier(), |
| 1937 | NewDI->getType(), |
| 1938 | NewDI, |
| 1939 | OldParam->getStorageClass(), |
| 1940 | NewDefArg.get()); |
| 1941 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
| 1942 | OldParam->getFunctionScopeIndex()); |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1943 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1944 | return NewParam; |
| 1945 | } |
| 1946 | |
| 1947 | NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams, |
| 1948 | bool Explicit, TypeSourceInfo *TInfo, |
| 1949 | SourceLocation LocStart, SourceLocation Loc, |
| 1950 | SourceLocation LocEnd) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1951 | DeclarationNameInfo Name(DeductionGuideName, Loc); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1952 | ArrayRef<ParmVarDecl *> Params = |
| 1953 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
| 1954 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1955 | // Build the implicit deduction guide template. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1956 | auto *Guide = |
| 1957 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, Explicit, |
| 1958 | Name, TInfo->getType(), TInfo, LocEnd); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1959 | Guide->setImplicit(); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1960 | Guide->setParams(Params); |
| 1961 | |
| 1962 | for (auto *Param : Params) |
| 1963 | Param->setDeclContext(Guide); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1964 | |
| 1965 | auto *GuideTemplate = FunctionTemplateDecl::Create( |
| 1966 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
| 1967 | GuideTemplate->setImplicit(); |
| 1968 | Guide->setDescribedFunctionTemplate(GuideTemplate); |
| 1969 | |
| 1970 | if (isa<CXXRecordDecl>(DC)) { |
| 1971 | Guide->setAccess(AS_public); |
| 1972 | GuideTemplate->setAccess(AS_public); |
| 1973 | } |
| 1974 | |
| 1975 | DC->addDecl(GuideTemplate); |
| 1976 | return GuideTemplate; |
| 1977 | } |
| 1978 | }; |
| 1979 | } |
| 1980 | |
| 1981 | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
| 1982 | SourceLocation Loc) { |
| 1983 | DeclContext *DC = Template->getDeclContext(); |
| 1984 | if (DC->isDependentContext()) |
| 1985 | return; |
| 1986 | |
| 1987 | ConvertConstructorToDeductionGuideTransform Transform( |
| 1988 | *this, cast<ClassTemplateDecl>(Template)); |
| 1989 | if (!isCompleteType(Loc, Transform.DeducedType)) |
| 1990 | return; |
| 1991 | |
| 1992 | // Check whether we've already declared deduction guides for this template. |
| 1993 | // FIXME: Consider storing a flag on the template to indicate this. |
| 1994 | auto Existing = DC->lookup(Transform.DeductionGuideName); |
| 1995 | for (auto *D : Existing) |
| 1996 | if (D->isImplicit()) |
| 1997 | return; |
| 1998 | |
| 1999 | // In case we were expanding a pack when we attempted to declare deduction |
| 2000 | // guides, turn off pack expansion for everything we're about to do. |
| 2001 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
| 2002 | // Create a template instantiation record to track the "instantiation" of |
| 2003 | // constructors into deduction guides. |
| 2004 | // FIXME: Add a kind for this to give more meaningful diagnostics. But can |
| 2005 | // this substitution process actually fail? |
| 2006 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
Volodymyr Sapsai | 2f649f3 | 2018-05-14 22:49:44 +0000 | [diff] [blame] | 2007 | if (BuildingDeductionGuides.isInvalid()) |
| 2008 | return; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2009 | |
| 2010 | // Convert declared constructors into deduction guide templates. |
| 2011 | // FIXME: Skip constructors for which deduction must necessarily fail (those |
| 2012 | // for which some class template parameter without a default argument never |
| 2013 | // appears in a deduced context). |
| 2014 | bool AddedAny = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2015 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
| 2016 | D = D->getUnderlyingDecl(); |
| 2017 | if (D->isInvalidDecl() || D->isImplicit()) |
| 2018 | continue; |
| 2019 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 2020 | |
| 2021 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2022 | auto *CD = |
| 2023 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2024 | // Class-scope explicit specializations (MS extension) do not result in |
| 2025 | // deduction guides. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2026 | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization())) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2027 | continue; |
| 2028 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2029 | Transform.transformConstructor(FTD, CD); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2030 | AddedAny = true; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2033 | // C++17 [over.match.class.deduct] |
| 2034 | // -- If C is not defined or does not declare any constructors, an |
| 2035 | // additional function template derived as above from a hypothetical |
| 2036 | // constructor C(). |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2037 | if (!AddedAny) |
| 2038 | Transform.buildSimpleDeductionGuide(None); |
| 2039 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2040 | // -- An additional function template derived as above from a hypothetical |
| 2041 | // constructor C(C), called the copy deduction candidate. |
| 2042 | cast<CXXDeductionGuideDecl>( |
| 2043 | cast<FunctionTemplateDecl>( |
| 2044 | Transform.buildSimpleDeductionGuide(Transform.DeducedType)) |
| 2045 | ->getTemplatedDecl()) |
| 2046 | ->setIsCopyDeductionCandidate(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2049 | /// Diagnose the presence of a default template argument on a |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2050 | /// template parameter, which is ill-formed in certain contexts. |
| 2051 | /// |
| 2052 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2053 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2054 | Sema::TemplateParamListContext TPC, |
| 2055 | SourceLocation ParamLoc, |
| 2056 | SourceRange DefArgRange) { |
| 2057 | switch (TPC) { |
| 2058 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2059 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2060 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2061 | return false; |
| 2062 | |
| 2063 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2064 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2065 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2066 | // A default template-argument shall not be specified in a |
| 2067 | // function template declaration or a function template |
| 2068 | // definition [...] |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2069 | // If a friend function template declaration specifies a default |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2070 | // template-argument, that declaration shall be a definition and shall be |
| 2071 | // the only declaration of the function template in the translation unit. |
| 2072 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2073 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2074 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 2075 | : diag::ext_template_parameter_default_in_function_template) |
| 2076 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2077 | return false; |
| 2078 | |
| 2079 | case Sema::TPC_ClassTemplateMember: |
| 2080 | // C++0x [temp.param]p9: |
| 2081 | // A default template-argument shall not be specified in the |
| 2082 | // template-parameter-lists of the definition of a member of a |
| 2083 | // class template that appears outside of the member's class. |
| 2084 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 2085 | << DefArgRange; |
| 2086 | return true; |
| 2087 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 2088 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2089 | case Sema::TPC_FriendFunctionTemplate: |
| 2090 | // C++ [temp.param]p9: |
| 2091 | // A default template-argument shall not be specified in a |
| 2092 | // friend template declaration. |
| 2093 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 2094 | << DefArgRange; |
| 2095 | return true; |
| 2096 | |
| 2097 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 2098 | // for friend function templates if there is only a single |
| 2099 | // declaration (and it is a definition). Strange! |
| 2100 | } |
| 2101 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2102 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2105 | /// Check for unexpanded parameter packs within the template parameters |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2106 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 2107 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 2108 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2109 | // A template template parameter which is a parameter pack is also a pack |
| 2110 | // expansion. |
| 2111 | if (TTP->isParameterPack()) |
| 2112 | return false; |
| 2113 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2114 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 2115 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 2116 | NamedDecl *P = Params->getParam(I); |
| 2117 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2118 | if (!NTTP->isParameterPack() && |
| 2119 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2120 | NTTP->getTypeSourceInfo(), |
| 2121 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 2122 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2124 | continue; |
| 2125 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2126 | |
| 2127 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2128 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 2129 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 2130 | return true; |
| 2131 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2133 | return false; |
| 2134 | } |
| 2135 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2136 | /// Checks the validity of a template parameter list, possibly |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2137 | /// considering the template parameter list from a previous |
| 2138 | /// declaration. |
| 2139 | /// |
| 2140 | /// If an "old" template parameter list is provided, it must be |
| 2141 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 2142 | /// template parameter list. |
| 2143 | /// |
| 2144 | /// \param NewParams Template parameter list for a new template |
| 2145 | /// declaration. This template parameter list will be updated with any |
| 2146 | /// default arguments that are carried through from the previous |
| 2147 | /// template parameter list. |
| 2148 | /// |
| 2149 | /// \param OldParams If provided, template parameter list from a |
| 2150 | /// previous declaration of the same template. Default template |
| 2151 | /// arguments will be merged from the old template parameter list to |
| 2152 | /// the new template parameter list. |
| 2153 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2154 | /// \param TPC Describes the context in which we are checking the given |
| 2155 | /// template parameter list. |
| 2156 | /// |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2157 | /// \param SkipBody If we might have already made a prior merged definition |
| 2158 | /// of this template visible, the corresponding body-skipping information. |
| 2159 | /// Default argument redefinition is not an error when skipping such a body, |
| 2160 | /// because (under the ODR) we can assume the default arguments are the same |
| 2161 | /// as the prior merged definition. |
| 2162 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2163 | /// \returns true if an error occurred, false otherwise. |
| 2164 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2165 | TemplateParameterList *OldParams, |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2166 | TemplateParamListContext TPC, |
| 2167 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2168 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2170 | // C++ [temp.param]p10: |
| 2171 | // The set of default template-arguments available for use with a |
| 2172 | // template declaration or definition is obtained by merging the |
| 2173 | // default arguments from the definition (if in scope) and all |
| 2174 | // declarations in scope in the same way default function |
| 2175 | // arguments are (8.3.6). |
| 2176 | bool SawDefaultArgument = false; |
| 2177 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2178 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 2179 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 2180 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2181 | if (OldParams) |
| 2182 | OldParam = OldParams->begin(); |
| 2183 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2184 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2185 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2186 | NewParamEnd = NewParams->end(); |
| 2187 | NewParam != NewParamEnd; ++NewParam) { |
| 2188 | // Variables used to diagnose redundant default arguments |
| 2189 | bool RedundantDefaultArg = false; |
| 2190 | SourceLocation OldDefaultLoc; |
| 2191 | SourceLocation NewDefaultLoc; |
| 2192 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2193 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2194 | bool MissingDefaultArg = false; |
| 2195 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2196 | // Variable used to diagnose non-final parameter packs |
| 2197 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2198 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2199 | if (TemplateTypeParmDecl *NewTypeParm |
| 2200 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2201 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2202 | if (NewTypeParm->hasDefaultArgument() && |
| 2203 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2204 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2205 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2206 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2207 | NewTypeParm->removeDefaultArgument(); |
| 2208 | |
| 2209 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2211 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2212 | if (NewTypeParm->isParameterPack()) { |
| 2213 | assert(!NewTypeParm->hasDefaultArgument() && |
| 2214 | "Parameter packs can't have a default argument!"); |
| 2215 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2216 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2217 | NewTypeParm->hasDefaultArgument() && |
| 2218 | (!SkipBody || !SkipBody->ShouldSkip)) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2219 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2220 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2221 | SawDefaultArgument = true; |
| 2222 | RedundantDefaultArg = true; |
| 2223 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2224 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 2225 | // Merge the default argument from the old declaration to the |
| 2226 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2227 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2228 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2229 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 2230 | SawDefaultArgument = true; |
| 2231 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2232 | } else if (SawDefaultArgument) |
| 2233 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2234 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2235 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2236 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2237 | if (!NewNonTypeParm->isParameterPack() && |
| 2238 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2239 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2240 | UPPC_NonTypeTemplateParameterType)) { |
| 2241 | Invalid = true; |
| 2242 | continue; |
| 2243 | } |
| 2244 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2245 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2246 | if (NewNonTypeParm->hasDefaultArgument() && |
| 2247 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2248 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2249 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2250 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2253 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2254 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2255 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2256 | if (NewNonTypeParm->isParameterPack()) { |
| 2257 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 2258 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2259 | if (!NewNonTypeParm->isPackExpansion()) |
| 2260 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2261 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2262 | NewNonTypeParm->hasDefaultArgument() && |
| 2263 | (!SkipBody || !SkipBody->ShouldSkip)) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2264 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2265 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2266 | SawDefaultArgument = true; |
| 2267 | RedundantDefaultArg = true; |
| 2268 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2269 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 2270 | // Merge the default argument from the old declaration to the |
| 2271 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2272 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2273 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2274 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 2275 | SawDefaultArgument = true; |
| 2276 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2277 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2279 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2280 | TemplateTemplateParmDecl *NewTemplateParm |
| 2281 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2282 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2283 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 2284 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2285 | Invalid = true; |
| 2286 | continue; |
| 2287 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2288 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2289 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2290 | if (NewTemplateParm->hasDefaultArgument() && |
| 2291 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2292 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2293 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2294 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2295 | |
| 2296 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2297 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2298 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2299 | if (NewTemplateParm->isParameterPack()) { |
| 2300 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 2301 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2302 | if (!NewTemplateParm->isPackExpansion()) |
| 2303 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2304 | } else if (OldTemplateParm && |
| 2305 | hasVisibleDefaultArgument(OldTemplateParm) && |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 2306 | NewTemplateParm->hasDefaultArgument() && |
| 2307 | (!SkipBody || !SkipBody->ShouldSkip)) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2308 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 2309 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2310 | SawDefaultArgument = true; |
| 2311 | RedundantDefaultArg = true; |
| 2312 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2313 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 2314 | // Merge the default argument from the old declaration to the |
| 2315 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2316 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2317 | PreviousDefaultArgLoc |
| 2318 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2319 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 2320 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2321 | PreviousDefaultArgLoc |
| 2322 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2323 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2327 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2328 | // If a template parameter of a primary class template or alias template |
| 2329 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2330 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2331 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 2332 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2333 | Diag((*NewParam)->getLocation(), |
| 2334 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 2335 | Invalid = true; |
| 2336 | } |
| 2337 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2338 | if (RedundantDefaultArg) { |
| 2339 | // C++ [temp.param]p12: |
| 2340 | // A template-parameter shall not be given default arguments |
| 2341 | // by two different declarations in the same scope. |
| 2342 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 2343 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 2344 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 2345 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2346 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2347 | // If a template-parameter of a class template has a default |
| 2348 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 2349 | // have a default template-argument supplied or be a template parameter |
| 2350 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2352 | diag::err_template_param_default_arg_missing); |
| 2353 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 2354 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2355 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | // If we have an old template parameter list that we're merging |
| 2359 | // in, move on to the next parameter. |
| 2360 | if (OldParams) |
| 2361 | ++OldParam; |
| 2362 | } |
| 2363 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2364 | // We were missing some default arguments at the end of the list, so remove |
| 2365 | // all of the default arguments. |
| 2366 | if (RemoveDefaultArguments) { |
| 2367 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2368 | NewParamEnd = NewParams->end(); |
| 2369 | NewParam != NewParamEnd; ++NewParam) { |
| 2370 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 2371 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2372 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2373 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 2374 | NTTP->removeDefaultArgument(); |
| 2375 | else |
| 2376 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 2377 | } |
| 2378 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2379 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2380 | return Invalid; |
| 2381 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2382 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2383 | namespace { |
| 2384 | |
| 2385 | /// A class which looks for a use of a certain level of template |
| 2386 | /// parameter. |
| 2387 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 2388 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 2389 | |
| 2390 | unsigned Depth; |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2391 | |
| 2392 | // Whether we're looking for a use of a template parameter that makes the |
| 2393 | // overall construct type-dependent / a dependent type. This is strictly |
| 2394 | // best-effort for now; we may fail to match at all for a dependent type |
| 2395 | // in some cases if this is set. |
| 2396 | bool IgnoreNonTypeDependent; |
| 2397 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2398 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2399 | SourceLocation MatchLoc; |
| 2400 | |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2401 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
| 2402 | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
| 2403 | Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2404 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2405 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2406 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
| 2407 | NamedDecl *ND = Params->getParam(0); |
| 2408 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 2409 | Depth = PD->getDepth(); |
| 2410 | } else if (NonTypeTemplateParmDecl *PD = |
| 2411 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 2412 | Depth = PD->getDepth(); |
| 2413 | } else { |
| 2414 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 2415 | } |
| 2416 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2417 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2418 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2419 | if (ParmDepth >= Depth) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2420 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2421 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2422 | return true; |
| 2423 | } |
| 2424 | return false; |
| 2425 | } |
| 2426 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2427 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 2428 | // Prune out non-type-dependent expressions if requested. This can |
| 2429 | // sometimes result in us failing to find a template parameter reference |
| 2430 | // (if a value-dependent expression creates a dependent type), but this |
| 2431 | // mode is best-effort only. |
| 2432 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 2433 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 2434 | return true; |
| 2435 | return super::TraverseStmt(S, Q); |
| 2436 | } |
| 2437 | |
| 2438 | bool TraverseTypeLoc(TypeLoc TL) { |
| 2439 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 2440 | !TL.getType()->isDependentType()) |
| 2441 | return true; |
| 2442 | return super::TraverseTypeLoc(TL); |
| 2443 | } |
| 2444 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2445 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2446 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 2447 | } |
| 2448 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2449 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2450 | // For a best-effort search, keep looking until we find a location. |
| 2451 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | bool TraverseTemplateName(TemplateName N) { |
| 2455 | if (TemplateTemplateParmDecl *PD = |
| 2456 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2457 | if (Matches(PD->getDepth())) |
| 2458 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2459 | return super::TraverseTemplateName(N); |
| 2460 | } |
| 2461 | |
| 2462 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 2463 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2464 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 2465 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2466 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2467 | return super::VisitDeclRefExpr(E); |
| 2468 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2469 | |
| 2470 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 2471 | return TraverseType(T->getReplacementType()); |
| 2472 | } |
| 2473 | |
| 2474 | bool |
| 2475 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 2476 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 2477 | } |
| 2478 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 2479 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 2480 | return TraverseType(T->getInjectedSpecializationType()); |
| 2481 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2482 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2483 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2484 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2485 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2486 | /// list. |
| 2487 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2488 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2489 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2490 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2491 | return Checker.Match; |
| 2492 | } |
| 2493 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2494 | // Find the source range corresponding to the named type in the given |
| 2495 | // nested-name-specifier, if any. |
| 2496 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 2497 | QualType T, |
| 2498 | const CXXScopeSpec &SS) { |
| 2499 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 2500 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 2501 | if (const Type *CurType = NNS->getAsType()) { |
| 2502 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 2503 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 2504 | } else |
| 2505 | break; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2506 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2507 | NNSLoc = NNSLoc.getPrefix(); |
| 2508 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2509 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2510 | return SourceRange(); |
| 2511 | } |
| 2512 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2513 | /// Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2514 | /// specifier, returning the template parameter list that applies to the |
| 2515 | /// name. |
| 2516 | /// |
| 2517 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 2518 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2520 | /// \param DeclLoc The location of the declaration itself. |
| 2521 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2522 | /// \param SS the scope specifier that will be matched to the given template |
| 2523 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 2524 | /// being declared. |
| 2525 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2526 | /// \param TemplateId The template-id following the scope specifier, if there |
| 2527 | /// is one. Used to check for a missing 'template<>'. |
| 2528 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2529 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 2530 | /// innermost template parameter lists. |
| 2531 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2532 | /// \param IsFriend Whether to apply the slightly different rules for |
| 2533 | /// matching template parameters to scope specifiers in friend |
| 2534 | /// declarations. |
| 2535 | /// |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2536 | /// \param IsMemberSpecialization will be set true if the scope specifier |
| 2537 | /// denotes a fully-specialized type, and therefore this is a declaration of |
| 2538 | /// a member specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2539 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2540 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2541 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2542 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2544 | /// 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] | 2545 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2546 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 2547 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2548 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2549 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2550 | bool &IsMemberSpecialization, bool &Invalid) { |
| 2551 | IsMemberSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2552 | Invalid = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2553 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2554 | // The sequence of nested types to which we will match up the template |
| 2555 | // parameter lists. We first build this list by starting with the type named |
| 2556 | // 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] | 2557 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2558 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2559 | if (SS.getScopeRep()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2560 | if (CXXRecordDecl *Record |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2561 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 2562 | T = Context.getTypeDeclType(Record); |
| 2563 | else |
| 2564 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 2565 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2566 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2567 | // If we found an explicit specialization that prevents us from needing |
| 2568 | // 'template<>' headers, this will be set to the location of that |
| 2569 | // explicit specialization. |
| 2570 | SourceLocation ExplicitSpecLoc; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2571 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2572 | while (!T.isNull()) { |
| 2573 | NestedTypes.push_back(T); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2574 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2575 | // Retrieve the parent of a record type. |
| 2576 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2577 | // If this type is an explicit specialization, we're done. |
| 2578 | if (ClassTemplateSpecializationDecl *Spec |
| 2579 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2580 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2581 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 2582 | ExplicitSpecLoc = Spec->getLocation(); |
| 2583 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 2584 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2585 | } else if (Record->getTemplateSpecializationKind() |
| 2586 | == TSK_ExplicitSpecialization) { |
| 2587 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2588 | break; |
| 2589 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2590 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2591 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 2592 | T = Context.getTypeDeclType(Parent); |
| 2593 | else |
| 2594 | T = QualType(); |
| 2595 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2598 | if (const TemplateSpecializationType *TST |
| 2599 | = T->getAs<TemplateSpecializationType>()) { |
| 2600 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 2601 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 2602 | T = Context.getTypeDeclType(Parent); |
| 2603 | else |
| 2604 | T = QualType(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2605 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2606 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2607 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2608 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2609 | // Look one step prior in a dependent template specialization type. |
| 2610 | if (const DependentTemplateSpecializationType *DependentTST |
| 2611 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2612 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 2613 | T = QualType(NNS->getAsType(), 0); |
| 2614 | else |
| 2615 | T = QualType(); |
| 2616 | continue; |
| 2617 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2618 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2619 | // Look one step prior in a dependent name type. |
| 2620 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 2621 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 2622 | T = QualType(NNS->getAsType(), 0); |
| 2623 | else |
| 2624 | T = QualType(); |
| 2625 | continue; |
| 2626 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2628 | // Retrieve the parent of an enumeration type. |
| 2629 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 2630 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 2631 | // check here. |
| 2632 | EnumDecl *Enum = EnumT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2634 | // Get to the parent type. |
| 2635 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 2636 | T = Context.getTypeDeclType(Parent); |
| 2637 | else |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2638 | T = QualType(); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2639 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2640 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2641 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2642 | T = QualType(); |
| 2643 | } |
| 2644 | // Reverse the nested types list, since we want to traverse from the outermost |
| 2645 | // to the innermost while checking template-parameter-lists. |
| 2646 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2647 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2648 | // C++0x [temp.expl.spec]p17: |
| 2649 | // A member or a member template may be nested within many |
| 2650 | // enclosing class templates. In an explicit specialization for |
| 2651 | // such a member, the member declaration shall be preceded by a |
| 2652 | // template<> for each enclosing class template that is |
| 2653 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2654 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2655 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2656 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2657 | if (SawNonEmptyTemplateParameterList) { |
| 2658 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 2659 | << !Recovery << Range; |
| 2660 | Invalid = true; |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2661 | IsMemberSpecialization = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2662 | return true; |
| 2663 | } |
| 2664 | |
| 2665 | return false; |
| 2666 | }; |
| 2667 | |
| 2668 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 2669 | // Check that we can have an explicit specialization here. |
| 2670 | if (CheckExplicitSpecialization(Range, true)) |
| 2671 | return true; |
| 2672 | |
| 2673 | // We don't have a template header, but we should. |
| 2674 | SourceLocation ExpectedTemplateLoc; |
| 2675 | if (!ParamLists.empty()) |
| 2676 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 2677 | else |
| 2678 | ExpectedTemplateLoc = DeclStartLoc; |
| 2679 | |
| 2680 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 2681 | << Range |
| 2682 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 2683 | return false; |
| 2684 | }; |
| 2685 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2686 | unsigned ParamIdx = 0; |
| 2687 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 2688 | ++TypeIdx) { |
| 2689 | T = NestedTypes[TypeIdx]; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2690 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2691 | // Whether we expect a 'template<>' header. |
| 2692 | bool NeedEmptyTemplateHeader = false; |
| 2693 | |
| 2694 | // Whether we expect a template header with parameters. |
| 2695 | bool NeedNonemptyTemplateHeader = false; |
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 | // For a dependent type, the set of template parameters that we |
| 2698 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2699 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2701 | // C++0x [temp.expl.spec]p15: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2702 | // A member or a member template may be nested within many enclosing |
| 2703 | // class templates. In an explicit specialization for such a member, the |
| 2704 | // member declaration shall be preceded by a template<> for each |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2705 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2706 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2707 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 2708 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 2709 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 2710 | NeedNonemptyTemplateHeader = true; |
| 2711 | } else if (Record->isDependentType()) { |
| 2712 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2713 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2714 | ->getTemplateParameters(); |
| 2715 | NeedNonemptyTemplateHeader = true; |
| 2716 | } |
| 2717 | } else if (ClassTemplateSpecializationDecl *Spec |
| 2718 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 2719 | // C++0x [temp.expl.spec]p4: |
| 2720 | // Members of an explicitly specialized class template are defined |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2721 | // in the same manner as members of normal classes, and not using |
| 2722 | // the template<> syntax. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2723 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 2724 | NeedEmptyTemplateHeader = true; |
| 2725 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 2726 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2727 | } else if (Record->getTemplateSpecializationKind()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2728 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2729 | != TSK_ExplicitSpecialization && |
| 2730 | TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2731 | IsMemberSpecialization = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2733 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2734 | } |
| 2735 | } else if (const TemplateSpecializationType *TST |
| 2736 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 2737 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2738 | ExpectedTemplateParams = Template->getTemplateParameters(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2739 | NeedNonemptyTemplateHeader = true; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2740 | } |
| 2741 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2742 | // FIXME: We actually could/should check the template arguments here |
| 2743 | // against the corresponding template parameter list. |
| 2744 | NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2747 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2748 | // In an explicit specialization declaration for a member of a class |
| 2749 | // template or a member template that ap- pears in namespace scope, the |
| 2750 | // member template and some of its enclosing class templates may remain |
| 2751 | // unspecialized, except that the declaration shall not explicitly |
| 2752 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2753 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2754 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2755 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2756 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2757 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2758 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2759 | } else |
| 2760 | SawNonEmptyTemplateParameterList = true; |
| 2761 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2762 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2763 | if (NeedEmptyTemplateHeader) { |
| 2764 | // 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] | 2765 | // here, then it's a member specialization. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2766 | if (TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2767 | IsMemberSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2768 | |
| 2769 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2770 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2771 | // The header has template parameters when it shouldn't. Complain. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2772 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2773 | diag::err_template_param_list_matches_nontemplate) |
| 2774 | << T |
| 2775 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2776 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2777 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2778 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2779 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2780 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2782 | // Consume this template header. |
| 2783 | ++ParamIdx; |
| 2784 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2785 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2786 | |
| 2787 | if (!IsFriend) |
| 2788 | if (DiagnoseMissingExplicitSpecialization( |
| 2789 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2790 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2792 | continue; |
| 2793 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2794 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2795 | if (NeedNonemptyTemplateHeader) { |
| 2796 | // In friend declarations we can have template-ids which don't |
| 2797 | // depend on the corresponding template parameter lists. But |
| 2798 | // assume that empty parameter lists are supposed to match this |
| 2799 | // template-id. |
| 2800 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2801 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2802 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2803 | ExpectedTemplateParams = nullptr; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2804 | else |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2805 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2807 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2808 | if (ParamIdx < ParamLists.size()) { |
| 2809 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2810 | if (ExpectedTemplateParams && |
| 2811 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2812 | ExpectedTemplateParams, |
| 2813 | true, TPL_TemplateMatch)) |
| 2814 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2816 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2817 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2818 | TPC_ClassTemplateMember)) |
| 2819 | Invalid = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2820 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2821 | ++ParamIdx; |
| 2822 | continue; |
| 2823 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2824 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2825 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2826 | << T |
| 2827 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2828 | Invalid = true; |
| 2829 | continue; |
| 2830 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2831 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2832 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2833 | // If there were at least as many template-ids as there were template |
| 2834 | // parameter lists, then there are no template parameter lists remaining for |
| 2835 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2836 | if (ParamIdx >= ParamLists.size()) { |
| 2837 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2838 | // We don't have a template header for the declaration itself, but we |
| 2839 | // should. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2840 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2841 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2842 | |
| 2843 | // Fabricate an empty template parameter list for the invented header. |
| 2844 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2845 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2846 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2849 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2850 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2852 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2853 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2854 | bool HasAnyExplicitSpecHeader = false; |
| 2855 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2856 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2857 | if (ParamLists[I]->size() == 0) |
| 2858 | HasAnyExplicitSpecHeader = true; |
| 2859 | else |
| 2860 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2861 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2863 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2864 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2865 | : diag::err_template_spec_extra_headers) |
| 2866 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2867 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2868 | |
| 2869 | // If there was a specialization somewhere, such that 'template<>' is |
| 2870 | // not required, and there were any 'template<>' headers, note where the |
| 2871 | // specialization occurred. |
| 2872 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2873 | Diag(ExplicitSpecLoc, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2874 | diag::note_explicit_template_spec_does_not_need_header) |
| 2875 | << NestedTypes.back(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2876 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2877 | // We have a template parameter list with no corresponding scope, which |
| 2878 | // means that the resulting template declaration can't be instantiated |
| 2879 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2880 | if (!AllExplicitSpecHeaders) |
| 2881 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2882 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2884 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2885 | // In an explicit specialization declaration for a member of a class |
| 2886 | // template or a member template that ap- pears in namespace scope, the |
| 2887 | // member template and some of its enclosing class templates may remain |
| 2888 | // unspecialized, except that the declaration shall not explicitly |
| 2889 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2890 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2891 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2892 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2893 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2894 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2895 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2896 | // Return the last template parameter list, which corresponds to the |
| 2897 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2898 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2901 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2902 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2903 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2904 | << (isa<FunctionTemplateDecl>(Template) |
| 2905 | ? 0 |
| 2906 | : isa<ClassTemplateDecl>(Template) |
| 2907 | ? 1 |
| 2908 | : isa<VarTemplateDecl>(Template) |
| 2909 | ? 2 |
| 2910 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2911 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2912 | return; |
| 2913 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2914 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2915 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2916 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2917 | IEnd = OST->end(); |
| 2918 | I != IEnd; ++I) |
| 2919 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2920 | << 0 << (*I)->getDeclName(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2921 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2922 | return; |
| 2923 | } |
| 2924 | } |
| 2925 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2926 | static QualType |
| 2927 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2928 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2929 | SourceLocation TemplateLoc, |
| 2930 | TemplateArgumentListInfo &TemplateArgs) { |
| 2931 | ASTContext &Context = SemaRef.getASTContext(); |
| 2932 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2933 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2934 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2935 | // S<T, 0, ..., N-1>. |
| 2936 | |
| 2937 | // C++14 [inteseq.intseq]p1: |
| 2938 | // T shall be an integer type. |
| 2939 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2940 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2941 | diag::err_integer_sequence_integral_element_type); |
| 2942 | return QualType(); |
| 2943 | } |
| 2944 | |
| 2945 | // C++14 [inteseq.make]p1: |
| 2946 | // If N is negative the program is ill-formed. |
| 2947 | TemplateArgument NumArgsArg = Converted[2]; |
| 2948 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2949 | if (NumArgs < 0) { |
| 2950 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2951 | diag::err_integer_sequence_negative_length); |
| 2952 | return QualType(); |
| 2953 | } |
| 2954 | |
| 2955 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2956 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2957 | // The type argument gets reused as the first template argument in the |
| 2958 | // synthetic template argument list. |
| 2959 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2960 | // Expand N into 0 ... N-1. |
| 2961 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2962 | I < NumArgs; ++I) { |
| 2963 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2964 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2965 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2966 | } |
| 2967 | // The first template argument will be reused as the template decl that |
| 2968 | // our synthetic template arguments will be applied to. |
| 2969 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2970 | TemplateLoc, SyntheticTemplateArgs); |
| 2971 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2972 | |
| 2973 | case BTK__type_pack_element: |
| 2974 | // Specializations of |
| 2975 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2976 | // are treated like T_Index. |
| 2977 | assert(Converted.size() == 2 && |
| 2978 | "__type_pack_element should be given an index and a parameter pack"); |
| 2979 | |
| 2980 | // If the Index is out of bounds, the program is ill-formed. |
| 2981 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2982 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2983 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2984 | "type std::size_t, and hence be non-negative"); |
| 2985 | if (Index >= Ts.pack_size()) { |
| 2986 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2987 | diag::err_type_pack_element_out_of_bounds); |
| 2988 | return QualType(); |
| 2989 | } |
| 2990 | |
| 2991 | // We simply return the type at index `Index`. |
| 2992 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2993 | return Nth->getAsType(); |
| 2994 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2995 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2996 | } |
| 2997 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 2998 | /// Determine whether this alias template is "enable_if_t". |
| 2999 | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { |
| 3000 | return AliasTemplate->getName().equals("enable_if_t"); |
| 3001 | } |
| 3002 | |
| 3003 | /// Collect all of the separable terms in the given condition, which |
| 3004 | /// might be a conjunction. |
| 3005 | /// |
| 3006 | /// FIXME: The right answer is to convert the logical expression into |
| 3007 | /// disjunctive normal form, so we can find the first failed term |
| 3008 | /// within each possible clause. |
| 3009 | static void collectConjunctionTerms(Expr *Clause, |
| 3010 | SmallVectorImpl<Expr *> &Terms) { |
| 3011 | if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { |
| 3012 | if (BinOp->getOpcode() == BO_LAnd) { |
| 3013 | collectConjunctionTerms(BinOp->getLHS(), Terms); |
| 3014 | collectConjunctionTerms(BinOp->getRHS(), Terms); |
| 3015 | } |
| 3016 | |
| 3017 | return; |
| 3018 | } |
| 3019 | |
| 3020 | Terms.push_back(Clause); |
| 3021 | } |
| 3022 | |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3023 | // The ranges-v3 library uses an odd pattern of a top-level "||" with |
| 3024 | // a left-hand side that is value-dependent but never true. Identify |
| 3025 | // the idiom and ignore that term. |
| 3026 | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { |
| 3027 | // Top-level '||'. |
| 3028 | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); |
| 3029 | if (!BinOp) return Cond; |
| 3030 | |
| 3031 | if (BinOp->getOpcode() != BO_LOr) return Cond; |
| 3032 | |
| 3033 | // With an inner '==' that has a literal on the right-hand side. |
| 3034 | Expr *LHS = BinOp->getLHS(); |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3035 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3036 | if (!InnerBinOp) return Cond; |
| 3037 | |
| 3038 | if (InnerBinOp->getOpcode() != BO_EQ || |
| 3039 | !isa<IntegerLiteral>(InnerBinOp->getRHS())) |
| 3040 | return Cond; |
| 3041 | |
| 3042 | // If the inner binary operation came from a macro expansion named |
| 3043 | // CONCEPT_REQUIRES or CONCEPT_REQUIRES_, return the right-hand side |
| 3044 | // of the '||', which is the real, user-provided condition. |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3045 | SourceLocation Loc = InnerBinOp->getExprLoc(); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3046 | if (!Loc.isMacroID()) return Cond; |
| 3047 | |
| 3048 | StringRef MacroName = PP.getImmediateMacroName(Loc); |
| 3049 | if (MacroName == "CONCEPT_REQUIRES" || MacroName == "CONCEPT_REQUIRES_") |
| 3050 | return BinOp->getRHS(); |
| 3051 | |
| 3052 | return Cond; |
| 3053 | } |
| 3054 | |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3055 | namespace { |
| 3056 | |
| 3057 | // A PrinterHelper that prints more helpful diagnostics for some sub-expressions |
| 3058 | // within failing boolean expression, such as substituting template parameters |
| 3059 | // for actual types. |
| 3060 | class FailedBooleanConditionPrinterHelper : public PrinterHelper { |
| 3061 | public: |
| 3062 | explicit FailedBooleanConditionPrinterHelper(const PrintingPolicy &P) |
| 3063 | : Policy(P) {} |
| 3064 | |
| 3065 | bool handledStmt(Stmt *E, raw_ostream &OS) override { |
| 3066 | const auto *DR = dyn_cast<DeclRefExpr>(E); |
| 3067 | if (DR && DR->getQualifier()) { |
| 3068 | // If this is a qualified name, expand the template arguments in nested |
| 3069 | // qualifiers. |
| 3070 | DR->getQualifier()->print(OS, Policy, true); |
| 3071 | // Then print the decl itself. |
| 3072 | const ValueDecl *VD = DR->getDecl(); |
| 3073 | OS << VD->getName(); |
| 3074 | if (const auto *IV = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
| 3075 | // This is a template variable, print the expanded template arguments. |
| 3076 | printTemplateArgumentList(OS, IV->getTemplateArgs().asArray(), Policy); |
| 3077 | } |
| 3078 | return true; |
Clement Courbet | 9d432e0 | 2018-12-04 07:59:57 +0000 | [diff] [blame] | 3079 | } |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3080 | return false; |
Clement Courbet | 9d432e0 | 2018-12-04 07:59:57 +0000 | [diff] [blame] | 3081 | } |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3082 | |
| 3083 | private: |
| 3084 | const PrintingPolicy Policy; |
| 3085 | }; |
| 3086 | |
| 3087 | } // end anonymous namespace |
Clement Courbet | 9d432e0 | 2018-12-04 07:59:57 +0000 | [diff] [blame] | 3088 | |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3089 | std::pair<Expr *, std::string> |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3090 | Sema::findFailedBooleanCondition(Expr *Cond) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3091 | Cond = lookThroughRangesV3Condition(PP, Cond); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3092 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3093 | // Separate out all of the terms in a conjunction. |
| 3094 | SmallVector<Expr *, 4> Terms; |
| 3095 | collectConjunctionTerms(Cond, Terms); |
| 3096 | |
| 3097 | // Determine which term failed. |
| 3098 | Expr *FailedCond = nullptr; |
| 3099 | for (Expr *Term : Terms) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3100 | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); |
| 3101 | |
Clement Courbet | d872041 | 2018-12-10 08:53:17 +0000 | [diff] [blame] | 3102 | // Literals are uninteresting. |
| 3103 | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || |
| 3104 | isa<IntegerLiteral>(TermAsWritten)) |
| 3105 | continue; |
| 3106 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3107 | // The initialization of the parameter from the argument is |
| 3108 | // a constant-evaluated context. |
| 3109 | EnterExpressionEvaluationContext ConstantEvaluated( |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3110 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3111 | |
| 3112 | bool Succeeded; |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3113 | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3114 | !Succeeded) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3115 | FailedCond = TermAsWritten; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3116 | break; |
| 3117 | } |
| 3118 | } |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3119 | if (!FailedCond) |
Clement Courbet | d872041 | 2018-12-10 08:53:17 +0000 | [diff] [blame] | 3120 | FailedCond = Cond->IgnoreParenImpCasts(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3121 | |
| 3122 | std::string Description; |
| 3123 | { |
| 3124 | llvm::raw_string_ostream Out(Description); |
Clement Courbet | fb2c74d | 2018-12-20 09:05:15 +0000 | [diff] [blame] | 3125 | PrintingPolicy Policy = getPrintingPolicy(); |
| 3126 | Policy.PrintCanonicalTypes = true; |
| 3127 | FailedBooleanConditionPrinterHelper Helper(Policy); |
| 3128 | FailedCond->printPretty(Out, &Helper, Policy, 0, "\n", nullptr); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3129 | } |
| 3130 | return { FailedCond, Description }; |
| 3131 | } |
| 3132 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3133 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 3134 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3135 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3136 | DependentTemplateName *DTN |
| 3137 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3138 | if (DTN && DTN->isIdentifier()) |
| 3139 | // When building a template-id where the template-name is dependent, |
| 3140 | // assume the template is a type template. Either our assumption is |
| 3141 | // correct, or the code is ill-formed and will be diagnosed when the |
| 3142 | // dependent name is substituted. |
| 3143 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 3144 | DTN->getQualifier(), |
| 3145 | DTN->getIdentifier(), |
| 3146 | TemplateArgs); |
| 3147 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3148 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 3149 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 3150 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3151 | // We might have a substituted template template parameter pack. If so, |
| 3152 | // build a template specialization type for it. |
| 3153 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 3154 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3155 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3156 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 3157 | << Name; |
| 3158 | NoteAllFoundTemplates(Name); |
| 3159 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3160 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3161 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3162 | // Check that the template argument list is well-formed for this |
| 3163 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3164 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3165 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3166 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3167 | return QualType(); |
| 3168 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3169 | QualType CanonType; |
| 3170 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3171 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3172 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 3173 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3174 | // Find the canonical type for this type alias template specialization. |
| 3175 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 3176 | if (Pattern->isInvalidDecl()) |
| 3177 | return QualType(); |
| 3178 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3179 | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, |
| 3180 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3181 | |
| 3182 | // Only substitute for the innermost template argument list. |
| 3183 | MultiLevelTemplateArgumentList TemplateArgLists; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3184 | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 3185 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 3186 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 3187 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3188 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3189 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3190 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3191 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3192 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3193 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3194 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 3195 | TemplateArgLists, AliasTemplate->getLocation(), |
| 3196 | AliasTemplate->getDeclName()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3197 | if (CanonType.isNull()) { |
| 3198 | // If this was enable_if and we failed to find the nested type |
| 3199 | // within enable_if in a SFINAE context, dig out the specific |
| 3200 | // enable_if condition that failed and present that instead. |
| 3201 | if (isEnableIfAliasTemplate(AliasTemplate)) { |
| 3202 | if (auto DeductionInfo = isSFINAEContext()) { |
| 3203 | if (*DeductionInfo && |
| 3204 | (*DeductionInfo)->hasSFINAEDiagnostic() && |
| 3205 | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == |
| 3206 | diag::err_typename_nested_not_found_enable_if && |
| 3207 | TemplateArgs[0].getArgument().getKind() |
| 3208 | == TemplateArgument::Expression) { |
| 3209 | Expr *FailedCond; |
| 3210 | std::string FailedDescription; |
| 3211 | std::tie(FailedCond, FailedDescription) = |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 3212 | findFailedBooleanCondition(TemplateArgs[0].getSourceExpression()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3213 | |
| 3214 | // Remove the old SFINAE diagnostic. |
| 3215 | PartialDiagnosticAt OldDiag = |
| 3216 | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; |
| 3217 | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); |
| 3218 | |
| 3219 | // Add a new SFINAE diagnostic specifying which condition |
| 3220 | // failed. |
| 3221 | (*DeductionInfo)->addSFINAEDiagnostic( |
| 3222 | OldDiag.first, |
| 3223 | PDiag(diag::err_typename_nested_not_found_requirement) |
| 3224 | << FailedDescription |
| 3225 | << FailedCond->getSourceRange()); |
| 3226 | } |
| 3227 | } |
| 3228 | } |
| 3229 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3230 | return QualType(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3231 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3232 | } else if (Name.isDependent() || |
| 3233 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3234 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3235 | // This class template specialization is a dependent |
| 3236 | // type. Therefore, its canonical type is another class template |
| 3237 | // specialization type that contains all of the converted |
| 3238 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 3239 | // A<T, T> have identical types when A is declared as: |
| 3240 | // |
| 3241 | // template<typename T, typename U = T> struct A; |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 3242 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3243 | |
| 3244 | // This might work out to be a current instantiation, in which |
| 3245 | // case the canonical type needs to be the InjectedClassNameType. |
| 3246 | // |
| 3247 | // TODO: in theory this could be a simple hashtable lookup; most |
| 3248 | // changes to CurContext don't change the set of current |
| 3249 | // instantiations. |
| 3250 | if (isa<ClassTemplateDecl>(Template)) { |
| 3251 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 3252 | // If we get out to a namespace, we're done. |
| 3253 | if (Ctx->isFileContext()) break; |
| 3254 | |
| 3255 | // If this isn't a record, keep looking. |
| 3256 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 3257 | if (!Record) continue; |
| 3258 | |
| 3259 | // Look for one of the two cases with InjectedClassNameTypes |
| 3260 | // and check whether it's the same template. |
| 3261 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 3262 | !Record->getDescribedClassTemplate()) |
| 3263 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3264 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3265 | // Fetch the injected class name type and check whether its |
| 3266 | // injected type is equal to the type we just built. |
| 3267 | QualType ICNT = Context.getTypeDeclType(Record); |
| 3268 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 3269 | ->getInjectedSpecializationType(); |
| 3270 | |
| 3271 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 3272 | continue; |
| 3273 | |
| 3274 | // If so, the canonical type of this TST is the injected |
| 3275 | // class name type of the record we just found. |
| 3276 | assert(ICNT.isCanonical()); |
| 3277 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3278 | break; |
| 3279 | } |
| 3280 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3282 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3283 | // Find the class template specialization declaration that |
| 3284 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3285 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3286 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3287 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3288 | if (!Decl) { |
| 3289 | // This is the first time we have referenced this class template |
| 3290 | // specialization. Create the canonical declaration and add it to |
| 3291 | // the set of specializations. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3292 | Decl = ClassTemplateSpecializationDecl::Create( |
| 3293 | Context, ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 3294 | ClassTemplate->getDeclContext(), |
| 3295 | ClassTemplate->getTemplatedDecl()->getBeginLoc(), |
| 3296 | ClassTemplate->getLocation(), ClassTemplate, Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 3297 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 3298 | if (ClassTemplate->isOutOfLine()) |
| 3299 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 3302 | if (Decl->getSpecializationKind() == TSK_Undeclared) { |
| 3303 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3304 | TemplateArgLists.addOuterTemplateArguments(Converted); |
| 3305 | InstantiateAttrsForDecl(TemplateArgLists, ClassTemplate->getTemplatedDecl(), |
| 3306 | Decl); |
| 3307 | } |
| 3308 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 3309 | // Diagnose uses of this specialization. |
| 3310 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 3311 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3312 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3313 | assert(isa<RecordType>(CanonType) && |
| 3314 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3315 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 3316 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 3317 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3318 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3320 | // Build the fully-sugared type for this class template |
| 3321 | // specialization, which refers back to the class template |
| 3322 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 3323 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3326 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3327 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3328 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
| 3329 | SourceLocation TemplateIILoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3330 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3331 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3332 | SourceLocation RAngleLoc, |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3333 | bool IsCtorOrDtorName, bool IsClassName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3334 | if (SS.isInvalid()) |
| 3335 | return true; |
| 3336 | |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3337 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { |
| 3338 | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); |
| 3339 | |
| 3340 | // C++ [temp.res]p3: |
| 3341 | // A qualified-id that refers to a type and in which the |
| 3342 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 3343 | // shall be prefixed by the keyword typename to indicate that the |
| 3344 | // qualified-id denotes a type, forming an |
| 3345 | // elaborated-type-specifier (7.1.5.3). |
| 3346 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { |
Richard Smith | 3411fbf | 2017-02-01 21:41:18 +0000 | [diff] [blame] | 3347 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3348 | << SS.getScopeRep() << TemplateII->getName(); |
| 3349 | // Recover as if 'typename' were specified. |
| 3350 | // FIXME: This is not quite correct recovery as we don't transform SS |
| 3351 | // into the corresponding dependent form (and we don't diagnose missing |
| 3352 | // 'template' keywords within SS as a result). |
| 3353 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
| 3354 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
| 3355 | TemplateArgsIn, RAngleLoc); |
| 3356 | } |
| 3357 | |
| 3358 | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
| 3359 | // it's not actually allowed to be used as a type in most cases. Because |
| 3360 | // we annotate it before we know whether it's valid, we have to check for |
| 3361 | // this case here. |
| 3362 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3363 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 3364 | Diag(TemplateIILoc, |
| 3365 | TemplateKWLoc.isInvalid() |
| 3366 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
| 3367 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 3368 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 3369 | << 1 /*if any keyword was present, it was 'template'*/; |
| 3370 | } |
| 3371 | } |
| 3372 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3373 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3374 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3375 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3376 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3377 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3378 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3379 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3380 | QualType T |
| 3381 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 3382 | DTN->getQualifier(), |
| 3383 | DTN->getIdentifier(), |
| 3384 | TemplateArgs); |
| 3385 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3386 | TypeLocBuilder TLB; |
| 3387 | DependentTemplateSpecializationTypeLoc SpecTL |
| 3388 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3389 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 3390 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3391 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3392 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3393 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3394 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3395 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3396 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3397 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3398 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3399 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3400 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3401 | if (Result.isNull()) |
| 3402 | return true; |
| 3403 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3404 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3405 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3406 | TemplateSpecializationTypeLoc SpecTL |
| 3407 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3408 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3409 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3410 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3411 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3412 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3413 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3414 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3415 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 3416 | // constructor or destructor name (in such a case, the scope specifier |
| 3417 | // will be attached to the enclosing Decl or Expr node). |
| 3418 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3419 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 3420 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 3421 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3422 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3423 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3424 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3426 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3427 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3428 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3429 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3430 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3431 | SourceLocation TagLoc, |
| 3432 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3433 | SourceLocation TemplateKWLoc, |
| 3434 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3435 | SourceLocation TemplateLoc, |
| 3436 | SourceLocation LAngleLoc, |
| 3437 | ASTTemplateArgsPtr TemplateArgsIn, |
| 3438 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3439 | TemplateName Template = TemplateD.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3440 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3441 | // Translate the parser's template argument list in our AST format. |
| 3442 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 3443 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3445 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3446 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3447 | ElaboratedTypeKeyword Keyword |
| 3448 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3450 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 3451 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3452 | DTN->getQualifier(), |
| 3453 | DTN->getIdentifier(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3454 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3455 | |
| 3456 | // Build type-source information. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3457 | TypeLocBuilder TLB; |
| 3458 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3459 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 3460 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 3461 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3462 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3463 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3464 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3465 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3466 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3467 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3468 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3469 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3470 | |
| 3471 | if (TypeAliasTemplateDecl *TAT = |
| 3472 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 3473 | // C++0x [dcl.type.elab]p2: |
| 3474 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 3475 | // resolves to an alias template specialization, the |
| 3476 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 3477 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 3478 | << TAT << NTK_TypeAliasTemplate << TagKind; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3479 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 3480 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3481 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3482 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 3483 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 3484 | return TypeResult(true); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3485 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3486 | // Check the tag kind |
| 3487 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3488 | RecordDecl *D = RT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3489 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3490 | IdentifierInfo *Id = D->getIdentifier(); |
| 3491 | assert(Id && "templated class must have an identifier"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3492 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 3493 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 3494 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3495 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3496 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3497 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3498 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3499 | } |
| 3500 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3502 | // Provide source-location information for the template specialization. |
| 3503 | TypeLocBuilder TLB; |
| 3504 | TemplateSpecializationTypeLoc SpecTL |
| 3505 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3506 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3507 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 3508 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3509 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3510 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3511 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3512 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3513 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3514 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3515 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 3516 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3517 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3518 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3519 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3522 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 3523 | NamedDecl *PrevDecl, |
| 3524 | SourceLocation Loc, |
| 3525 | bool IsPartialSpecialization); |
| 3526 | |
| 3527 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3528 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3529 | static bool isTemplateArgumentTemplateParameter( |
| 3530 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 3531 | switch (Arg.getKind()) { |
| 3532 | case TemplateArgument::Null: |
| 3533 | case TemplateArgument::NullPtr: |
| 3534 | case TemplateArgument::Integral: |
| 3535 | case TemplateArgument::Declaration: |
| 3536 | case TemplateArgument::Pack: |
| 3537 | case TemplateArgument::TemplateExpansion: |
| 3538 | return false; |
| 3539 | |
| 3540 | case TemplateArgument::Type: { |
| 3541 | QualType Type = Arg.getAsType(); |
| 3542 | const TemplateTypeParmType *TPT = |
| 3543 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 3544 | return TPT && !Type.hasQualifiers() && |
| 3545 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 3546 | } |
| 3547 | |
| 3548 | case TemplateArgument::Expression: { |
| 3549 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 3550 | if (!DRE || !DRE->getDecl()) |
| 3551 | return false; |
| 3552 | const NonTypeTemplateParmDecl *NTTP = |
| 3553 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 3554 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 3555 | } |
| 3556 | |
| 3557 | case TemplateArgument::Template: |
| 3558 | const TemplateTemplateParmDecl *TTP = |
| 3559 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 3560 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 3561 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 3562 | } |
| 3563 | llvm_unreachable("unexpected kind of template argument"); |
| 3564 | } |
| 3565 | |
| 3566 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 3567 | ArrayRef<TemplateArgument> Args) { |
| 3568 | if (Params->size() != Args.size()) |
| 3569 | return false; |
| 3570 | |
| 3571 | unsigned Depth = Params->getDepth(); |
| 3572 | |
| 3573 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3574 | TemplateArgument Arg = Args[I]; |
| 3575 | |
| 3576 | // If the parameter is a pack expansion, the argument must be a pack |
| 3577 | // whose only element is a pack expansion. |
| 3578 | if (Params->getParam(I)->isParameterPack()) { |
| 3579 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 3580 | !Arg.pack_begin()->isPackExpansion()) |
| 3581 | return false; |
| 3582 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 3583 | } |
| 3584 | |
| 3585 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 3586 | return false; |
| 3587 | } |
| 3588 | |
| 3589 | return true; |
| 3590 | } |
| 3591 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3592 | /// Convert the parser's template argument list representation into our form. |
| 3593 | static TemplateArgumentListInfo |
| 3594 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 3595 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 3596 | TemplateId.RAngleLoc); |
| 3597 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 3598 | TemplateId.NumArgs); |
| 3599 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 3600 | return TemplateArgs; |
| 3601 | } |
| 3602 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3603 | template<typename PartialSpecDecl> |
| 3604 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 3605 | if (Partial->getDeclContext()->isDependentContext()) |
| 3606 | return; |
| 3607 | |
| 3608 | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
| 3609 | // for non-substitution-failure issues? |
| 3610 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 3611 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 3612 | return; |
| 3613 | |
| 3614 | auto *Template = Partial->getSpecializedTemplate(); |
| 3615 | S.Diag(Partial->getLocation(), |
Richard Smith | fa4a09d | 2016-12-27 20:03:09 +0000 | [diff] [blame] | 3616 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 3617 | << isa<VarTemplateDecl>(Template); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3618 | |
| 3619 | if (Info.hasSFINAEDiagnostic()) { |
| 3620 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 3621 | PartialDiagnostic::NullDiagnostic()}; |
| 3622 | Info.takeSFINAEDiagnostic(Diag); |
| 3623 | SmallString<128> SFINAEArgString; |
| 3624 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 3625 | S.Diag(Diag.first, |
| 3626 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 3627 | << SFINAEArgString; |
| 3628 | } |
| 3629 | |
| 3630 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 3631 | } |
| 3632 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3633 | static void |
| 3634 | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
| 3635 | const llvm::SmallBitVector &DeducibleParams) { |
| 3636 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3637 | if (!DeducibleParams[I]) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 3638 | NamedDecl *Param = TemplateParams->getParam(I); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3639 | if (Param->getDeclName()) |
| 3640 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3641 | << Param->getDeclName(); |
| 3642 | else |
| 3643 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3644 | << "(anonymous)"; |
| 3645 | } |
| 3646 | } |
| 3647 | } |
| 3648 | |
| 3649 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3650 | template<typename PartialSpecDecl> |
| 3651 | static void checkTemplatePartialSpecialization(Sema &S, |
| 3652 | PartialSpecDecl *Partial) { |
| 3653 | // C++1z [temp.class.spec]p8: (DR1495) |
| 3654 | // - The specialization shall be more specialized than the primary |
| 3655 | // template (14.5.5.2). |
| 3656 | checkMoreSpecializedThanPrimary(S, Partial); |
| 3657 | |
| 3658 | // C++ [temp.class.spec]p8: (DR1315) |
| 3659 | // - Each template-parameter shall appear at least once in the |
| 3660 | // template-id outside a non-deduced context. |
| 3661 | // C++1z [temp.class.spec.match]p3 (P0127R2) |
| 3662 | // If the template arguments of a partial specialization cannot be |
| 3663 | // deduced because of the structure of its template-parameter-list |
| 3664 | // and the template-id, the program is ill-formed. |
| 3665 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 3666 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3667 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 3668 | TemplateParams->getDepth(), DeducibleParams); |
| 3669 | |
| 3670 | if (!DeducibleParams.all()) { |
| 3671 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3672 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 3673 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 3674 | << (NumNonDeducible > 1) |
| 3675 | << SourceRange(Partial->getLocation(), |
| 3676 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3677 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | void Sema::CheckTemplatePartialSpecialization( |
| 3682 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 3683 | checkTemplatePartialSpecialization(*this, Partial); |
| 3684 | } |
| 3685 | |
| 3686 | void Sema::CheckTemplatePartialSpecialization( |
| 3687 | VarTemplatePartialSpecializationDecl *Partial) { |
| 3688 | checkTemplatePartialSpecialization(*this, Partial); |
| 3689 | } |
| 3690 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3691 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
| 3692 | // C++1z [temp.param]p11: |
| 3693 | // A template parameter of a deduction guide template that does not have a |
| 3694 | // default-argument shall be deducible from the parameter-type-list of the |
| 3695 | // deduction guide template. |
| 3696 | auto *TemplateParams = TD->getTemplateParameters(); |
| 3697 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3698 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
| 3699 | for (unsigned I = 0; I != TemplateParams->size(); ++I) { |
| 3700 | // A parameter pack is deducible (to an empty pack). |
| 3701 | auto *Param = TemplateParams->getParam(I); |
| 3702 | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)) |
| 3703 | DeducibleParams[I] = true; |
| 3704 | } |
| 3705 | |
| 3706 | if (!DeducibleParams.all()) { |
| 3707 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3708 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
| 3709 | << (NumNonDeducible > 1); |
| 3710 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
| 3711 | } |
| 3712 | } |
| 3713 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3714 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3715 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 3716 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3717 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3718 | // D must be variable template id. |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 3719 | assert(D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3720 | "Variable template specialization is declared with a template it."); |
| 3721 | |
| 3722 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3723 | TemplateArgumentListInfo TemplateArgs = |
| 3724 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3725 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 3726 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 3727 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3728 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3729 | TemplateName Name = TemplateId->Template.get(); |
| 3730 | |
| 3731 | // The template-id must name a variable template. |
| 3732 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3733 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 3734 | if (!VarTemplate) { |
| 3735 | NamedDecl *FnTemplate; |
| 3736 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 3737 | FnTemplate = *OTS->begin(); |
| 3738 | else |
| 3739 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 3740 | if (FnTemplate) |
| 3741 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 3742 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3743 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 3744 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3745 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3746 | |
| 3747 | // Check for unexpanded parameter packs in any of the template arguments. |
| 3748 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 3749 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 3750 | UPPC_PartialSpecialization)) |
| 3751 | return true; |
| 3752 | |
| 3753 | // Check that the template argument list is well-formed for this |
| 3754 | // template. |
| 3755 | SmallVector<TemplateArgument, 4> Converted; |
| 3756 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 3757 | false, Converted)) |
| 3758 | return true; |
| 3759 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3760 | // Find the variable template (partial) specialization declaration that |
| 3761 | // corresponds to these arguments. |
| 3762 | if (IsPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3763 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 3764 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3765 | return true; |
| 3766 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3767 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
| 3768 | // also do them during instantiation. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3769 | bool InstantiationDependent; |
| 3770 | if (!Name.isDependent() && |
| 3771 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3772 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3773 | InstantiationDependent)) { |
| 3774 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3775 | << VarTemplate->getDeclName(); |
| 3776 | IsPartialSpecialization = false; |
| 3777 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3778 | |
| 3779 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 3780 | Converted)) { |
| 3781 | // C++ [temp.class.spec]p9b3: |
| 3782 | // |
| 3783 | // -- The argument list of the specialization shall not be identical |
| 3784 | // to the implicit argument list of the primary template. |
| 3785 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 3786 | << /*variable template*/ 1 |
| 3787 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 3788 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 3789 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 3790 | // of the primary template. |
| 3791 | return true; |
| 3792 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3795 | void *InsertPos = nullptr; |
| 3796 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3797 | |
| 3798 | if (IsPartialSpecialization) |
| 3799 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3800 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3801 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3802 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3803 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3804 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3805 | |
| 3806 | // Check whether we can declare a variable template specialization in |
| 3807 | // the current scope. |
| 3808 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 3809 | TemplateNameLoc, |
| 3810 | IsPartialSpecialization)) |
| 3811 | return true; |
| 3812 | |
| 3813 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 3814 | // Since the only prior variable template specialization with these |
| 3815 | // arguments was referenced but not declared, reuse that |
| 3816 | // declaration node as our own, updating its source location and |
| 3817 | // the list of outer template parameters to reflect our new declaration. |
| 3818 | Specialization = PrevDecl; |
| 3819 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3820 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3821 | } else if (IsPartialSpecialization) { |
| 3822 | // Create a new class template partial specialization declaration node. |
| 3823 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 3824 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3825 | VarTemplatePartialSpecializationDecl *Partial = |
| 3826 | VarTemplatePartialSpecializationDecl::Create( |
| 3827 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 3828 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3829 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3830 | |
| 3831 | if (!PrevPartial) |
| 3832 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 3833 | Specialization = Partial; |
| 3834 | |
| 3835 | // If we are providing an explicit specialization of a member variable |
| 3836 | // template specialization, make a note of that. |
| 3837 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3838 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3839 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3840 | CheckTemplatePartialSpecialization(Partial); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3841 | } else { |
| 3842 | // Create a new class template specialization declaration node for |
| 3843 | // this explicit specialization or friend declaration. |
| 3844 | Specialization = VarTemplateSpecializationDecl::Create( |
| 3845 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3846 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3847 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 3848 | |
| 3849 | if (!PrevDecl) |
| 3850 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 3851 | } |
| 3852 | |
| 3853 | // C++ [temp.expl.spec]p6: |
| 3854 | // If a template, a member template or the member of a class template is |
| 3855 | // explicitly specialized then that specialization shall be declared |
| 3856 | // before the first use of that specialization that would cause an implicit |
| 3857 | // instantiation to take place, in every translation unit in which such a |
| 3858 | // use occurs; no diagnostic is required. |
| 3859 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3860 | bool Okay = false; |
| 3861 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 3862 | // Is there any previous explicit specialization declaration? |
| 3863 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3864 | Okay = true; |
| 3865 | break; |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | if (!Okay) { |
| 3870 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3871 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3872 | << Name << Range; |
| 3873 | |
| 3874 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3875 | diag::note_instantiation_required_here) |
| 3876 | << (PrevDecl->getTemplateSpecializationKind() != |
| 3877 | TSK_ImplicitInstantiation); |
| 3878 | return true; |
| 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 3883 | Specialization->setLexicalDeclContext(CurContext); |
| 3884 | |
| 3885 | // Add the specialization into its lexical context, so that it can |
| 3886 | // be seen when iterating through the list of declarations in that |
| 3887 | // context. However, specializations are not found by name lookup. |
| 3888 | CurContext->addDecl(Specialization); |
| 3889 | |
| 3890 | // Note that this is an explicit specialization. |
| 3891 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 3892 | |
| 3893 | if (PrevDecl) { |
| 3894 | // Check that this isn't a redefinition of this specialization, |
| 3895 | // merging with previous declarations. |
| 3896 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 3897 | forRedeclarationInCurContext()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3898 | PrevSpec.addDecl(PrevDecl); |
| 3899 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3900 | } else if (Specialization->isStaticDataMember() && |
| 3901 | Specialization->isOutOfLine()) { |
| 3902 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | // Link instantiations of static data members back to the template from |
| 3906 | // which they were instantiated. |
| 3907 | if (Specialization->isStaticDataMember()) |
| 3908 | Specialization->setInstantiationOfStaticDataMember( |
| 3909 | VarTemplate->getTemplatedDecl(), |
| 3910 | Specialization->getSpecializationKind()); |
| 3911 | |
| 3912 | return Specialization; |
| 3913 | } |
| 3914 | |
| 3915 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3916 | /// A partial specialization whose template arguments have matched |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3917 | /// a given template-id. |
| 3918 | struct PartialSpecMatchResult { |
| 3919 | VarTemplatePartialSpecializationDecl *Partial; |
| 3920 | TemplateArgumentList *Args; |
| 3921 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 3922 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3923 | |
| 3924 | DeclResult |
| 3925 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3926 | SourceLocation TemplateNameLoc, |
| 3927 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3928 | assert(Template && "A variable template id without template?"); |
| 3929 | |
| 3930 | // Check that the template argument list is well-formed for this template. |
| 3931 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3932 | if (CheckTemplateArgumentList( |
| 3933 | Template, TemplateNameLoc, |
| 3934 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3935 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3936 | return true; |
| 3937 | |
| 3938 | // Find the variable template specialization declaration that |
| 3939 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3940 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3941 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3942 | Converted, InsertPos)) { |
| 3943 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3944 | // If we already have a variable template specialization, return it. |
| 3945 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3946 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3947 | |
| 3948 | // This is the first time we have referenced this variable template |
| 3949 | // specialization. Create the canonical declaration and add it to |
| 3950 | // the set of specializations, based on the closest partial specialization |
| 3951 | // that it represents. That is, |
| 3952 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 3953 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3954 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3955 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 3956 | bool AmbiguousPartialSpec = false; |
| 3957 | typedef PartialSpecMatchResult MatchResult; |
| 3958 | SmallVector<MatchResult, 4> Matched; |
| 3959 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3960 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 3961 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3962 | |
| 3963 | // 1. Attempt to find the closest partial specialization that this |
| 3964 | // specializes, if any. |
| 3965 | // If any of the template arguments is dependent, then this is probably |
| 3966 | // a placeholder for an incomplete declarative context; which must be |
| 3967 | // complete by instantiation time. Thus, do not search through the partial |
| 3968 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3969 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 3970 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 3971 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3972 | bool InstantiationDependent = false; |
| 3973 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3974 | TemplateArgs, InstantiationDependent)) { |
| 3975 | |
| 3976 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3977 | Template->getPartialSpecializations(PartialSpecs); |
| 3978 | |
| 3979 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 3980 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 3981 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 3982 | |
| 3983 | if (TemplateDeductionResult Result = |
| 3984 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 3985 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3986 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3987 | FailedCandidates.addCandidate().set( |
| 3988 | DeclAccessPair::make(Template, AS_public), Partial, |
| 3989 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3990 | (void)Result; |
| 3991 | } else { |
| 3992 | Matched.push_back(PartialSpecMatchResult()); |
| 3993 | Matched.back().Partial = Partial; |
| 3994 | Matched.back().Args = Info.take(); |
| 3995 | } |
| 3996 | } |
| 3997 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3998 | if (Matched.size() >= 1) { |
| 3999 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 4000 | if (Matched.size() == 1) { |
| 4001 | // -- If exactly one matching specialization is found, the |
| 4002 | // instantiation is generated from that specialization. |
| 4003 | // We don't need to do anything for this. |
| 4004 | } else { |
| 4005 | // -- If more than one matching specialization is found, the |
| 4006 | // partial order rules (14.5.4.2) are used to determine |
| 4007 | // whether one of the specializations is more specialized |
| 4008 | // than the others. If none of the specializations is more |
| 4009 | // specialized than all of the other matching |
| 4010 | // specializations, then the use of the variable template is |
| 4011 | // ambiguous and the program is ill-formed. |
| 4012 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 4013 | PEnd = Matched.end(); |
| 4014 | P != PEnd; ++P) { |
| 4015 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 4016 | PointOfInstantiation) == |
| 4017 | P->Partial) |
| 4018 | Best = P; |
| 4019 | } |
| 4020 | |
| 4021 | // Determine if the best partial specialization is more specialized than |
| 4022 | // the others. |
| 4023 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 4024 | PEnd = Matched.end(); |
| 4025 | P != PEnd; ++P) { |
| 4026 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 4027 | P->Partial, Best->Partial, |
| 4028 | PointOfInstantiation) != Best->Partial) { |
| 4029 | AmbiguousPartialSpec = true; |
| 4030 | break; |
| 4031 | } |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | // Instantiate using the best variable template partial specialization. |
| 4036 | InstantiationPattern = Best->Partial; |
| 4037 | InstantiationArgs = Best->Args; |
| 4038 | } else { |
| 4039 | // -- If no match is found, the instantiation is generated |
| 4040 | // from the primary template. |
| 4041 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 4042 | } |
| 4043 | } |
| 4044 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4045 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4046 | // Note that we do not instantiate a definition until we see an odr-use |
| 4047 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4048 | // FIXME: LateAttrs et al.? |
| 4049 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 4050 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 4051 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 4052 | if (!Decl) |
| 4053 | return true; |
| 4054 | |
| 4055 | if (AmbiguousPartialSpec) { |
| 4056 | // Partial ordering did not produce a clear winner. Complain. |
| 4057 | Decl->setInvalidDecl(); |
| 4058 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 4059 | << Decl; |
| 4060 | |
| 4061 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 4062 | for (MatchResult P : Matched) |
| 4063 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 4064 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 4065 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4066 | return true; |
| 4067 | } |
| 4068 | |
| 4069 | if (VarTemplatePartialSpecializationDecl *D = |
| 4070 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 4071 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 4072 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4073 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 4074 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4075 | assert(Decl && "No variable template specialization?"); |
| 4076 | return Decl; |
| 4077 | } |
| 4078 | |
| 4079 | ExprResult |
| 4080 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 4081 | const DeclarationNameInfo &NameInfo, |
| 4082 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 4083 | const TemplateArgumentListInfo *TemplateArgs) { |
| 4084 | |
| 4085 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 4086 | *TemplateArgs); |
| 4087 | if (Decl.isInvalid()) |
| 4088 | return ExprError(); |
| 4089 | |
| 4090 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 4091 | if (!Var->getTemplateSpecializationKind()) |
| 4092 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 4093 | NameInfo.getLoc()); |
| 4094 | |
| 4095 | // Build an ordinary singleton decl ref. |
| 4096 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4097 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
Richard Smith | ecad88d | 2018-04-26 01:08:00 +0000 | [diff] [blame] | 4100 | void Sema::diagnoseMissingTemplateArguments(TemplateName Name, |
| 4101 | SourceLocation Loc) { |
| 4102 | Diag(Loc, diag::err_template_missing_args) |
| 4103 | << (int)getTemplateNameKindForDiagnostics(Name) << Name; |
| 4104 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) { |
| 4105 | Diag(TD->getLocation(), diag::note_template_decl_here) |
| 4106 | << TD->getTemplateParameters()->getSourceRange(); |
| 4107 | } |
| 4108 | } |
| 4109 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4110 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4111 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4112 | LookupResult &R, |
| 4113 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4114 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4115 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 4116 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | // 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] | 4118 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4119 | // foo<int> could identify a single function unambiguously |
| 4120 | // This approach does NOT work, since f<int>(1); |
| 4121 | // gets resolved prior to resorting to overload resolution |
| 4122 | // i.e., template<class T> void f(double); |
| 4123 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4124 | |
| 4125 | // These should be filtered out by our callers. |
| 4126 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 4127 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 4128 | |
Richard Smith | 0410094 | 2018-04-26 02:10:22 +0000 | [diff] [blame] | 4129 | // Non-function templates require a template argument list. |
| 4130 | if (auto *TD = R.getAsSingle<TemplateDecl>()) { |
| 4131 | if (!TemplateArgs && !isa<FunctionTemplateDecl>(TD)) { |
| 4132 | diagnoseMissingTemplateArguments(TemplateName(TD), R.getNameLoc()); |
| 4133 | return ExprError(); |
| 4134 | } |
| 4135 | } |
| 4136 | |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4137 | auto AnyDependentArguments = [&]() -> bool { |
| 4138 | bool InstantiationDependent; |
| 4139 | return TemplateArgs && |
| 4140 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 4141 | *TemplateArgs, InstantiationDependent); |
| 4142 | }; |
| 4143 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4144 | // In C++1y, check variable template ids. |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4145 | if (R.getAsSingle<VarTemplateDecl>() && !AnyDependentArguments()) { |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 4146 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 4147 | R.getAsSingle<VarTemplateDecl>(), |
| 4148 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4151 | // We don't want lookup warnings at this point. |
| 4152 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4153 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4154 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4155 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4156 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4157 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4158 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4159 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 4160 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4161 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4162 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4165 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4166 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4167 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4168 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4169 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4170 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4171 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4172 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4173 | DeclContext *DC; |
| 4174 | if (!(DC = computeDeclContext(SS, false)) || |
| 4175 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4176 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 4177 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4178 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4179 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4180 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4181 | if (LookupTemplateName(R, (Scope *)nullptr, SS, QualType(), |
| 4182 | /*Entering*/false, MemberOfUnknownSpecialization, |
| 4183 | TemplateKWLoc)) |
| 4184 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4185 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4186 | if (R.isAmbiguous()) |
| 4187 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4188 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4189 | if (R.empty()) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4190 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 4191 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4192 | return ExprError(); |
| 4193 | } |
| 4194 | |
| 4195 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4196 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4197 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 4198 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4199 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 4200 | return ExprError(); |
| 4201 | } |
| 4202 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4203 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4206 | /// Form a dependent template name. |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4207 | /// |
| 4208 | /// This action forms a dependent template name given the template |
| 4209 | /// name and its (presumably dependent) scope specifier. For |
| 4210 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 4211 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 4212 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4213 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4214 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4215 | SourceLocation TemplateKWLoc, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 4216 | const UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4217 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4218 | bool EnteringContext, |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4219 | TemplateTy &Result, |
| 4220 | bool AllowInjectedClassName) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4221 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 4222 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4223 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4224 | diag::warn_cxx98_compat_template_outside_of_template : |
| 4225 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4226 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 4227 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4228 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4229 | if (SS.isSet()) |
| 4230 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 4231 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4232 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4233 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4234 | // C++0x [temp.names]p5: |
| 4235 | // If a name prefixed by the keyword template is not the name of |
| 4236 | // a template, the program is ill-formed. [Note: the keyword |
| 4237 | // template may not be applied to non-template members of class |
| 4238 | // templates. -end note ] [ Note: as is the case with the |
| 4239 | // typename prefix, the template prefix is allowed in cases |
| 4240 | // where it is not strictly necessary; i.e., when the |
| 4241 | // nested-name-specifier or the expression on the left of the -> |
| 4242 | // or . is not dependent on a template-parameter, or the use |
| 4243 | // does not appear in the scope of a template. -end note] |
| 4244 | // |
| 4245 | // Note: C++03 was more strict here, because it banned the use of |
| 4246 | // the "template" keyword prior to a template-name that was not a |
| 4247 | // dependent name. C++ DR468 relaxed this requirement (the |
| 4248 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 4249 | // 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] | 4250 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 4251 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 4252 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4253 | MemberOfUnknownSpecialization); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4254 | if (TNK == TNK_Non_template && MemberOfUnknownSpecialization) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4255 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4256 | } else if (TNK == TNK_Non_template) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4257 | // Do the lookup again to determine if this is a "nothing found" case or |
| 4258 | // a "not a template" case. FIXME: Refactor isTemplateName so we don't |
| 4259 | // need to do this. |
| 4260 | DeclarationNameInfo DNI = GetNameFromUnqualifiedId(Name); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4261 | LookupResult R(*this, DNI.getName(), Name.getBeginLoc(), |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4262 | LookupOrdinaryName); |
| 4263 | bool MOUS; |
| 4264 | if (!LookupTemplateName(R, S, SS, ObjectType.get(), EnteringContext, |
| 4265 | MOUS, TemplateKWLoc)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4266 | Diag(Name.getBeginLoc(), diag::err_no_member) |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4267 | << DNI.getName() << LookupCtx << SS.getRange(); |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4268 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4269 | } else { |
| 4270 | // We found something; return it. |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4271 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
| 4272 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4273 | Name.getKind() == UnqualifiedIdKind::IK_Identifier && |
| 4274 | Name.Identifier && LookupRD->getIdentifier() == Name.Identifier) { |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4275 | // C++14 [class.qual]p2: |
| 4276 | // In a lookup in which function names are not ignored and the |
| 4277 | // nested-name-specifier nominates a class C, if the name specified |
| 4278 | // [...] is the injected-class-name of C, [...] the name is instead |
| 4279 | // considered to name the constructor |
| 4280 | // |
| 4281 | // We don't get here if naming the constructor would be valid, so we |
| 4282 | // just reject immediately and recover by treating the |
| 4283 | // injected-class-name as naming the template. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4284 | Diag(Name.getBeginLoc(), |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4285 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4286 | << Name.Identifier |
| 4287 | << 0 /*injected-class-name used as template name*/ |
| 4288 | << 1 /*'template' keyword was used*/; |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4289 | } |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4290 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4291 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4294 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4295 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4296 | switch (Name.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4297 | case UnqualifiedIdKind::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4298 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4299 | Name.Identifier)); |
| 4300 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4301 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4302 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4303 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4304 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 4305 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4306 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4307 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 4308 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4309 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4310 | default: |
| 4311 | break; |
| 4312 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4313 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4314 | Diag(Name.getBeginLoc(), diag::err_template_kw_refers_to_non_template) |
| 4315 | << GetNameFromUnqualifiedId(Name).getName() << Name.getSourceRange() |
| 4316 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4317 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4321 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4322 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4323 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4324 | QualType ArgType; |
| 4325 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4326 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4327 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4328 | switch(Arg.getKind()) { |
| 4329 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4330 | // C++ [temp.arg.type]p1: |
| 4331 | // A template-argument for a template-parameter which is a |
| 4332 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4333 | ArgType = Arg.getAsType(); |
| 4334 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4335 | break; |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 4336 | case TemplateArgument::Template: |
| 4337 | case TemplateArgument::TemplateExpansion: { |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4338 | // We have a template type parameter but the template argument |
| 4339 | // is a template without any arguments. |
| 4340 | SourceRange SR = AL.getSourceRange(); |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 4341 | TemplateName Name = Arg.getAsTemplateOrTemplatePattern(); |
Richard Smith | ecad88d | 2018-04-26 01:08:00 +0000 | [diff] [blame] | 4342 | diagnoseMissingTemplateArguments(Name, SR.getEnd()); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4343 | return true; |
| 4344 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4345 | case TemplateArgument::Expression: { |
| 4346 | // We have a template type parameter but the template argument is an |
| 4347 | // expression; see if maybe it is missing the "typename" keyword. |
| 4348 | CXXScopeSpec SS; |
| 4349 | DeclarationNameInfo NameInfo; |
| 4350 | |
| 4351 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 4352 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4353 | NameInfo = ArgExpr->getNameInfo(); |
| 4354 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 4355 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 4356 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4357 | NameInfo = ArgExpr->getNameInfo(); |
| 4358 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 4359 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 4360 | if (ArgExpr->isImplicitAccess()) { |
| 4361 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4362 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 4363 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4366 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4367 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 4368 | LookupParsedName(Result, CurScope, &SS); |
| 4369 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 4370 | if (Result.getAsSingle<TypeDecl>() || |
| 4371 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4372 | LookupResult::NotFoundInCurrentInstantiation) { |
| 4373 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4374 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4375 | Diag(Loc, getLangOpts().MSVCCompat |
| 4376 | ? diag::ext_ms_template_type_arg_missing_typename |
| 4377 | : diag::err_template_arg_must_be_type_suggest) |
| 4378 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4379 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4380 | |
| 4381 | // Recover by synthesizing a type using the location information that we |
| 4382 | // already have. |
| 4383 | ArgType = |
| 4384 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 4385 | TypeLocBuilder TLB; |
| 4386 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 4387 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 4388 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 4389 | TL.setNameLoc(NameInfo.getLoc()); |
| 4390 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 4391 | |
| 4392 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 4393 | // properly. |
| 4394 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 4395 | TemplateArgumentLocInfo(TSI)); |
| 4396 | |
| 4397 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | // fallthrough |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 4401 | LLVM_FALLTHROUGH; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4402 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4403 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4404 | // We have a template type parameter but the template argument |
| 4405 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 4406 | SourceRange SR = AL.getSourceRange(); |
| 4407 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4408 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4409 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4410 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4411 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4412 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4413 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4414 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4415 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4416 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4417 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4418 | ArgType = Context.getCanonicalType(ArgType); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4419 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4420 | // Objective-C ARC: |
| 4421 | // If an explicitly-specified template argument type is a lifetime type |
| 4422 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4423 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4424 | ArgType->isObjCLifetimeType() && |
| 4425 | !ArgType.getObjCLifetime()) { |
| 4426 | Qualifiers Qs; |
| 4427 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 4428 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 4429 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4430 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4431 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4432 | return false; |
| 4433 | } |
| 4434 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4435 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4436 | /// the given template type parameter. |
| 4437 | /// |
| 4438 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4439 | /// the substitution. |
| 4440 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4441 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4442 | /// for. |
| 4443 | /// |
| 4444 | /// \param TemplateLoc the location of the template name that started the |
| 4445 | /// template-id we are checking. |
| 4446 | /// |
| 4447 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4448 | /// terminates the template-id. |
| 4449 | /// |
| 4450 | /// \param Param the template template parameter whose default we are |
| 4451 | /// substituting into. |
| 4452 | /// |
| 4453 | /// \param Converted the list of template arguments provided for template |
| 4454 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4455 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4456 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4457 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4458 | TemplateDecl *Template, |
| 4459 | SourceLocation TemplateLoc, |
| 4460 | SourceLocation RAngleLoc, |
| 4461 | TemplateTypeParmDecl *Param, |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 4462 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4463 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4464 | |
| 4465 | // If the argument type is dependent, instantiate it now based |
| 4466 | // on the previously-computed template arguments. |
Erik Pilkington | ba88e21 | 2018-11-12 21:31:06 +0000 | [diff] [blame] | 4467 | if (ArgType->getType()->isInstantiationDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4468 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4469 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4470 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4471 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4472 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4473 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4474 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4475 | |
| 4476 | // Only substitute for the innermost template argument list. |
| 4477 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4478 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4479 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4480 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4481 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4482 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4483 | ArgType = |
| 4484 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 4485 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | return ArgType; |
| 4489 | } |
| 4490 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4491 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4492 | /// the given non-type template parameter. |
| 4493 | /// |
| 4494 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4495 | /// the substitution. |
| 4496 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4497 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4498 | /// for. |
| 4499 | /// |
| 4500 | /// \param TemplateLoc the location of the template name that started the |
| 4501 | /// template-id we are checking. |
| 4502 | /// |
| 4503 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4504 | /// terminates the template-id. |
| 4505 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4506 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4507 | /// substituting into. |
| 4508 | /// |
| 4509 | /// \param Converted the list of template arguments provided for template |
| 4510 | /// parameters that precede \p Param in the template parameter list. |
| 4511 | /// |
| 4512 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4513 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4514 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4515 | TemplateDecl *Template, |
| 4516 | SourceLocation TemplateLoc, |
| 4517 | SourceLocation RAngleLoc, |
| 4518 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4519 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4520 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4521 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4522 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4523 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4524 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4525 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4526 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4527 | |
| 4528 | // Only substitute for the innermost template argument list. |
| 4529 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4530 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4531 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4532 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4533 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4534 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 4535 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4536 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4539 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4540 | /// the given template template parameter. |
| 4541 | /// |
| 4542 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4543 | /// the substitution. |
| 4544 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4546 | /// for. |
| 4547 | /// |
| 4548 | /// \param TemplateLoc the location of the template name that started the |
| 4549 | /// template-id we are checking. |
| 4550 | /// |
| 4551 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4552 | /// terminates the template-id. |
| 4553 | /// |
| 4554 | /// \param Param the template template parameter whose default we are |
| 4555 | /// substituting into. |
| 4556 | /// |
| 4557 | /// \param Converted the list of template arguments provided for template |
| 4558 | /// parameters that precede \p Param in the template parameter list. |
| 4559 | /// |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4560 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4561 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4562 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4563 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 4564 | static TemplateName |
| 4565 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4566 | TemplateDecl *Template, |
| 4567 | SourceLocation TemplateLoc, |
| 4568 | SourceLocation RAngleLoc, |
| 4569 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4570 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4571 | NestedNameSpecifierLoc &QualifierLoc) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4572 | Sema::InstantiatingTemplate Inst( |
| 4573 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 4574 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4575 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4576 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4577 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4578 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4579 | |
| 4580 | // Only substitute for the innermost template argument list. |
| 4581 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4582 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4583 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4584 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4585 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4586 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4587 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4588 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4589 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4590 | QualifierLoc = |
| 4591 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4592 | if (!QualifierLoc) |
| 4593 | return TemplateName(); |
| 4594 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4595 | |
| 4596 | return SemaRef.SubstTemplateName( |
| 4597 | QualifierLoc, |
| 4598 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 4599 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 4600 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4603 | /// If the given template parameter has a default template |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4604 | /// argument, substitute into that default template argument and |
| 4605 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4606 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4607 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 4608 | SourceLocation TemplateLoc, |
| 4609 | SourceLocation RAngleLoc, |
| 4610 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4611 | SmallVectorImpl<TemplateArgument> |
| 4612 | &Converted, |
| 4613 | bool &HasDefaultArg) { |
| 4614 | HasDefaultArg = false; |
| 4615 | |
| 4616 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4617 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4618 | return TemplateArgumentLoc(); |
| 4619 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4620 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4621 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4622 | TemplateLoc, |
| 4623 | RAngleLoc, |
| 4624 | TypeParm, |
| 4625 | Converted); |
| 4626 | if (DI) |
| 4627 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 4628 | |
| 4629 | return TemplateArgumentLoc(); |
| 4630 | } |
| 4631 | |
| 4632 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 4633 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4634 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4635 | return TemplateArgumentLoc(); |
| 4636 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4637 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4638 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4639 | TemplateLoc, |
| 4640 | RAngleLoc, |
| 4641 | NonTypeParm, |
| 4642 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4643 | if (Arg.isInvalid()) |
| 4644 | return TemplateArgumentLoc(); |
| 4645 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4646 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4647 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 4648 | } |
| 4649 | |
| 4650 | TemplateTemplateParmDecl *TempTempParm |
| 4651 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4652 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4653 | return TemplateArgumentLoc(); |
| 4654 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4655 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4656 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4657 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4658 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4659 | RAngleLoc, |
| 4660 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4661 | Converted, |
| 4662 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4663 | if (TName.isNull()) |
| 4664 | return TemplateArgumentLoc(); |
| 4665 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4666 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4667 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4668 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 4669 | } |
| 4670 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4671 | /// Convert a template-argument that we parsed as a type into a template, if |
| 4672 | /// possible. C++ permits injected-class-names to perform dual service as |
| 4673 | /// template template arguments and as template type arguments. |
| 4674 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { |
| 4675 | // Extract and step over any surrounding nested-name-specifier. |
| 4676 | NestedNameSpecifierLoc QualLoc; |
| 4677 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { |
| 4678 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
| 4679 | return TemplateArgumentLoc(); |
| 4680 | |
| 4681 | QualLoc = ETLoc.getQualifierLoc(); |
| 4682 | TLoc = ETLoc.getNamedTypeLoc(); |
| 4683 | } |
| 4684 | |
| 4685 | // If this type was written as an injected-class-name, it can be used as a |
| 4686 | // template template argument. |
| 4687 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) |
| 4688 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), |
| 4689 | QualLoc, InjLoc.getNameLoc()); |
| 4690 | |
| 4691 | // If this type was written as an injected-class-name, it may have been |
| 4692 | // converted to a RecordType during instantiation. If the RecordType is |
| 4693 | // *not* wrapped in a TemplateSpecializationType and denotes a class |
| 4694 | // template specialization, it must have come from an injected-class-name. |
| 4695 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) |
| 4696 | if (auto *CTSD = |
| 4697 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
| 4698 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), |
| 4699 | QualLoc, RecLoc.getNameLoc()); |
| 4700 | |
| 4701 | return TemplateArgumentLoc(); |
| 4702 | } |
| 4703 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4704 | /// Check that the given template argument corresponds to the given |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4705 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4706 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4707 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4708 | /// checked. |
| 4709 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4710 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4711 | /// |
| 4712 | /// \param Template The template in which the template argument resides. |
| 4713 | /// |
| 4714 | /// \param TemplateLoc The location of the template name for the template |
| 4715 | /// whose argument list we're matching. |
| 4716 | /// |
| 4717 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 4718 | /// the template argument list. |
| 4719 | /// |
| 4720 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 4721 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 4722 | /// |
| 4723 | /// \param Converted The checked, converted argument will be added to the |
| 4724 | /// end of this small vector. |
| 4725 | /// |
| 4726 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 4727 | /// explicitly written, deduced, etc. |
| 4728 | /// |
| 4729 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4730 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4731 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4732 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4733 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4734 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4735 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4736 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4737 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4738 | // Check template type parameters. |
| 4739 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4740 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4741 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4742 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4743 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4744 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4745 | // with the template arguments we've seen thus far. But if the |
| 4746 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4747 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4748 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 4749 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4750 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4751 | // FIXME: Do we need to substitute into parameters here if they're |
| 4752 | // instantiation-dependent but not dependent? |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4753 | if (NTTPType->isDependentType() && |
| 4754 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4755 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4756 | // Do substitution on the type of the non-type template parameter. |
| 4757 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4758 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4759 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4760 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4761 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4762 | |
| 4763 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4764 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4765 | NTTPType = SubstType(NTTPType, |
| 4766 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 4767 | NTTP->getLocation(), |
| 4768 | NTTP->getDeclName()); |
| 4769 | // If that worked, check the non-type template parameter type |
| 4770 | // for validity. |
| 4771 | if (!NTTPType.isNull()) |
| 4772 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 4773 | NTTP->getLocation()); |
| 4774 | if (NTTPType.isNull()) |
| 4775 | return true; |
| 4776 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4777 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4778 | switch (Arg.getArgument().getKind()) { |
| 4779 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4780 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4781 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4782 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4783 | TemplateArgument Result; |
Erich Keane | c90bb6d | 2018-05-07 17:05:20 +0000 | [diff] [blame] | 4784 | unsigned CurSFINAEErrors = NumSFINAEErrors; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4785 | ExprResult Res = |
| 4786 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 4787 | Result, CTAK); |
| 4788 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4789 | return true; |
Erich Keane | c90bb6d | 2018-05-07 17:05:20 +0000 | [diff] [blame] | 4790 | // If the current template argument causes an error, give up now. |
| 4791 | if (CurSFINAEErrors < NumSFINAEErrors) |
| 4792 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4793 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4794 | // If the resulting expression is new, then use it in place of the |
| 4795 | // old expression in the template argument. |
| 4796 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 4797 | TemplateArgument TA(Res.get()); |
| 4798 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 4799 | } |
| 4800 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4801 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4802 | break; |
| 4803 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4804 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4805 | case TemplateArgument::Declaration: |
| 4806 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4807 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4808 | // We've already checked this template argument, so just copy |
| 4809 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4810 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4811 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4812 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4813 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4814 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4815 | // We were given a template template argument. It may not be ill-formed; |
| 4816 | // see below. |
| 4817 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4818 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 4819 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4820 | // We have a template argument such as \c T::template X, which we |
| 4821 | // parsed as a template template argument. However, since we now |
| 4822 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4823 | // template name into an expression. |
| 4824 | |
| 4825 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 4826 | Arg.getTemplateNameLoc()); |
| 4827 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 4828 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4829 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4830 | // FIXME: the template-template arg was a DependentTemplateName, |
| 4831 | // so it was provided with a template keyword. However, its source |
| 4832 | // location is not stored in the template argument structure. |
| 4833 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4834 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 4835 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 4836 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4837 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4838 | // If we parsed the template argument as a pack expansion, create a |
| 4839 | // pack expansion expression. |
| 4840 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4841 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4842 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4843 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4844 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4845 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4846 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4847 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4848 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4849 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4850 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4851 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4852 | break; |
| 4853 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4854 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4855 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4856 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4857 | // therefore cannot be a non-type template argument. |
| 4858 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 4859 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4860 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4861 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4862 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4863 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4864 | case TemplateArgument::Type: { |
| 4865 | // We have a non-type template parameter but the template |
| 4866 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4867 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4868 | // C++ [temp.arg]p2: |
| 4869 | // In a template-argument, an ambiguity between a type-id and |
| 4870 | // an expression is resolved to a type-id, regardless of the |
| 4871 | // form of the corresponding template-parameter. |
| 4872 | // |
| 4873 | // We warn specifically about this case, since it can be rather |
| 4874 | // confusing for users. |
| 4875 | QualType T = Arg.getArgument().getAsType(); |
| 4876 | SourceRange SR = Arg.getSourceRange(); |
| 4877 | if (T->isFunctionType()) |
| 4878 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 4879 | else |
| 4880 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 4881 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4882 | return true; |
| 4883 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4884 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4885 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4886 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4887 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4888 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4889 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4893 | // Check template template parameters. |
| 4894 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4895 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4896 | TemplateParameterList *Params = TempParm->getTemplateParameters(); |
| 4897 | if (TempParm->isExpandedParameterPack()) |
| 4898 | Params = TempParm->getExpansionTemplateParameters(ArgumentPackIndex); |
| 4899 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4900 | // Substitute into the template parameter list of the template |
| 4901 | // template parameter, since previously-supplied template arguments |
| 4902 | // may appear within the template template parameter. |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4903 | // |
| 4904 | // FIXME: Skip this if the parameters aren't instantiation-dependent. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4905 | { |
| 4906 | // Set up a template instantiation context. |
| 4907 | LocalInstantiationScope Scope(*this); |
| 4908 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4909 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4910 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4911 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4912 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4913 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4914 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4915 | Params = SubstTemplateParams(Params, CurContext, |
| 4916 | MultiLevelTemplateArgumentList(TemplateArgs)); |
| 4917 | if (!Params) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4918 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4919 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4920 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4921 | // C++1z [temp.local]p1: (DR1004) |
| 4922 | // When [the injected-class-name] is used [...] as a template-argument for |
| 4923 | // a template template-parameter [...] it refers to the class template |
| 4924 | // itself. |
| 4925 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { |
| 4926 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
| 4927 | Arg.getTypeSourceInfo()->getTypeLoc()); |
| 4928 | if (!ConvertedArg.getArgument().isNull()) |
| 4929 | Arg = ConvertedArg; |
| 4930 | } |
| 4931 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4932 | switch (Arg.getArgument().getKind()) { |
| 4933 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4934 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4936 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4937 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4938 | if (CheckTemplateTemplateArgument(Params, Arg)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4939 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4940 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4941 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4942 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4943 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4944 | case TemplateArgument::Expression: |
| 4945 | case TemplateArgument::Type: |
| 4946 | // We have a template template parameter but the template |
| 4947 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4948 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4949 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4950 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4951 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4952 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4953 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4954 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4955 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4956 | case TemplateArgument::NullPtr: |
| 4957 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4958 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4959 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4960 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4961 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4962 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4963 | return false; |
| 4964 | } |
| 4965 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4966 | /// Check whether the template parameter is a pack expansion, and if so, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4967 | /// determine the number of parameters produced by that expansion. For instance: |
| 4968 | /// |
| 4969 | /// \code |
| 4970 | /// template<typename ...Ts> struct A { |
| 4971 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 4972 | /// }; |
| 4973 | /// \endcode |
| 4974 | /// |
| 4975 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 4976 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4977 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4978 | if (NonTypeTemplateParmDecl *NTTP |
| 4979 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4980 | if (NTTP->isExpandedParameterPack()) |
| 4981 | return NTTP->getNumExpansionTypes(); |
| 4982 | } |
| 4983 | |
| 4984 | if (TemplateTemplateParmDecl *TTP |
| 4985 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 4986 | if (TTP->isExpandedParameterPack()) |
| 4987 | return TTP->getNumExpansionTemplateParameters(); |
| 4988 | } |
| 4989 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 4990 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4991 | } |
| 4992 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4993 | /// Diagnose a missing template argument. |
| 4994 | template<typename TemplateParmDecl> |
| 4995 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 4996 | TemplateDecl *TD, |
| 4997 | const TemplateParmDecl *D, |
| 4998 | TemplateArgumentListInfo &Args) { |
| 4999 | // Dig out the most recent declaration of the template parameter; there may be |
| 5000 | // declarations of the template that are more recent than TD. |
| 5001 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 5002 | ->getTemplateParameters() |
| 5003 | ->getParam(D->getIndex())); |
| 5004 | |
| 5005 | // If there's a default argument that's not visible, diagnose that we're |
| 5006 | // missing a module import. |
| 5007 | llvm::SmallVector<Module*, 8> Modules; |
| 5008 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 5009 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 5010 | D->getDefaultArgumentLoc(), Modules, |
| 5011 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 5012 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5013 | return true; |
| 5014 | } |
| 5015 | |
| 5016 | // FIXME: If there's a more recent default argument that *is* visible, |
| 5017 | // diagnose that it was declared too late. |
| 5018 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5019 | TemplateParameterList *Params = TD->getTemplateParameters(); |
| 5020 | |
| 5021 | S.Diag(Loc, diag::err_template_arg_list_different_arity) |
| 5022 | << /*not enough args*/0 |
| 5023 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(TD)) |
| 5024 | << TD; |
| 5025 | S.Diag(TD->getLocation(), diag::note_template_decl_here) |
| 5026 | << Params->getSourceRange(); |
| 5027 | return true; |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5030 | /// Check that the given template argument list is well-formed |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5031 | /// for specializing the given template. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5032 | bool Sema::CheckTemplateArgumentList( |
| 5033 | TemplateDecl *Template, SourceLocation TemplateLoc, |
| 5034 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
| 5035 | SmallVectorImpl<TemplateArgument> &Converted, |
| 5036 | bool UpdateArgsWithConversions) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5037 | // Make a copy of the template arguments for processing. Only make the |
| 5038 | // changes at the end when successful in matching the arguments to the |
| 5039 | // template. |
| 5040 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 5041 | |
Erich Keane | af0795b | 2017-10-24 01:39:56 +0000 | [diff] [blame] | 5042 | // Make sure we get the template parameter list from the most |
| 5043 | // recentdeclaration, since that is the only one that has is guaranteed to |
| 5044 | // have all the default template argument information. |
| 5045 | TemplateParameterList *Params = |
| 5046 | cast<TemplateDecl>(Template->getMostRecentDecl()) |
| 5047 | ->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5048 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5049 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5050 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5051 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5052 | // [...] The type and form of each template-argument specified in |
| 5053 | // a template-id shall match the type and form specified for the |
| 5054 | // corresponding parameter declared by the template in its |
| 5055 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5056 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5057 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5058 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 5059 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5060 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5061 | ParamEnd = Params->end(); |
| 5062 | Param != ParamEnd; /* increment in loop */) { |
| 5063 | // If we have an expanded parameter pack, make sure we don't have too |
| 5064 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 5065 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5066 | if (*Expansions == ArgumentPack.size()) { |
| 5067 | // We're done with this parameter pack. Pack up its arguments and add |
| 5068 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5069 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5070 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5071 | ArgumentPack.clear(); |
| 5072 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5073 | // This argument is assigned to the next parameter. |
| 5074 | ++Param; |
| 5075 | continue; |
| 5076 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 5077 | // Not enough arguments for this parameter pack. |
| 5078 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5079 | << /*not enough args*/0 |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 5080 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5081 | << Template; |
| 5082 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5083 | << Params->getSourceRange(); |
| 5084 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5085 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5086 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5087 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5088 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5089 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5090 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5091 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5092 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5093 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5094 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5095 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5096 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5097 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 5098 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5099 | // 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] | 5100 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5101 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5102 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5103 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5104 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5105 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 5106 | return true; |
| 5107 | } |
| 5108 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5109 | // We're now done with this argument. |
| 5110 | ++ArgIdx; |
| 5111 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5112 | if ((*Param)->isTemplateParameterPack()) { |
| 5113 | // The template parameter was a template parameter pack, so take the |
| 5114 | // deduced argument and place it on the argument pack. Note that we |
| 5115 | // stay on the same template parameter so that we can deduce more |
| 5116 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 5117 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5118 | } else { |
| 5119 | // Move to the next template parameter. |
| 5120 | ++Param; |
| 5121 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5122 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5123 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 5124 | // the remaining arguments, because we don't know what parameters they'll |
| 5125 | // match up with. |
| 5126 | if (PackExpansionIntoNonPack) { |
| 5127 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5128 | // If we were part way through filling in an expanded parameter pack, |
| 5129 | // fall back to just producing individual arguments. |
| 5130 | Converted.insert(Converted.end(), |
| 5131 | ArgumentPack.begin(), ArgumentPack.end()); |
| 5132 | ArgumentPack.clear(); |
| 5133 | } |
| 5134 | |
| 5135 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5136 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5137 | ++ArgIdx; |
| 5138 | } |
| 5139 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5140 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5141 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5142 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5143 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5144 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5145 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5146 | // If we're checking a partial template argument list, we're done. |
| 5147 | if (PartialTemplateArgs) { |
| 5148 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5149 | Converted.push_back( |
| 5150 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 5151 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5152 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5155 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5156 | // 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] | 5157 | if ((*Param)->isTemplateParameterPack()) { |
| 5158 | assert(!getExpandedPackSize(*Param) && |
| 5159 | "Should have dealt with this already"); |
| 5160 | |
| 5161 | // A non-expanded parameter pack before the end of the parameter list |
| 5162 | // only occurs for an ill-formed template parameter list, unless we've |
| 5163 | // got a partial argument list for a function template, so just bail out. |
| 5164 | if (Param + 1 != ParamEnd) |
| 5165 | return true; |
| 5166 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5167 | Converted.push_back( |
| 5168 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5169 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5170 | |
| 5171 | ++Param; |
| 5172 | continue; |
| 5173 | } |
| 5174 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5175 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5176 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5177 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5178 | // Retrieve the default template argument from the template |
| 5179 | // parameter. For each kind of template parameter, we substitute the |
| 5180 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5181 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5182 | // the default argument. |
| 5183 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5184 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5185 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 5186 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5187 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5188 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5189 | Template, |
| 5190 | TemplateLoc, |
| 5191 | RAngleLoc, |
| 5192 | TTP, |
| 5193 | Converted); |
| 5194 | if (!ArgType) |
| 5195 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5196 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5197 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 5198 | ArgType); |
| 5199 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5200 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5201 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5202 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 5203 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5204 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5205 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5206 | TemplateLoc, |
| 5207 | RAngleLoc, |
| 5208 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5209 | Converted); |
| 5210 | if (E.isInvalid()) |
| 5211 | return true; |
| 5212 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5213 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5214 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 5215 | } else { |
| 5216 | TemplateTemplateParmDecl *TempParm |
| 5217 | = cast<TemplateTemplateParmDecl>(*Param); |
| 5218 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5219 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5220 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 5221 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5222 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 5223 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5224 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5225 | TemplateLoc, |
| 5226 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5227 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5228 | Converted, |
| 5229 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5230 | if (Name.isNull()) |
| 5231 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5232 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5233 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 5234 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5235 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5236 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5237 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 5238 | // the default template argument. We're not actually instantiating a |
| 5239 | // template here, we just create this object to put a note into the |
| 5240 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 5241 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 5242 | SourceRange(TemplateLoc, RAngleLoc)); |
| 5243 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 5244 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5245 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5246 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 5247 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5248 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5249 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5250 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5251 | // Core issue 150 (assumed resolution): if this is a template template |
| 5252 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5253 | // template definition. |
| 5254 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5255 | NewArgs.addArgument(Arg); |
| 5256 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5257 | // Move to the next template parameter and argument. |
| 5258 | ++Param; |
| 5259 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5260 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5261 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5262 | // If we're performing a partial argument substitution, allow any trailing |
| 5263 | // pack expansions; they might be empty. This can happen even if |
| 5264 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 5265 | // still dependent). |
| 5266 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 5267 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5268 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 5269 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5272 | // If we have any leftover arguments, then there were too many arguments. |
| 5273 | // Complain and fail. |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5274 | if (ArgIdx < NumArgs) { |
| 5275 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 5276 | << /*too many args*/1 |
| 5277 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
| 5278 | << Template |
| 5279 | << SourceRange(NewArgs[ArgIdx].getLocation(), NewArgs.getRAngleLoc()); |
| 5280 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5281 | << Params->getSourceRange(); |
| 5282 | return true; |
| 5283 | } |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5284 | |
| 5285 | // No problems found with the new argument list, propagate changes back |
| 5286 | // to caller. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5287 | if (UpdateArgsWithConversions) |
| 5288 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5289 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5290 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5291 | } |
| 5292 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5293 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5294 | class UnnamedLocalNoLinkageFinder |
| 5295 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5296 | { |
| 5297 | Sema &S; |
| 5298 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5299 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5300 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5301 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5302 | public: |
| 5303 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 5304 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5305 | bool Visit(QualType T) { |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5306 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5307 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5308 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5309 | #define TYPE(Class, Parent) \ |
| 5310 | bool Visit##Class##Type(const Class##Type *); |
| 5311 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 5312 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5313 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 5314 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5315 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5316 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5317 | bool VisitTagDecl(const TagDecl *Tag); |
| 5318 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 5319 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 5320 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5321 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5322 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5323 | return false; |
| 5324 | } |
| 5325 | |
| 5326 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 5327 | return Visit(T->getElementType()); |
| 5328 | } |
| 5329 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5330 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5331 | return Visit(T->getPointeeType()); |
| 5332 | } |
| 5333 | |
| 5334 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5335 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5336 | return Visit(T->getPointeeType()); |
| 5337 | } |
| 5338 | |
| 5339 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5340 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5341 | return Visit(T->getPointeeType()); |
| 5342 | } |
| 5343 | |
| 5344 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5345 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5346 | return Visit(T->getPointeeType()); |
| 5347 | } |
| 5348 | |
| 5349 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5350 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5351 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 5352 | } |
| 5353 | |
| 5354 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5355 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5356 | return Visit(T->getElementType()); |
| 5357 | } |
| 5358 | |
| 5359 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5360 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5361 | return Visit(T->getElementType()); |
| 5362 | } |
| 5363 | |
| 5364 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5365 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5366 | return Visit(T->getElementType()); |
| 5367 | } |
| 5368 | |
| 5369 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5370 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5371 | return Visit(T->getElementType()); |
| 5372 | } |
| 5373 | |
| 5374 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5375 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5376 | return Visit(T->getElementType()); |
| 5377 | } |
| 5378 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 5379 | bool UnnamedLocalNoLinkageFinder::VisitDependentAddressSpaceType( |
| 5380 | const DependentAddressSpaceType *T) { |
| 5381 | return Visit(T->getPointeeType()); |
| 5382 | } |
| 5383 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5384 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 5385 | return Visit(T->getElementType()); |
| 5386 | } |
| 5387 | |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 5388 | bool UnnamedLocalNoLinkageFinder::VisitDependentVectorType( |
| 5389 | const DependentVectorType *T) { |
| 5390 | return Visit(T->getElementType()); |
| 5391 | } |
| 5392 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5393 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 5394 | return Visit(T->getElementType()); |
| 5395 | } |
| 5396 | |
| 5397 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 5398 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 5399 | for (const auto &A : T->param_types()) { |
| 5400 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5401 | return true; |
| 5402 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5403 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5404 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 5408 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5409 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 5413 | const UnresolvedUsingType*) { |
| 5414 | return false; |
| 5415 | } |
| 5416 | |
| 5417 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 5418 | return false; |
| 5419 | } |
| 5420 | |
| 5421 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 5422 | return Visit(T->getUnderlyingType()); |
| 5423 | } |
| 5424 | |
| 5425 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 5426 | return false; |
| 5427 | } |
| 5428 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5429 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 5430 | const UnaryTransformType*) { |
| 5431 | return false; |
| 5432 | } |
| 5433 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5434 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 5435 | return Visit(T->getDeducedType()); |
| 5436 | } |
| 5437 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5438 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
| 5439 | const DeducedTemplateSpecializationType *T) { |
| 5440 | return Visit(T->getDeducedType()); |
| 5441 | } |
| 5442 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5443 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 5444 | return VisitTagDecl(T->getDecl()); |
| 5445 | } |
| 5446 | |
| 5447 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 5448 | return VisitTagDecl(T->getDecl()); |
| 5449 | } |
| 5450 | |
| 5451 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 5452 | const TemplateTypeParmType*) { |
| 5453 | return false; |
| 5454 | } |
| 5455 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 5456 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 5457 | const SubstTemplateTypeParmPackType *) { |
| 5458 | return false; |
| 5459 | } |
| 5460 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5461 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 5462 | const TemplateSpecializationType*) { |
| 5463 | return false; |
| 5464 | } |
| 5465 | |
| 5466 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 5467 | const InjectedClassNameType* T) { |
| 5468 | return VisitTagDecl(T->getDecl()); |
| 5469 | } |
| 5470 | |
| 5471 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 5472 | const DependentNameType* T) { |
| 5473 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5474 | } |
| 5475 | |
| 5476 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 5477 | const DependentTemplateSpecializationType* T) { |
| 5478 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5479 | } |
| 5480 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5481 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 5482 | const PackExpansionType* T) { |
| 5483 | return Visit(T->getPattern()); |
| 5484 | } |
| 5485 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5486 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 5487 | return false; |
| 5488 | } |
| 5489 | |
| 5490 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 5491 | const ObjCInterfaceType *) { |
| 5492 | return false; |
| 5493 | } |
| 5494 | |
| 5495 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 5496 | const ObjCObjectPointerType *) { |
| 5497 | return false; |
| 5498 | } |
| 5499 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5500 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 5501 | return Visit(T->getValueType()); |
| 5502 | } |
| 5503 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5504 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 5505 | return false; |
| 5506 | } |
| 5507 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5508 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 5509 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5510 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5511 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5512 | diag::warn_cxx98_compat_template_arg_local_type : |
| 5513 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5514 | << S.Context.getTypeDeclType(Tag) << SR; |
| 5515 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5516 | } |
| 5517 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 5518 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5519 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5520 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5521 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 5522 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5523 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 5524 | return true; |
| 5525 | } |
| 5526 | |
| 5527 | return false; |
| 5528 | } |
| 5529 | |
| 5530 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 5531 | NestedNameSpecifier *NNS) { |
| 5532 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 5533 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5534 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5535 | switch (NNS->getKind()) { |
| 5536 | case NestedNameSpecifier::Identifier: |
| 5537 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5538 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5539 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5540 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5541 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5542 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5543 | case NestedNameSpecifier::TypeSpec: |
| 5544 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 5545 | return Visit(QualType(NNS->getAsType(), 0)); |
| 5546 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5547 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5550 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5551 | /// template type parameter. |
| 5552 | /// |
| 5553 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 5554 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5555 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5556 | TypeSourceInfo *ArgInfo) { |
| 5557 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5558 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 5559 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 5560 | |
| 5561 | if (Arg->isVariablyModifiedType()) { |
| 5562 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5563 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5564 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5567 | // C++03 [temp.arg.type]p2: |
| 5568 | // A local type, a type with no linkage, an unnamed type or a type |
| 5569 | // compounded from any of these types shall not be used as a |
| 5570 | // template-argument for a template type-parameter. |
| 5571 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5572 | // 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] | 5573 | // a warning. |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5574 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5575 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 5576 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 5577 | } |
| 5578 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5579 | return false; |
| 5580 | } |
| 5581 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5582 | enum NullPointerValueKind { |
| 5583 | NPV_NotNullPointer, |
| 5584 | NPV_NullPointer, |
| 5585 | NPV_Error |
| 5586 | }; |
| 5587 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5588 | /// Determine whether the given template argument is a null pointer |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5589 | /// value of the appropriate type. |
| 5590 | static NullPointerValueKind |
| 5591 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5592 | QualType ParamType, Expr *Arg, |
| 5593 | Decl *Entity = nullptr) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5594 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 5595 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5596 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5597 | // dllimport'd entities aren't constant but are available inside of template |
| 5598 | // arguments. |
| 5599 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 5600 | return NPV_NotNullPointer; |
| 5601 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5602 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 5603 | llvm_unreachable( |
| 5604 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5605 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 5606 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5607 | return NPV_NotNullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5608 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5609 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5610 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 5611 | if (ArgRV.isInvalid()) |
| 5612 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5613 | Arg = ArgRV.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5615 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5616 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5617 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5618 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5619 | EvalResult.HasSideEffects) { |
| 5620 | SourceLocation DiagLoc = Arg->getExprLoc(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5621 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5622 | // If our only note is the usual "invalid subexpression" note, just point |
| 5623 | // the caret at its location rather than producing an essentially |
| 5624 | // redundant note. |
| 5625 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 5626 | diag::note_invalid_subexpr_in_const_expr) { |
| 5627 | DiagLoc = Notes[0].first; |
| 5628 | Notes.clear(); |
| 5629 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5630 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5631 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 5632 | << Arg->getType() << Arg->getSourceRange(); |
| 5633 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 5634 | S.Diag(Notes[I].first, Notes[I].second); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5635 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5636 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5637 | return NPV_Error; |
| 5638 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5639 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5640 | // C++11 [temp.arg.nontype]p1: |
| 5641 | // - an address constant expression of type std::nullptr_t |
| 5642 | if (Arg->getType()->isNullPtrType()) |
| 5643 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5644 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5645 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 5646 | // - a constant expression that evaluates to a null member pointer value |
| 5647 | // (4.11); or |
| 5648 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 5649 | (EvalResult.Val.isMemberPointer() && |
| 5650 | !EvalResult.Val.getMemberPointerDecl())) { |
| 5651 | // If our expression has an appropriate type, we've succeeded. |
| 5652 | bool ObjCLifetimeConversion; |
| 5653 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 5654 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 5655 | ObjCLifetimeConversion)) |
| 5656 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5657 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5658 | // The types didn't match, but we know we got a null pointer; complain, |
| 5659 | // then recover as if the types were correct. |
| 5660 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 5661 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 5662 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5663 | return NPV_NullPointer; |
| 5664 | } |
| 5665 | |
| 5666 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 5667 | // constant, suggest a cast to the appropriate type. |
| 5668 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 5669 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 5670 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5671 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), Code) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5672 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getEndLoc()), |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5673 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5674 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5675 | return NPV_NullPointer; |
| 5676 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5677 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5678 | // FIXME: If we ever want to support general, address-constant expressions |
| 5679 | // as non-type template arguments, we should return the ExprResult here to |
| 5680 | // be interpreted by the caller. |
| 5681 | return NPV_NotNullPointer; |
| 5682 | } |
| 5683 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5684 | /// Checks whether the given template argument is compatible with its |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5685 | /// template parameter. |
| 5686 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 5687 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 5688 | Expr *Arg, QualType ArgType) { |
| 5689 | bool ObjCLifetimeConversion; |
| 5690 | if (ParamType->isPointerType() && |
| 5691 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 5692 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 5693 | ObjCLifetimeConversion)) { |
| 5694 | // For pointer-to-object types, qualification conversions are |
| 5695 | // permitted. |
| 5696 | } else { |
| 5697 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 5698 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 5699 | // C++ [temp.arg.nontype]p5b3: |
| 5700 | // For a non-type template-parameter of type reference to |
| 5701 | // object, no conversions apply. The type referred to by the |
| 5702 | // reference may be more cv-qualified than the (otherwise |
| 5703 | // identical) type of the template- argument. The |
| 5704 | // template-parameter is bound directly to the |
| 5705 | // template-argument, which shall be an lvalue. |
| 5706 | |
| 5707 | // FIXME: Other qualifiers? |
| 5708 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 5709 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 5710 | |
| 5711 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5712 | S.Diag(Arg->getBeginLoc(), |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5713 | diag::err_template_arg_ref_bind_ignores_quals) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5714 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5715 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5716 | return true; |
| 5717 | } |
| 5718 | } |
| 5719 | } |
| 5720 | |
| 5721 | // At this point, the template argument refers to an object or |
| 5722 | // function with external linkage. We now need to check whether the |
| 5723 | // argument and parameter types are compatible. |
| 5724 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 5725 | ParamType.getNonReferenceType())) { |
| 5726 | // We can't perform this conversion or binding. |
| 5727 | if (ParamType->isReferenceType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5728 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_no_ref_bind) |
| 5729 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5730 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5731 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 5732 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5733 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5734 | return true; |
| 5735 | } |
| 5736 | } |
| 5737 | |
| 5738 | return false; |
| 5739 | } |
| 5740 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5741 | /// Checks whether the given template argument is the address |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5742 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5743 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5744 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 5745 | NonTypeTemplateParmDecl *Param, |
| 5746 | QualType ParamType, |
| 5747 | Expr *ArgIn, |
| 5748 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5749 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5750 | Expr *Arg = ArgIn; |
| 5751 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5752 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5753 | bool AddressTaken = false; |
| 5754 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5755 | if (S.getLangOpts().MicrosoftExt) { |
| 5756 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 5757 | // dereference and address-of operators. |
| 5758 | Arg = Arg->IgnoreParenCasts(); |
| 5759 | |
| 5760 | bool ExtWarnMSTemplateArg = false; |
| 5761 | UnaryOperatorKind FirstOpKind; |
| 5762 | SourceLocation FirstOpLoc; |
| 5763 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5764 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 5765 | if (UnOpKind == UO_Deref) |
| 5766 | ExtWarnMSTemplateArg = true; |
| 5767 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 5768 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 5769 | if (!AddrOpLoc.isValid()) { |
| 5770 | FirstOpKind = UnOpKind; |
| 5771 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 5772 | } |
| 5773 | } else |
| 5774 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5775 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5776 | if (FirstOpLoc.isValid()) { |
| 5777 | if (ExtWarnMSTemplateArg) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5778 | S.Diag(ArgIn->getBeginLoc(), diag::ext_ms_deref_template_argument) |
| 5779 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5780 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5781 | if (FirstOpKind == UO_AddrOf) |
| 5782 | AddressTaken = true; |
| 5783 | else if (Arg->getType()->isPointerType()) { |
| 5784 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 5785 | // constant expression. |
| 5786 | assert(FirstOpKind == UO_Deref); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5787 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 5788 | << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5789 | } |
| 5790 | } |
| 5791 | } else { |
| 5792 | // See through any implicit casts we added to fix the type. |
| 5793 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5794 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5795 | // C++ [temp.arg.nontype]p1: |
| 5796 | // |
| 5797 | // A template-argument for a non-type, non-template |
| 5798 | // template-parameter shall be one of: [...] |
| 5799 | // |
| 5800 | // -- the address of an object or function with external |
| 5801 | // linkage, including function templates and function |
| 5802 | // template-ids but excluding non-static class members, |
| 5803 | // expressed as & id-expression where the & is optional if |
| 5804 | // the name refers to a function or array, or if the |
| 5805 | // corresponding template-parameter is a reference; or |
| 5806 | |
| 5807 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 5808 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 5809 | bool ExtraParens = false; |
| 5810 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 5811 | if (!Invalid && !ExtraParens) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5812 | S.Diag(Arg->getBeginLoc(), |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5813 | S.getLangOpts().CPlusPlus11 |
| 5814 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 5815 | : diag::ext_template_arg_extra_parens) |
| 5816 | << Arg->getSourceRange(); |
| 5817 | ExtraParens = true; |
| 5818 | } |
| 5819 | |
| 5820 | Arg = Parens->getSubExpr(); |
| 5821 | } |
| 5822 | |
| 5823 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5824 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5825 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5826 | |
| 5827 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5828 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 5829 | Arg = UnOp->getSubExpr(); |
| 5830 | AddressTaken = true; |
| 5831 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 5832 | } |
| 5833 | } |
| 5834 | |
| 5835 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5836 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5837 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5838 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5839 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5840 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 5841 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 5842 | |
| 5843 | // If our parameter has pointer type, check for a null template value. |
| 5844 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5845 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn, |
| 5846 | Entity)) { |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5847 | case NPV_NullPointer: |
| 5848 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5849 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 5850 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5851 | return false; |
| 5852 | |
| 5853 | case NPV_Error: |
| 5854 | return true; |
| 5855 | |
| 5856 | case NPV_NotNullPointer: |
| 5857 | break; |
| 5858 | } |
| 5859 | } |
| 5860 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5861 | // Stop checking the precise nature of the argument if it is value dependent, |
| 5862 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5863 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5864 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5865 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5866 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5867 | |
| 5868 | if (isa<CXXUuidofExpr>(Arg)) { |
| 5869 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 5870 | ArgIn, Arg, ArgType)) |
| 5871 | return true; |
| 5872 | |
| 5873 | Converted = TemplateArgument(ArgIn); |
| 5874 | return false; |
| 5875 | } |
| 5876 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5877 | if (!DRE) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5878 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 5879 | << Arg->getSourceRange(); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5880 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5881 | return true; |
| 5882 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5883 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5884 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 5885 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5886 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_field) |
| 5887 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5888 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5889 | return true; |
| 5890 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5891 | |
| 5892 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5893 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5894 | if (!Method->isStatic()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5895 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_method) |
| 5896 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5897 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5898 | return true; |
| 5899 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5900 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5901 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5902 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 5903 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5904 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5905 | // A non-type template argument must refer to an object or function. |
| 5906 | if (!Func && !Var) { |
| 5907 | // We found something, but we don't know specifically what it is. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5908 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_object_or_func) |
| 5909 | << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5910 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 5911 | return true; |
| 5912 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5913 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5914 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5915 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5916 | S.Diag(Arg->getBeginLoc(), |
| 5917 | S.getLangOpts().CPlusPlus11 |
| 5918 | ? diag::warn_cxx98_compat_template_arg_object_internal |
| 5919 | : diag::ext_template_arg_object_internal) |
| 5920 | << !Func << Entity << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5921 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5922 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5923 | } else if (!Entity->hasLinkage()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5924 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_object_no_linkage) |
| 5925 | << !Func << Entity << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5926 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5927 | << !Func; |
| 5928 | return true; |
| 5929 | } |
| 5930 | |
| 5931 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5932 | // If the template parameter has pointer type, the function decays. |
| 5933 | if (ParamType->isPointerType() && !AddressTaken) |
| 5934 | ArgType = S.Context.getPointerType(Func->getType()); |
| 5935 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 5936 | // If we originally had an address-of operator, but the |
| 5937 | // parameter has reference type, complain and (if things look |
| 5938 | // like they will work) drop the address-of operator. |
| 5939 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 5940 | ParamType.getNonReferenceType())) { |
| 5941 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5942 | << ParamType; |
| 5943 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5944 | return true; |
| 5945 | } |
| 5946 | |
| 5947 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5948 | << ParamType |
| 5949 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5950 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5951 | |
| 5952 | ArgType = Func->getType(); |
| 5953 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5954 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5955 | // A value of reference type is not an object. |
| 5956 | if (Var->getType()->isReferenceType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5957 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_reference_var) |
| 5958 | << Var->getType() << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5959 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5960 | return true; |
| 5961 | } |
| 5962 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5963 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 5964 | if (Var->getTLSKind()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5965 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_thread_local) |
| 5966 | << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5967 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 5968 | return true; |
| 5969 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5970 | |
| 5971 | // If the template parameter has pointer type, we must have taken |
| 5972 | // the address of this object. |
| 5973 | if (ParamType->isReferenceType()) { |
| 5974 | if (AddressTaken) { |
| 5975 | // If we originally had an address-of operator, but the |
| 5976 | // parameter has reference type, complain and (if things look |
| 5977 | // like they will work) drop the address-of operator. |
| 5978 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 5979 | ParamType.getNonReferenceType())) { |
| 5980 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5981 | << ParamType; |
| 5982 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5983 | return true; |
| 5984 | } |
| 5985 | |
| 5986 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5987 | << ParamType |
| 5988 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5989 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5990 | |
| 5991 | ArgType = Var->getType(); |
| 5992 | } |
| 5993 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 5994 | if (Var->getType()->isArrayType()) { |
| 5995 | // Array-to-pointer decay. |
| 5996 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 5997 | } else { |
| 5998 | // If the template parameter has pointer type but the address of |
| 5999 | // this object was not taken, complain and (possibly) recover by |
| 6000 | // taking the address of the entity. |
| 6001 | ArgType = S.Context.getPointerType(Var->getType()); |
| 6002 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6003 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 6004 | << ParamType; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6005 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6006 | return true; |
| 6007 | } |
| 6008 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6009 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 6010 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), "&"); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6011 | |
| 6012 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6013 | } |
| 6014 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6015 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6016 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 6017 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 6018 | Arg, ArgType)) |
| 6019 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6020 | |
| 6021 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 6022 | Converted = |
| 6023 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6024 | S.MarkAnyDeclReferenced(Arg->getBeginLoc(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6025 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6026 | } |
| 6027 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6028 | /// Checks whether the given template argument is a pointer to |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6029 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6030 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 6031 | NonTypeTemplateParmDecl *Param, |
| 6032 | QualType ParamType, |
| 6033 | Expr *&ResultArg, |
| 6034 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6035 | bool Invalid = false; |
| 6036 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6037 | Expr *Arg = ResultArg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6038 | bool ObjCLifetimeConversion; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6039 | |
| 6040 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6041 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6042 | // A template-argument for a non-type, non-template |
| 6043 | // template-parameter shall be one of: [...] |
| 6044 | // |
| 6045 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6046 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6047 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6048 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 6049 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 6050 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6051 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6052 | if (!Invalid && !ExtraParens) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6053 | S.Diag(Arg->getBeginLoc(), |
| 6054 | S.getLangOpts().CPlusPlus11 |
| 6055 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 6056 | : diag::ext_template_arg_extra_parens) |
| 6057 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6058 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | Arg = Parens->getSubExpr(); |
| 6062 | } |
| 6063 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6064 | while (SubstNonTypeTemplateParmExpr *subst = |
| 6065 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 6066 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 6067 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6068 | // A pointer-to-member constant written &Class::member. |
| 6069 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6070 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6071 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 6072 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6073 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6074 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6075 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6076 | // A constant of pointer-to-member type. |
| 6077 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6078 | ValueDecl *VD = DRE->getDecl(); |
| 6079 | if (VD->getType()->isMemberPointerType()) { |
| 6080 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
| 6081 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6082 | Converted = TemplateArgument(Arg); |
| 6083 | } else { |
| 6084 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
| 6085 | Converted = TemplateArgument(VD, ParamType); |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6086 | } |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6087 | return Invalid; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6088 | } |
| 6089 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6090 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6091 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6092 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6093 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 6094 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 6095 | |
| 6096 | // Check for a null pointer value. |
| 6097 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ResultArg, |
| 6098 | Entity)) { |
| 6099 | case NPV_Error: |
| 6100 | return true; |
| 6101 | case NPV_NullPointer: |
| 6102 | S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
| 6103 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 6104 | /*isNullPtr*/true); |
| 6105 | return false; |
| 6106 | case NPV_NotNullPointer: |
| 6107 | break; |
| 6108 | } |
| 6109 | |
| 6110 | if (S.IsQualificationConversion(ResultArg->getType(), |
| 6111 | ParamType.getNonReferenceType(), false, |
| 6112 | ObjCLifetimeConversion)) { |
| 6113 | ResultArg = S.ImpCastExprToType(ResultArg, ParamType, CK_NoOp, |
| 6114 | ResultArg->getValueKind()) |
| 6115 | .get(); |
| 6116 | } else if (!S.Context.hasSameUnqualifiedType( |
| 6117 | ResultArg->getType(), ParamType.getNonReferenceType())) { |
| 6118 | // We can't perform this conversion. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6119 | S.Diag(ResultArg->getBeginLoc(), diag::err_template_arg_not_convertible) |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 6120 | << ResultArg->getType() << ParamType << ResultArg->getSourceRange(); |
| 6121 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6122 | return true; |
| 6123 | } |
| 6124 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6125 | if (!DRE) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6126 | return S.Diag(Arg->getBeginLoc(), |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6127 | diag::err_template_arg_not_pointer_to_member_form) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6128 | << Arg->getSourceRange(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6129 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6130 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 6131 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 6132 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6133 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6134 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6135 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 6136 | "Only non-static member pointers can make it here"); |
| 6137 | |
| 6138 | // Okay: this is the address of a non-static member, and therefore |
| 6139 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6140 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 6141 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6142 | } else { |
| 6143 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 6144 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6145 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6146 | return Invalid; |
| 6147 | } |
| 6148 | |
| 6149 | // 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] | 6150 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_pointer_to_member_form) |
| 6151 | << Arg->getSourceRange(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6152 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6153 | return true; |
| 6154 | } |
| 6155 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6156 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6157 | /// non-type template parameter. |
| 6158 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 6159 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6160 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6161 | /// returns the converted template argument. \p ParamType is the |
| 6162 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6163 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6164 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6165 | TemplateArgument &Converted, |
| 6166 | CheckTemplateArgumentKind CTAK) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6167 | SourceLocation StartLoc = Arg->getBeginLoc(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6168 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6169 | // If the parameter type somehow involves auto, deduce the type now. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6170 | if (getLangOpts().CPlusPlus17 && ParamType->isUndeducedType()) { |
Richard Smith | 4ae5ec8 | 2017-02-22 20:01:55 +0000 | [diff] [blame] | 6171 | // During template argument deduction, we allow 'decltype(auto)' to |
| 6172 | // match an arbitrary dependent argument. |
| 6173 | // FIXME: The language rules don't say what happens in this case. |
| 6174 | // FIXME: We get an opaque dependent type out of decltype(auto) if the |
| 6175 | // expression is merely instantiation-dependent; is this enough? |
| 6176 | if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) { |
| 6177 | auto *AT = dyn_cast<AutoType>(ParamType); |
| 6178 | if (AT && AT->isDecltypeAuto()) { |
| 6179 | Converted = TemplateArgument(Arg); |
| 6180 | return Arg; |
| 6181 | } |
| 6182 | } |
| 6183 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6184 | // When checking a deduced template argument, deduce from its type even if |
| 6185 | // the type is dependent, in order to check the types of non-type template |
| 6186 | // arguments line up properly in partial ordering. |
| 6187 | Optional<unsigned> Depth; |
| 6188 | if (CTAK != CTAK_Specified) |
| 6189 | Depth = Param->getDepth() + 1; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6190 | if (DeduceAutoType( |
| 6191 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6192 | Arg, ParamType, Depth) == DAR_Failed) { |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6193 | Diag(Arg->getExprLoc(), |
| 6194 | diag::err_non_type_template_parm_type_deduction_failure) |
| 6195 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 6196 | << Arg->getSourceRange(); |
| 6197 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6198 | return ExprError(); |
| 6199 | } |
| 6200 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 6201 | // an error. The error message normally references the parameter |
| 6202 | // declaration, but here we'll pass the argument location because that's |
| 6203 | // where the parameter type is deduced. |
| 6204 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 6205 | if (ParamType.isNull()) { |
| 6206 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6207 | return ExprError(); |
| 6208 | } |
| 6209 | } |
| 6210 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6211 | // We should have already dropped all cv-qualifiers by now. |
| 6212 | assert(!ParamType.hasQualifiers() && |
| 6213 | "non-type template parameter type cannot be qualified"); |
| 6214 | |
| 6215 | if (CTAK == CTAK_Deduced && |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 6216 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6217 | Arg->getType())) { |
Richard Smith | 957fbf1 | 2017-01-17 02:14:37 +0000 | [diff] [blame] | 6218 | // FIXME: If either type is dependent, we skip the check. This isn't |
| 6219 | // correct, since during deduction we're supposed to have replaced each |
| 6220 | // template parameter with some unique (non-dependent) placeholder. |
| 6221 | // FIXME: If the argument type contains 'auto', we carry on and fail the |
| 6222 | // type check in order to force specific types to be more specialized than |
| 6223 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to |
| 6224 | // work. |
| 6225 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
| 6226 | !Arg->getType()->getContainedAutoType()) { |
| 6227 | Converted = TemplateArgument(Arg); |
| 6228 | return Arg; |
| 6229 | } |
| 6230 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, |
| 6231 | // we should actually be checking the type of the template argument in P, |
| 6232 | // not the type of the template argument deduced from A, against the |
| 6233 | // template parameter type. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6234 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6235 | << Arg->getType() |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6236 | << ParamType.getUnqualifiedType(); |
| 6237 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6238 | return ExprError(); |
| 6239 | } |
| 6240 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6241 | // If either the parameter has a dependent type or the argument is |
| 6242 | // type-dependent, there's nothing we can check now. |
| 6243 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 6244 | // FIXME: Produce a cloned, canonical expression? |
| 6245 | Converted = TemplateArgument(Arg); |
| 6246 | return Arg; |
| 6247 | } |
| 6248 | |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6249 | // The initialization of the parameter from the argument is |
| 6250 | // a constant-evaluated context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6251 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 6252 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6253 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6254 | if (getLangOpts().CPlusPlus17) { |
| 6255 | // C++17 [temp.arg.nontype]p1: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6256 | // A template-argument for a non-type template parameter shall be |
| 6257 | // a converted constant expression of the type of the template-parameter. |
| 6258 | APValue Value; |
| 6259 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 6260 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 6261 | if (ArgResult.isInvalid()) |
| 6262 | return ExprError(); |
| 6263 | |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6264 | // For a value-dependent argument, CheckConvertedConstantExpression is |
| 6265 | // permitted (and expected) to be unable to determine a value. |
| 6266 | if (ArgResult.get()->isValueDependent()) { |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6267 | Converted = TemplateArgument(ArgResult.get()); |
| 6268 | return ArgResult; |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6269 | } |
| 6270 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6271 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 6272 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6273 | // Convert the APValue to a TemplateArgument. |
| 6274 | switch (Value.getKind()) { |
| 6275 | case APValue::Uninitialized: |
| 6276 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6277 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6278 | break; |
| 6279 | case APValue::Int: |
| 6280 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6281 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6282 | break; |
| 6283 | case APValue::MemberPointer: { |
| 6284 | assert(ParamType->isMemberPointerType()); |
| 6285 | |
| 6286 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 6287 | if (!Value.getMemberPointerPath().empty()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6288 | Diag(Arg->getBeginLoc(), |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6289 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 6290 | << Value.getMemberPointerDecl() << ParamType |
| 6291 | << Arg->getSourceRange(); |
| 6292 | return ExprError(); |
| 6293 | } |
| 6294 | |
| 6295 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6296 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6297 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6298 | break; |
| 6299 | } |
| 6300 | case APValue::LValue: { |
| 6301 | // For a non-type template-parameter of pointer or reference type, |
| 6302 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6303 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 6304 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6305 | // -- a temporary object |
| 6306 | // -- a string literal |
| 6307 | // -- the result of a typeid expression, or |
Eric Christopher | 0d2c56a | 2017-03-31 01:45:39 +0000 | [diff] [blame] | 6308 | // -- a predefined __func__ variable |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6309 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 6310 | if (isa<CXXUuidofExpr>(E)) { |
Bill Wendling | ff57307 | 2019-01-27 07:24:03 +0000 | [diff] [blame] | 6311 | Converted = TemplateArgument(ArgResult.get()->IgnoreImpCasts()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6312 | break; |
| 6313 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6314 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 6315 | << Arg->getSourceRange(); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6316 | return ExprError(); |
| 6317 | } |
| 6318 | auto *VD = const_cast<ValueDecl *>( |
| 6319 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 6320 | // -- a subobject |
| 6321 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 6322 | VD && VD->getType()->isArrayType() && |
| 6323 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 6324 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 6325 | // Per defect report (no number yet): |
| 6326 | // ... other than a pointer to the first element of a complete array |
| 6327 | // object. |
| 6328 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 6329 | Value.isLValueOnePastTheEnd()) { |
| 6330 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 6331 | << Value.getAsString(Context, ParamType); |
| 6332 | return ExprError(); |
| 6333 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6334 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6335 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6336 | assert((!VD || !ParamType->isNullPtrType()) && |
| 6337 | "non-null value of type nullptr_t?"); |
| 6338 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6339 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6340 | break; |
| 6341 | } |
| 6342 | case APValue::AddrLabelDiff: |
| 6343 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
Leonard Chan | 86285d2 | 2019-01-16 18:53:05 +0000 | [diff] [blame] | 6344 | case APValue::FixedPoint: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6345 | case APValue::Float: |
| 6346 | case APValue::ComplexInt: |
| 6347 | case APValue::ComplexFloat: |
| 6348 | case APValue::Vector: |
| 6349 | case APValue::Array: |
| 6350 | case APValue::Struct: |
| 6351 | case APValue::Union: |
| 6352 | llvm_unreachable("invalid kind for template argument"); |
| 6353 | } |
| 6354 | |
| 6355 | return ArgResult.get(); |
| 6356 | } |
| 6357 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6358 | // C++ [temp.arg.nontype]p5: |
| 6359 | // The following conversions are performed on each expression used |
| 6360 | // as a non-type template-argument. If a non-type |
| 6361 | // template-argument cannot be converted to the type of the |
| 6362 | // corresponding template-parameter then the program is |
| 6363 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6364 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6365 | // C++11: |
| 6366 | // -- for a non-type template-parameter of integral or |
| 6367 | // enumeration type, conversions permitted in a converted |
| 6368 | // constant expression are applied. |
| 6369 | // |
| 6370 | // C++98: |
| 6371 | // -- for a non-type template-parameter of integral or |
| 6372 | // enumeration type, integral promotions (4.5) and integral |
| 6373 | // conversions (4.7) are applied. |
| 6374 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6375 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6376 | // C++ [temp.arg.nontype]p1: |
| 6377 | // A template-argument for a non-type, non-template template-parameter |
| 6378 | // shall be one of: |
| 6379 | // |
| 6380 | // -- for a non-type template-parameter of integral or enumeration |
| 6381 | // type, a converted constant expression of the type of the |
| 6382 | // template-parameter; or |
| 6383 | llvm::APSInt Value; |
| 6384 | ExprResult ArgResult = |
| 6385 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 6386 | CCEK_TemplateArg); |
| 6387 | if (ArgResult.isInvalid()) |
| 6388 | return ExprError(); |
| 6389 | |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6390 | // We can't check arbitrary value-dependent arguments. |
| 6391 | if (ArgResult.get()->isValueDependent()) { |
| 6392 | Converted = TemplateArgument(ArgResult.get()); |
| 6393 | return ArgResult; |
| 6394 | } |
| 6395 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6396 | // Widen the argument value to sizeof(parameter type). This is almost |
| 6397 | // always a no-op, except when the parameter type is bool. In |
| 6398 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 6399 | QualType IntegerType = ParamType; |
| 6400 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 6401 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 6402 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 6403 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6404 | Converted = TemplateArgument(Context, Value, |
| 6405 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6406 | return ArgResult; |
| 6407 | } |
| 6408 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6409 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 6410 | if (ArgResult.isInvalid()) |
| 6411 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6412 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6413 | |
| 6414 | QualType ArgType = Arg->getType(); |
| 6415 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6416 | // C++ [temp.arg.nontype]p1: |
| 6417 | // A template-argument for a non-type, non-template |
| 6418 | // template-parameter shall be one of: |
| 6419 | // |
| 6420 | // -- an integral constant-expression of integral or enumeration |
| 6421 | // type; or |
| 6422 | // -- the name of a non-type template-parameter; or |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6423 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6424 | if (!ArgType->isIntegralOrEnumerationType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6425 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_integral_or_enumeral) |
| 6426 | << ArgType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6427 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6428 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6429 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6430 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 6431 | QualType T; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6432 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6433 | public: |
| 6434 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 6435 | |
| 6436 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 6437 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6438 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 6439 | } |
| 6440 | } Diagnoser(ArgType); |
| 6441 | |
| 6442 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6443 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6444 | if (!Arg) |
| 6445 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6446 | } |
| 6447 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6448 | // From here on out, all we care about is the unqualified form |
| 6449 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6450 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6451 | |
| 6452 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 6453 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6454 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6455 | } else if (ParamType->isBooleanType()) { |
| 6456 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6457 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6458 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 6459 | !ParamType->isEnumeralType()) { |
| 6460 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6461 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6462 | } else { |
| 6463 | // We can't perform this conversion. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6464 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 6465 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6466 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6467 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6468 | } |
| 6469 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6470 | // Add the value of this argument to the list of converted |
| 6471 | // arguments. We use the bitwidth and signedness of the template |
| 6472 | // parameter. |
| 6473 | if (Arg->isValueDependent()) { |
| 6474 | // The argument is value-dependent. Create a new |
| 6475 | // TemplateArgument with the converted expression. |
| 6476 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6477 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6478 | } |
| 6479 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6480 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6481 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 6482 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6483 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6484 | if (ParamType->isBooleanType()) { |
| 6485 | // Value must be zero or one. |
| 6486 | Value = Value != 0; |
| 6487 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 6488 | if (Value.getBitWidth() != AllowedBits) |
| 6489 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6490 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6491 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6492 | llvm::APSInt OldValue = Value; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6493 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6494 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6495 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6496 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6497 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6498 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6499 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6500 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6501 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6502 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6503 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6504 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_negative) |
| 6505 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6506 | << Arg->getSourceRange(); |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6507 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6508 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6509 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6510 | // Complain if we overflowed the template parameter's type. |
| 6511 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6512 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6513 | RequiredBits = OldValue.getActiveBits(); |
| 6514 | else if (OldValue.isUnsigned()) |
| 6515 | RequiredBits = OldValue.getActiveBits() + 1; |
| 6516 | else |
| 6517 | RequiredBits = OldValue.getMinSignedBits(); |
| 6518 | if (RequiredBits > AllowedBits) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6519 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_too_large) |
| 6520 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6521 | << Arg->getSourceRange(); |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6522 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6523 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6524 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6525 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6526 | Converted = TemplateArgument(Context, Value, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6527 | ParamType->isEnumeralType() |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 6528 | ? Context.getCanonicalType(ParamType) |
| 6529 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6530 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6531 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6532 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6533 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6534 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 6535 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6536 | // Handle pointer-to-function, reference-to-function, and |
| 6537 | // pointer-to-member-function all in (roughly) the same way. |
| 6538 | if (// -- For a non-type template-parameter of type pointer to |
| 6539 | // function, only the function-to-pointer conversion (4.3) is |
| 6540 | // applied. If the template-argument represents a set of |
| 6541 | // overloaded functions (or a pointer to such), the matching |
| 6542 | // function is selected from the set (13.4). |
| 6543 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6544 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6545 | // -- For a non-type template-parameter of type reference to |
| 6546 | // function, no conversions apply. If the template-argument |
| 6547 | // represents a set of overloaded functions, the matching |
| 6548 | // function is selected from the set (13.4). |
| 6549 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6550 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6551 | // -- For a non-type template-parameter of type pointer to |
| 6552 | // member function, no conversions apply. If the |
| 6553 | // template-argument represents a set of overloaded member |
| 6554 | // functions, the matching member function is selected from |
| 6555 | // the set (13.4). |
| 6556 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6557 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6558 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6559 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6560 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6561 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6562 | true, |
| 6563 | FoundResult)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6564 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6565 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6566 | |
| 6567 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6568 | ArgType = Arg->getType(); |
| 6569 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6570 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6571 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6572 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6573 | if (!ParamType->isMemberPointerType()) { |
| 6574 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6575 | ParamType, |
| 6576 | Arg, Converted)) |
| 6577 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6578 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6579 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6580 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6581 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6582 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6583 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6584 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6585 | } |
| 6586 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 6587 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6588 | // -- for a non-type template-parameter of type pointer to |
| 6589 | // object, qualification conversions (4.4) and the |
| 6590 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6591 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6592 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6593 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6594 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6595 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6596 | ParamType, |
| 6597 | Arg, Converted)) |
| 6598 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6599 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6600 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6601 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6602 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6603 | // -- For a non-type template-parameter of type reference to |
| 6604 | // object, no conversions apply. The type referred to by the |
| 6605 | // reference may be more cv-qualified than the (otherwise |
| 6606 | // identical) type of the template-argument. The |
| 6607 | // template-parameter is bound directly to the |
| 6608 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6609 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6610 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6611 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6612 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6613 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 6614 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6615 | true, |
| 6616 | FoundResult)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6617 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6618 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6619 | |
| 6620 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6621 | ArgType = Arg->getType(); |
| 6622 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6623 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6624 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6625 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6626 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6627 | ParamType, |
| 6628 | Arg, Converted)) |
| 6629 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6630 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6631 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6632 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6633 | // Deal with parameters of type std::nullptr_t. |
| 6634 | if (ParamType->isNullPtrType()) { |
| 6635 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6636 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6637 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6638 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6639 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6640 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 6641 | case NPV_NotNullPointer: |
| 6642 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 6643 | << Arg->getType() << ParamType; |
| 6644 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6645 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6646 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6647 | case NPV_Error: |
| 6648 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6649 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6650 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 6651 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 6652 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 6653 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6654 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6655 | } |
| 6656 | } |
| 6657 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6658 | // -- For a non-type template-parameter of type pointer to data |
| 6659 | // member, qualification conversions (4.4) are applied. |
| 6660 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 6661 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6662 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6663 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6664 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6665 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6668 | static void DiagnoseTemplateParameterListArityMismatch( |
| 6669 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 6670 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 6671 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6672 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6673 | /// template template parameter. |
| 6674 | /// |
| 6675 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 6676 | /// It returns true if an error occurred, and false otherwise. |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 6677 | bool Sema::CheckTemplateTemplateArgument(TemplateParameterList *Params, |
| 6678 | TemplateArgumentLoc &Arg) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6679 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6680 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 6681 | if (!Template) { |
| 6682 | // Any dependent template name is fine. |
| 6683 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 6684 | return false; |
| 6685 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6686 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6687 | if (Template->isInvalidDecl()) |
| 6688 | return true; |
| 6689 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6690 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6691 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6692 | // the name of a class template or an alias template, expressed as an |
| 6693 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6694 | // primary class templates are considered when matching the |
| 6695 | // template template argument with the corresponding parameter; |
| 6696 | // partial specializations are not considered even if their |
| 6697 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6698 | // |
| 6699 | // Note that we also allow template template parameters here, which |
| 6700 | // will happen when we are dealing with, e.g., class template |
| 6701 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6702 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6703 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6704 | !isa<TypeAliasTemplateDecl>(Template) && |
| 6705 | !isa<BuiltinTemplateDecl>(Template)) { |
| 6706 | assert(isa<FunctionTemplateDecl>(Template) && |
| 6707 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 6708 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6709 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 6710 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6713 | // C++1z [temp.arg.template]p3: (DR 150) |
| 6714 | // A template-argument matches a template template-parameter P when P |
| 6715 | // is at least as specialized as the template-argument A. |
| 6716 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 6717 | // Quick check for the common case: |
| 6718 | // If P contains a parameter pack, then A [...] matches P if each of A's |
| 6719 | // template parameters matches the corresponding template parameter in |
| 6720 | // the template-parameter-list of P. |
| 6721 | if (TemplateParameterListsAreEqual( |
| 6722 | Template->getTemplateParameters(), Params, false, |
| 6723 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 6724 | return false; |
| 6725 | |
| 6726 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 6727 | Arg.getLocation())) |
| 6728 | return false; |
| 6729 | // FIXME: Produce better diagnostics for deduction failures. |
| 6730 | } |
| 6731 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6732 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 6733 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6734 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6735 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6736 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6737 | } |
| 6738 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6739 | /// Given a non-type template argument that refers to a |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6740 | /// declaration and the type of its corresponding non-type template |
| 6741 | /// parameter, produce an expression that properly refers to that |
| 6742 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6743 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6744 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 6745 | QualType ParamType, |
| 6746 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 6747 | // C++ [temp.param]p8: |
| 6748 | // |
| 6749 | // A non-type template-parameter of type "array of T" or |
| 6750 | // "function returning T" is adjusted to be of type "pointer to |
| 6751 | // T" or "pointer to function returning T", respectively. |
| 6752 | if (ParamType->isArrayType()) |
| 6753 | ParamType = Context.getArrayDecayedType(ParamType); |
| 6754 | else if (ParamType->isFunctionType()) |
| 6755 | ParamType = Context.getPointerType(ParamType); |
| 6756 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6757 | // For a NULL non-type template argument, return nullptr casted to the |
| 6758 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6759 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6760 | return ImpCastExprToType( |
| 6761 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 6762 | ParamType, |
| 6763 | ParamType->getAs<MemberPointerType>() |
| 6764 | ? CK_NullToMemberPointer |
| 6765 | : CK_NullToPointer); |
| 6766 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6767 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 6768 | "Only declaration template arguments permitted here"); |
| 6769 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6770 | ValueDecl *VD = Arg.getAsDecl(); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6771 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6772 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 6773 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 6774 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6775 | // If the value is a class member, we might have a pointer-to-member. |
| 6776 | // Determine whether the non-type template template parameter is of |
| 6777 | // pointer-to-member type. If so, we need to build an appropriate |
| 6778 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 6779 | // would refer to the member itself. |
| 6780 | if (ParamType->isMemberPointerType()) { |
| 6781 | QualType ClassType |
| 6782 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 6783 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6784 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6785 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6786 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 6787 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6788 | |
| 6789 | // The actual value-ness of this is unimportant, but for |
| 6790 | // internal consistency's sake, references to instance methods |
| 6791 | // are r-values. |
| 6792 | ExprValueKind VK = VK_LValue; |
| 6793 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 6794 | VK = VK_RValue; |
| 6795 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6796 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6797 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6798 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6799 | Loc, |
| 6800 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6801 | if (RefExpr.isInvalid()) |
| 6802 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6803 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6804 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6805 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6806 | // We might need to perform a trailing qualification conversion, since |
| 6807 | // the element type on the parameter could be more qualified than the |
| 6808 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6809 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6810 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6811 | ParamType.getUnqualifiedType(), false, |
| 6812 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6813 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6814 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6815 | assert(!RefExpr.isInvalid() && |
| 6816 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6817 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6818 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6819 | } |
| 6820 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6821 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6822 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6823 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6824 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6825 | // When the non-type template parameter is a pointer, take the |
| 6826 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6827 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6828 | if (RefExpr.isInvalid()) |
| 6829 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6830 | |
Richard Smith | fc6fca1 | 2017-01-28 00:38:35 +0000 | [diff] [blame] | 6831 | if (!Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
| 6832 | (T->isFunctionType() || T->isArrayType())) { |
| 6833 | // Decay functions and arrays unless we're forming a pointer to array. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6834 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6835 | if (RefExpr.isInvalid()) |
| 6836 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6837 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6838 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6839 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6840 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6841 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6842 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6843 | } |
| 6844 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6845 | ExprValueKind VK = VK_RValue; |
| 6846 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6847 | // If the non-type template parameter has reference type, qualify the |
| 6848 | // resulting declaration reference with the extra qualifiers on the |
| 6849 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6850 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 6851 | VK = VK_LValue; |
| 6852 | T = Context.getQualifiedType(T, |
| 6853 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6854 | } else if (isa<FunctionDecl>(VD)) { |
| 6855 | // References to functions are always lvalues. |
| 6856 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6857 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6858 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6859 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6860 | } |
| 6861 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6862 | /// Construct a new expression that refers to the given |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6863 | /// integral template argument with the given source-location |
| 6864 | /// information. |
| 6865 | /// |
| 6866 | /// This routine takes care of the mapping from an integral template |
| 6867 | /// argument (which may have any integral type) to the appropriate |
| 6868 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6869 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6870 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 6871 | SourceLocation Loc) { |
| 6872 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 6873 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6874 | QualType OrigT = Arg.getIntegralType(); |
| 6875 | |
| 6876 | // If this is an enum type that we're instantiating, we need to use an integer |
| 6877 | // type the same size as the enumerator. We don't want to build an |
| 6878 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 6879 | // any integral type with C++11 enum classes, make sure we create the right |
| 6880 | // type of literal for it. |
| 6881 | QualType T = OrigT; |
| 6882 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 6883 | T = ET->getDecl()->getIntegerType(); |
| 6884 | |
| 6885 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6886 | if (T->isAnyCharacterType()) { |
| 6887 | CharacterLiteral::CharacterKind Kind; |
| 6888 | if (T->isWideCharType()) |
| 6889 | Kind = CharacterLiteral::Wide; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 6890 | else if (T->isChar8Type() && getLangOpts().Char8) |
| 6891 | Kind = CharacterLiteral::UTF8; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6892 | else if (T->isChar16Type()) |
| 6893 | Kind = CharacterLiteral::UTF16; |
| 6894 | else if (T->isChar32Type()) |
| 6895 | Kind = CharacterLiteral::UTF32; |
| 6896 | else |
| 6897 | Kind = CharacterLiteral::Ascii; |
| 6898 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6899 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 6900 | Kind, T, Loc); |
| 6901 | } else if (T->isBooleanType()) { |
| 6902 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 6903 | T, Loc); |
| 6904 | } else if (T->isNullPtrType()) { |
| 6905 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 6906 | } else { |
| 6907 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6908 | } |
| 6909 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6910 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6911 | // FIXME: This is a hack. We need a better way to handle substituted |
| 6912 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6913 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 6914 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6915 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6916 | Loc, Loc); |
| 6917 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6918 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6919 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6922 | /// Match two template parameters within template parameter lists. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6923 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 6924 | bool Complain, |
| 6925 | Sema::TemplateParameterListEqualKind Kind, |
| 6926 | SourceLocation TemplateArgLoc) { |
| 6927 | // Check the actual kind (type, non-type, template). |
| 6928 | if (Old->getKind() != New->getKind()) { |
| 6929 | if (Complain) { |
| 6930 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 6931 | if (TemplateArgLoc.isValid()) { |
| 6932 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6933 | NextDiag = diag::note_template_param_different_kind; |
| 6934 | } |
| 6935 | S.Diag(New->getLocation(), NextDiag) |
| 6936 | << (Kind != Sema::TPL_TemplateMatch); |
| 6937 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 6938 | << (Kind != Sema::TPL_TemplateMatch); |
| 6939 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6940 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6941 | return false; |
| 6942 | } |
| 6943 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6944 | // Check that both are parameter packs or neither are parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6945 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6946 | // template template parameter, the template template parameter can have |
| 6947 | // a parameter pack where the template template argument does not. |
| 6948 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 6949 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 6950 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6951 | if (Complain) { |
| 6952 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 6953 | if (TemplateArgLoc.isValid()) { |
| 6954 | S.Diag(TemplateArgLoc, |
| 6955 | diag::err_template_arg_template_params_mismatch); |
| 6956 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 6957 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6958 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6959 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 6960 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 6961 | : 2; |
| 6962 | S.Diag(New->getLocation(), NextDiag) |
| 6963 | << ParamKind << New->isParameterPack(); |
| 6964 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 6965 | << ParamKind << Old->isParameterPack(); |
| 6966 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6967 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6968 | return false; |
| 6969 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6970 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6971 | // For non-type template parameters, check the type of the parameter. |
| 6972 | if (NonTypeTemplateParmDecl *OldNTTP |
| 6973 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 6974 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6975 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6976 | // If we are matching a template template argument to a template |
| 6977 | // template parameter and one of the non-type template parameter types |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 6978 | // is dependent, then we must wait until template instantiation time |
| 6979 | // to actually compare the arguments. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6980 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 6981 | (OldNTTP->getType()->isDependentType() || |
| 6982 | NewNTTP->getType()->isDependentType())) |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6983 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6984 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6985 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 6986 | if (Complain) { |
| 6987 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 6988 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6989 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6990 | diag::err_template_arg_template_params_mismatch); |
| 6991 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 6992 | } |
| 6993 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 6994 | << NewNTTP->getType() |
| 6995 | << (Kind != Sema::TPL_TemplateMatch); |
| 6996 | S.Diag(OldNTTP->getLocation(), |
| 6997 | diag::note_template_nontype_parm_prev_declaration) |
| 6998 | << OldNTTP->getType(); |
| 6999 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7000 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7001 | return false; |
| 7002 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7003 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7004 | return true; |
| 7005 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7006 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7007 | // For template template parameters, check the template parameter types. |
| 7008 | // The template parameter lists of template template |
| 7009 | // parameters must agree. |
| 7010 | if (TemplateTemplateParmDecl *OldTTP |
| 7011 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7012 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7013 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 7014 | OldTTP->getTemplateParameters(), |
| 7015 | Complain, |
| 7016 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7017 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7018 | : Kind), |
| 7019 | TemplateArgLoc); |
| 7020 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7021 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7022 | return true; |
| 7023 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 7024 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7025 | /// Diagnose a known arity mismatch when comparing template argument |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7026 | /// lists. |
| 7027 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7028 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7029 | TemplateParameterList *New, |
| 7030 | TemplateParameterList *Old, |
| 7031 | Sema::TemplateParameterListEqualKind Kind, |
| 7032 | SourceLocation TemplateArgLoc) { |
| 7033 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 7034 | if (TemplateArgLoc.isValid()) { |
| 7035 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 7036 | NextDiag = diag::note_template_param_list_different_arity; |
| 7037 | } |
| 7038 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 7039 | << (New->size() > Old->size()) |
| 7040 | << (Kind != Sema::TPL_TemplateMatch) |
| 7041 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 7042 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 7043 | << (Kind != Sema::TPL_TemplateMatch) |
| 7044 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 7045 | } |
| 7046 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7047 | /// Determine whether the given template parameter lists are |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7048 | /// equivalent. |
| 7049 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7050 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7051 | /// source code as part of a new template declaration. |
| 7052 | /// |
| 7053 | /// \param Old The old template parameter list, typically found via |
| 7054 | /// name lookup of the template declared with this template parameter |
| 7055 | /// list. |
| 7056 | /// |
| 7057 | /// \param Complain If true, this routine will produce a diagnostic if |
| 7058 | /// the template parameter lists are not equivalent. |
| 7059 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7060 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7061 | /// |
| 7062 | /// \param TemplateArgLoc If this source location is valid, then we |
| 7063 | /// are actually checking the template parameter list of a template |
| 7064 | /// argument (New) against the template parameter list of its |
| 7065 | /// corresponding template template parameter (Old). We produce |
| 7066 | /// slightly different diagnostics in this scenario. |
| 7067 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7068 | /// \returns True if the template parameter lists are equal, false |
| 7069 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7070 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7071 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 7072 | TemplateParameterList *Old, |
| 7073 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7074 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7075 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7076 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 7077 | if (Complain) |
| 7078 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7079 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7080 | |
| 7081 | return false; |
| 7082 | } |
| 7083 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7084 | // C++0x [temp.arg.template]p3: |
| 7085 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7086 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7087 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7088 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7089 | // template-parameter-list of P. [...] |
| 7090 | TemplateParameterList::iterator NewParm = New->begin(); |
| 7091 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7092 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7093 | OldParmEnd = Old->end(); |
| 7094 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 7095 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 7096 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7097 | if (NewParm == NewParmEnd) { |
| 7098 | if (Complain) |
| 7099 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7100 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7101 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7102 | return false; |
| 7103 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7104 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7105 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7106 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7107 | return false; |
| 7108 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7109 | ++NewParm; |
| 7110 | continue; |
| 7111 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7112 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7113 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7114 | // [...] When P's template- parameter-list contains a template parameter |
| 7115 | // pack (14.5.3), the template parameter pack will match zero or more |
| 7116 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7117 | // template-parameter-list of A with the same type and form as the |
| 7118 | // template parameter pack in P (ignoring whether those template |
| 7119 | // parameters are template parameter packs). |
| 7120 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 7121 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7122 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7123 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7124 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7125 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7126 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7127 | // Make sure we exhausted all of the arguments. |
| 7128 | if (NewParm != NewParmEnd) { |
| 7129 | if (Complain) |
| 7130 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7131 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7132 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7133 | return false; |
| 7134 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7135 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7136 | return true; |
| 7137 | } |
| 7138 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7139 | /// Check whether a template can be declared within this scope. |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7140 | /// |
| 7141 | /// If the template declaration is valid in this scope, returns |
| 7142 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7143 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7144 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 7145 | if (!S) |
| 7146 | return false; |
| 7147 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7148 | // Find the nearest enclosing declaration scope. |
| 7149 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7150 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7151 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7152 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7153 | // C++ [temp]p4: |
| 7154 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7155 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 7156 | if (Ctx && Ctx->isExternCContext()) { |
| 7157 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 7158 | << TemplateParams->getSourceRange(); |
| 7159 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 7160 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 7161 | return true; |
| 7162 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 7163 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7164 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7165 | // C++ [temp]p2: |
| 7166 | // A template-declaration can appear only as a namespace scope or |
| 7167 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 7168 | if (Ctx) { |
| 7169 | if (Ctx->isFileContext()) |
| 7170 | return false; |
| 7171 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 7172 | // C++ [temp.mem]p2: |
| 7173 | // A local class shall not have member templates. |
| 7174 | if (RD->isLocalClass()) |
| 7175 | return Diag(TemplateParams->getTemplateLoc(), |
| 7176 | diag::err_template_inside_local_class) |
| 7177 | << TemplateParams->getSourceRange(); |
| 7178 | else |
| 7179 | return false; |
| 7180 | } |
| 7181 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7182 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7183 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7184 | diag::err_template_outside_namespace_or_class_scope) |
| 7185 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7186 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7187 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7188 | /// Determine what kind of template specialization the given declaration |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7189 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7190 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7191 | if (!D) |
| 7192 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7193 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7194 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 7195 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7196 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 7197 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7198 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 7199 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7200 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7201 | return TSK_Undeclared; |
| 7202 | } |
| 7203 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7204 | /// Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7205 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7206 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7207 | /// This routine determines whether a template specialization can be declared |
| 7208 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7209 | /// |
| 7210 | /// \param S the semantic analysis object for which this check is being |
| 7211 | /// performed. |
| 7212 | /// |
| 7213 | /// \param Specialized the entity being specialized or instantiated, which |
| 7214 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7215 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7216 | /// member class). |
| 7217 | /// |
| 7218 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 7219 | /// |
| 7220 | /// \param Loc the location of the explicit specialization or instantiation of |
| 7221 | /// this entity. |
| 7222 | /// |
| 7223 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 7224 | /// a class template. |
| 7225 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7226 | /// \returns true if there was an error that we cannot recover from, false |
| 7227 | /// otherwise. |
| 7228 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 7229 | NamedDecl *Specialized, |
| 7230 | NamedDecl *PrevDecl, |
| 7231 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7232 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7233 | // Keep these "kind" numbers in sync with the %select statements in the |
| 7234 | // various diagnostics emitted by this routine. |
| 7235 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7236 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7237 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7238 | else if (isa<VarTemplateDecl>(Specialized)) |
| 7239 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7240 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7241 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7242 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7243 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7244 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7245 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7246 | else if (isa<RecordDecl>(Specialized)) |
| 7247 | EntityKind = 7; |
| 7248 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 7249 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7250 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7251 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7252 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7253 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7254 | return true; |
| 7255 | } |
| 7256 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7257 | // C++ [temp.expl.spec]p2: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7258 | // An explicit specialization may be declared in any scope in which |
| 7259 | // the corresponding primary template may be defined. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7260 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7261 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7262 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7263 | return true; |
| 7264 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 7265 | |
| 7266 | // C++ [temp.class.spec]p6: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7267 | // A class template partial specialization may be declared in any |
| 7268 | // scope in which the primary template may be defined. |
| 7269 | DeclContext *SpecializedContext = |
| 7270 | Specialized->getDeclContext()->getRedeclContext(); |
| 7271 | DeclContext *DC = S.CurContext->getRedeclContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7272 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7273 | // Make sure that this redeclaration (or definition) occurs in the same |
| 7274 | // scope or an enclosing namespace. |
| 7275 | if (!(DC->isFileContext() ? DC->Encloses(SpecializedContext) |
| 7276 | : DC->Equals(SpecializedContext))) { |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7277 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 7278 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 7279 | << EntityKind << Specialized; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7280 | else { |
| 7281 | auto *ND = cast<NamedDecl>(SpecializedContext); |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7282 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7283 | if (S.getLangOpts().MicrosoftExt && !DC->isRecord()) |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7284 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 7285 | S.Diag(Loc, Diag) << EntityKind << Specialized |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7286 | << ND << isa<CXXRecordDecl>(ND); |
| 7287 | } |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7288 | |
| 7289 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7290 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7291 | // Don't allow specializing in the wrong class during error recovery. |
| 7292 | // Otherwise, things can go horribly wrong. |
| 7293 | if (DC->isRecord()) |
| 7294 | return true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7295 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7296 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7297 | return false; |
| 7298 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7299 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7300 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 7301 | if (!E->isTypeDependent()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7302 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7303 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7304 | Checker.TraverseStmt(E); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7305 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7306 | return E->getSourceRange(); |
| 7307 | return Checker.MatchLoc; |
| 7308 | } |
| 7309 | |
| 7310 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 7311 | if (!TL.getType()->isDependentType()) |
| 7312 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7313 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7314 | Checker.TraverseTypeLoc(TL); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7315 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7316 | return TL.getSourceRange(); |
| 7317 | return Checker.MatchLoc; |
| 7318 | } |
| 7319 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7320 | /// Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7321 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7322 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7323 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 7324 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7325 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 7326 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7327 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7328 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 7329 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7330 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7331 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7332 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7333 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7334 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7335 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7336 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7337 | |
| 7338 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7339 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 7340 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7341 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 7342 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7343 | |
| 7344 | // Strip off any implicit casts we added as part of type checking. |
| 7345 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 7346 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7347 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7348 | // C++ [temp.class.spec]p8: |
| 7349 | // A non-type argument is non-specialized if it is the name of a |
| 7350 | // non-type parameter. All other non-type arguments are |
| 7351 | // specialized. |
| 7352 | // |
| 7353 | // Below, we check the two conditions that only apply to |
| 7354 | // specialized non-type arguments, so skip any non-specialized |
| 7355 | // arguments. |
| 7356 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7357 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7358 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7359 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7360 | // C++ [temp.class.spec]p9: |
| 7361 | // Within the argument list of a class template partial |
| 7362 | // specialization, the following restrictions apply: |
| 7363 | // -- A partially specialized non-type argument expression |
| 7364 | // shall not involve a template parameter of the partial |
| 7365 | // specialization except when the argument expression is a |
| 7366 | // simple identifier. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7367 | // -- The type of a template parameter corresponding to a |
| 7368 | // specialized non-type argument shall not be dependent on a |
| 7369 | // parameter of the specialization. |
| 7370 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
| 7371 | // We implement a compromise between the original rules and DR1315: |
| 7372 | // -- A specialized non-type template argument shall not be |
| 7373 | // type-dependent and the corresponding template parameter |
| 7374 | // shall have a non-dependent type. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7375 | SourceRange ParamUseRange = |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7376 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7377 | if (ParamUseRange.isValid()) { |
| 7378 | if (IsDefaultArgument) { |
| 7379 | S.Diag(TemplateNameLoc, |
| 7380 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 7381 | S.Diag(ParamUseRange.getBegin(), |
| 7382 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 7383 | << ParamUseRange; |
| 7384 | } else { |
| 7385 | S.Diag(ParamUseRange.getBegin(), |
| 7386 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 7387 | << ParamUseRange; |
| 7388 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7389 | return true; |
| 7390 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7391 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7392 | ParamUseRange = findTemplateParameter( |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7393 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7394 | if (ParamUseRange.isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7395 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getBeginLoc(), |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7396 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7397 | << Param->getType(); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7398 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7399 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 7400 | << ParamUseRange; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7401 | return true; |
| 7402 | } |
| 7403 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7404 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7405 | return false; |
| 7406 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7407 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7408 | /// Check the non-type template arguments of a class template |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7409 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 7410 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7411 | /// \param TemplateNameLoc the location of the template name. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7412 | /// \param PrimaryTemplate the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7413 | /// template. |
| 7414 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7415 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7416 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7417 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7418 | /// \returns \c true if there was an error, \c false otherwise. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7419 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 7420 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 7421 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 7422 | // We have to be conservative when checking a template in a dependent |
| 7423 | // context. |
| 7424 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 7425 | return false; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7426 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7427 | TemplateParameterList *TemplateParams = |
| 7428 | PrimaryTemplate->getTemplateParameters(); |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7429 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7430 | NonTypeTemplateParmDecl *Param |
| 7431 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 7432 | if (!Param) |
| 7433 | continue; |
| 7434 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7435 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 7436 | Param, &TemplateArgs[I], |
| 7437 | 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7438 | return true; |
| 7439 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7440 | |
| 7441 | return false; |
| 7442 | } |
| 7443 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7444 | DeclResult Sema::ActOnClassTemplateSpecialization( |
| 7445 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
| 7446 | SourceLocation ModulePrivateLoc, TemplateIdAnnotation &TemplateId, |
| 7447 | const ParsedAttributesView &Attr, |
| 7448 | MultiTemplateParamsArg TemplateParameterLists, SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7449 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 7450 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7451 | CXXScopeSpec &SS = TemplateId.SS; |
| 7452 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7453 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 7454 | // store the location of the outermost template keyword in the declaration. |
| 7455 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7456 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 7457 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 7458 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 7459 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7460 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7461 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7462 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7463 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7464 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 7465 | |
| 7466 | if (!ClassTemplate) { |
| 7467 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7468 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7469 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 7470 | return true; |
| 7471 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7472 | |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7473 | bool isMemberSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7474 | bool isPartialSpecialization = false; |
| 7475 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7476 | // Check the validity of the template headers that introduce this |
| 7477 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7478 | // FIXME: We probably shouldn't complain about these headers for |
| 7479 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7480 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 7481 | TemplateParameterList *TemplateParams = |
| 7482 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7483 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7484 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7485 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7486 | if (Invalid) |
| 7487 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7488 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7489 | if (TemplateParams && TemplateParams->size() > 0) { |
| 7490 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7491 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 7492 | if (TUK == TUK_Friend) { |
| 7493 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 7494 | << SourceRange(LAngleLoc, RAngleLoc); |
| 7495 | return true; |
| 7496 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7497 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7498 | // C++ [temp.class.spec]p10: |
| 7499 | // The template parameter list of a specialization shall not |
| 7500 | // contain default template argument values. |
| 7501 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7502 | Decl *Param = TemplateParams->getParam(I); |
| 7503 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 7504 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7505 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7506 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 7507 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7508 | } |
| 7509 | } else if (NonTypeTemplateParmDecl *NTTP |
| 7510 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 7511 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7512 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7513 | diag::err_default_arg_in_partial_spec) |
| 7514 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7515 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7516 | } |
| 7517 | } else { |
| 7518 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7519 | if (TTP->hasDefaultArgument()) { |
| 7520 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7521 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7522 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7523 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7524 | } |
| 7525 | } |
| 7526 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7527 | } else if (TemplateParams) { |
| 7528 | if (TUK == TUK_Friend) |
| 7529 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7530 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7531 | SourceRange(TemplateParams->getTemplateLoc(), |
| 7532 | TemplateParams->getRAngleLoc())) |
| 7533 | << SourceRange(LAngleLoc, RAngleLoc); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7534 | } else { |
| 7535 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7536 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7537 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7538 | // Check that the specialization uses the same tag kind as the |
| 7539 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7540 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7541 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7542 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7543 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7544 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7545 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7546 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7547 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7548 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7549 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7550 | diag::note_previous_use); |
| 7551 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7552 | } |
| 7553 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7554 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7555 | TemplateArgumentListInfo TemplateArgs = |
| 7556 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7557 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7558 | // Check for unexpanded parameter packs in any of the template arguments. |
| 7559 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7560 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7561 | UPPC_PartialSpecialization)) |
| 7562 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7563 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7564 | // Check that the template argument list is well-formed for this |
| 7565 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7566 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7567 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7568 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7569 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7570 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7571 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7572 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7573 | if (isPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7574 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 7575 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7576 | return true; |
| 7577 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7578 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
| 7579 | // also do it during instantiation. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 7580 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7581 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7582 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7583 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7584 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 7585 | << ClassTemplate->getDeclName(); |
| 7586 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7587 | } |
| 7588 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7589 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7590 | void *InsertPos = nullptr; |
| 7591 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7592 | |
| 7593 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7594 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7595 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7596 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7597 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7598 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7599 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7600 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7601 | // Check whether we can declare a class template specialization in |
| 7602 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7603 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7604 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 7605 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7606 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7607 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7608 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7609 | // The canonical type |
| 7610 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 7611 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7612 | // Build the canonical type that describes the converted template |
| 7613 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7614 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7615 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7616 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7617 | |
| 7618 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7619 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 7620 | // C++ [temp.class.spec]p9b3: |
| 7621 | // |
| 7622 | // -- The argument list of the specialization shall not be identical |
| 7623 | // to the implicit argument list of the primary template. |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 7624 | // |
| 7625 | // This rule has since been removed, because it's redundant given DR1495, |
| 7626 | // but we keep it because it produces better diagnostics and recovery. |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7627 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 7628 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 7629 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7630 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 7631 | ClassTemplate->getIdentifier(), |
| 7632 | TemplateNameLoc, |
| 7633 | Attr, |
| 7634 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7635 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 7636 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7637 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7638 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7639 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7640 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7641 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7642 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 7643 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7644 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7645 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7646 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7647 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 7648 | TemplateParams, |
| 7649 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7650 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7651 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7652 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 7653 | PrevPartial); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 7654 | SetNestedNameSpecifier(*this, Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7655 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7656 | Partial->setTemplateParameterListsInfo( |
| 7657 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7658 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7659 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7660 | if (!PrevPartial) |
| 7661 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7662 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 7663 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7664 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 7665 | // template specialization, make a note of that. |
| 7666 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 7667 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7668 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7669 | CheckTemplatePartialSpecialization(Partial); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7670 | } else { |
| 7671 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7672 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7673 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7674 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7675 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7676 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7677 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7678 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7679 | PrevDecl); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 7680 | SetNestedNameSpecifier(*this, Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7681 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 7682 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7683 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7684 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7685 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7686 | if (!PrevDecl) |
| 7687 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7688 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7689 | if (CurContext->isDependentContext()) { |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7690 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7691 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7692 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7693 | } else { |
| 7694 | CanonType = Context.getTypeDeclType(Specialization); |
| 7695 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7696 | } |
| 7697 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7698 | // C++ [temp.expl.spec]p6: |
| 7699 | // 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] | 7700 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7701 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7702 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7703 | // use occurs; no diagnostic is required. |
| 7704 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7705 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7706 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7707 | // Is there any previous explicit specialization declaration? |
| 7708 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7709 | Okay = true; |
| 7710 | break; |
| 7711 | } |
| 7712 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7713 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7714 | if (!Okay) { |
| 7715 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 7716 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 7717 | << Context.getTypeDeclType(Specialization) << Range; |
| 7718 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7719 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7720 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7721 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7722 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7723 | return true; |
| 7724 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7725 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7726 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7727 | // If this is not a friend, note that this is an explicit specialization. |
| 7728 | if (TUK != TUK_Friend) |
| 7729 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7730 | |
| 7731 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7732 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7733 | RecordDecl *Def = Specialization->getDefinition(); |
| 7734 | NamedDecl *Hidden = nullptr; |
| 7735 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 7736 | SkipBody->ShouldSkip = true; |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7737 | SkipBody->Previous = Def; |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 7738 | makeMergedDefinitionVisible(Hidden); |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7739 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7740 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 7741 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7742 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 7743 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7744 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7745 | } |
| 7746 | } |
| 7747 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7748 | ProcessDeclAttributeList(S, Specialization, Attr); |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 7749 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 7750 | // Add alignment attributes if necessary; these attributes are checked when |
| 7751 | // the ASTContext lays out the structure. |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7752 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 7753 | AddAlignmentAttributesForRecord(Specialization); |
| 7754 | AddMsStructLayoutForRecord(Specialization); |
| 7755 | } |
| 7756 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 7757 | if (ModulePrivateLoc.isValid()) |
| 7758 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 7759 | << (isPartialSpecialization? 1 : 0) |
| 7760 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7761 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 7762 | // Build the fully-sugared type for this class template |
| 7763 | // specialization as the user wrote in the specialization |
| 7764 | // itself. This means that we'll pretty-print the type retrieved |
| 7765 | // from the specialization's declaration the way that the user |
| 7766 | // actually wrote the specialization, rather than formatting the |
| 7767 | // name based on the "canonical" representation used to store the |
| 7768 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7769 | TypeSourceInfo *WrittenTy |
| 7770 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7771 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7772 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7773 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7774 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7775 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7776 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 7777 | // C++ [temp.expl.spec]p9: |
| 7778 | // A template explicit specialization is in the scope of the |
| 7779 | // namespace in which the template was defined. |
| 7780 | // |
| 7781 | // We actually implement this paragraph where we set the semantic |
| 7782 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 7783 | // but we also maintain the lexical context where the actual |
| 7784 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7785 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7786 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7787 | // We may be starting the definition of this specialization. |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7788 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7789 | Specialization->startDefinition(); |
| 7790 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7791 | if (TUK == TUK_Friend) { |
| 7792 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 7793 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 7794 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7795 | /*FIXME:*/KWLoc); |
| 7796 | Friend->setAccess(AS_public); |
| 7797 | CurContext->addDecl(Friend); |
| 7798 | } else { |
| 7799 | // Add the specialization into its lexical context, so that it can |
| 7800 | // be seen when iterating through the list of declarations in that |
| 7801 | // context. However, specializations are not found by name lookup. |
| 7802 | CurContext->addDecl(Specialization); |
| 7803 | } |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 7804 | |
| 7805 | if (SkipBody && SkipBody->ShouldSkip) |
| 7806 | return SkipBody->Previous; |
| 7807 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7808 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7809 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7810 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7811 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7812 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7813 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7814 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 7815 | ActOnDocumentableDecl(NewDecl); |
| 7816 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7817 | } |
| 7818 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7819 | /// Strips various properties off an implicit instantiation |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7820 | /// that has just been explicitly specialized. |
| 7821 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7822 | D->dropAttr<DLLImportAttr>(); |
| 7823 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7824 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7825 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7826 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7827 | } |
| 7828 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7829 | /// Compute the diagnostic location for an explicit instantiation |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7830 | // declaration or definition. |
| 7831 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7832 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7833 | // Explicit instantiations following a specialization have no effect and |
| 7834 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 7835 | // until a valid name loc is found. |
| 7836 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7837 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 7838 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7839 | PrevDiagLoc = Prev->getLocation(); |
| 7840 | } |
| 7841 | assert(PrevDiagLoc.isValid() && |
| 7842 | "Explicit instantiation without point of instantiation?"); |
| 7843 | return PrevDiagLoc; |
| 7844 | } |
| 7845 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7846 | /// Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7847 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7848 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7849 | /// new specialization/instantiation will have any effect. |
| 7850 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7851 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7852 | /// instantiation. |
| 7853 | /// |
| 7854 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 7855 | /// |
| 7856 | /// \param PrevDecl the previous declaration of the entity. |
| 7857 | /// |
| 7858 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 7859 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7860 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7861 | /// declaration was instantiated (either implicitly or explicitly). |
| 7862 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7863 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7864 | /// specialization or instantiation has no effect and should be ignored. |
| 7865 | /// |
| 7866 | /// \returns true if there was an error that should prevent the introduction of |
| 7867 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7868 | bool |
| 7869 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 7870 | TemplateSpecializationKind NewTSK, |
| 7871 | NamedDecl *PrevDecl, |
| 7872 | TemplateSpecializationKind PrevTSK, |
| 7873 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7874 | bool &HasNoEffect) { |
| 7875 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7876 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7877 | switch (NewTSK) { |
| 7878 | case TSK_Undeclared: |
| 7879 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 7880 | assert( |
| 7881 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 7882 | "previous declaration must be implicit!"); |
| 7883 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7884 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7885 | case TSK_ExplicitSpecialization: |
| 7886 | switch (PrevTSK) { |
| 7887 | case TSK_Undeclared: |
| 7888 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7889 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7890 | // explicitly specialized or has merely been mentioned without any |
| 7891 | // instantiation. |
| 7892 | return false; |
| 7893 | |
| 7894 | case TSK_ImplicitInstantiation: |
| 7895 | if (PrevPointOfInstantiation.isInvalid()) { |
| 7896 | // The declaration itself has not actually been instantiated, so it is |
| 7897 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7898 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7899 | return false; |
| 7900 | } |
| 7901 | // Fall through |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 7902 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7903 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7904 | case TSK_ExplicitInstantiationDeclaration: |
| 7905 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7906 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 7907 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7908 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7909 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7910 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7911 | // 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] | 7912 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7913 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7914 | // implicit instantiation to take place, in every translation unit in |
| 7915 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7916 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7917 | // Is there any previous explicit specialization declaration? |
| 7918 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 7919 | return false; |
| 7920 | } |
| 7921 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7922 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7923 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7924 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7925 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7926 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7927 | return true; |
| 7928 | } |
Galina Kistanova | 1d36e83 | 2017-06-08 18:20:32 +0000 | [diff] [blame] | 7929 | llvm_unreachable("The switch over PrevTSK must be exhaustive."); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7930 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7931 | case TSK_ExplicitInstantiationDeclaration: |
| 7932 | switch (PrevTSK) { |
| 7933 | case TSK_ExplicitInstantiationDeclaration: |
| 7934 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7935 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7936 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7937 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7938 | case TSK_Undeclared: |
| 7939 | case TSK_ImplicitInstantiation: |
| 7940 | // We're explicitly instantiating something that may have already been |
| 7941 | // implicitly instantiated; that's fine. |
| 7942 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7943 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7944 | case TSK_ExplicitSpecialization: |
| 7945 | // C++0x [temp.explicit]p4: |
| 7946 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7947 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7948 | // specialization for that template, the explicit instantiation has no |
| 7949 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7950 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7951 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7952 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7953 | case TSK_ExplicitInstantiationDefinition: |
| 7954 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7955 | // If an entity is the subject of both an explicit instantiation |
| 7956 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7957 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7958 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7959 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7960 | |
| 7961 | // Explicit instantiations following a specialization have no effect and |
| 7962 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 7963 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7964 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 7965 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7966 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7967 | return false; |
| 7968 | } |
Bruno Ricci | d8c1767 | 2018-12-21 20:38:06 +0000 | [diff] [blame] | 7969 | llvm_unreachable("Unexpected TemplateSpecializationKind!"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7970 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7971 | case TSK_ExplicitInstantiationDefinition: |
| 7972 | switch (PrevTSK) { |
| 7973 | case TSK_Undeclared: |
| 7974 | case TSK_ImplicitInstantiation: |
| 7975 | // We're explicitly instantiating something that may have already been |
| 7976 | // implicitly instantiated; that's fine. |
| 7977 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7978 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7979 | case TSK_ExplicitSpecialization: |
| 7980 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 7981 | // For a given set of template parameters, if an explicit |
| 7982 | // instantiation of a template appears after a declaration of |
| 7983 | // an explicit specialization for that template, the explicit |
| 7984 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 7985 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7986 | << PrevDecl; |
| 7987 | Diag(PrevDecl->getLocation(), |
| 7988 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7989 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7990 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7991 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7992 | case TSK_ExplicitInstantiationDeclaration: |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 7993 | // We're explicitly instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7994 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7995 | |
| 7996 | // C++0x [temp.explicit]p4: |
| 7997 | // For a given set of template parameters, if an explicit instantiation |
| 7998 | // of a template appears after a declaration of an explicit |
| 7999 | // specialization for that template, the explicit instantiation has no |
| 8000 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 8001 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 8002 | // Is there any previous explicit specialization declaration? |
| 8003 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 8004 | HasNoEffect = true; |
| 8005 | break; |
| 8006 | } |
| 8007 | } |
| 8008 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8009 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8010 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8011 | case TSK_ExplicitInstantiationDefinition: |
| 8012 | // C++0x [temp.spec]p5: |
| 8013 | // For a given template and a given set of template-arguments, |
| 8014 | // - an explicit instantiation definition shall appear at most once |
| 8015 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 8016 | |
| 8017 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 8018 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 8019 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 8020 | : diag::err_explicit_instantiation_duplicate) |
| 8021 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 8022 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8023 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8024 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8025 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8026 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8027 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8028 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8029 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8030 | } |
| 8031 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8032 | /// Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8033 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8034 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8035 | /// The only possible way to get a dependent function template specialization |
| 8036 | /// is with a friend declaration, like so: |
| 8037 | /// |
| 8038 | /// \code |
| 8039 | /// template \<class T> void foo(T); |
| 8040 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8041 | /// friend void foo<>(T); |
| 8042 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8043 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8044 | /// |
| 8045 | /// There really isn't any useful analysis we can do here, so we |
| 8046 | /// just store the information. |
| 8047 | bool |
| 8048 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 8049 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 8050 | LookupResult &Previous) { |
| 8051 | // Remove anything from Previous that isn't a function template in |
| 8052 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8053 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8054 | LookupResult::Filter F = Previous.makeFilter(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8055 | enum DiscardReason { NotAFunctionTemplate, NotAMemberOfEnclosing }; |
| 8056 | SmallVector<std::pair<DiscardReason, Decl *>, 8> DiscardedCandidates; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8057 | while (F.hasNext()) { |
| 8058 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8059 | if (!isa<FunctionTemplateDecl>(D)) { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8060 | F.erase(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8061 | DiscardedCandidates.push_back(std::make_pair(NotAFunctionTemplate, D)); |
| 8062 | continue; |
| 8063 | } |
| 8064 | |
| 8065 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8066 | D->getDeclContext()->getRedeclContext())) { |
| 8067 | F.erase(); |
| 8068 | DiscardedCandidates.push_back(std::make_pair(NotAMemberOfEnclosing, D)); |
| 8069 | continue; |
| 8070 | } |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8071 | } |
| 8072 | F.done(); |
| 8073 | |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8074 | if (Previous.empty()) { |
| 8075 | Diag(FD->getLocation(), |
| 8076 | diag::err_dependent_function_template_spec_no_match); |
| 8077 | for (auto &P : DiscardedCandidates) |
| 8078 | Diag(P.second->getLocation(), |
| 8079 | diag::note_dependent_function_template_spec_discard_reason) |
| 8080 | << P.first; |
| 8081 | return true; |
| 8082 | } |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8083 | |
| 8084 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 8085 | ExplicitTemplateArgs); |
| 8086 | return false; |
| 8087 | } |
| 8088 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8089 | /// Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8090 | /// specialization. |
| 8091 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8092 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8093 | /// explicit function template specialization. On successful completion, |
| 8094 | /// the function declaration \p FD will become a function template |
| 8095 | /// specialization. |
| 8096 | /// |
| 8097 | /// \param FD the function declaration, which will be updated to become a |
| 8098 | /// function template specialization. |
| 8099 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8100 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 8101 | /// if any. Note that this may be valid info even when 0 arguments are |
| 8102 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 8103 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8104 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8105 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8106 | /// this function specialization. |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8107 | /// |
| 8108 | /// \param QualifiedFriend whether this is a lookup for a qualified friend |
| 8109 | /// declaration with no explicit template argument list that might be |
| 8110 | /// befriending a function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8111 | bool Sema::CheckFunctionTemplateSpecialization( |
| 8112 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8113 | LookupResult &Previous, bool QualifiedFriend) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8114 | // The set of function template specializations that could match this |
| 8115 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8116 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8117 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 8118 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8119 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8120 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 8121 | ConvertedTemplateArgs; |
| 8122 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8123 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8124 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8125 | I != E; ++I) { |
| 8126 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 8127 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8128 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8129 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8130 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8131 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8132 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8133 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8134 | // When matching a constexpr member function template specialization |
| 8135 | // against the primary template, we don't yet know whether the |
| 8136 | // specialization has an implicit 'const' (because we don't know whether |
| 8137 | // it will be a static member function until we know which template it |
| 8138 | // specializes), so adjust it now assuming it specializes this template. |
| 8139 | QualType FT = FD->getType(); |
| 8140 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8141 | CXXMethodDecl *OldMD = |
| 8142 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8143 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8144 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8145 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 8146 | EPI.TypeQuals.addConst(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8147 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8148 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8149 | } |
| 8150 | } |
| 8151 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8152 | TemplateArgumentListInfo Args; |
| 8153 | if (ExplicitTemplateArgs) |
| 8154 | Args = *ExplicitTemplateArgs; |
| 8155 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8156 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8157 | // A trailing template-argument can be left unspecified in the |
| 8158 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8159 | // provided it can be deduced from the function argument type. |
| 8160 | // Perform template argument deduction to determine whether we may be |
| 8161 | // specializing this template. |
| 8162 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8163 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8164 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 8165 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 8166 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8167 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 8168 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8169 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8170 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8171 | FailedCandidates.addCandidate().set( |
| 8172 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8173 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8174 | (void)TDK; |
| 8175 | continue; |
| 8176 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8177 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8178 | // Target attributes are part of the cuda function signature, so |
| 8179 | // the deduced template's cuda target must match that of the |
| 8180 | // specialization. Given that C++ template deduction does not |
| 8181 | // take target attributes into account, we reject candidates |
| 8182 | // here that have a different target. |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8183 | if (LangOpts.CUDA && |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8184 | IdentifyCUDATarget(Specialization, |
| 8185 | /* IgnoreImplicitHDAttributes = */ true) != |
| 8186 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8187 | FailedCandidates.addCandidate().set( |
| 8188 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8189 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8190 | continue; |
| 8191 | } |
| 8192 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8193 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8194 | if (ExplicitTemplateArgs) |
| 8195 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8196 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8197 | } |
| 8198 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8199 | |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8200 | // For a qualified friend declaration (with no explicit marker to indicate |
| 8201 | // that a template specialization was intended), note all (template and |
| 8202 | // non-template) candidates. |
| 8203 | if (QualifiedFriend && Candidates.empty()) { |
| 8204 | Diag(FD->getLocation(), diag::err_qualified_friend_no_match) |
| 8205 | << FD->getDeclName() << FDLookupContext; |
| 8206 | // FIXME: We should form a single candidate list and diagnose all |
| 8207 | // candidates at once, to get proper sorting and limiting. |
| 8208 | for (auto *OldND : Previous) { |
| 8209 | if (auto *OldFD = dyn_cast<FunctionDecl>(OldND->getUnderlyingDecl())) |
| 8210 | NoteOverloadCandidate(OldND, OldFD, FD->getType(), false); |
| 8211 | } |
| 8212 | FailedCandidates.NoteCandidates(*this, FD->getLocation()); |
| 8213 | return true; |
| 8214 | } |
| 8215 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 8216 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8217 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 8ce732b | 2019-01-07 06:00:46 +0000 | [diff] [blame] | 8218 | Candidates.begin(), Candidates.end(), FailedCandidates, FD->getLocation(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8219 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 8220 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8221 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8222 | PDiag(diag::note_function_template_spec_matched)); |
| 8223 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8224 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8225 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8226 | |
| 8227 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 8228 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 8229 | |
| 8230 | FunctionTemplateSpecializationInfo *SpecInfo |
| 8231 | = Specialization->getTemplateSpecializationInfo(); |
| 8232 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8233 | |
| 8234 | // Note: do not overwrite location info if previous template |
| 8235 | // specialization kind was explicit. |
| 8236 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8237 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8238 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8239 | Specialization->setLexicalDeclContext(FD->getLexicalDeclContext()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8240 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 8241 | // function can differ from the template declaration with respect to |
| 8242 | // the constexpr specifier. |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8243 | // FIXME: We need an update record for this AST mutation. |
| 8244 | // FIXME: What if there are multiple such prior declarations (for instance, |
| 8245 | // from different modules)? |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8246 | Specialization->setConstexpr(FD->isConstexpr()); |
| 8247 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8248 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8249 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8250 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8251 | |
| 8252 | // If this is a friend declaration, then we're not really declaring |
| 8253 | // an explicit specialization. |
| 8254 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8255 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8256 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8257 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8258 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8259 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8260 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8261 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8262 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8263 | |
| 8264 | // C++ [temp.expl.spec]p6: |
| 8265 | // 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] | 8266 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8267 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8268 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8269 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8270 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8271 | if (!isFriend && |
| 8272 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8273 | TSK_ExplicitSpecialization, |
| 8274 | Specialization, |
| 8275 | SpecInfo->getTemplateSpecializationKind(), |
| 8276 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8277 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8278 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8279 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8280 | // Mark the prior declaration as an explicit specialization, so that later |
| 8281 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8282 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8283 | // Since explicit specializations do not inherit '=delete' from their |
| 8284 | // primary function template - check if the 'specialization' that was |
| 8285 | // implicitly generated (during template argument deduction for partial |
| 8286 | // ordering) from the most specialized of all the function templates that |
| 8287 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 8288 | // first check that it was implicitly generated during template argument |
| 8289 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 8290 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 8291 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 8292 | !Specialization->getCanonicalDecl()->isReferenced()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8293 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8294 | assert( |
| 8295 | Specialization->getCanonicalDecl() == Specialization && |
| 8296 | "This must be the only existing declaration of this specialization"); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8297 | // FIXME: We need an update record for this AST mutation. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8298 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8299 | } |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8300 | // FIXME: We need an update record for this AST mutation. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8301 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8302 | MarkUnusedFileScopedDecl(Specialization); |
| 8303 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8304 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8305 | // Turn the given function declaration into a function template |
| 8306 | // specialization, with the template arguments from the previous |
| 8307 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8308 | // Take copies of (semantic and syntactic) template argument lists. |
| 8309 | const TemplateArgumentList* TemplArgs = new (Context) |
| 8310 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8311 | FD->setFunctionTemplateSpecialization( |
| 8312 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 8313 | SpecInfo->getTemplateSpecializationKind(), |
| 8314 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8315 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8316 | // A function template specialization inherits the target attributes |
| 8317 | // of its template. (We require the attributes explicitly in the |
| 8318 | // code to match, but a template may have implicit attributes by |
| 8319 | // virtue e.g. of being constexpr, and it passes these implicit |
| 8320 | // attributes on to its specializations.) |
| 8321 | if (LangOpts.CUDA) |
| 8322 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 8323 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8324 | // The "previous declaration" for this function template specialization is |
| 8325 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8326 | Previous.clear(); |
| 8327 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8328 | return false; |
| 8329 | } |
| 8330 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8331 | /// Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8332 | /// specialization. |
| 8333 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8334 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8335 | /// explicit member function specialization. On successful completion, |
| 8336 | /// the function declaration \p FD will become a member function |
| 8337 | /// specialization. |
| 8338 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8339 | /// \param Member the member declaration, which will be updated to become a |
| 8340 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8341 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8342 | /// \param Previous the set of declarations, one of which may be specialized |
| 8343 | /// by this function specialization; the set will be modified to contain the |
| 8344 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8345 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8346 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8347 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8348 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8349 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8350 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8351 | NamedDecl *Instantiation = nullptr; |
| 8352 | NamedDecl *InstantiatedFrom = nullptr; |
| 8353 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8354 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8355 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8356 | // Nowhere to look anyway. |
| 8357 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8358 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8359 | I != E; ++I) { |
| 8360 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 8361 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 8362 | QualType Adjusted = Function->getType(); |
| 8363 | if (!hasExplicitCallingConv(Adjusted)) |
| 8364 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
Richard Smith | 4576a77 | 2018-09-10 06:35:32 +0000 | [diff] [blame] | 8365 | // This doesn't handle deduced return types, but both function |
| 8366 | // declarations should be undeduced at this point. |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 8367 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8368 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8369 | Instantiation = Method; |
| 8370 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8371 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8372 | break; |
| 8373 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8374 | } |
| 8375 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8376 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8377 | VarDecl *PrevVar; |
| 8378 | if (Previous.isSingleResult() && |
| 8379 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8380 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8381 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8382 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8383 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8384 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8385 | } |
| 8386 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8387 | CXXRecordDecl *PrevRecord; |
| 8388 | if (Previous.isSingleResult() && |
| 8389 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8390 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8391 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8392 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8393 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8394 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8395 | } else if (isa<EnumDecl>(Member)) { |
| 8396 | EnumDecl *PrevEnum; |
| 8397 | if (Previous.isSingleResult() && |
| 8398 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8399 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8400 | Instantiation = PrevEnum; |
| 8401 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 8402 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 8403 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8404 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8405 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8406 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8407 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8408 | // specializations are always out-of-line, the caller will complain about |
| 8409 | // this mismatch later. |
| 8410 | return false; |
| 8411 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8412 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8413 | // A member specialization in a friend declaration isn't really declaring |
| 8414 | // an explicit specialization, just identifying a specific (possibly implicit) |
| 8415 | // specialization. Don't change the template specialization kind. |
| 8416 | // |
| 8417 | // FIXME: Is this really valid? Other compilers reject. |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8418 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 8419 | // Preserve instantiation information. |
| 8420 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 8421 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 8422 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 8423 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8424 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 8425 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 8426 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 8427 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8428 | } |
| 8429 | |
| 8430 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8431 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8432 | return false; |
| 8433 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8434 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8435 | // Make sure that this is a specialization of a member. |
| 8436 | if (!InstantiatedFrom) { |
| 8437 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 8438 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8439 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 8440 | return true; |
| 8441 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8442 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8443 | // C++ [temp.expl.spec]p6: |
| 8444 | // 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] | 8445 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8446 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8447 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8448 | // use occurs; no diagnostic is required. |
| 8449 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8450 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8451 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8452 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 8453 | TSK_ExplicitSpecialization, |
| 8454 | Instantiation, |
| 8455 | MSInfo->getTemplateSpecializationKind(), |
| 8456 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8457 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8458 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8459 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8460 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8461 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8462 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8463 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8464 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8465 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 8466 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8467 | // Note that this member specialization is an "instantiation of" the |
| 8468 | // corresponding member of the original template. |
| 8469 | if (auto *MemberFunction = dyn_cast<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8470 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 8471 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 8472 | TSK_ImplicitInstantiation) { |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8473 | // Explicit specializations of member functions of class templates do not |
| 8474 | // inherit '=delete' from the member function they are specializing. |
| 8475 | if (InstantiationFunction->isDeleted()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8476 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8477 | assert(InstantiationFunction->getCanonicalDecl() == |
| 8478 | InstantiationFunction); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8479 | // FIXME: We need an update record for this AST mutation. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 8480 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8481 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8482 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8483 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8484 | MemberFunction->setInstantiationOfMemberFunction( |
| 8485 | cast<CXXMethodDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8486 | } else if (auto *MemberVar = dyn_cast<VarDecl>(Member)) { |
| 8487 | MemberVar->setInstantiationOfStaticDataMember( |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8488 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8489 | } else if (auto *MemberClass = dyn_cast<CXXRecordDecl>(Member)) { |
| 8490 | MemberClass->setInstantiationOfMemberClass( |
| 8491 | cast<CXXRecordDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8492 | } else if (auto *MemberEnum = dyn_cast<EnumDecl>(Member)) { |
| 8493 | MemberEnum->setInstantiationOfMemberEnum( |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8494 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8495 | } else { |
| 8496 | llvm_unreachable("unknown member specialization kind"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8497 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8498 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8499 | // Save the caller the trouble of having to figure out which declaration |
| 8500 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8501 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8502 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8503 | return false; |
| 8504 | } |
| 8505 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8506 | /// Complete the explicit specialization of a member of a class template by |
| 8507 | /// updating the instantiated member to be marked as an explicit specialization. |
| 8508 | /// |
| 8509 | /// \param OrigD The member declaration instantiated from the template. |
| 8510 | /// \param Loc The location of the explicit specialization of the member. |
| 8511 | template<typename DeclT> |
| 8512 | static void completeMemberSpecializationImpl(Sema &S, DeclT *OrigD, |
| 8513 | SourceLocation Loc) { |
| 8514 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 8515 | return; |
| 8516 | |
| 8517 | // FIXME: Inform AST mutation listeners of this AST mutation. |
| 8518 | // FIXME: If there are multiple in-class declarations of the member (from |
| 8519 | // multiple modules, or a declaration and later definition of a member type), |
| 8520 | // should we update all of them? |
| 8521 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
| 8522 | OrigD->setLocation(Loc); |
| 8523 | } |
| 8524 | |
| 8525 | void Sema::CompleteMemberSpecialization(NamedDecl *Member, |
| 8526 | LookupResult &Previous) { |
| 8527 | NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl()); |
| 8528 | if (Instantiation == Member) |
| 8529 | return; |
| 8530 | |
| 8531 | if (auto *Function = dyn_cast<CXXMethodDecl>(Instantiation)) |
| 8532 | completeMemberSpecializationImpl(*this, Function, Member->getLocation()); |
| 8533 | else if (auto *Var = dyn_cast<VarDecl>(Instantiation)) |
| 8534 | completeMemberSpecializationImpl(*this, Var, Member->getLocation()); |
| 8535 | else if (auto *Record = dyn_cast<CXXRecordDecl>(Instantiation)) |
| 8536 | completeMemberSpecializationImpl(*this, Record, Member->getLocation()); |
| 8537 | else if (auto *Enum = dyn_cast<EnumDecl>(Instantiation)) |
| 8538 | completeMemberSpecializationImpl(*this, Enum, Member->getLocation()); |
| 8539 | else |
| 8540 | llvm_unreachable("unknown member specialization kind"); |
| 8541 | } |
| 8542 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8543 | /// Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8544 | /// |
| 8545 | /// \returns true if a serious error occurs, false otherwise. |
| 8546 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8547 | SourceLocation InstLoc, |
| 8548 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8549 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 8550 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8551 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8552 | if (CurContext->isRecord()) { |
| 8553 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 8554 | << D; |
| 8555 | return true; |
| 8556 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8557 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8558 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8559 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8560 | // template. If the name declared in the explicit instantiation is an |
| 8561 | // unqualified name, the explicit instantiation shall appear in the |
| 8562 | // namespace where its template is declared or, if that namespace is inline |
| 8563 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8564 | // |
| 8565 | // 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] | 8566 | if (WasQualifiedName) { |
| 8567 | if (CurContext->Encloses(OrigContext)) |
| 8568 | return false; |
| 8569 | } else { |
| 8570 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 8571 | return false; |
| 8572 | } |
| 8573 | |
| 8574 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 8575 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8576 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8577 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8578 | diag::err_explicit_instantiation_out_of_scope : |
| 8579 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8580 | << D << NS; |
| 8581 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8582 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8583 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8584 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 8585 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 8586 | << D << NS; |
| 8587 | } else |
| 8588 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8589 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8590 | diag::err_explicit_instantiation_must_be_global : |
| 8591 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 8592 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8593 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8594 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8595 | } |
| 8596 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8597 | /// Determine whether the given scope specifier has a template-id in it. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8598 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 8599 | if (!SS.isSet()) |
| 8600 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8601 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8602 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8603 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8604 | // or a static data member of a class template specialization, the name of |
| 8605 | // the class template specialization in the qualified-id for the member |
| 8606 | // name shall be a simple-template-id. |
| 8607 | // |
| 8608 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8609 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 8610 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8611 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8612 | if (isa<TemplateSpecializationType>(T)) |
| 8613 | return true; |
| 8614 | |
| 8615 | return false; |
| 8616 | } |
| 8617 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8618 | /// Make a dllexport or dllimport attr on a class template specialization take |
| 8619 | /// effect. |
| 8620 | static void dllExportImportClassTemplateSpecialization( |
| 8621 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 8622 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 8623 | assert(A && "dllExportImportClassTemplateSpecialization called " |
| 8624 | "on Def without dllexport or dllimport"); |
| 8625 | |
| 8626 | // We reject explicit instantiations in class scope, so there should |
| 8627 | // never be any delayed exported classes to worry about. |
| 8628 | assert(S.DelayedDllExportClasses.empty() && |
| 8629 | "delayed exports present at explicit instantiation"); |
| 8630 | S.checkClassLevelDLLAttribute(Def); |
| 8631 | |
| 8632 | // Propagate attribute to base class templates. |
| 8633 | for (auto &B : Def->bases()) { |
| 8634 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 8635 | B.getType()->getAsCXXRecordDecl())) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8636 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getBeginLoc()); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8637 | } |
| 8638 | |
| 8639 | S.referenceDLLExportedClassMethods(); |
| 8640 | } |
| 8641 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8642 | // Explicit instantiation of a class template specialization |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8643 | DeclResult Sema::ActOnExplicitInstantiation( |
| 8644 | Scope *S, SourceLocation ExternLoc, SourceLocation TemplateLoc, |
| 8645 | unsigned TagSpec, SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 8646 | TemplateTy TemplateD, SourceLocation TemplateNameLoc, |
| 8647 | SourceLocation LAngleLoc, ASTTemplateArgsPtr TemplateArgsIn, |
| 8648 | SourceLocation RAngleLoc, const ParsedAttributesView &Attr) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8649 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 8650 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8651 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8652 | // Check that the specialization uses the same tag kind as the |
| 8653 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8654 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 8655 | assert(Kind != TTK_Enum && |
| 8656 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8657 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8658 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 8659 | |
| 8660 | if (!ClassTemplate) { |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 8661 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 8662 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8663 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8664 | return true; |
| 8665 | } |
| 8666 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 8667 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 8668 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 8669 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8670 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8671 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8672 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8673 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8674 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8675 | diag::note_previous_use); |
| 8676 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 8677 | } |
| 8678 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8679 | // C++0x [temp.explicit]p2: |
| 8680 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8681 | // definition and an explicit instantiation declaration. An explicit |
| 8682 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8683 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 8684 | ? TSK_ExplicitInstantiationDefinition |
| 8685 | : TSK_ExplicitInstantiationDeclaration; |
| 8686 | |
| 8687 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 8688 | // Check for dllexport class template instantiation declarations. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8689 | for (const ParsedAttr &AL : Attr) { |
| 8690 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8691 | Diag(ExternLoc, |
| 8692 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8693 | Diag(AL.getLoc(), diag::note_attribute); |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8694 | break; |
| 8695 | } |
| 8696 | } |
| 8697 | |
| 8698 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 8699 | Diag(ExternLoc, |
| 8700 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8701 | Diag(A->getLocation(), diag::note_attribute); |
| 8702 | } |
| 8703 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8704 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8705 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 8706 | // instantiation declarations for most purposes. |
| 8707 | bool DLLImportExplicitInstantiationDef = false; |
| 8708 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 8709 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 8710 | // Check for dllimport class template instantiation definitions. |
| 8711 | bool DLLImport = |
| 8712 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8713 | for (const ParsedAttr &AL : Attr) { |
| 8714 | if (AL.getKind() == ParsedAttr::AT_DLLImport) |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8715 | DLLImport = true; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8716 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8717 | // dllexport trumps dllimport here. |
| 8718 | DLLImport = false; |
| 8719 | break; |
| 8720 | } |
| 8721 | } |
| 8722 | if (DLLImport) { |
| 8723 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 8724 | DLLImportExplicitInstantiationDef = true; |
| 8725 | } |
| 8726 | } |
| 8727 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8728 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8729 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 8730 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8731 | |
| 8732 | // Check that the template argument list is well-formed for this |
| 8733 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8734 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8735 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 8736 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8737 | return true; |
| 8738 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8739 | // Find the class template specialization declaration that |
| 8740 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8741 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8742 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 8743 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8744 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8745 | TemplateSpecializationKind PrevDecl_TSK |
| 8746 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 8747 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8748 | // C++0x [temp.explicit]p2: |
| 8749 | // [...] An explicit instantiation shall appear in an enclosing |
| 8750 | // namespace of its template. [...] |
| 8751 | // |
| 8752 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8753 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 8754 | SS.isSet())) |
| 8755 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8756 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8757 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8758 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8759 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8760 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8761 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8762 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8763 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8764 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8765 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8766 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8767 | // Even though HasNoEffect == true means that this explicit instantiation |
| 8768 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 8769 | |
| 8770 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 8771 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8772 | // Since the only prior class template specialization with these |
| 8773 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8774 | // declaration node as our own, updating the source location |
| 8775 | // for the template name to reflect our new declaration. |
| 8776 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8777 | Specialization = PrevDecl; |
| 8778 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8779 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8780 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8781 | |
| 8782 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 8783 | DLLImportExplicitInstantiationDef) { |
| 8784 | // The new specialization might add a dllimport attribute. |
| 8785 | HasNoEffect = false; |
| 8786 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8787 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8788 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8789 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8790 | // Create a new class template specialization declaration node for |
| 8791 | // this explicit specialization. |
| 8792 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 8793 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8794 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 8795 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8796 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 8797 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8798 | PrevDecl); |
Bruno Ricci | 4224c87 | 2018-12-21 14:35:24 +0000 | [diff] [blame] | 8799 | SetNestedNameSpecifier(*this, Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8800 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8801 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8802 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8803 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8804 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8805 | } |
| 8806 | |
| 8807 | // Build the fully-sugared type for this explicit instantiation as |
| 8808 | // the user wrote in the explicit instantiation itself. This means |
| 8809 | // that we'll pretty-print the type retrieved from the |
| 8810 | // specialization's declaration the way that the user actually wrote |
| 8811 | // the explicit instantiation, rather than formatting the name based |
| 8812 | // on the "canonical" representation used to store the template |
| 8813 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 8814 | TypeSourceInfo *WrittenTy |
| 8815 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 8816 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8817 | Context.getTypeDeclType(Specialization)); |
| 8818 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8819 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8820 | // Set source locations for keywords. |
| 8821 | Specialization->setExternLoc(ExternLoc); |
| 8822 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 8823 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8824 | |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8825 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8826 | ProcessDeclAttributeList(S, Specialization, Attr); |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 8827 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8828 | // Add the explicit instantiation into its lexical context. However, |
| 8829 | // since explicit instantiations are never found by name lookup, we |
| 8830 | // just put it into the declaration context directly. |
| 8831 | Specialization->setLexicalDeclContext(CurContext); |
| 8832 | CurContext->addDecl(Specialization); |
| 8833 | |
| 8834 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 8835 | if (HasNoEffect) { |
| 8836 | // Set the template specialization kind. |
| 8837 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8838 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 8839 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8840 | |
| 8841 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8842 | // A definition of a class template or class member template |
| 8843 | // shall be in scope at the point of the explicit instantiation of |
| 8844 | // the class template or class member template. |
| 8845 | // |
| 8846 | // This check comes when we actually try to perform the |
| 8847 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8848 | ClassTemplateSpecializationDecl *Def |
| 8849 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8850 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8851 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8852 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8853 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8854 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8855 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 8856 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8857 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8858 | // Instantiate the members of this class template specialization. |
| 8859 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8860 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8861 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8862 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8863 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 8864 | // TSK_ExplicitInstantiationDefinition |
| 8865 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8866 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 8867 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8868 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8869 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8870 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 8871 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
Shoaib Meenai | ab3f96c | 2016-11-09 23:52:20 +0000 | [diff] [blame] | 8872 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8873 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 8874 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 8875 | // attribute to a template with a previous instantiation declaration. |
| 8876 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8877 | auto *A = cast<InheritableAttr>( |
| 8878 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 8879 | A->setInherited(true); |
| 8880 | Def->addAttr(A); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8881 | dllExportImportClassTemplateSpecialization(*this, Def); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8882 | } |
| 8883 | } |
| 8884 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8885 | // Fix a TSK_ImplicitInstantiation followed by a |
| 8886 | // TSK_ExplicitInstantiationDefinition |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8887 | bool NewlyDLLExported = |
| 8888 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
| 8889 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8890 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8891 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 8892 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 8893 | // attribute to a template with a previous implicit instantiation. |
| 8894 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to |
| 8895 | // avoid potentially strange codegen behavior. For example, if we extend |
| 8896 | // this conditional to dllimport, and we have a source file calling a |
| 8897 | // method on an implicitly instantiated template class instance and then |
| 8898 | // declaring a dllimport explicit instantiation definition for the same |
| 8899 | // template class, the codegen for the method call will not respect the |
| 8900 | // dllimport, while it will with cl. The Def will already have the DLL |
| 8901 | // attribute, since the Def and Specialization will be the same in the |
| 8902 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the |
| 8903 | // attribute to the Specialization; we just need to make it take effect. |
| 8904 | assert(Def == Specialization && |
| 8905 | "Def and Specialization should match for implicit instantiation"); |
| 8906 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 8907 | } |
| 8908 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8909 | // Set the template specialization kind. Make sure it is set before |
| 8910 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 8911 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8912 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8913 | } else { |
| 8914 | |
| 8915 | // Set the template specialization kind. |
| 8916 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8917 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8918 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8919 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8920 | } |
| 8921 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8922 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8923 | DeclResult |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8924 | Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation ExternLoc, |
| 8925 | SourceLocation TemplateLoc, unsigned TagSpec, |
| 8926 | SourceLocation KWLoc, CXXScopeSpec &SS, |
| 8927 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 8928 | const ParsedAttributesView &Attr) { |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8929 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 8930 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8931 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8932 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8933 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 8934 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8935 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 8936 | SourceLocation(), false, TypeResult(), |
Akira Hatanaka | 12ddcee | 2017-06-26 18:46:12 +0000 | [diff] [blame] | 8937 | /*IsTypeSpecifier*/false, |
| 8938 | /*IsTemplateParamOrArg*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8939 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 8940 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8941 | if (!TagD) |
| 8942 | return true; |
| 8943 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8944 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8945 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8946 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 8947 | if (Tag->isInvalidDecl()) |
| 8948 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8949 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8950 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 8951 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 8952 | if (!Pattern) { |
| 8953 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 8954 | << Context.getTypeDeclType(Record); |
| 8955 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 8956 | return true; |
| 8957 | } |
| 8958 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8959 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8960 | // If the explicit instantiation is for a class or member class, the |
| 8961 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8962 | // simple-template-id. |
| 8963 | // |
| 8964 | // C++98 has the same restriction, just worded differently. |
| 8965 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8966 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8967 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8968 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8969 | // C++0x [temp.explicit]p2: |
| 8970 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8971 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8972 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 8973 | TemplateSpecializationKind TSK |
| 8974 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8975 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8976 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8977 | // C++0x [temp.explicit]p2: |
| 8978 | // [...] An explicit instantiation shall appear in an enclosing |
| 8979 | // namespace of its template. [...] |
| 8980 | // |
| 8981 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8982 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8983 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8984 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8985 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8986 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8987 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8988 | PrevDecl = Record; |
| 8989 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8990 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8991 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8992 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8993 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8994 | PrevDecl, |
| 8995 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8996 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8997 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8998 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8999 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9000 | return TagD; |
| 9001 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9002 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 9003 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9004 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 9005 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 9006 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9007 | // 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] | 9008 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9009 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9010 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 9011 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 9012 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 9013 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 9014 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 9015 | << Pattern; |
| 9016 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9017 | } else { |
| 9018 | if (InstantiateClass(NameLoc, Record, Def, |
| 9019 | getTemplateInstantiationArgs(Record), |
| 9020 | TSK)) |
| 9021 | return true; |
| 9022 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 9023 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9024 | if (!RecordDef) |
| 9025 | return true; |
| 9026 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9027 | } |
| 9028 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9029 | // Instantiate all of the members of the class. |
| 9030 | InstantiateClassMembers(NameLoc, RecordDef, |
| 9031 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9032 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9033 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9034 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 9035 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 9036 | // FIXME: We don't have any representation for explicit instantiations of |
| 9037 | // member classes. Such a representation is not needed for compilation, but it |
| 9038 | // should be available for clients that want to see all of the declarations in |
| 9039 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 9040 | return TagD; |
| 9041 | } |
| 9042 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9043 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 9044 | SourceLocation ExternLoc, |
| 9045 | SourceLocation TemplateLoc, |
| 9046 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9047 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9048 | // TODO: check if/when DNInfo should replace Name. |
| 9049 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 9050 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9051 | if (!Name) { |
| 9052 | if (!D.isInvalidType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9053 | Diag(D.getDeclSpec().getBeginLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9054 | diag::err_explicit_instantiation_requires_name) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9055 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9056 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9057 | return true; |
| 9058 | } |
| 9059 | |
| 9060 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 9061 | // we find one that is. |
| 9062 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 9063 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 9064 | S = S->getParent(); |
| 9065 | |
| 9066 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9067 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 9068 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9069 | if (R.isNull()) |
| 9070 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9071 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9072 | // C++ [dcl.stc]p1: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9073 | // A storage-class-specifier shall not be specified in [...] an explicit |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9074 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9075 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9076 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 9077 | << Name; |
| 9078 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9079 | } else if (D.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9080 | != DeclSpec::SCS_unspecified) { |
| 9081 | // Complain about then remove the storage class specifier. |
| 9082 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 9083 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9084 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9085 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9086 | } |
| 9087 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 9088 | // C++0x [temp.explicit]p1: |
| 9089 | // [...] An explicit instantiation of a function template shall not use the |
| 9090 | // inline or constexpr specifiers. |
| 9091 | // Presumably, this also applies to member functions of class templates as |
| 9092 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9093 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9094 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9095 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9096 | diag::err_explicit_instantiation_inline : |
| 9097 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9098 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9099 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9100 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 9101 | // not already specified. |
| 9102 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 9103 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9104 | |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 9105 | // A deduction guide is not on the list of entities that can be explicitly |
| 9106 | // instantiated. |
| 9107 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9108 | Diag(D.getDeclSpec().getBeginLoc(), diag::err_deduction_guide_specialized) |
| 9109 | << /*explicit instantiation*/ 0; |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 9110 | return true; |
| 9111 | } |
| 9112 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9113 | // C++0x [temp.explicit]p2: |
| 9114 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9115 | // definition and an explicit instantiation declaration. An explicit |
| 9116 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9117 | TemplateSpecializationKind TSK |
| 9118 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 9119 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9120 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9121 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9122 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9123 | |
| 9124 | if (!R->isFunctionType()) { |
| 9125 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9126 | // A [...] static data member of a class template can be explicitly |
| 9127 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9128 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9129 | // C++1y [temp.explicit]p1: |
| 9130 | // A [...] variable [...] template specialization can be explicitly |
| 9131 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9132 | if (Previous.isAmbiguous()) |
| 9133 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9134 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 9135 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9136 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9137 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9138 | if (!PrevTemplate) { |
| 9139 | if (!Prev || !Prev->isStaticDataMember()) { |
| 9140 | // We expect to see a data data member here. |
| 9141 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 9142 | << Name; |
| 9143 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9144 | P != PEnd; ++P) |
| 9145 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 9146 | return true; |
| 9147 | } |
| 9148 | |
| 9149 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 9150 | // FIXME: Check for explicit specialization? |
| 9151 | Diag(D.getIdentifierLoc(), |
| 9152 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 9153 | << Prev; |
| 9154 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 9155 | // FIXME: Can we provide a note showing where this was declared? |
| 9156 | return true; |
| 9157 | } |
| 9158 | } else { |
| 9159 | // Explicitly instantiate a variable template. |
| 9160 | |
| 9161 | // C++1y [dcl.spec.auto]p6: |
| 9162 | // ... A program that uses auto or decltype(auto) in a context not |
| 9163 | // explicitly allowed in this section is ill-formed. |
| 9164 | // |
| 9165 | // This includes auto-typed variable template instantiations. |
| 9166 | if (R->isUndeducedType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9167 | Diag(T->getTypeLoc().getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9168 | diag::err_auto_not_allowed_var_inst); |
| 9169 | return true; |
| 9170 | } |
| 9171 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9172 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9173 | // C++1y [temp.explicit]p3: |
| 9174 | // If the explicit instantiation is for a variable, the unqualified-id |
| 9175 | // in the declaration shall be a template-id. |
| 9176 | Diag(D.getIdentifierLoc(), |
| 9177 | diag::err_explicit_instantiation_without_template_id) |
| 9178 | << PrevTemplate; |
| 9179 | Diag(PrevTemplate->getLocation(), |
| 9180 | diag::note_explicit_instantiation_here); |
| 9181 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9182 | } |
| 9183 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9184 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9185 | TemplateArgumentListInfo TemplateArgs = |
| 9186 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9187 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9188 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 9189 | D.getIdentifierLoc(), TemplateArgs); |
| 9190 | if (Res.isInvalid()) |
| 9191 | return true; |
| 9192 | |
| 9193 | // Ignore access control bits, we don't need them for redeclaration |
| 9194 | // checking. |
| 9195 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9196 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9197 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9198 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9199 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9200 | // or a static data member of a class template specialization, the name of |
| 9201 | // the class template specialization in the qualified-id for the member |
| 9202 | // name shall be a simple-template-id. |
| 9203 | // |
| 9204 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9205 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 9206 | // This does not apply to variable template specializations, where the |
| 9207 | // template-id is in the unqualified-id instead. |
| 9208 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9209 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9210 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9211 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9212 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9213 | // Check the scope of this explicit instantiation. |
| 9214 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9215 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9216 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 9217 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 9218 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9219 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9220 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9221 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9222 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9223 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9224 | if (!HasNoEffect) { |
| 9225 | // Instantiate static data member or variable template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9226 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Louis Dionne | e6e8175 | 2018-10-10 15:32:29 +0000 | [diff] [blame] | 9227 | // Merge attributes. |
| 9228 | ProcessDeclAttributeList(S, Prev, D.getDeclSpec().getAttributes()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9229 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9230 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 9231 | } |
| 9232 | |
| 9233 | // Check the new variable specialization against the parsed input. |
| 9234 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9235 | Diag(T->getTypeLoc().getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9236 | diag::err_invalid_var_template_spec_type) |
| 9237 | << 0 << PrevTemplate << R << Prev->getType(); |
| 9238 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 9239 | << 2 << PrevTemplate->getDeclName(); |
| 9240 | return true; |
| 9241 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9242 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9243 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9244 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9245 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9246 | |
| 9247 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 9248 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9249 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9250 | TemplateArgumentListInfo TemplateArgs; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9251 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9252 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9253 | HasExplicitTemplateArgs = true; |
| 9254 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9255 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9256 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9257 | // A [...] function [...] can be explicitly instantiated from its template. |
| 9258 | // A member function [...] of a class template can be explicitly |
| 9259 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9260 | // template. |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9261 | UnresolvedSet<8> TemplateMatches; |
| 9262 | FunctionDecl *NonTemplateMatch = nullptr; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9263 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9264 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9265 | P != PEnd; ++P) { |
| 9266 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9267 | if (!HasExplicitTemplateArgs) { |
| 9268 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 9269 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 9270 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 9271 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9272 | if (Method->getPrimaryTemplate()) { |
| 9273 | TemplateMatches.addDecl(Method, P.getAccess()); |
| 9274 | } else { |
| 9275 | // FIXME: Can this assert ever happen? Needs a test. |
| 9276 | assert(!NonTemplateMatch && "Multiple NonTemplateMatches"); |
| 9277 | NonTemplateMatch = Method; |
| 9278 | } |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9279 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9280 | } |
| 9281 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9282 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9283 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 9284 | if (!FunTmpl) |
| 9285 | continue; |
| 9286 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9287 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9288 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9289 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9290 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9291 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 9292 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9293 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9294 | // Keep track of almost-matches. |
| 9295 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9296 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9297 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9298 | (void)TDK; |
| 9299 | continue; |
| 9300 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9301 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 9302 | // Target attributes are part of the cuda function signature, so |
| 9303 | // the cuda target of the instantiated function must match that of its |
| 9304 | // template. Given that C++ template deduction does not take |
| 9305 | // target attributes into account, we reject candidates here that |
| 9306 | // have a different target. |
| 9307 | if (LangOpts.CUDA && |
| 9308 | IdentifyCUDATarget(Specialization, |
| 9309 | /* IgnoreImplicitHDAttributes = */ true) != |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9310 | IdentifyCUDATarget(D.getDeclSpec().getAttributes())) { |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 9311 | FailedCandidates.addCandidate().set( |
| 9312 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 9313 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 9314 | continue; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 9315 | } |
| 9316 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9317 | TemplateMatches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9318 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9319 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9320 | FunctionDecl *Specialization = NonTemplateMatch; |
| 9321 | if (!Specialization) { |
| 9322 | // Find the most specialized function template specialization. |
| 9323 | UnresolvedSetIterator Result = getMostSpecialized( |
| 9324 | TemplateMatches.begin(), TemplateMatches.end(), FailedCandidates, |
| 9325 | D.getIdentifierLoc(), |
| 9326 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 9327 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 9328 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9329 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9330 | if (Result == TemplateMatches.end()) |
| 9331 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9332 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9333 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 9334 | Specialization = cast<FunctionDecl>(*Result); |
| 9335 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9336 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 9337 | // C++11 [except.spec]p4 |
| 9338 | // In an explicit instantiation an exception-specification may be specified, |
| 9339 | // but is not required. |
| 9340 | // If an exception-specification is specified in an explicit instantiation |
| 9341 | // directive, it shall be compatible with the exception-specifications of |
| 9342 | // other declarations of that function. |
| 9343 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 9344 | if (FPT->hasExceptionSpec()) { |
| 9345 | unsigned DiagID = |
| 9346 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 9347 | if (getLangOpts().MicrosoftExt) |
| 9348 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 9349 | bool Result = CheckEquivalentExceptionSpec( |
| 9350 | PDiag(DiagID) << Specialization->getType(), |
| 9351 | PDiag(diag::note_explicit_instantiation_here), |
| 9352 | Specialization->getType()->getAs<FunctionProtoType>(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9353 | Specialization->getLocation(), FPT, D.getBeginLoc()); |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 9354 | // In Microsoft mode, mismatching exception specifications just cause a |
| 9355 | // warning. |
| 9356 | if (!getLangOpts().MicrosoftExt && Result) |
| 9357 | return true; |
| 9358 | } |
| 9359 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9360 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9361 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9362 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 9363 | << Specialization |
| 9364 | << (Specialization->getTemplateSpecializationKind() == |
| 9365 | TSK_ExplicitSpecialization); |
| 9366 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 9367 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9368 | } |
| 9369 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9370 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 9371 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 9372 | PrevDecl = Specialization; |
| 9373 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9374 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9375 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9376 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9377 | PrevDecl, |
| 9378 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9379 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9380 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9381 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9382 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9383 | // FIXME: We may still want to build some representation of this |
| 9384 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9385 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9386 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9387 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 9388 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9389 | ProcessDeclAttributeList(S, Specialization, D.getDeclSpec().getAttributes()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9390 | |
Hans Wennborg | b8304a6 | 2017-11-29 23:44:11 +0000 | [diff] [blame] | 9391 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 9392 | // instantiation declarations. |
| 9393 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 9394 | Specialization->hasAttr<DLLImportAttr>() && |
| 9395 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 9396 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 9397 | |
| 9398 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 9399 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 9400 | if (Specialization->isDefined()) { |
| 9401 | // Let the ASTConsumer know that this function has been explicitly |
| 9402 | // instantiated now, and its linkage might have changed. |
| 9403 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 9404 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 9405 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9406 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9407 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9408 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9409 | // or a static data member of a class template specialization, the name of |
| 9410 | // the class template specialization in the qualified-id for the member |
| 9411 | // name shall be a simple-template-id. |
| 9412 | // |
| 9413 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9414 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9415 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9416 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9417 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9418 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9419 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9420 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9421 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9422 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9423 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9424 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9425 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9426 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9427 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9428 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9429 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9430 | } |
| 9431 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9432 | TypeResult |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 9433 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9434 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 9435 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 9436 | // This has to hold, because SS is expected to be defined. |
| 9437 | assert(Name && "Expected a name in a dependent tag"); |
| 9438 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9439 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9440 | if (!NNS) |
| 9441 | return true; |
| 9442 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9443 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 9444 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9445 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 9446 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9447 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9448 | return true; |
| 9449 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9450 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9451 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9452 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9453 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9454 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9455 | // Create type-source location information for this type. |
| 9456 | TypeLocBuilder TLB; |
| 9457 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9458 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9459 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 9460 | TL.setNameLoc(NameLoc); |
| 9461 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9462 | } |
| 9463 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9464 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9465 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 9466 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 9467 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9468 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9469 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9470 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9471 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9472 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9473 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9474 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9475 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9476 | << FixItHint::CreateRemoval(TypenameLoc); |
| 9477 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9478 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9479 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 9480 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 9481 | if (T.isNull()) |
| 9482 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9483 | |
| 9484 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9485 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9486 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9487 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9488 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9489 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9490 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9491 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9492 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9493 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9494 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9495 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9496 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9497 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9498 | } |
| 9499 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9500 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9501 | Sema::ActOnTypenameType(Scope *S, |
| 9502 | SourceLocation TypenameLoc, |
| 9503 | const CXXScopeSpec &SS, |
| 9504 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9505 | TemplateTy TemplateIn, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9506 | IdentifierInfo *TemplateII, |
| 9507 | SourceLocation TemplateIILoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9508 | SourceLocation LAngleLoc, |
| 9509 | ASTTemplateArgsPtr TemplateArgsIn, |
| 9510 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9511 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9512 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9513 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9514 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9515 | diag::ext_typename_outside_of_template) |
| 9516 | << FixItHint::CreateRemoval(TypenameLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9517 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9518 | // Strangely, non-type results are not ignored by this lookup, so the |
| 9519 | // program is ill-formed if it finds an injected-class-name. |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 9520 | if (TypenameLoc.isValid()) { |
| 9521 | auto *LookupRD = |
| 9522 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
| 9523 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 9524 | Diag(TemplateIILoc, |
| 9525 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9526 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 9527 | << (TemplateKWLoc.isValid() ? 1 : 0 /*'template'/'typename' keyword*/); |
| 9528 | } |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9529 | } |
| 9530 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9531 | // Translate the parser's template argument list in our AST format. |
| 9532 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 9533 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9534 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9535 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9536 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 9537 | // Construct a dependent template specialization type. |
| 9538 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9539 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9540 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 9541 | DTN->getQualifier(), |
| 9542 | DTN->getIdentifier(), |
| 9543 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9544 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9545 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9546 | TypeLocBuilder Builder; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9547 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9548 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9549 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 9550 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 9551 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9552 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9553 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9554 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9555 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9556 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9557 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 9558 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9559 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9560 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9561 | if (T.isNull()) |
| 9562 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9563 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9564 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9565 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9566 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9567 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9568 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9569 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9570 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9571 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9572 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9573 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9574 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9575 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 9576 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9577 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9578 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9579 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9580 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 9581 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 9582 | } |
| 9583 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9584 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9585 | /// Determine whether this failed name lookup should be treated as being |
| 9586 | /// disabled by a usage of std::enable_if. |
| 9587 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9588 | SourceRange &CondRange, Expr *&Cond) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9589 | // We must be looking for a ::type... |
| 9590 | if (!II.isStr("type")) |
| 9591 | return false; |
| 9592 | |
| 9593 | // ... within an explicitly-written template specialization... |
| 9594 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 9595 | return false; |
| 9596 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9597 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 9598 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 9599 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9600 | return false; |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 9601 | const TemplateSpecializationType *EnableIfTST = EnableIfTSTLoc.getTypePtr(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9602 | |
| 9603 | // ... which names a complete class template declaration... |
| 9604 | const TemplateDecl *EnableIfDecl = |
| 9605 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 9606 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 9607 | return false; |
| 9608 | |
| 9609 | // ... called "enable_if". |
| 9610 | const IdentifierInfo *EnableIfII = |
| 9611 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 9612 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 9613 | return false; |
| 9614 | |
| 9615 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9616 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9617 | |
| 9618 | // Dig out the condition. |
| 9619 | Cond = nullptr; |
| 9620 | if (EnableIfTSTLoc.getArgLoc(0).getArgument().getKind() |
| 9621 | != TemplateArgument::Expression) |
| 9622 | return true; |
| 9623 | |
| 9624 | Cond = EnableIfTSTLoc.getArgLoc(0).getSourceExpression(); |
| 9625 | |
| 9626 | // Ignore Boolean literals; they add no value. |
| 9627 | if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts())) |
| 9628 | Cond = nullptr; |
| 9629 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9630 | return true; |
| 9631 | } |
| 9632 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9633 | /// Build the type that describes a C++ typename specifier, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9634 | /// e.g., "typename T::type". |
| 9635 | QualType |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9636 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9637 | SourceLocation KeywordLoc, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9638 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9639 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9640 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9641 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9642 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9643 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9644 | DeclContext *Ctx = computeDeclContext(SS); |
| 9645 | if (!Ctx) { |
| 9646 | // If the nested-name-specifier is dependent and couldn't be |
| 9647 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9648 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9649 | return Context.getDependentNameType(Keyword, |
| 9650 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9651 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 9652 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9653 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9654 | // If the nested-name-specifier refers to the current instantiation, |
| 9655 | // the "typename" keyword itself is superfluous. In C++03, the |
| 9656 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 9657 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 9658 | // 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] | 9659 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9660 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 9661 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9662 | |
| 9663 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9664 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 9665 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9666 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9667 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9668 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9669 | case LookupResult::NotFound: { |
| 9670 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 9671 | // a more specific diagnostic. |
| 9672 | SourceRange CondRange; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9673 | Expr *Cond = nullptr; |
| 9674 | if (isEnableIf(QualifierLoc, II, CondRange, Cond)) { |
| 9675 | // If we have a condition, narrow it down to the specific failed |
| 9676 | // condition. |
| 9677 | if (Cond) { |
| 9678 | Expr *FailedCond; |
| 9679 | std::string FailedDescription; |
| 9680 | std::tie(FailedCond, FailedDescription) = |
Clement Courbet | f44c6f4 | 2018-12-11 08:39:11 +0000 | [diff] [blame] | 9681 | findFailedBooleanCondition(Cond); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9682 | |
| 9683 | Diag(FailedCond->getExprLoc(), |
| 9684 | diag::err_typename_nested_not_found_requirement) |
| 9685 | << FailedDescription |
| 9686 | << FailedCond->getSourceRange(); |
| 9687 | return QualType(); |
| 9688 | } |
| 9689 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9690 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9691 | << Ctx << CondRange; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9692 | return QualType(); |
| 9693 | } |
| 9694 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 9695 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9696 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9697 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9698 | |
| 9699 | case LookupResult::FoundUnresolvedValue: { |
| 9700 | // We found a using declaration that is a value. Most likely, the using |
| 9701 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9702 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9703 | IILoc); |
| 9704 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 9705 | << Name << Ctx << FullRange; |
| 9706 | if (UnresolvedUsingValueDecl *Using |
| 9707 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 9708 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9709 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 9710 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 9711 | } |
| 9712 | } |
| 9713 | // Fall through to create a dependent typename type, from which we can recover |
| 9714 | // better. |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 9715 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9716 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 9717 | case LookupResult::NotFoundInCurrentInstantiation: |
| 9718 | // Okay, it's a member of an unknown instantiation. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9719 | return Context.getDependentNameType(Keyword, |
| 9720 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9721 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9722 | |
| 9723 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9724 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9725 | // C++ [class.qual]p2: |
| 9726 | // In a lookup in which function names are not ignored and the |
| 9727 | // nested-name-specifier nominates a class C, if the name specified |
| 9728 | // after the nested-name-specifier, when looked up in C, is the |
| 9729 | // injected-class-name of C [...] then the name is instead considered |
| 9730 | // to name the constructor of class C. |
| 9731 | // |
| 9732 | // Unlike in an elaborated-type-specifier, function names are not ignored |
| 9733 | // in typename-specifier lookup. However, they are ignored in all the |
| 9734 | // contexts where we form a typename type with no keyword (that is, in |
| 9735 | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). |
| 9736 | // |
| 9737 | // FIXME: That's not strictly true: mem-initializer-id lookup does not |
| 9738 | // ignore functions, but that appears to be an oversight. |
| 9739 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
| 9740 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
| 9741 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
| 9742 | FoundRD->isInjectedClassName() && |
| 9743 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
| 9744 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9745 | << &II << 1 << 0 /*'typename' keyword used*/; |
| 9746 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9747 | // We found a type. Build an ElaboratedType, since the |
| 9748 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 9749 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9750 | return Context.getElaboratedType(Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9751 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9752 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9753 | } |
| 9754 | |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9755 | // C++ [dcl.type.simple]p2: |
| 9756 | // A type-specifier of the form |
| 9757 | // typename[opt] nested-name-specifier[opt] template-name |
| 9758 | // is a placeholder for a deduced class type [...]. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 9759 | if (getLangOpts().CPlusPlus17) { |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9760 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
| 9761 | return Context.getElaboratedType( |
| 9762 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
| 9763 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
| 9764 | QualType(), false)); |
| 9765 | } |
| 9766 | } |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 9767 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9768 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 9769 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9770 | break; |
| 9771 | |
| 9772 | case LookupResult::FoundOverloaded: |
| 9773 | DiagID = diag::err_typename_nested_not_type; |
| 9774 | Referenced = *Result.begin(); |
| 9775 | break; |
| 9776 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 9777 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9778 | return QualType(); |
| 9779 | } |
| 9780 | |
| 9781 | // If we get here, it's because name lookup did not find a |
| 9782 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9783 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9784 | IILoc); |
| 9785 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9786 | if (Referenced) |
| 9787 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 9788 | << Name; |
| 9789 | return QualType(); |
| 9790 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9791 | |
| 9792 | namespace { |
| 9793 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 9794 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9795 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9796 | SourceLocation Loc; |
| 9797 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9798 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9799 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 9800 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9801 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9802 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9803 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9804 | DeclarationName Entity) |
| 9805 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9806 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9807 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9808 | /// Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9809 | /// transformed. |
| 9810 | /// |
| 9811 | /// For the purposes of type reconstruction, a type has already been |
| 9812 | /// transformed if it is NULL or if it is not dependent. |
| 9813 | bool AlreadyTransformed(QualType T) { |
| 9814 | return T.isNull() || !T->isDependentType(); |
| 9815 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9816 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9817 | /// Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9818 | /// rebuilt. |
| 9819 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9820 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9821 | /// Returns the name of the entity whose type is being rebuilt. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9822 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9823 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9824 | /// Sets the "base" location and entity when that |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 9825 | /// information is known based on another transformation. |
| 9826 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 9827 | this->Loc = Loc; |
| 9828 | this->Entity = Entity; |
| 9829 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9830 | |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 9831 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 9832 | // Lambdas never need to be transformed. |
| 9833 | return E; |
| 9834 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9835 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 9836 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9837 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9838 | /// Rebuilds a type within the context of the current instantiation. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9839 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9840 | /// 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] | 9841 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9842 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9843 | /// partial specialization thereof). This routine will rebuild that type now |
| 9844 | /// that we have entered the declarator's scope, which may produce different |
| 9845 | /// canonical types, e.g., |
| 9846 | /// |
| 9847 | /// \code |
| 9848 | /// template<typename T> |
| 9849 | /// struct X { |
| 9850 | /// typedef T* pointer; |
| 9851 | /// pointer data(); |
| 9852 | /// }; |
| 9853 | /// |
| 9854 | /// template<typename T> |
| 9855 | /// typename X<T>::pointer X<T>::data() { ... } |
| 9856 | /// \endcode |
| 9857 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 9858 | /// 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] | 9859 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 9860 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9861 | /// 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] | 9862 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 9863 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9864 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 9865 | SourceLocation Loc, |
| 9866 | DeclarationName Name) { |
| 9867 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9868 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9869 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9870 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 9871 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 9872 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9873 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9874 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9875 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 9876 | DeclarationName()); |
| 9877 | return Rebuilder.TransformExpr(E); |
| 9878 | } |
| 9879 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9880 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9881 | if (SS.isInvalid()) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9882 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9883 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9884 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9885 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 9886 | DeclarationName()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9887 | NestedNameSpecifierLoc Rebuilt |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9888 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9889 | if (!Rebuilt) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9890 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9891 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9892 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9893 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9894 | } |
| 9895 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9896 | /// 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] | 9897 | /// instantiation. |
| 9898 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 9899 | TemplateParameterList *Params) { |
| 9900 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 9901 | Decl *Param = Params->getParam(I); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9902 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9903 | // There is nothing to rebuild in a type parameter. |
| 9904 | if (isa<TemplateTypeParmDecl>(Param)) |
| 9905 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9906 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9907 | // Rebuild the template parameter list of a template template parameter. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9908 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9909 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 9910 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 9911 | TTP->getTemplateParameters())) |
| 9912 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9913 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9914 | continue; |
| 9915 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9916 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9917 | // Rebuild the type of a non-type template parameter. |
| 9918 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9919 | TypeSourceInfo *NewTSI |
| 9920 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 9921 | NTTP->getLocation(), |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9922 | NTTP->getDeclName()); |
| 9923 | if (!NewTSI) |
| 9924 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9925 | |
Erik Pilkington | 9f9462a | 2018-08-07 22:59:02 +0000 | [diff] [blame] | 9926 | if (NewTSI->getType()->isUndeducedType()) { |
| 9927 | // C++17 [temp.dep.expr]p3: |
| 9928 | // An id-expression is type-dependent if it contains |
| 9929 | // - an identifier associated by name lookup with a non-type |
| 9930 | // template-parameter declared with a type that contains a |
| 9931 | // placeholder type (7.1.7.4), |
| 9932 | NewTSI = SubstAutoTypeSourceInfo(NewTSI, Context.DependentTy); |
| 9933 | } |
| 9934 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9935 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 9936 | NTTP->setTypeSourceInfo(NewTSI); |
| 9937 | NTTP->setType(NewTSI->getType()); |
| 9938 | } |
| 9939 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9940 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9941 | return false; |
| 9942 | } |
| 9943 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9944 | /// Produces a formatted string that describes the binding of |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9945 | /// template parameters to template arguments. |
| 9946 | std::string |
| 9947 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9948 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9949 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9950 | } |
| 9951 | |
| 9952 | std::string |
| 9953 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9954 | const TemplateArgument *Args, |
| 9955 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9956 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9957 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9958 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9959 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9960 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9961 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9962 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9963 | if (I >= NumArgs) |
| 9964 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9965 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9966 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9967 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9968 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9969 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9970 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9971 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9972 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9973 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9974 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9975 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9976 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9977 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 9978 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9979 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9980 | |
| 9981 | Out << ']'; |
| 9982 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9983 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9984 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9985 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 9986 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9987 | if (!FD) |
| 9988 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9989 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9990 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9991 | |
| 9992 | // Take tokens to avoid allocations |
| 9993 | LPT->Toks.swap(Toks); |
| 9994 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9995 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9996 | |
| 9997 | FD->setLateTemplateParsed(true); |
| 9998 | } |
| 9999 | |
| 10000 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 10001 | if (!FD) |
| 10002 | return; |
| 10003 | FD->setLateTemplateParsed(false); |
| 10004 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 10005 | |
| 10006 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 10007 | DeclContext *DC = CurContext; |
| 10008 | |
| 10009 | while (DC) { |
| 10010 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 10011 | const FunctionDecl *FD = RD->isLocalClass(); |
| 10012 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 10013 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 10014 | return false; |
| 10015 | |
| 10016 | DC = DC->getParent(); |
| 10017 | } |
| 10018 | return false; |
| 10019 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10020 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 10021 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10022 | /// Walk the path from which a declaration was instantiated, and check |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10023 | /// that every explicit specialization along that path is visible. This enforces |
| 10024 | /// C++ [temp.expl.spec]/6: |
| 10025 | /// |
| 10026 | /// If a template, a member template or a member of a class template is |
| 10027 | /// explicitly specialized then that specialization shall be declared before |
| 10028 | /// the first use of that specialization that would cause an implicit |
| 10029 | /// instantiation to take place, in every translation unit in which such a |
| 10030 | /// use occurs; no diagnostic is required. |
| 10031 | /// |
| 10032 | /// and also C++ [temp.class.spec]/1: |
| 10033 | /// |
| 10034 | /// A partial specialization shall be declared before the first use of a |
| 10035 | /// class template specialization that would make use of the partial |
| 10036 | /// specialization as the result of an implicit or explicit instantiation |
| 10037 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 10038 | /// required. |
| 10039 | class ExplicitSpecializationVisibilityChecker { |
| 10040 | Sema &S; |
| 10041 | SourceLocation Loc; |
| 10042 | llvm::SmallVector<Module *, 8> Modules; |
| 10043 | |
| 10044 | public: |
| 10045 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 10046 | : S(S), Loc(Loc) {} |
| 10047 | |
| 10048 | void check(NamedDecl *ND) { |
| 10049 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 10050 | return checkImpl(FD); |
| 10051 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 10052 | return checkImpl(RD); |
| 10053 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 10054 | return checkImpl(VD); |
| 10055 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 10056 | return checkImpl(ED); |
| 10057 | } |
| 10058 | |
| 10059 | private: |
| 10060 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 10061 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 10062 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 10063 | const bool Recover = true; |
| 10064 | |
| 10065 | // If we got a custom set of modules (because only a subset of the |
| 10066 | // declarations are interesting), use them, otherwise let |
| 10067 | // diagnoseMissingImport intelligently pick some. |
| 10068 | if (Modules.empty()) |
| 10069 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 10070 | else |
| 10071 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 10072 | } |
| 10073 | |
| 10074 | // Check a specific declaration. There are three problematic cases: |
| 10075 | // |
| 10076 | // 1) The declaration is an explicit specialization of a template |
| 10077 | // specialization. |
| 10078 | // 2) The declaration is an explicit specialization of a member of an |
| 10079 | // templated class. |
| 10080 | // 3) The declaration is an instantiation of a template, and that template |
| 10081 | // is an explicit specialization of a member of a templated class. |
| 10082 | // |
| 10083 | // We don't need to go any deeper than that, as the instantiation of the |
| 10084 | // surrounding class / etc is not triggered by whatever triggered this |
| 10085 | // instantiation, and thus should be checked elsewhere. |
| 10086 | template<typename SpecDecl> |
| 10087 | void checkImpl(SpecDecl *Spec) { |
| 10088 | bool IsHiddenExplicitSpecialization = false; |
| 10089 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 10090 | IsHiddenExplicitSpecialization = |
| 10091 | Spec->getMemberSpecializationInfo() |
| 10092 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 10093 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10094 | } else { |
| 10095 | checkInstantiated(Spec); |
| 10096 | } |
| 10097 | |
| 10098 | if (IsHiddenExplicitSpecialization) |
| 10099 | diagnose(Spec->getMostRecentDecl(), false); |
| 10100 | } |
| 10101 | |
| 10102 | void checkInstantiated(FunctionDecl *FD) { |
| 10103 | if (auto *TD = FD->getPrimaryTemplate()) |
| 10104 | checkTemplate(TD); |
| 10105 | } |
| 10106 | |
| 10107 | void checkInstantiated(CXXRecordDecl *RD) { |
| 10108 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 10109 | if (!SD) |
| 10110 | return; |
| 10111 | |
| 10112 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10113 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 10114 | checkTemplate(TD); |
| 10115 | else if (auto *TD = |
| 10116 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 10117 | if (!S.hasVisibleDeclaration(TD)) |
| 10118 | diagnose(TD, true); |
| 10119 | checkTemplate(TD); |
| 10120 | } |
| 10121 | } |
| 10122 | |
| 10123 | void checkInstantiated(VarDecl *RD) { |
| 10124 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 10125 | if (!SD) |
| 10126 | return; |
| 10127 | |
| 10128 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10129 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 10130 | checkTemplate(TD); |
| 10131 | else if (auto *TD = |
| 10132 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 10133 | if (!S.hasVisibleDeclaration(TD)) |
| 10134 | diagnose(TD, true); |
| 10135 | checkTemplate(TD); |
| 10136 | } |
| 10137 | } |
| 10138 | |
| 10139 | void checkInstantiated(EnumDecl *FD) {} |
| 10140 | |
| 10141 | template<typename TemplDecl> |
| 10142 | void checkTemplate(TemplDecl *TD) { |
| 10143 | if (TD->isMemberSpecialization()) { |
| 10144 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 10145 | diagnose(TD->getMostRecentDecl(), false); |
| 10146 | } |
| 10147 | } |
| 10148 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 10149 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10150 | |
| 10151 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 10152 | if (!getLangOpts().Modules) |
| 10153 | return; |
| 10154 | |
| 10155 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 10156 | } |
| 10157 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10158 | /// Check whether a template partial specialization that we've discovered |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10159 | /// is hidden, and produce suitable diagnostics if so. |
| 10160 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 10161 | NamedDecl *Spec) { |
| 10162 | llvm::SmallVector<Module *, 8> Modules; |
| 10163 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 10164 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 10165 | MissingImportKind::PartialSpecialization, |
| 10166 | /*Recover*/true); |
| 10167 | } |