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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements semantic analysis for C++ templates. |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 12 | #include "TreeTransform.h" |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/ExprCXX.h" |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeVisitor.h" |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Builtins.h" |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DeclSpec.h" |
| 26 | #include "clang/Sema/Lookup.h" |
| 27 | #include "clang/Sema/ParsedTemplate.h" |
| 28 | #include "clang/Sema/Scope.h" |
| 29 | #include "clang/Sema/SemaInternal.h" |
| 30 | #include "clang/Sema/Template.h" |
| 31 | #include "clang/Sema/TemplateDeduction.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallBitVector.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 35 | |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 36 | #include <iterator> |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 37 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 38 | using namespace sema; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 39 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 40 | // Exported for use by Parser. |
| 41 | SourceRange |
| 42 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 43 | unsigned N) { |
| 44 | if (!N) return SourceRange(); |
| 45 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 46 | } |
| 47 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 48 | namespace clang { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 49 | /// [temp.constr.decl]p2: A template's associated constraints are |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 50 | /// defined as a single constraint-expression derived from the introduced |
| 51 | /// constraint-expressions [ ... ]. |
| 52 | /// |
| 53 | /// \param Params The template parameter list and optional requires-clause. |
| 54 | /// |
| 55 | /// \param FD The underlying templated function declaration for a function |
| 56 | /// template. |
| 57 | static Expr *formAssociatedConstraints(TemplateParameterList *Params, |
| 58 | FunctionDecl *FD); |
| 59 | } |
| 60 | |
| 61 | static Expr *clang::formAssociatedConstraints(TemplateParameterList *Params, |
| 62 | FunctionDecl *FD) { |
| 63 | // FIXME: Concepts: collect additional introduced constraint-expressions |
| 64 | assert(!FD && "Cannot collect constraints from function declaration yet."); |
| 65 | return Params->getRequiresClause(); |
| 66 | } |
| 67 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 68 | /// Determine whether the declaration found is acceptable as the name |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 69 | /// of a template and, if so, return that template declaration. Otherwise, |
| 70 | /// returns NULL. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 71 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 72 | NamedDecl *Orig, |
| 73 | bool AllowFunctionTemplates) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 74 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 76 | if (isa<TemplateDecl>(D)) { |
| 77 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 78 | return nullptr; |
| 79 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 80 | return Orig; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 81 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 83 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 84 | // C++ [temp.local]p1: |
| 85 | // Like normal (non-template) classes, class templates have an |
| 86 | // injected-class-name (Clause 9). The injected-class-name |
| 87 | // can be used with or without a template-argument-list. When |
| 88 | // it is used without a template-argument-list, it is |
| 89 | // equivalent to the injected-class-name followed by the |
| 90 | // template-parameters of the class template enclosed in |
| 91 | // <>. When it is used with a template-argument-list, it |
| 92 | // refers to the specified class template specialization, |
| 93 | // which could be the current specialization or another |
| 94 | // specialization. |
| 95 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 96 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 97 | if (Record->getDescribedClassTemplate()) |
| 98 | return Record->getDescribedClassTemplate(); |
| 99 | |
| 100 | if (ClassTemplateSpecializationDecl *Spec |
| 101 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 102 | return Spec->getSpecializedTemplate(); |
| 103 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 105 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 106 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 108 | // 'using Dependent::foo;' can resolve to a template name. |
| 109 | // 'using typename Dependent::foo;' cannot (not even if 'foo' is an |
| 110 | // injected-class-name). |
| 111 | if (isa<UnresolvedUsingValueDecl>(D)) |
| 112 | return D; |
| 113 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 114 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 117 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 118 | bool AllowFunctionTemplates) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 119 | // The set of class templates we've already seen. |
| 120 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 121 | LookupResult::Filter filter = R.makeFilter(); |
| 122 | while (filter.hasNext()) { |
| 123 | NamedDecl *Orig = filter.next(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 124 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 125 | AllowFunctionTemplates); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 126 | if (!Repl) |
| 127 | filter.erase(); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 128 | else if (Repl != Orig) { |
| 129 | |
| 130 | // C++ [temp.local]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 131 | // 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] | 132 | // 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] | 133 | // one base class). If all of the injected-class-names that are found |
| 134 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 135 | // is used as a template-name, the reference refers to the class |
| 136 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 137 | // ambiguous. |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 138 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 139 | if (!ClassTemplates.insert(ClassTmpl).second) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 140 | filter.erase(); |
| 141 | continue; |
| 142 | } |
John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 143 | |
| 144 | // FIXME: we promote access to public here as a workaround to |
| 145 | // the fact that LookupResult doesn't let us remember that we |
| 146 | // found this template through a particular injected class name, |
| 147 | // which means we end up doing nasty things to the invariants. |
| 148 | // Pretending that access is public is *much* safer. |
| 149 | filter.replace(Repl, AS_public); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 150 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 151 | } |
| 152 | filter.done(); |
| 153 | } |
| 154 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 155 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 156 | bool AllowFunctionTemplates) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 157 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 158 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 159 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 160 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 161 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 164 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 165 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 166 | bool hasTemplateKeyword, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 167 | const UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 168 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 169 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 170 | TemplateTy &TemplateResult, |
| 171 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 172 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 173 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 174 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 175 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 177 | switch (Name.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 178 | case UnqualifiedIdKind::IK_Identifier: |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 179 | TName = DeclarationName(Name.Identifier); |
| 180 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 181 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 182 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 183 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 184 | Name.OperatorFunctionId.Operator); |
| 185 | break; |
| 186 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 187 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 188 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 189 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 190 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 191 | default: |
| 192 | return TNK_Non_template; |
| 193 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 195 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 197 | LookupResult R(*this, TName, Name.getBeginLoc(), LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 198 | if (LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 199 | MemberOfUnknownSpecialization)) |
| 200 | return TNK_Non_template; |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 201 | if (R.empty()) return TNK_Non_template; |
| 202 | if (R.isAmbiguous()) { |
| 203 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 204 | R.suppressDiagnostics(); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 205 | |
| 206 | // FIXME: we might have ambiguous templates, in which case we |
| 207 | // should at least parse them properly! |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 208 | return TNK_Non_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 209 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 210 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 211 | TemplateName Template; |
| 212 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 214 | unsigned ResultCount = R.end() - R.begin(); |
| 215 | if (ResultCount > 1) { |
| 216 | // We assume that we'll preserve the qualifier from a function |
| 217 | // template name in other ways. |
| 218 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 219 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 220 | |
| 221 | // We'll do this lookup again later. |
| 222 | R.suppressDiagnostics(); |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 223 | } else if (isa<UnresolvedUsingValueDecl>((*R.begin())->getUnderlyingDecl())) { |
| 224 | // We don't yet know whether this is a template-name or not. |
| 225 | MemberOfUnknownSpecialization = true; |
| 226 | return TNK_Non_template; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 227 | } else { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 228 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 229 | |
| 230 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 231 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 232 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 233 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 234 | } else { |
| 235 | Template = TemplateName(TD); |
| 236 | } |
| 237 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 238 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 239 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 240 | |
| 241 | // We'll do this lookup again later. |
| 242 | R.suppressDiagnostics(); |
| 243 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 244 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 245 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 246 | isa<BuiltinTemplateDecl>(TD)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 247 | TemplateKind = |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 248 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 249 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 250 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 252 | TemplateResult = TemplateTy::make(Template); |
| 253 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Richard Smith | 278890f | 2017-02-10 20:39:58 +0000 | [diff] [blame] | 256 | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, |
| 257 | SourceLocation NameLoc, |
| 258 | ParsedTemplateTy *Template) { |
| 259 | CXXScopeSpec SS; |
| 260 | bool MemberOfUnknownSpecialization = false; |
| 261 | |
| 262 | // We could use redeclaration lookup here, but we don't need to: the |
| 263 | // syntactic form of a deduction guide is enough to identify it even |
| 264 | // if we can't look up the template name at all. |
| 265 | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 266 | if (LookupTemplateName(R, S, SS, /*ObjectType*/ QualType(), |
| 267 | /*EnteringContext*/ false, |
| 268 | MemberOfUnknownSpecialization)) |
| 269 | return false; |
Richard Smith | 278890f | 2017-02-10 20:39:58 +0000 | [diff] [blame] | 270 | |
| 271 | if (R.empty()) return false; |
| 272 | if (R.isAmbiguous()) { |
| 273 | // FIXME: Diagnose an ambiguity if we find at least one template. |
| 274 | R.suppressDiagnostics(); |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | // We only treat template-names that name type templates as valid deduction |
| 279 | // guide names. |
| 280 | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); |
| 281 | if (!TD || !getAsTypeTemplateDecl(TD)) |
| 282 | return false; |
| 283 | |
| 284 | if (Template) |
| 285 | *Template = TemplateTy::make(TemplateName(TD)); |
| 286 | return true; |
| 287 | } |
| 288 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 289 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 290 | SourceLocation IILoc, |
| 291 | Scope *S, |
| 292 | const CXXScopeSpec *SS, |
| 293 | TemplateTy &SuggestedTemplate, |
| 294 | TemplateNameKind &SuggestedKind) { |
| 295 | // We can't recover unless there's a dependent scope specifier preceding the |
| 296 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 297 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 298 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 299 | computeDeclContext(*SS)) |
| 300 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 301 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 302 | // The code is missing a 'template' keyword prior to the dependent template |
| 303 | // name. |
| 304 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 305 | Diag(IILoc, diag::err_template_kw_missing) |
| 306 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 307 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 308 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 309 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 310 | SuggestedKind = TNK_Dependent_template_name; |
| 311 | return true; |
| 312 | } |
| 313 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 314 | bool Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 315 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 316 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 317 | bool EnteringContext, |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 318 | bool &MemberOfUnknownSpecialization, |
| 319 | SourceLocation TemplateKWLoc) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 320 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 321 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 322 | DeclContext *LookupCtx = nullptr; |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 323 | bool IsDependent = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 324 | if (!ObjectType.isNull()) { |
| 325 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 326 | // x->B::f, and we are looking into the type of the object. |
| 327 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 328 | LookupCtx = computeDeclContext(ObjectType); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 329 | IsDependent = !LookupCtx; |
| 330 | assert((IsDependent || !ObjectType->isIncompleteType() || |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 331 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 332 | "Caller should have completed object type"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 333 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 334 | // Template names cannot appear inside an Objective-C class or object type. |
| 335 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 336 | Found.clear(); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 337 | return false; |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 338 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 339 | } else if (SS.isSet()) { |
| 340 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 341 | // so long into the context associated with the prior nested-name-specifier. |
| 342 | LookupCtx = computeDeclContext(SS, EnteringContext); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 343 | IsDependent = !LookupCtx; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 344 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 345 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 346 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 347 | return true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 351 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 352 | if (LookupCtx) { |
| 353 | // Perform "qualified" name lookup into the declaration context we |
| 354 | // computed, which is either the type of the base of a member access |
| 355 | // expression or the declaration context associated with a prior |
| 356 | // nested-name-specifier. |
| 357 | LookupQualifiedName(Found, LookupCtx); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 358 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 359 | // FIXME: The C++ standard does not clearly specify what happens in the |
| 360 | // case where the object type is dependent, and implementations vary. In |
| 361 | // Clang, we treat a name after a . or -> as a template-name if lookup |
| 362 | // finds a non-dependent member or member of the current instantiation that |
| 363 | // is a type template, or finds no such members and lookup in the context |
| 364 | // of the postfix-expression finds a type template. In the latter case, the |
| 365 | // name is nonetheless dependent, and we may resolve it to a member of an |
| 366 | // unknown specialization when we come to instantiate the template. |
| 367 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 370 | if (!SS.isSet() && (ObjectType.isNull() || Found.empty())) { |
| 371 | // C++ [basic.lookup.classref]p1: |
| 372 | // In a class member access expression (5.2.5), if the . or -> token is |
| 373 | // immediately followed by an identifier followed by a <, the |
| 374 | // identifier must be looked up to determine whether the < is the |
| 375 | // beginning of a template argument list (14.2) or a less-than operator. |
| 376 | // The identifier is first looked up in the class of the object |
| 377 | // expression. If the identifier is not found, it is then looked up in |
| 378 | // the context of the entire postfix-expression and shall name a class |
| 379 | // template. |
| 380 | if (S) |
| 381 | LookupName(Found, S); |
| 382 | |
| 383 | if (!ObjectType.isNull()) { |
| 384 | // FIXME: We should filter out all non-type templates here, particularly |
| 385 | // variable templates and concepts. But the exclusion of alias templates |
| 386 | // and template template parameters is a wording defect. |
| 387 | AllowFunctionTemplatesInLookup = false; |
| 388 | ObjectTypeSearchedInScope = true; |
| 389 | } |
| 390 | |
| 391 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
| 392 | } |
| 393 | |
| 394 | if (Found.empty() && !IsDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 395 | // If we did not find any names, attempt to correct any typos. |
| 396 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 397 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 398 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 399 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 400 | FilterCCC->WantTypeSpecifiers = false; |
| 401 | FilterCCC->WantExpressionKeywords = false; |
| 402 | FilterCCC->WantRemainingKeywords = false; |
| 403 | FilterCCC->WantCXXNamedCasts = true; |
| 404 | if (TypoCorrection Corrected = CorrectTypo( |
| 405 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 406 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 407 | Found.setLookupName(Corrected.getCorrection()); |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 408 | if (auto *ND = Corrected.getFoundDecl()) |
| 409 | Found.addDecl(ND); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 410 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 411 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 412 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 413 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 414 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 415 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 416 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 417 | << Name << LookupCtx << DroppedSpecifier |
| 418 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 419 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 420 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 421 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 422 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 423 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 424 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 428 | NamedDecl *ExampleLookupResult = |
| 429 | Found.empty() ? nullptr : Found.getRepresentativeDecl(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 430 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 431 | if (Found.empty()) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 432 | if (IsDependent) { |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 433 | MemberOfUnknownSpecialization = true; |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 434 | return false; |
| 435 | } |
| 436 | |
| 437 | // If a 'template' keyword was used, a lookup that finds only non-template |
| 438 | // names is an error. |
| 439 | if (ExampleLookupResult && TemplateKWLoc.isValid()) { |
| 440 | Diag(Found.getNameLoc(), diag::err_template_kw_refers_to_non_template) |
| 441 | << Found.getLookupName() << SS.getRange(); |
Richard Smith | cbebd62 | 2018-05-14 20:52:48 +0000 | [diff] [blame] | 442 | Diag(ExampleLookupResult->getUnderlyingDecl()->getLocation(), |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 443 | diag::note_template_kw_refers_to_non_template) |
| 444 | << Found.getLookupName(); |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | return false; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 449 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 450 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 451 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 452 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 453 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 454 | // [...] If the lookup in the class of the object expression finds a |
| 455 | // template, the name is also looked up in the context of the entire |
| 456 | // postfix-expression and [...] |
| 457 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 458 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 459 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 460 | LookupOrdinaryName); |
| 461 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 462 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 463 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 464 | if (FoundOuter.empty()) { |
| 465 | // - if the name is not found, the name found in the class of the |
| 466 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 467 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 468 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 469 | // - if the name is found in the context of the entire |
| 470 | // postfix-expression and does not name a class template, the name |
| 471 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 472 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 473 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 474 | // - if the name found is a class template, it must refer to the same |
| 475 | // entity as the one found in the class of the object expression, |
| 476 | // otherwise the program is ill-formed. |
| 477 | if (!Found.isSingleResult() || |
| 478 | Found.getFoundDecl()->getCanonicalDecl() |
| 479 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 480 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 481 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 482 | << Found.getLookupName() |
| 483 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 484 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 485 | diag::note_ambig_member_ref_object_type) |
| 486 | << ObjectType; |
| 487 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 488 | diag::note_ambig_member_ref_scope); |
| 489 | |
| 490 | // Recover by taking the template that we found in the object |
| 491 | // expression's type. |
| 492 | } |
| 493 | } |
| 494 | } |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 495 | |
| 496 | return false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 499 | void Sema::diagnoseExprIntendedAsTemplateName(Scope *S, ExprResult TemplateName, |
| 500 | SourceLocation Less, |
| 501 | SourceLocation Greater) { |
| 502 | if (TemplateName.isInvalid()) |
| 503 | return; |
| 504 | |
| 505 | DeclarationNameInfo NameInfo; |
| 506 | CXXScopeSpec SS; |
| 507 | LookupNameKind LookupKind; |
| 508 | |
| 509 | DeclContext *LookupCtx = nullptr; |
| 510 | NamedDecl *Found = nullptr; |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 511 | bool MissingTemplateKeyword = false; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 512 | |
| 513 | // Figure out what name we looked up. |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 514 | if (auto *DRE = dyn_cast<DeclRefExpr>(TemplateName.get())) { |
| 515 | NameInfo = DRE->getNameInfo(); |
| 516 | SS.Adopt(DRE->getQualifierLoc()); |
| 517 | LookupKind = LookupOrdinaryName; |
| 518 | Found = DRE->getFoundDecl(); |
| 519 | } else if (auto *ME = dyn_cast<MemberExpr>(TemplateName.get())) { |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 520 | NameInfo = ME->getMemberNameInfo(); |
| 521 | SS.Adopt(ME->getQualifierLoc()); |
| 522 | LookupKind = LookupMemberName; |
| 523 | LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl(); |
| 524 | Found = ME->getMemberDecl(); |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 525 | } else if (auto *DSDRE = |
| 526 | dyn_cast<DependentScopeDeclRefExpr>(TemplateName.get())) { |
| 527 | NameInfo = DSDRE->getNameInfo(); |
| 528 | SS.Adopt(DSDRE->getQualifierLoc()); |
| 529 | MissingTemplateKeyword = true; |
| 530 | } else if (auto *DSME = |
| 531 | dyn_cast<CXXDependentScopeMemberExpr>(TemplateName.get())) { |
| 532 | NameInfo = DSME->getMemberNameInfo(); |
| 533 | SS.Adopt(DSME->getQualifierLoc()); |
| 534 | MissingTemplateKeyword = true; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 535 | } else { |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 536 | llvm_unreachable("unexpected kind of potential template name"); |
| 537 | } |
| 538 | |
| 539 | // If this is a dependent-scope lookup, diagnose that the 'template' keyword |
| 540 | // was missing. |
| 541 | if (MissingTemplateKeyword) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 542 | Diag(NameInfo.getBeginLoc(), diag::err_template_kw_missing) |
| 543 | << "" << NameInfo.getName().getAsString() << SourceRange(Less, Greater); |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 544 | return; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // Try to correct the name by looking for templates and C++ named casts. |
| 548 | struct TemplateCandidateFilter : CorrectionCandidateCallback { |
| 549 | TemplateCandidateFilter() { |
| 550 | WantTypeSpecifiers = false; |
| 551 | WantExpressionKeywords = false; |
| 552 | WantRemainingKeywords = false; |
| 553 | WantCXXNamedCasts = true; |
| 554 | }; |
| 555 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| 556 | if (auto *ND = Candidate.getCorrectionDecl()) |
| 557 | return isAcceptableTemplateName(ND->getASTContext(), ND, true); |
| 558 | return Candidate.isKeyword(); |
| 559 | } |
| 560 | }; |
| 561 | |
| 562 | DeclarationName Name = NameInfo.getName(); |
| 563 | if (TypoCorrection Corrected = |
| 564 | CorrectTypo(NameInfo, LookupKind, S, &SS, |
| 565 | llvm::make_unique<TemplateCandidateFilter>(), |
| 566 | CTK_ErrorRecovery, LookupCtx)) { |
| 567 | auto *ND = Corrected.getFoundDecl(); |
| 568 | if (ND) |
| 569 | ND = isAcceptableTemplateName(Context, ND, |
| 570 | /*AllowFunctionTemplates*/ true); |
| 571 | if (ND || Corrected.isKeyword()) { |
| 572 | if (LookupCtx) { |
| 573 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 574 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| 575 | Name.getAsString() == CorrectedStr; |
| 576 | diagnoseTypo(Corrected, |
| 577 | PDiag(diag::err_non_template_in_member_template_id_suggest) |
| 578 | << Name << LookupCtx << DroppedSpecifier |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 579 | << SS.getRange(), false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 580 | } else { |
| 581 | diagnoseTypo(Corrected, |
| 582 | PDiag(diag::err_non_template_in_template_id_suggest) |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 583 | << Name, false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 584 | } |
| 585 | if (Found) |
| 586 | Diag(Found->getLocation(), |
| 587 | diag::note_non_template_in_template_id_found); |
| 588 | return; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) |
| 593 | << Name << SourceRange(Less, Greater); |
| 594 | if (Found) |
| 595 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); |
| 596 | } |
| 597 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 598 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 599 | /// was just parsed. This is only possible with an explicit scope |
| 600 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 601 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 602 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 603 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 604 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 605 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 606 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 607 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 608 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 609 | // C++11 [expr.prim.general]p12: |
| 610 | // An id-expression that denotes a non-static data member or non-static |
| 611 | // member function of a class can only be used: |
| 612 | // (...) |
| 613 | // - if that id-expression denotes a non-static data member and it |
| 614 | // appears in an unevaluated operand. |
| 615 | // |
| 616 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 617 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 618 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 619 | // always a MemberExpr. |
| 620 | bool MightBeCxx11UnevalField = |
| 621 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 622 | |
Akira Hatanaka | d644e02 | 2016-12-16 03:19:41 +0000 | [diff] [blame] | 623 | // Check if the nested name specifier is an enum type. |
| 624 | bool IsEnum = false; |
| 625 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
| 626 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
| 627 | |
| 628 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 629 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 630 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 631 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 632 | // Since the 'this' expression is synthesized, we don't need to |
| 633 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 634 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 635 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 636 | return CXXDependentScopeMemberExpr::Create( |
| 637 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 638 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 639 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 642 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 643 | } |
| 644 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 645 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 646 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 647 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 648 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 649 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 650 | return DependentScopeDeclRefExpr::Create( |
| 651 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 652 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 655 | |
| 656 | /// Determine whether we would be unable to instantiate this template (because |
| 657 | /// it either has no definition, or is in the process of being instantiated). |
| 658 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 659 | NamedDecl *Instantiation, |
| 660 | bool InstantiatedFromMember, |
| 661 | const NamedDecl *Pattern, |
| 662 | const NamedDecl *PatternDef, |
| 663 | TemplateSpecializationKind TSK, |
| 664 | bool Complain /*= true*/) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 665 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 666 | isa<VarDecl>(Instantiation)); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 667 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 668 | bool IsEntityBeingDefined = false; |
| 669 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 670 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 671 | |
| 672 | if (PatternDef && !IsEntityBeingDefined) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 673 | NamedDecl *SuggestedDef = nullptr; |
| 674 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 675 | /*OnlyNeedComplete*/false)) { |
| 676 | // If we're allowed to diagnose this and recover, do so. |
| 677 | bool Recover = Complain && !isSFINAEContext(); |
| 678 | if (Complain) |
| 679 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 680 | Sema::MissingImportKind::Definition, Recover); |
| 681 | return !Recover; |
| 682 | } |
| 683 | return false; |
| 684 | } |
| 685 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 686 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 687 | return true; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 688 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 689 | llvm::Optional<unsigned> Note; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 690 | QualType InstantiationTy; |
| 691 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 692 | InstantiationTy = Context.getTypeDeclType(TD); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 693 | if (PatternDef) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 694 | Diag(PointOfInstantiation, |
| 695 | diag::err_template_instantiate_within_definition) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 696 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 697 | << InstantiationTy; |
| 698 | // Not much point in noting the template declaration here, since |
| 699 | // we're lexically inside it. |
| 700 | Instantiation->setInvalidDecl(); |
| 701 | } else if (InstantiatedFromMember) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 702 | if (isa<FunctionDecl>(Instantiation)) { |
| 703 | Diag(PointOfInstantiation, |
| 704 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 705 | << /*member function*/ 1 << Instantiation->getDeclName() |
| 706 | << Instantiation->getDeclContext(); |
| 707 | Note = diag::note_explicit_instantiation_here; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 708 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 709 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 710 | Diag(PointOfInstantiation, |
| 711 | diag::err_implicit_instantiate_member_undefined) |
| 712 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 713 | Note = diag::note_member_declared_at; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 714 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 715 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 716 | if (isa<FunctionDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 717 | Diag(PointOfInstantiation, |
| 718 | diag::err_explicit_instantiation_undefined_func_template) |
| 719 | << Pattern; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 720 | Note = diag::note_explicit_instantiation_here; |
| 721 | } else if (isa<TagDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 722 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 723 | << (TSK != TSK_ImplicitInstantiation) |
| 724 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 725 | Note = diag::note_template_decl_here; |
| 726 | } else { |
| 727 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 728 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 729 | Diag(PointOfInstantiation, |
| 730 | diag::err_explicit_instantiation_undefined_var_template) |
| 731 | << Instantiation; |
| 732 | Instantiation->setInvalidDecl(); |
| 733 | } else |
| 734 | Diag(PointOfInstantiation, |
| 735 | diag::err_explicit_instantiation_undefined_member) |
| 736 | << /*static data member*/ 2 << Instantiation->getDeclName() |
| 737 | << Instantiation->getDeclContext(); |
| 738 | Note = diag::note_explicit_instantiation_here; |
| 739 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 740 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 741 | if (Note) // Diagnostics were emitted. |
| 742 | Diag(Pattern->getLocation(), Note.getValue()); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 743 | |
| 744 | // In general, Instantiation isn't marked invalid to get more than one |
| 745 | // error for multiple undefined instantiations. But the code that does |
| 746 | // explicit declaration -> explicit definition conversion can't handle |
| 747 | // invalid declarations, so mark as invalid in that case. |
| 748 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 749 | Instantiation->setInvalidDecl(); |
| 750 | return true; |
| 751 | } |
| 752 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 753 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 754 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 755 | /// declaration at location Loc. Returns true to indicate that this is |
| 756 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 757 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 758 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 759 | |
| 760 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 761 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 762 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 763 | |
| 764 | // C++ [temp.local]p4: |
| 765 | // A template-parameter shall not be redeclared within its |
| 766 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 768 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 769 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 772 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 773 | /// the parameter D to reference the templated declaration and return a pointer |
| 774 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 775 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 776 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 777 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 778 | return Temp; |
| 779 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 780 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 783 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 784 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 785 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 786 | "Only template template arguments can be pack expansions here"); |
| 787 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 788 | "Template template argument pack expansion without packs"); |
| 789 | ParsedTemplateArgument Result(*this); |
| 790 | Result.EllipsisLoc = EllipsisLoc; |
| 791 | return Result; |
| 792 | } |
| 793 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 794 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 795 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 796 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 797 | switch (Arg.getKind()) { |
| 798 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 799 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 800 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 801 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 802 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 803 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 804 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 805 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 806 | case ParsedTemplateArgument::NonType: { |
| 807 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 808 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 809 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 810 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 811 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 812 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 813 | TemplateArgument TArg; |
| 814 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 815 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 816 | else |
| 817 | TArg = Template; |
| 818 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 819 | Arg.getScopeSpec().getWithLocInContext( |
| 820 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 821 | Arg.getLocation(), |
| 822 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 825 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 826 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 827 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 828 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 829 | /// Translates template arguments as provided by the parser |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 830 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 831 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 832 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 833 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 834 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 835 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 836 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 837 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 838 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 839 | SourceLocation Loc, |
| 840 | IdentifierInfo *Name) { |
| 841 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 842 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 843 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 844 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 845 | } |
| 846 | |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 847 | /// Convert a parsed type into a parsed template argument. This is mostly |
| 848 | /// trivial, except that we may have parsed a C++17 deduced class template |
| 849 | /// specialization type, in which case we should form a template template |
| 850 | /// argument instead of a type template argument. |
| 851 | ParsedTemplateArgument Sema::ActOnTemplateTypeArgument(TypeResult ParsedType) { |
| 852 | TypeSourceInfo *TInfo; |
| 853 | QualType T = GetTypeFromParser(ParsedType.get(), &TInfo); |
| 854 | if (T.isNull()) |
| 855 | return ParsedTemplateArgument(); |
| 856 | assert(TInfo && "template argument with no location"); |
| 857 | |
| 858 | // If we might have formed a deduced template specialization type, convert |
| 859 | // it to a template template argument. |
| 860 | if (getLangOpts().CPlusPlus17) { |
| 861 | TypeLoc TL = TInfo->getTypeLoc(); |
| 862 | SourceLocation EllipsisLoc; |
| 863 | if (auto PET = TL.getAs<PackExpansionTypeLoc>()) { |
| 864 | EllipsisLoc = PET.getEllipsisLoc(); |
| 865 | TL = PET.getPatternLoc(); |
| 866 | } |
| 867 | |
| 868 | CXXScopeSpec SS; |
| 869 | if (auto ET = TL.getAs<ElaboratedTypeLoc>()) { |
| 870 | SS.Adopt(ET.getQualifierLoc()); |
| 871 | TL = ET.getNamedTypeLoc(); |
| 872 | } |
| 873 | |
| 874 | if (auto DTST = TL.getAs<DeducedTemplateSpecializationTypeLoc>()) { |
| 875 | TemplateName Name = DTST.getTypePtr()->getTemplateName(); |
| 876 | if (SS.isSet()) |
| 877 | Name = Context.getQualifiedTemplateName(SS.getScopeRep(), |
| 878 | /*HasTemplateKeyword*/ false, |
| 879 | Name.getAsTemplateDecl()); |
| 880 | ParsedTemplateArgument Result(SS, TemplateTy::make(Name), |
| 881 | DTST.getTemplateNameLoc()); |
| 882 | if (EllipsisLoc.isValid()) |
| 883 | Result = Result.getTemplatePackExpansion(EllipsisLoc); |
| 884 | return Result; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | // This is a normal type template argument. Note, if the type template |
| 889 | // 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] | 890 | // 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] | 891 | // convertTypeTemplateArgumentToTemplate. |
| 892 | return ParsedTemplateArgument(ParsedTemplateArgument::Type, |
| 893 | ParsedType.get().getAsOpaquePtr(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 894 | TInfo->getTypeLoc().getBeginLoc()); |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 897 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 898 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 899 | /// the keyword "typename" was used to declare the type parameter |
| 900 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 901 | /// "class" or "typename" keyword. ParamName is the name of the |
| 902 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 903 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 904 | /// If the type parameter has a default argument, it will be added |
| 905 | /// later via ActOnTypeParameterDefault. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 906 | NamedDecl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 907 | SourceLocation EllipsisLoc, |
| 908 | SourceLocation KeyLoc, |
| 909 | IdentifierInfo *ParamName, |
| 910 | SourceLocation ParamNameLoc, |
| 911 | unsigned Depth, unsigned Position, |
| 912 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 913 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | assert(S->isTemplateParamScope() && |
| 915 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 916 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 917 | SourceLocation Loc = ParamNameLoc; |
| 918 | if (!ParamName) |
| 919 | Loc = KeyLoc; |
| 920 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 921 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 922 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 923 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 924 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 925 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 926 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 927 | |
| 928 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 929 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 930 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 931 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 932 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 933 | IdResolver.AddDecl(Param); |
| 934 | } |
| 935 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 936 | // C++0x [temp.param]p9: |
| 937 | // A default template-argument may be specified for any kind of |
| 938 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 939 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 940 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 941 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 944 | // Handle the default argument, if provided. |
| 945 | if (DefaultArg) { |
| 946 | TypeSourceInfo *DefaultTInfo; |
| 947 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 948 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 949 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 950 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 951 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 952 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 953 | UPPC_DefaultArgument)) |
| 954 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 955 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 956 | // Check the template argument itself. |
| 957 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 958 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 959 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 960 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 961 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 962 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 963 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 964 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 965 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 968 | /// Check that the type of a non-type template parameter is |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 969 | /// well-formed. |
| 970 | /// |
| 971 | /// \returns the (possibly-promoted) parameter type if valid; |
| 972 | /// otherwise, produces a diagnostic and returns a NULL type. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 973 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
| 974 | SourceLocation Loc) { |
| 975 | if (TSI->getType()->isUndeducedType()) { |
Erik Pilkington | 9f9462a | 2018-08-07 22:59:02 +0000 | [diff] [blame] | 976 | // C++17 [temp.dep.expr]p3: |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 977 | // An id-expression is type-dependent if it contains |
| 978 | // - an identifier associated by name lookup with a non-type |
| 979 | // template-parameter declared with a type that contains a |
| 980 | // placeholder type (7.1.7.4), |
| 981 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
| 982 | } |
| 983 | |
| 984 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
| 985 | } |
| 986 | |
| 987 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
| 988 | SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 989 | // We don't allow variably-modified types as the type of non-type template |
| 990 | // parameters. |
| 991 | if (T->isVariablyModifiedType()) { |
| 992 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 993 | << T; |
| 994 | return QualType(); |
| 995 | } |
| 996 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 997 | // C++ [temp.param]p4: |
| 998 | // |
| 999 | // A non-type template-parameter shall have one of the following |
| 1000 | // (optionally cv-qualified) types: |
| 1001 | // |
| 1002 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1003 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1005 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1007 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1008 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1009 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1010 | // -- std::nullptr_t. |
| 1011 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1012 | // If T is a dependent type, we can't do the check now, so we |
| 1013 | // assume that it is well-formed. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1014 | T->isDependentType() || |
| 1015 | // Allow use of auto in template parameter declarations. |
| 1016 | T->isUndeducedType()) { |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 1017 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 1018 | // are ignored when determining its type. |
| 1019 | return T.getUnqualifiedType(); |
| 1020 | } |
| 1021 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1022 | // C++ [temp.param]p8: |
| 1023 | // |
| 1024 | // A non-type template-parameter of type "array of T" or |
| 1025 | // "function returning T" is adjusted to be of type "pointer to |
| 1026 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1027 | else if (T->isArrayType() || T->isFunctionType()) |
| 1028 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1029 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1030 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 1031 | << T; |
| 1032 | |
| 1033 | return QualType(); |
| 1034 | } |
| 1035 | |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1036 | NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1037 | unsigned Depth, |
| 1038 | unsigned Position, |
| 1039 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1040 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1041 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1042 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1043 | // Check that we have valid decl-specifiers specified. |
| 1044 | auto CheckValidDeclSpecifiers = [this, &D] { |
| 1045 | // C++ [temp.param] |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1046 | // p1 |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 1047 | // template-parameter: |
| 1048 | // ... |
| 1049 | // parameter-declaration |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1050 | // p2 |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1051 | // ... A storage class shall not be specified in a template-parameter |
| 1052 | // declaration. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1053 | // [dcl.typedef]p1: |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1054 | // The typedef specifier [...] shall not be used in the decl-specifier-seq |
| 1055 | // of a parameter-declaration |
| 1056 | const DeclSpec &DS = D.getDeclSpec(); |
| 1057 | auto EmitDiag = [this](SourceLocation Loc) { |
| 1058 | Diag(Loc, diag::err_invalid_decl_specifier_in_nontype_parm) |
| 1059 | << FixItHint::CreateRemoval(Loc); |
| 1060 | }; |
| 1061 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) |
| 1062 | EmitDiag(DS.getStorageClassSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1063 | |
Sam McCall | 1371cba | 2017-12-22 07:09:51 +0000 | [diff] [blame] | 1064 | if (DS.getThreadStorageClassSpec() != TSCS_unspecified) |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1065 | EmitDiag(DS.getThreadStorageClassSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1066 | |
| 1067 | // [dcl.inline]p1: |
| 1068 | // The inline specifier can be applied only to the declaration or |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1069 | // definition of a variable or function. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1070 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1071 | if (DS.isInlineSpecified()) |
| 1072 | EmitDiag(DS.getInlineSpecLoc()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1073 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1074 | // [dcl.constexpr]p1: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1075 | // The constexpr specifier shall be applied only to the definition of a |
| 1076 | // variable or variable template or the declaration of a function or |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1077 | // function template. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1078 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1079 | if (DS.isConstexprSpecified()) |
| 1080 | EmitDiag(DS.getConstexprSpecLoc()); |
| 1081 | |
| 1082 | // [dcl.fct.spec]p1: |
| 1083 | // Function-specifiers can be used only in function declarations. |
| 1084 | |
| 1085 | if (DS.isVirtualSpecified()) |
| 1086 | EmitDiag(DS.getVirtualSpecLoc()); |
| 1087 | |
| 1088 | if (DS.isExplicitSpecified()) |
| 1089 | EmitDiag(DS.getExplicitSpecLoc()); |
| 1090 | |
| 1091 | if (DS.isNoreturnSpecified()) |
| 1092 | EmitDiag(DS.getNoreturnSpecLoc()); |
| 1093 | }; |
| 1094 | |
| 1095 | CheckValidDeclSpecifiers(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1096 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1097 | if (TInfo->getType()->isUndeducedType()) { |
| 1098 | Diag(D.getIdentifierLoc(), |
| 1099 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 1100 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
| 1101 | } |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1103 | assert(S->isTemplateParamScope() && |
| 1104 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1105 | bool Invalid = false; |
| 1106 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1107 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1108 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1109 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 1110 | Invalid = true; |
| 1111 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1112 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1113 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1114 | bool IsParameterPack = D.hasEllipsis(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1115 | NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create( |
| 1116 | Context, Context.getTranslationUnitDecl(), D.getBeginLoc(), |
| 1117 | D.getIdentifierLoc(), Depth, Position, ParamName, T, IsParameterPack, |
| 1118 | TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1119 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1120 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1121 | if (Invalid) |
| 1122 | Param->setInvalidDecl(); |
| 1123 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1124 | if (ParamName) { |
| 1125 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 1126 | ParamName); |
| 1127 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1128 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1129 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1130 | IdResolver.AddDecl(Param); |
| 1131 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1133 | // C++0x [temp.param]p9: |
| 1134 | // A default template-argument may be specified for any kind of |
| 1135 | // template-parameter that is not a template parameter pack. |
| 1136 | if (Default && IsParameterPack) { |
| 1137 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1138 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1141 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1142 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1143 | // Check for unexpanded parameter packs. |
| 1144 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 1145 | return Param; |
| 1146 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1147 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1148 | ExprResult DefaultRes = |
| 1149 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1150 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1151 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1152 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1153 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1154 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1155 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1156 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1157 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1158 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1159 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1160 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1161 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1162 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1163 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1164 | /// has been parsed. S is the current scope. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1165 | NamedDecl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1166 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1167 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1168 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1169 | IdentifierInfo *Name, |
| 1170 | SourceLocation NameLoc, |
| 1171 | unsigned Depth, |
| 1172 | unsigned Position, |
| 1173 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1174 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1175 | assert(S->isTemplateParamScope() && |
| 1176 | "Template template parameter not in template parameter scope!"); |
| 1177 | |
| 1178 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1179 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1180 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 1181 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1182 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 1183 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1184 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1185 | Param->setAccess(AS_public); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1186 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1187 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1188 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1189 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1190 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 1191 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1192 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1193 | IdResolver.AddDecl(Param); |
| 1194 | } |
| 1195 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1196 | if (Params->size() == 0) { |
| 1197 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 1198 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 1199 | Param->setInvalidDecl(); |
| 1200 | } |
| 1201 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1202 | // C++0x [temp.param]p9: |
| 1203 | // A default template-argument may be specified for any kind of |
| 1204 | // template-parameter that is not a template parameter pack. |
| 1205 | if (IsParameterPack && !Default.isInvalid()) { |
| 1206 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 1207 | Default = ParsedTemplateArgument(); |
| 1208 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1209 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1210 | if (!Default.isInvalid()) { |
| 1211 | // Check only that we have a template template argument. We don't want to |
| 1212 | // try to check well-formedness now, because our template template parameter |
| 1213 | // might have dependent types in its template parameters, which we wouldn't |
| 1214 | // be able to match now. |
| 1215 | // |
| 1216 | // If none of the template template parameter's template arguments mention |
| 1217 | // other template parameters, we could actually perform more checking here. |
| 1218 | // However, it isn't worth doing. |
| 1219 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 1220 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 1221 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1222 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1223 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1224 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1225 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1226 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1227 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1228 | DefaultArg.getArgument().getAsTemplate(), |
| 1229 | UPPC_DefaultArgument)) |
| 1230 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1231 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1232 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1233 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1234 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1235 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1238 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 1239 | /// constrained by RequiresClause, that contains the template parameters in |
| 1240 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1241 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1242 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 1243 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1245 | SourceLocation LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1246 | ArrayRef<NamedDecl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1247 | SourceLocation RAngleLoc, |
| 1248 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1249 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1250 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1251 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1252 | return TemplateParameterList::Create( |
| 1253 | Context, TemplateLoc, LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1254 | llvm::makeArrayRef(Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 1255 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1256 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1257 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1258 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 1259 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1260 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
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 | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1490 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1491 | assert(Tmpl && "original definition of a class template is not a " |
| 1492 | "class template?"); |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 1493 | makeMergedDefinitionVisible(Hidden); |
| 1494 | makeMergedDefinitionVisible(Tmpl); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1495 | return Def; |
| 1496 | } |
| 1497 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1498 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1499 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1500 | // FIXME: Would it make sense to try to "forget" the previous |
| 1501 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1502 | return true; |
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, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1523 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1524 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1525 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1526 | SemanticContext->isDependentContext()) |
| 1527 | ? TPC_ClassTemplateMember |
| 1528 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1529 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1530 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1532 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1533 | // 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] | 1534 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1535 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1536 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1537 | : diag::err_member_decl_does_not_match) |
| 1538 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1539 | Invalid = true; |
| 1540 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1543 | // If this is a templated friend in a dependent context we should not put it |
| 1544 | // on the redecl chain. In some cases, the templated friend can be the most |
| 1545 | // recent declaration tricking the template instantiator to make substitutions |
| 1546 | // there. |
| 1547 | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
| 1548 | bool ShouldAddRedecl |
| 1549 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
| 1550 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1552 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1553 | PrevClassTemplate && ShouldAddRedecl ? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1554 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1555 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1556 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1557 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1558 | NewClass->setTemplateParameterListsInfo( |
| 1559 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1560 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1561 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1562 | // Add alignment attributes if necessary; these attributes are checked when |
| 1563 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1564 | if (TUK == TUK_Definition) { |
| 1565 | AddAlignmentAttributesForRecord(NewClass); |
| 1566 | AddMsStructLayoutForRecord(NewClass); |
| 1567 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1568 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1569 | // Attach the associated constraints when the declaration will not be part of |
| 1570 | // a decl chain. |
| 1571 | Expr *const ACtoAttach = |
| 1572 | PrevClassTemplate && ShouldAddRedecl ? nullptr : CurAC; |
| 1573 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1574 | ClassTemplateDecl *NewTemplate |
| 1575 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1576 | DeclarationName(Name), TemplateParams, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1577 | NewClass, ACtoAttach); |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1578 | |
| 1579 | if (ShouldAddRedecl) |
| 1580 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
| 1581 | |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1582 | NewClass->setDescribedClassTemplate(NewTemplate); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1583 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1584 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1585 | NewTemplate->setModulePrivate(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1587 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1588 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1589 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1590 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1591 | (void)T; |
| 1592 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1593 | // 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] | 1594 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1595 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1596 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1597 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1598 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1599 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1600 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1601 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1603 | // Set the lexical context of these templates |
| 1604 | NewClass->setLexicalDeclContext(CurContext); |
| 1605 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1606 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1607 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1608 | NewClass->startDefinition(); |
| 1609 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1610 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1611 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1612 | if (PrevClassTemplate) |
| 1613 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1614 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1615 | AddPushedVisibilityAttribute(NewClass); |
| 1616 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1617 | if (TUK != TUK_Friend) { |
| 1618 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1619 | Scope *Outer = S; |
| 1620 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1621 | Outer = Outer->getParent(); |
| 1622 | PushOnScopeChains(NewTemplate, Outer); |
| 1623 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1624 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1625 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1626 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1627 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1628 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1629 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1630 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1631 | // Friend templates are visible in fairly strange ways. |
| 1632 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1633 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1634 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1635 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1636 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1637 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1638 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1639 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1640 | FriendDecl *Friend = FriendDecl::Create( |
| 1641 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1642 | Friend->setAccess(AS_public); |
| 1643 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1644 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1645 | |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1646 | if (PrevClassTemplate) |
| 1647 | CheckRedeclarationModuleOwnership(NewTemplate, PrevClassTemplate); |
| 1648 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1649 | if (Invalid) { |
| 1650 | NewTemplate->setInvalidDecl(); |
| 1651 | NewClass->setInvalidDecl(); |
| 1652 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1653 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1654 | ActOnDocumentableDecl(NewTemplate); |
| 1655 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1656 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1659 | namespace { |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1660 | /// Tree transform to "extract" a transformed type from a class template's |
| 1661 | /// constructor to a deduction guide. |
| 1662 | class ExtractTypeForDeductionGuide |
| 1663 | : public TreeTransform<ExtractTypeForDeductionGuide> { |
| 1664 | public: |
| 1665 | typedef TreeTransform<ExtractTypeForDeductionGuide> Base; |
| 1666 | ExtractTypeForDeductionGuide(Sema &SemaRef) : Base(SemaRef) {} |
| 1667 | |
| 1668 | TypeSourceInfo *transform(TypeSourceInfo *TSI) { return TransformType(TSI); } |
| 1669 | |
| 1670 | QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) { |
| 1671 | return TransformType( |
| 1672 | TLB, |
| 1673 | TL.getTypedefNameDecl()->getTypeSourceInfo()->getTypeLoc()); |
| 1674 | } |
| 1675 | }; |
| 1676 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1677 | /// Transform to convert portions of a constructor declaration into the |
| 1678 | /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1. |
| 1679 | struct ConvertConstructorToDeductionGuideTransform { |
| 1680 | ConvertConstructorToDeductionGuideTransform(Sema &S, |
| 1681 | ClassTemplateDecl *Template) |
| 1682 | : SemaRef(S), Template(Template) {} |
| 1683 | |
| 1684 | Sema &SemaRef; |
| 1685 | ClassTemplateDecl *Template; |
| 1686 | |
| 1687 | DeclContext *DC = Template->getDeclContext(); |
| 1688 | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
| 1689 | DeclarationName DeductionGuideName = |
| 1690 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
| 1691 | |
| 1692 | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
| 1693 | |
| 1694 | // Index adjustment to apply to convert depth-1 template parameters into |
| 1695 | // depth-0 template parameters. |
| 1696 | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
| 1697 | |
| 1698 | /// Transform a constructor declaration into a deduction guide. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1699 | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
| 1700 | CXXConstructorDecl *CD) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1701 | SmallVector<TemplateArgument, 16> SubstArgs; |
| 1702 | |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1703 | LocalInstantiationScope Scope(SemaRef); |
| 1704 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1705 | // C++ [over.match.class.deduct]p1: |
| 1706 | // -- For each constructor of the class template designated by the |
| 1707 | // template-name, a function template with the following properties: |
| 1708 | |
| 1709 | // -- The template parameters are the template parameters of the class |
| 1710 | // template followed by the template parameters (including default |
| 1711 | // template arguments) of the constructor, if any. |
| 1712 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 1713 | if (FTD) { |
| 1714 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
| 1715 | SmallVector<NamedDecl *, 16> AllParams; |
| 1716 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
| 1717 | AllParams.insert(AllParams.begin(), |
| 1718 | TemplateParams->begin(), TemplateParams->end()); |
| 1719 | SubstArgs.reserve(InnerParams->size()); |
| 1720 | |
| 1721 | // Later template parameters could refer to earlier ones, so build up |
| 1722 | // a list of substituted template arguments as we go. |
| 1723 | for (NamedDecl *Param : *InnerParams) { |
| 1724 | MultiLevelTemplateArgumentList Args; |
| 1725 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1726 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1727 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
| 1728 | if (!NewParam) |
| 1729 | return nullptr; |
| 1730 | AllParams.push_back(NewParam); |
| 1731 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
| 1732 | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
| 1733 | } |
| 1734 | TemplateParams = TemplateParameterList::Create( |
| 1735 | SemaRef.Context, InnerParams->getTemplateLoc(), |
| 1736 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
| 1737 | /*FIXME: RequiresClause*/ nullptr); |
| 1738 | } |
| 1739 | |
| 1740 | // If we built a new template-parameter-list, track that we need to |
| 1741 | // substitute references to the old parameters into references to the |
| 1742 | // new ones. |
| 1743 | MultiLevelTemplateArgumentList Args; |
| 1744 | if (FTD) { |
| 1745 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1746 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1749 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1750 | .getAsAdjusted<FunctionProtoTypeLoc>(); |
| 1751 | assert(FPTL && "no prototype for constructor declaration"); |
| 1752 | |
| 1753 | // Transform the type of the function, adjusting the return type and |
| 1754 | // replacing references to the old parameters with references to the |
| 1755 | // new ones. |
| 1756 | TypeLocBuilder TLB; |
| 1757 | SmallVector<ParmVarDecl*, 8> Params; |
| 1758 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args); |
| 1759 | if (NewType.isNull()) |
| 1760 | return nullptr; |
| 1761 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
| 1762 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1763 | return buildDeductionGuide(TemplateParams, CD->isExplicit(), NewTInfo, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1764 | CD->getBeginLoc(), CD->getLocation(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1765 | CD->getEndLoc()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | /// Build a deduction guide with the specified parameter types. |
| 1769 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
| 1770 | SourceLocation Loc = Template->getLocation(); |
| 1771 | |
| 1772 | // Build the requested type. |
| 1773 | FunctionProtoType::ExtProtoInfo EPI; |
| 1774 | EPI.HasTrailingReturn = true; |
| 1775 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
| 1776 | DeductionGuideName, EPI); |
| 1777 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
| 1778 | |
| 1779 | FunctionProtoTypeLoc FPTL = |
| 1780 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
| 1781 | |
| 1782 | // Build the parameters, needed during deduction / substitution. |
| 1783 | SmallVector<ParmVarDecl*, 4> Params; |
| 1784 | for (auto T : ParamTypes) { |
| 1785 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
| 1786 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
| 1787 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
| 1788 | NewParam->setScopeInfo(0, Params.size()); |
| 1789 | FPTL.setParam(Params.size(), NewParam); |
| 1790 | Params.push_back(NewParam); |
| 1791 | } |
| 1792 | |
| 1793 | return buildDeductionGuide(Template->getTemplateParameters(), false, TSI, |
| 1794 | Loc, Loc, Loc); |
| 1795 | } |
| 1796 | |
| 1797 | private: |
| 1798 | /// Transform a constructor template parameter into a deduction guide template |
| 1799 | /// parameter, rebuilding any internal references to earlier parameters and |
| 1800 | /// renumbering as we go. |
| 1801 | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
| 1802 | MultiLevelTemplateArgumentList &Args) { |
| 1803 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
| 1804 | // TemplateTypeParmDecl's index cannot be changed after creation, so |
| 1805 | // substitute it directly. |
| 1806 | auto *NewTTP = TemplateTypeParmDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1807 | SemaRef.Context, DC, TTP->getBeginLoc(), TTP->getLocation(), |
| 1808 | /*Depth*/ 0, Depth1IndexAdjustment + TTP->getIndex(), |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1809 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
| 1810 | TTP->isParameterPack()); |
| 1811 | if (TTP->hasDefaultArgument()) { |
| 1812 | TypeSourceInfo *InstantiatedDefaultArg = |
| 1813 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
| 1814 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
| 1815 | if (InstantiatedDefaultArg) |
| 1816 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
| 1817 | } |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1818 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
| 1819 | NewTTP); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1820 | return NewTTP; |
| 1821 | } |
| 1822 | |
| 1823 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
| 1824 | return transformTemplateParameterImpl(TTP, Args); |
| 1825 | |
| 1826 | return transformTemplateParameterImpl( |
| 1827 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
| 1828 | } |
| 1829 | template<typename TemplateParmDecl> |
| 1830 | TemplateParmDecl * |
| 1831 | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
| 1832 | MultiLevelTemplateArgumentList &Args) { |
| 1833 | // Ask the template instantiator to do the heavy lifting for us, then adjust |
| 1834 | // the index of the parameter once it's done. |
| 1835 | auto *NewParam = |
| 1836 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
| 1837 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
| 1838 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
| 1839 | return NewParam; |
| 1840 | } |
| 1841 | |
| 1842 | QualType transformFunctionProtoType(TypeLocBuilder &TLB, |
| 1843 | FunctionProtoTypeLoc TL, |
| 1844 | SmallVectorImpl<ParmVarDecl*> &Params, |
| 1845 | MultiLevelTemplateArgumentList &Args) { |
| 1846 | SmallVector<QualType, 4> ParamTypes; |
| 1847 | const FunctionProtoType *T = TL.getTypePtr(); |
| 1848 | |
| 1849 | // -- The types of the function parameters are those of the constructor. |
| 1850 | for (auto *OldParam : TL.getParams()) { |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1851 | ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1852 | if (!NewParam) |
| 1853 | return QualType(); |
| 1854 | ParamTypes.push_back(NewParam->getType()); |
| 1855 | Params.push_back(NewParam); |
| 1856 | } |
| 1857 | |
| 1858 | // -- The return type is the class template specialization designated by |
| 1859 | // the template-name and template arguments corresponding to the |
| 1860 | // template parameters obtained from the class template. |
| 1861 | // |
| 1862 | // We use the injected-class-name type of the primary template instead. |
| 1863 | // This has the convenient property that it is different from any type that |
| 1864 | // the user can write in a deduction-guide (because they cannot enter the |
| 1865 | // context of the template), so implicit deduction guides can never collide |
| 1866 | // with explicit ones. |
| 1867 | QualType ReturnType = DeducedType; |
| 1868 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
| 1869 | |
| 1870 | // Resolving a wording defect, we also inherit the variadicness of the |
| 1871 | // constructor. |
| 1872 | FunctionProtoType::ExtProtoInfo EPI; |
| 1873 | EPI.Variadic = T->isVariadic(); |
| 1874 | EPI.HasTrailingReturn = true; |
| 1875 | |
| 1876 | QualType Result = SemaRef.BuildFunctionType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1877 | ReturnType, ParamTypes, TL.getBeginLoc(), DeductionGuideName, EPI); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1878 | if (Result.isNull()) |
| 1879 | return QualType(); |
| 1880 | |
| 1881 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
| 1882 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
| 1883 | NewTL.setLParenLoc(TL.getLParenLoc()); |
| 1884 | NewTL.setRParenLoc(TL.getRParenLoc()); |
| 1885 | NewTL.setExceptionSpecRange(SourceRange()); |
| 1886 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
| 1887 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I) |
| 1888 | NewTL.setParam(I, Params[I]); |
| 1889 | |
| 1890 | return Result; |
| 1891 | } |
| 1892 | |
| 1893 | ParmVarDecl * |
| 1894 | transformFunctionTypeParam(ParmVarDecl *OldParam, |
| 1895 | MultiLevelTemplateArgumentList &Args) { |
| 1896 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
Richard Smith | 479ba8e | 2017-04-20 01:15:31 +0000 | [diff] [blame] | 1897 | TypeSourceInfo *NewDI; |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1898 | if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { |
Richard Smith | 479ba8e | 2017-04-20 01:15:31 +0000 | [diff] [blame] | 1899 | // Expand out the one and only element in each inner pack. |
| 1900 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); |
| 1901 | NewDI = |
| 1902 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, |
| 1903 | OldParam->getLocation(), OldParam->getDeclName()); |
| 1904 | if (!NewDI) return nullptr; |
| 1905 | NewDI = |
| 1906 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), |
| 1907 | PackTL.getTypePtr()->getNumExpansions()); |
| 1908 | } else |
| 1909 | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
| 1910 | OldParam->getDeclName()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1911 | if (!NewDI) |
| 1912 | return nullptr; |
| 1913 | |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1914 | // Extract the type. This (for instance) replaces references to typedef |
| 1915 | // members of the current instantiations with the definitions of those |
| 1916 | // typedefs, avoiding triggering instantiation of the deduced type during |
| 1917 | // deduction. |
| 1918 | NewDI = ExtractTypeForDeductionGuide(SemaRef).transform(NewDI); |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1919 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1920 | // Resolving a wording defect, we also inherit default arguments from the |
| 1921 | // constructor. |
| 1922 | ExprResult NewDefArg; |
| 1923 | if (OldParam->hasDefaultArg()) { |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1924 | NewDefArg = SemaRef.SubstExpr(OldParam->getDefaultArg(), Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1925 | if (NewDefArg.isInvalid()) |
| 1926 | return nullptr; |
| 1927 | } |
| 1928 | |
| 1929 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
| 1930 | OldParam->getInnerLocStart(), |
| 1931 | OldParam->getLocation(), |
| 1932 | OldParam->getIdentifier(), |
| 1933 | NewDI->getType(), |
| 1934 | NewDI, |
| 1935 | OldParam->getStorageClass(), |
| 1936 | NewDefArg.get()); |
| 1937 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
| 1938 | OldParam->getFunctionScopeIndex()); |
Erik Pilkington | 69770d3 | 2018-07-27 21:23:48 +0000 | [diff] [blame] | 1939 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1940 | return NewParam; |
| 1941 | } |
| 1942 | |
| 1943 | NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams, |
| 1944 | bool Explicit, TypeSourceInfo *TInfo, |
| 1945 | SourceLocation LocStart, SourceLocation Loc, |
| 1946 | SourceLocation LocEnd) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1947 | DeclarationNameInfo Name(DeductionGuideName, Loc); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1948 | ArrayRef<ParmVarDecl *> Params = |
| 1949 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
| 1950 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1951 | // Build the implicit deduction guide template. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1952 | auto *Guide = |
| 1953 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, Explicit, |
| 1954 | Name, TInfo->getType(), TInfo, LocEnd); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1955 | Guide->setImplicit(); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1956 | Guide->setParams(Params); |
| 1957 | |
| 1958 | for (auto *Param : Params) |
| 1959 | Param->setDeclContext(Guide); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1960 | |
| 1961 | auto *GuideTemplate = FunctionTemplateDecl::Create( |
| 1962 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
| 1963 | GuideTemplate->setImplicit(); |
| 1964 | Guide->setDescribedFunctionTemplate(GuideTemplate); |
| 1965 | |
| 1966 | if (isa<CXXRecordDecl>(DC)) { |
| 1967 | Guide->setAccess(AS_public); |
| 1968 | GuideTemplate->setAccess(AS_public); |
| 1969 | } |
| 1970 | |
| 1971 | DC->addDecl(GuideTemplate); |
| 1972 | return GuideTemplate; |
| 1973 | } |
| 1974 | }; |
| 1975 | } |
| 1976 | |
| 1977 | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
| 1978 | SourceLocation Loc) { |
| 1979 | DeclContext *DC = Template->getDeclContext(); |
| 1980 | if (DC->isDependentContext()) |
| 1981 | return; |
| 1982 | |
| 1983 | ConvertConstructorToDeductionGuideTransform Transform( |
| 1984 | *this, cast<ClassTemplateDecl>(Template)); |
| 1985 | if (!isCompleteType(Loc, Transform.DeducedType)) |
| 1986 | return; |
| 1987 | |
| 1988 | // Check whether we've already declared deduction guides for this template. |
| 1989 | // FIXME: Consider storing a flag on the template to indicate this. |
| 1990 | auto Existing = DC->lookup(Transform.DeductionGuideName); |
| 1991 | for (auto *D : Existing) |
| 1992 | if (D->isImplicit()) |
| 1993 | return; |
| 1994 | |
| 1995 | // In case we were expanding a pack when we attempted to declare deduction |
| 1996 | // guides, turn off pack expansion for everything we're about to do. |
| 1997 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
| 1998 | // Create a template instantiation record to track the "instantiation" of |
| 1999 | // constructors into deduction guides. |
| 2000 | // FIXME: Add a kind for this to give more meaningful diagnostics. But can |
| 2001 | // this substitution process actually fail? |
| 2002 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
Volodymyr Sapsai | 2f649f3 | 2018-05-14 22:49:44 +0000 | [diff] [blame] | 2003 | if (BuildingDeductionGuides.isInvalid()) |
| 2004 | return; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2005 | |
| 2006 | // Convert declared constructors into deduction guide templates. |
| 2007 | // FIXME: Skip constructors for which deduction must necessarily fail (those |
| 2008 | // for which some class template parameter without a default argument never |
| 2009 | // appears in a deduced context). |
| 2010 | bool AddedAny = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2011 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
| 2012 | D = D->getUnderlyingDecl(); |
| 2013 | if (D->isInvalidDecl() || D->isImplicit()) |
| 2014 | continue; |
| 2015 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 2016 | |
| 2017 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2018 | auto *CD = |
| 2019 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2020 | // Class-scope explicit specializations (MS extension) do not result in |
| 2021 | // deduction guides. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2022 | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization())) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2023 | continue; |
| 2024 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2025 | Transform.transformConstructor(FTD, CD); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2026 | AddedAny = true; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2029 | // C++17 [over.match.class.deduct] |
| 2030 | // -- If C is not defined or does not declare any constructors, an |
| 2031 | // additional function template derived as above from a hypothetical |
| 2032 | // constructor C(). |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2033 | if (!AddedAny) |
| 2034 | Transform.buildSimpleDeductionGuide(None); |
| 2035 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2036 | // -- An additional function template derived as above from a hypothetical |
| 2037 | // constructor C(C), called the copy deduction candidate. |
| 2038 | cast<CXXDeductionGuideDecl>( |
| 2039 | cast<FunctionTemplateDecl>( |
| 2040 | Transform.buildSimpleDeductionGuide(Transform.DeducedType)) |
| 2041 | ->getTemplatedDecl()) |
| 2042 | ->setIsCopyDeductionCandidate(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2045 | /// Diagnose the presence of a default template argument on a |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2046 | /// template parameter, which is ill-formed in certain contexts. |
| 2047 | /// |
| 2048 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2049 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2050 | Sema::TemplateParamListContext TPC, |
| 2051 | SourceLocation ParamLoc, |
| 2052 | SourceRange DefArgRange) { |
| 2053 | switch (TPC) { |
| 2054 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2055 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2056 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2057 | return false; |
| 2058 | |
| 2059 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2060 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2061 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2062 | // A default template-argument shall not be specified in a |
| 2063 | // function template declaration or a function template |
| 2064 | // definition [...] |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2065 | // If a friend function template declaration specifies a default |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2066 | // template-argument, that declaration shall be a definition and shall be |
| 2067 | // the only declaration of the function template in the translation unit. |
| 2068 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2069 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2070 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 2071 | : diag::ext_template_parameter_default_in_function_template) |
| 2072 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2073 | return false; |
| 2074 | |
| 2075 | case Sema::TPC_ClassTemplateMember: |
| 2076 | // C++0x [temp.param]p9: |
| 2077 | // A default template-argument shall not be specified in the |
| 2078 | // template-parameter-lists of the definition of a member of a |
| 2079 | // class template that appears outside of the member's class. |
| 2080 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 2081 | << DefArgRange; |
| 2082 | return true; |
| 2083 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 2084 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2085 | case Sema::TPC_FriendFunctionTemplate: |
| 2086 | // C++ [temp.param]p9: |
| 2087 | // A default template-argument shall not be specified in a |
| 2088 | // friend template declaration. |
| 2089 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 2090 | << DefArgRange; |
| 2091 | return true; |
| 2092 | |
| 2093 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 2094 | // for friend function templates if there is only a single |
| 2095 | // declaration (and it is a definition). Strange! |
| 2096 | } |
| 2097 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2098 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2101 | /// Check for unexpanded parameter packs within the template parameters |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2102 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 2103 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 2104 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2105 | // A template template parameter which is a parameter pack is also a pack |
| 2106 | // expansion. |
| 2107 | if (TTP->isParameterPack()) |
| 2108 | return false; |
| 2109 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2110 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 2111 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 2112 | NamedDecl *P = Params->getParam(I); |
| 2113 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2114 | if (!NTTP->isParameterPack() && |
| 2115 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2116 | NTTP->getTypeSourceInfo(), |
| 2117 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 2118 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2120 | continue; |
| 2121 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2122 | |
| 2123 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2124 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 2125 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 2126 | return true; |
| 2127 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2128 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2129 | return false; |
| 2130 | } |
| 2131 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2132 | /// Checks the validity of a template parameter list, possibly |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2133 | /// considering the template parameter list from a previous |
| 2134 | /// declaration. |
| 2135 | /// |
| 2136 | /// If an "old" template parameter list is provided, it must be |
| 2137 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 2138 | /// template parameter list. |
| 2139 | /// |
| 2140 | /// \param NewParams Template parameter list for a new template |
| 2141 | /// declaration. This template parameter list will be updated with any |
| 2142 | /// default arguments that are carried through from the previous |
| 2143 | /// template parameter list. |
| 2144 | /// |
| 2145 | /// \param OldParams If provided, template parameter list from a |
| 2146 | /// previous declaration of the same template. Default template |
| 2147 | /// arguments will be merged from the old template parameter list to |
| 2148 | /// the new template parameter list. |
| 2149 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2150 | /// \param TPC Describes the context in which we are checking the given |
| 2151 | /// template parameter list. |
| 2152 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2153 | /// \returns true if an error occurred, false otherwise. |
| 2154 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2155 | TemplateParameterList *OldParams, |
| 2156 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2157 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2159 | // C++ [temp.param]p10: |
| 2160 | // The set of default template-arguments available for use with a |
| 2161 | // template declaration or definition is obtained by merging the |
| 2162 | // default arguments from the definition (if in scope) and all |
| 2163 | // declarations in scope in the same way default function |
| 2164 | // arguments are (8.3.6). |
| 2165 | bool SawDefaultArgument = false; |
| 2166 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2167 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 2168 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 2169 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2170 | if (OldParams) |
| 2171 | OldParam = OldParams->begin(); |
| 2172 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2173 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2174 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2175 | NewParamEnd = NewParams->end(); |
| 2176 | NewParam != NewParamEnd; ++NewParam) { |
| 2177 | // Variables used to diagnose redundant default arguments |
| 2178 | bool RedundantDefaultArg = false; |
| 2179 | SourceLocation OldDefaultLoc; |
| 2180 | SourceLocation NewDefaultLoc; |
| 2181 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2182 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2183 | bool MissingDefaultArg = false; |
| 2184 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2185 | // Variable used to diagnose non-final parameter packs |
| 2186 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2187 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2188 | if (TemplateTypeParmDecl *NewTypeParm |
| 2189 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2190 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2191 | if (NewTypeParm->hasDefaultArgument() && |
| 2192 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2193 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2194 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2195 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2196 | NewTypeParm->removeDefaultArgument(); |
| 2197 | |
| 2198 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2200 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2201 | if (NewTypeParm->isParameterPack()) { |
| 2202 | assert(!NewTypeParm->hasDefaultArgument() && |
| 2203 | "Parameter packs can't have a default argument!"); |
| 2204 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2205 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2206 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2207 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2208 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2209 | SawDefaultArgument = true; |
| 2210 | RedundantDefaultArg = true; |
| 2211 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2212 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 2213 | // Merge the default argument from the old declaration to the |
| 2214 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2215 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2216 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2217 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 2218 | SawDefaultArgument = true; |
| 2219 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2220 | } else if (SawDefaultArgument) |
| 2221 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2222 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2223 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2224 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2225 | if (!NewNonTypeParm->isParameterPack() && |
| 2226 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2227 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2228 | UPPC_NonTypeTemplateParameterType)) { |
| 2229 | Invalid = true; |
| 2230 | continue; |
| 2231 | } |
| 2232 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2233 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2234 | if (NewNonTypeParm->hasDefaultArgument() && |
| 2235 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2236 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2237 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2238 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2241 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2242 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2243 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2244 | if (NewNonTypeParm->isParameterPack()) { |
| 2245 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 2246 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2247 | if (!NewNonTypeParm->isPackExpansion()) |
| 2248 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2249 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 2250 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2251 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2252 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2253 | SawDefaultArgument = true; |
| 2254 | RedundantDefaultArg = true; |
| 2255 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2256 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 2257 | // Merge the default argument from the old declaration to the |
| 2258 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2259 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2260 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2261 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 2262 | SawDefaultArgument = true; |
| 2263 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2264 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2266 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2267 | TemplateTemplateParmDecl *NewTemplateParm |
| 2268 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2269 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2270 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 2271 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2272 | Invalid = true; |
| 2273 | continue; |
| 2274 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2275 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2276 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2277 | if (NewTemplateParm->hasDefaultArgument() && |
| 2278 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2279 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2280 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2281 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2282 | |
| 2283 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2284 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2285 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2286 | if (NewTemplateParm->isParameterPack()) { |
| 2287 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 2288 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2289 | if (!NewTemplateParm->isPackExpansion()) |
| 2290 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2291 | } else if (OldTemplateParm && |
| 2292 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 2293 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2294 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 2295 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2296 | SawDefaultArgument = true; |
| 2297 | RedundantDefaultArg = true; |
| 2298 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2299 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 2300 | // Merge the default argument from the old declaration to the |
| 2301 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2302 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2303 | PreviousDefaultArgLoc |
| 2304 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2305 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 2306 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2307 | PreviousDefaultArgLoc |
| 2308 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2309 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2310 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2313 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2314 | // If a template parameter of a primary class template or alias template |
| 2315 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2316 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2317 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 2318 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2319 | Diag((*NewParam)->getLocation(), |
| 2320 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 2321 | Invalid = true; |
| 2322 | } |
| 2323 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2324 | if (RedundantDefaultArg) { |
| 2325 | // C++ [temp.param]p12: |
| 2326 | // A template-parameter shall not be given default arguments |
| 2327 | // by two different declarations in the same scope. |
| 2328 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 2329 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 2330 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 2331 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2332 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2333 | // If a template-parameter of a class template has a default |
| 2334 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 2335 | // have a default template-argument supplied or be a template parameter |
| 2336 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2337 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2338 | diag::err_template_param_default_arg_missing); |
| 2339 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 2340 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2341 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | // If we have an old template parameter list that we're merging |
| 2345 | // in, move on to the next parameter. |
| 2346 | if (OldParams) |
| 2347 | ++OldParam; |
| 2348 | } |
| 2349 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2350 | // We were missing some default arguments at the end of the list, so remove |
| 2351 | // all of the default arguments. |
| 2352 | if (RemoveDefaultArguments) { |
| 2353 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2354 | NewParamEnd = NewParams->end(); |
| 2355 | NewParam != NewParamEnd; ++NewParam) { |
| 2356 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 2357 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2358 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2359 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 2360 | NTTP->removeDefaultArgument(); |
| 2361 | else |
| 2362 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 2363 | } |
| 2364 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2365 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2366 | return Invalid; |
| 2367 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2368 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2369 | namespace { |
| 2370 | |
| 2371 | /// A class which looks for a use of a certain level of template |
| 2372 | /// parameter. |
| 2373 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 2374 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 2375 | |
| 2376 | unsigned Depth; |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2377 | |
| 2378 | // Whether we're looking for a use of a template parameter that makes the |
| 2379 | // overall construct type-dependent / a dependent type. This is strictly |
| 2380 | // best-effort for now; we may fail to match at all for a dependent type |
| 2381 | // in some cases if this is set. |
| 2382 | bool IgnoreNonTypeDependent; |
| 2383 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2384 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2385 | SourceLocation MatchLoc; |
| 2386 | |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2387 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
| 2388 | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
| 2389 | Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2390 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2391 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2392 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
| 2393 | NamedDecl *ND = Params->getParam(0); |
| 2394 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 2395 | Depth = PD->getDepth(); |
| 2396 | } else if (NonTypeTemplateParmDecl *PD = |
| 2397 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 2398 | Depth = PD->getDepth(); |
| 2399 | } else { |
| 2400 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 2401 | } |
| 2402 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2403 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2404 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2405 | if (ParmDepth >= Depth) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2406 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2407 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2408 | return true; |
| 2409 | } |
| 2410 | return false; |
| 2411 | } |
| 2412 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2413 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 2414 | // Prune out non-type-dependent expressions if requested. This can |
| 2415 | // sometimes result in us failing to find a template parameter reference |
| 2416 | // (if a value-dependent expression creates a dependent type), but this |
| 2417 | // mode is best-effort only. |
| 2418 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 2419 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 2420 | return true; |
| 2421 | return super::TraverseStmt(S, Q); |
| 2422 | } |
| 2423 | |
| 2424 | bool TraverseTypeLoc(TypeLoc TL) { |
| 2425 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 2426 | !TL.getType()->isDependentType()) |
| 2427 | return true; |
| 2428 | return super::TraverseTypeLoc(TL); |
| 2429 | } |
| 2430 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2431 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2432 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 2433 | } |
| 2434 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2435 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2436 | // For a best-effort search, keep looking until we find a location. |
| 2437 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | bool TraverseTemplateName(TemplateName N) { |
| 2441 | if (TemplateTemplateParmDecl *PD = |
| 2442 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2443 | if (Matches(PD->getDepth())) |
| 2444 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2445 | return super::TraverseTemplateName(N); |
| 2446 | } |
| 2447 | |
| 2448 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 2449 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2450 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 2451 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2452 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2453 | return super::VisitDeclRefExpr(E); |
| 2454 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2455 | |
| 2456 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 2457 | return TraverseType(T->getReplacementType()); |
| 2458 | } |
| 2459 | |
| 2460 | bool |
| 2461 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 2462 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 2463 | } |
| 2464 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 2465 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 2466 | return TraverseType(T->getInjectedSpecializationType()); |
| 2467 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2468 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2469 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2471 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2472 | /// list. |
| 2473 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2474 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2475 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2476 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2477 | return Checker.Match; |
| 2478 | } |
| 2479 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2480 | // Find the source range corresponding to the named type in the given |
| 2481 | // nested-name-specifier, if any. |
| 2482 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 2483 | QualType T, |
| 2484 | const CXXScopeSpec &SS) { |
| 2485 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 2486 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 2487 | if (const Type *CurType = NNS->getAsType()) { |
| 2488 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 2489 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 2490 | } else |
| 2491 | break; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2493 | NNSLoc = NNSLoc.getPrefix(); |
| 2494 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2495 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2496 | return SourceRange(); |
| 2497 | } |
| 2498 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2499 | /// Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2500 | /// specifier, returning the template parameter list that applies to the |
| 2501 | /// name. |
| 2502 | /// |
| 2503 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 2504 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2505 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2506 | /// \param DeclLoc The location of the declaration itself. |
| 2507 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2508 | /// \param SS the scope specifier that will be matched to the given template |
| 2509 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 2510 | /// being declared. |
| 2511 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2512 | /// \param TemplateId The template-id following the scope specifier, if there |
| 2513 | /// is one. Used to check for a missing 'template<>'. |
| 2514 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2515 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 2516 | /// innermost template parameter lists. |
| 2517 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2518 | /// \param IsFriend Whether to apply the slightly different rules for |
| 2519 | /// matching template parameters to scope specifiers in friend |
| 2520 | /// declarations. |
| 2521 | /// |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2522 | /// \param IsMemberSpecialization will be set true if the scope specifier |
| 2523 | /// denotes a fully-specialized type, and therefore this is a declaration of |
| 2524 | /// a member specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2525 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2527 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2528 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2529 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2530 | /// 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] | 2531 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2532 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 2533 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2534 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2535 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2536 | bool &IsMemberSpecialization, bool &Invalid) { |
| 2537 | IsMemberSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2538 | Invalid = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2539 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2540 | // The sequence of nested types to which we will match up the template |
| 2541 | // parameter lists. We first build this list by starting with the type named |
| 2542 | // 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] | 2543 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2544 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2545 | if (SS.getScopeRep()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2546 | if (CXXRecordDecl *Record |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2547 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 2548 | T = Context.getTypeDeclType(Record); |
| 2549 | else |
| 2550 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 2551 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2552 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2553 | // If we found an explicit specialization that prevents us from needing |
| 2554 | // 'template<>' headers, this will be set to the location of that |
| 2555 | // explicit specialization. |
| 2556 | SourceLocation ExplicitSpecLoc; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2558 | while (!T.isNull()) { |
| 2559 | NestedTypes.push_back(T); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2560 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2561 | // Retrieve the parent of a record type. |
| 2562 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2563 | // If this type is an explicit specialization, we're done. |
| 2564 | if (ClassTemplateSpecializationDecl *Spec |
| 2565 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2566 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2567 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 2568 | ExplicitSpecLoc = Spec->getLocation(); |
| 2569 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 2570 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2571 | } else if (Record->getTemplateSpecializationKind() |
| 2572 | == TSK_ExplicitSpecialization) { |
| 2573 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2574 | break; |
| 2575 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2576 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2577 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 2578 | T = Context.getTypeDeclType(Parent); |
| 2579 | else |
| 2580 | T = QualType(); |
| 2581 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2584 | if (const TemplateSpecializationType *TST |
| 2585 | = T->getAs<TemplateSpecializationType>()) { |
| 2586 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 2587 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 2588 | T = Context.getTypeDeclType(Parent); |
| 2589 | else |
| 2590 | T = QualType(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2591 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2592 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2593 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2594 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2595 | // Look one step prior in a dependent template specialization type. |
| 2596 | if (const DependentTemplateSpecializationType *DependentTST |
| 2597 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2598 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 2599 | T = QualType(NNS->getAsType(), 0); |
| 2600 | else |
| 2601 | T = QualType(); |
| 2602 | continue; |
| 2603 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2604 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2605 | // Look one step prior in a dependent name type. |
| 2606 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 2607 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 2608 | T = QualType(NNS->getAsType(), 0); |
| 2609 | else |
| 2610 | T = QualType(); |
| 2611 | continue; |
| 2612 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2613 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2614 | // Retrieve the parent of an enumeration type. |
| 2615 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 2616 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 2617 | // check here. |
| 2618 | EnumDecl *Enum = EnumT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2619 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2620 | // Get to the parent type. |
| 2621 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 2622 | T = Context.getTypeDeclType(Parent); |
| 2623 | else |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2624 | T = QualType(); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2625 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2626 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2628 | T = QualType(); |
| 2629 | } |
| 2630 | // Reverse the nested types list, since we want to traverse from the outermost |
| 2631 | // to the innermost while checking template-parameter-lists. |
| 2632 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2634 | // C++0x [temp.expl.spec]p17: |
| 2635 | // A member or a member template may be nested within many |
| 2636 | // enclosing class templates. In an explicit specialization for |
| 2637 | // such a member, the member declaration shall be preceded by a |
| 2638 | // template<> for each enclosing class template that is |
| 2639 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2640 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2641 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2642 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2643 | if (SawNonEmptyTemplateParameterList) { |
| 2644 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 2645 | << !Recovery << Range; |
| 2646 | Invalid = true; |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2647 | IsMemberSpecialization = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2648 | return true; |
| 2649 | } |
| 2650 | |
| 2651 | return false; |
| 2652 | }; |
| 2653 | |
| 2654 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 2655 | // Check that we can have an explicit specialization here. |
| 2656 | if (CheckExplicitSpecialization(Range, true)) |
| 2657 | return true; |
| 2658 | |
| 2659 | // We don't have a template header, but we should. |
| 2660 | SourceLocation ExpectedTemplateLoc; |
| 2661 | if (!ParamLists.empty()) |
| 2662 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 2663 | else |
| 2664 | ExpectedTemplateLoc = DeclStartLoc; |
| 2665 | |
| 2666 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 2667 | << Range |
| 2668 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 2669 | return false; |
| 2670 | }; |
| 2671 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2672 | unsigned ParamIdx = 0; |
| 2673 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 2674 | ++TypeIdx) { |
| 2675 | T = NestedTypes[TypeIdx]; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2676 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2677 | // Whether we expect a 'template<>' header. |
| 2678 | bool NeedEmptyTemplateHeader = false; |
| 2679 | |
| 2680 | // Whether we expect a template header with parameters. |
| 2681 | bool NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2682 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2683 | // For a dependent type, the set of template parameters that we |
| 2684 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2685 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2686 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2687 | // C++0x [temp.expl.spec]p15: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2688 | // A member or a member template may be nested within many enclosing |
| 2689 | // class templates. In an explicit specialization for such a member, the |
| 2690 | // member declaration shall be preceded by a template<> for each |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2691 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2692 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2693 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 2694 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 2695 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 2696 | NeedNonemptyTemplateHeader = true; |
| 2697 | } else if (Record->isDependentType()) { |
| 2698 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2699 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2700 | ->getTemplateParameters(); |
| 2701 | NeedNonemptyTemplateHeader = true; |
| 2702 | } |
| 2703 | } else if (ClassTemplateSpecializationDecl *Spec |
| 2704 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 2705 | // C++0x [temp.expl.spec]p4: |
| 2706 | // Members of an explicitly specialized class template are defined |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2707 | // in the same manner as members of normal classes, and not using |
| 2708 | // the template<> syntax. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2709 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 2710 | NeedEmptyTemplateHeader = true; |
| 2711 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 2712 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2713 | } else if (Record->getTemplateSpecializationKind()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2714 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2715 | != TSK_ExplicitSpecialization && |
| 2716 | TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2717 | IsMemberSpecialization = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2718 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2719 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2720 | } |
| 2721 | } else if (const TemplateSpecializationType *TST |
| 2722 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 2723 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2724 | ExpectedTemplateParams = Template->getTemplateParameters(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2725 | NeedNonemptyTemplateHeader = true; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2726 | } |
| 2727 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2728 | // FIXME: We actually could/should check the template arguments here |
| 2729 | // against the corresponding template parameter list. |
| 2730 | NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2733 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2734 | // In an explicit specialization declaration for a member of a class |
| 2735 | // template or a member template that ap- pears in namespace scope, the |
| 2736 | // member template and some of its enclosing class templates may remain |
| 2737 | // unspecialized, except that the declaration shall not explicitly |
| 2738 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2739 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2740 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2741 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2742 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2743 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2744 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2745 | } else |
| 2746 | SawNonEmptyTemplateParameterList = true; |
| 2747 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2749 | if (NeedEmptyTemplateHeader) { |
| 2750 | // 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] | 2751 | // here, then it's a member specialization. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2752 | if (TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2753 | IsMemberSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2754 | |
| 2755 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2756 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2757 | // The header has template parameters when it shouldn't. Complain. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2758 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2759 | diag::err_template_param_list_matches_nontemplate) |
| 2760 | << T |
| 2761 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2762 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2763 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2764 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2765 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2766 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2767 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2768 | // Consume this template header. |
| 2769 | ++ParamIdx; |
| 2770 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2771 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2772 | |
| 2773 | if (!IsFriend) |
| 2774 | if (DiagnoseMissingExplicitSpecialization( |
| 2775 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2776 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2777 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2778 | continue; |
| 2779 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2780 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2781 | if (NeedNonemptyTemplateHeader) { |
| 2782 | // In friend declarations we can have template-ids which don't |
| 2783 | // depend on the corresponding template parameter lists. But |
| 2784 | // assume that empty parameter lists are supposed to match this |
| 2785 | // template-id. |
| 2786 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2787 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2788 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2789 | ExpectedTemplateParams = nullptr; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2790 | else |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2791 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2793 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2794 | if (ParamIdx < ParamLists.size()) { |
| 2795 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2796 | if (ExpectedTemplateParams && |
| 2797 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2798 | ExpectedTemplateParams, |
| 2799 | true, TPL_TemplateMatch)) |
| 2800 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2802 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2803 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2804 | TPC_ClassTemplateMember)) |
| 2805 | Invalid = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2806 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2807 | ++ParamIdx; |
| 2808 | continue; |
| 2809 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2810 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2811 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2812 | << T |
| 2813 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2814 | Invalid = true; |
| 2815 | continue; |
| 2816 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2817 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2818 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2819 | // If there were at least as many template-ids as there were template |
| 2820 | // parameter lists, then there are no template parameter lists remaining for |
| 2821 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2822 | if (ParamIdx >= ParamLists.size()) { |
| 2823 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2824 | // We don't have a template header for the declaration itself, but we |
| 2825 | // should. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2826 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2827 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2828 | |
| 2829 | // Fabricate an empty template parameter list for the invented header. |
| 2830 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2831 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2832 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2835 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2836 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2838 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2839 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2840 | bool HasAnyExplicitSpecHeader = false; |
| 2841 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2842 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2843 | if (ParamLists[I]->size() == 0) |
| 2844 | HasAnyExplicitSpecHeader = true; |
| 2845 | else |
| 2846 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2847 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2848 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2849 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2850 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2851 | : diag::err_template_spec_extra_headers) |
| 2852 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2853 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2854 | |
| 2855 | // If there was a specialization somewhere, such that 'template<>' is |
| 2856 | // not required, and there were any 'template<>' headers, note where the |
| 2857 | // specialization occurred. |
| 2858 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2859 | Diag(ExplicitSpecLoc, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2860 | diag::note_explicit_template_spec_does_not_need_header) |
| 2861 | << NestedTypes.back(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2863 | // We have a template parameter list with no corresponding scope, which |
| 2864 | // means that the resulting template declaration can't be instantiated |
| 2865 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2866 | if (!AllExplicitSpecHeaders) |
| 2867 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2868 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2869 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2870 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2871 | // In an explicit specialization declaration for a member of a class |
| 2872 | // template or a member template that ap- pears in namespace scope, the |
| 2873 | // member template and some of its enclosing class templates may remain |
| 2874 | // unspecialized, except that the declaration shall not explicitly |
| 2875 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2876 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2877 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2878 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2879 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2880 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2881 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2882 | // Return the last template parameter list, which corresponds to the |
| 2883 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2884 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2887 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2888 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2889 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2890 | << (isa<FunctionTemplateDecl>(Template) |
| 2891 | ? 0 |
| 2892 | : isa<ClassTemplateDecl>(Template) |
| 2893 | ? 1 |
| 2894 | : isa<VarTemplateDecl>(Template) |
| 2895 | ? 2 |
| 2896 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2897 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2898 | return; |
| 2899 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2900 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2901 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2902 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2903 | IEnd = OST->end(); |
| 2904 | I != IEnd; ++I) |
| 2905 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2906 | << 0 << (*I)->getDeclName(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2907 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2908 | return; |
| 2909 | } |
| 2910 | } |
| 2911 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2912 | static QualType |
| 2913 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2914 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2915 | SourceLocation TemplateLoc, |
| 2916 | TemplateArgumentListInfo &TemplateArgs) { |
| 2917 | ASTContext &Context = SemaRef.getASTContext(); |
| 2918 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2919 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2920 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2921 | // S<T, 0, ..., N-1>. |
| 2922 | |
| 2923 | // C++14 [inteseq.intseq]p1: |
| 2924 | // T shall be an integer type. |
| 2925 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2926 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2927 | diag::err_integer_sequence_integral_element_type); |
| 2928 | return QualType(); |
| 2929 | } |
| 2930 | |
| 2931 | // C++14 [inteseq.make]p1: |
| 2932 | // If N is negative the program is ill-formed. |
| 2933 | TemplateArgument NumArgsArg = Converted[2]; |
| 2934 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2935 | if (NumArgs < 0) { |
| 2936 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2937 | diag::err_integer_sequence_negative_length); |
| 2938 | return QualType(); |
| 2939 | } |
| 2940 | |
| 2941 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2942 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2943 | // The type argument gets reused as the first template argument in the |
| 2944 | // synthetic template argument list. |
| 2945 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2946 | // Expand N into 0 ... N-1. |
| 2947 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2948 | I < NumArgs; ++I) { |
| 2949 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2950 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2951 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2952 | } |
| 2953 | // The first template argument will be reused as the template decl that |
| 2954 | // our synthetic template arguments will be applied to. |
| 2955 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2956 | TemplateLoc, SyntheticTemplateArgs); |
| 2957 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2958 | |
| 2959 | case BTK__type_pack_element: |
| 2960 | // Specializations of |
| 2961 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2962 | // are treated like T_Index. |
| 2963 | assert(Converted.size() == 2 && |
| 2964 | "__type_pack_element should be given an index and a parameter pack"); |
| 2965 | |
| 2966 | // If the Index is out of bounds, the program is ill-formed. |
| 2967 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2968 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2969 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2970 | "type std::size_t, and hence be non-negative"); |
| 2971 | if (Index >= Ts.pack_size()) { |
| 2972 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2973 | diag::err_type_pack_element_out_of_bounds); |
| 2974 | return QualType(); |
| 2975 | } |
| 2976 | |
| 2977 | // We simply return the type at index `Index`. |
| 2978 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2979 | return Nth->getAsType(); |
| 2980 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2981 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2982 | } |
| 2983 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 2984 | /// Determine whether this alias template is "enable_if_t". |
| 2985 | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { |
| 2986 | return AliasTemplate->getName().equals("enable_if_t"); |
| 2987 | } |
| 2988 | |
| 2989 | /// Collect all of the separable terms in the given condition, which |
| 2990 | /// might be a conjunction. |
| 2991 | /// |
| 2992 | /// FIXME: The right answer is to convert the logical expression into |
| 2993 | /// disjunctive normal form, so we can find the first failed term |
| 2994 | /// within each possible clause. |
| 2995 | static void collectConjunctionTerms(Expr *Clause, |
| 2996 | SmallVectorImpl<Expr *> &Terms) { |
| 2997 | if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { |
| 2998 | if (BinOp->getOpcode() == BO_LAnd) { |
| 2999 | collectConjunctionTerms(BinOp->getLHS(), Terms); |
| 3000 | collectConjunctionTerms(BinOp->getRHS(), Terms); |
| 3001 | } |
| 3002 | |
| 3003 | return; |
| 3004 | } |
| 3005 | |
| 3006 | Terms.push_back(Clause); |
| 3007 | } |
| 3008 | |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3009 | // The ranges-v3 library uses an odd pattern of a top-level "||" with |
| 3010 | // a left-hand side that is value-dependent but never true. Identify |
| 3011 | // the idiom and ignore that term. |
| 3012 | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { |
| 3013 | // Top-level '||'. |
| 3014 | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); |
| 3015 | if (!BinOp) return Cond; |
| 3016 | |
| 3017 | if (BinOp->getOpcode() != BO_LOr) return Cond; |
| 3018 | |
| 3019 | // With an inner '==' that has a literal on the right-hand side. |
| 3020 | Expr *LHS = BinOp->getLHS(); |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3021 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3022 | if (!InnerBinOp) return Cond; |
| 3023 | |
| 3024 | if (InnerBinOp->getOpcode() != BO_EQ || |
| 3025 | !isa<IntegerLiteral>(InnerBinOp->getRHS())) |
| 3026 | return Cond; |
| 3027 | |
| 3028 | // If the inner binary operation came from a macro expansion named |
| 3029 | // CONCEPT_REQUIRES or CONCEPT_REQUIRES_, return the right-hand side |
| 3030 | // of the '||', which is the real, user-provided condition. |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3031 | SourceLocation Loc = InnerBinOp->getExprLoc(); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3032 | if (!Loc.isMacroID()) return Cond; |
| 3033 | |
| 3034 | StringRef MacroName = PP.getImmediateMacroName(Loc); |
| 3035 | if (MacroName == "CONCEPT_REQUIRES" || MacroName == "CONCEPT_REQUIRES_") |
| 3036 | return BinOp->getRHS(); |
| 3037 | |
| 3038 | return Cond; |
| 3039 | } |
| 3040 | |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3041 | std::pair<Expr *, std::string> |
| 3042 | Sema::findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond) { |
| 3043 | Cond = lookThroughRangesV3Condition(PP, Cond); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3044 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3045 | // Separate out all of the terms in a conjunction. |
| 3046 | SmallVector<Expr *, 4> Terms; |
| 3047 | collectConjunctionTerms(Cond, Terms); |
| 3048 | |
| 3049 | // Determine which term failed. |
| 3050 | Expr *FailedCond = nullptr; |
| 3051 | for (Expr *Term : Terms) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3052 | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); |
| 3053 | |
| 3054 | // Literals are uninteresting. |
| 3055 | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || |
| 3056 | isa<IntegerLiteral>(TermAsWritten)) |
| 3057 | continue; |
| 3058 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3059 | // The initialization of the parameter from the argument is |
| 3060 | // a constant-evaluated context. |
| 3061 | EnterExpressionEvaluationContext ConstantEvaluated( |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3062 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3063 | |
| 3064 | bool Succeeded; |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3065 | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3066 | !Succeeded) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3067 | FailedCond = TermAsWritten; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3068 | break; |
| 3069 | } |
| 3070 | } |
| 3071 | |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3072 | if (!FailedCond) { |
| 3073 | if (!AllowTopLevelCond) |
| 3074 | return { nullptr, "" }; |
| 3075 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3076 | FailedCond = Cond->IgnoreParenImpCasts(); |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3077 | } |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3078 | |
| 3079 | std::string Description; |
| 3080 | { |
| 3081 | llvm::raw_string_ostream Out(Description); |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3082 | FailedCond->printPretty(Out, nullptr, getPrintingPolicy()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3083 | } |
| 3084 | return { FailedCond, Description }; |
| 3085 | } |
| 3086 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3087 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 3088 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3089 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3090 | DependentTemplateName *DTN |
| 3091 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3092 | if (DTN && DTN->isIdentifier()) |
| 3093 | // When building a template-id where the template-name is dependent, |
| 3094 | // assume the template is a type template. Either our assumption is |
| 3095 | // correct, or the code is ill-formed and will be diagnosed when the |
| 3096 | // dependent name is substituted. |
| 3097 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 3098 | DTN->getQualifier(), |
| 3099 | DTN->getIdentifier(), |
| 3100 | TemplateArgs); |
| 3101 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3102 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 3103 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 3104 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3105 | // We might have a substituted template template parameter pack. If so, |
| 3106 | // build a template specialization type for it. |
| 3107 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 3108 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3109 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3110 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 3111 | << Name; |
| 3112 | NoteAllFoundTemplates(Name); |
| 3113 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3114 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3115 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3116 | // Check that the template argument list is well-formed for this |
| 3117 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3118 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3119 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3120 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3121 | return QualType(); |
| 3122 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3123 | QualType CanonType; |
| 3124 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3125 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3126 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 3127 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3128 | // Find the canonical type for this type alias template specialization. |
| 3129 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 3130 | if (Pattern->isInvalidDecl()) |
| 3131 | return QualType(); |
| 3132 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3133 | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, |
| 3134 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3135 | |
| 3136 | // Only substitute for the innermost template argument list. |
| 3137 | MultiLevelTemplateArgumentList TemplateArgLists; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3138 | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 3139 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 3140 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 3141 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3142 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3143 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3144 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3145 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3146 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3147 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3148 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 3149 | TemplateArgLists, AliasTemplate->getLocation(), |
| 3150 | AliasTemplate->getDeclName()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3151 | if (CanonType.isNull()) { |
| 3152 | // If this was enable_if and we failed to find the nested type |
| 3153 | // within enable_if in a SFINAE context, dig out the specific |
| 3154 | // enable_if condition that failed and present that instead. |
| 3155 | if (isEnableIfAliasTemplate(AliasTemplate)) { |
| 3156 | if (auto DeductionInfo = isSFINAEContext()) { |
| 3157 | if (*DeductionInfo && |
| 3158 | (*DeductionInfo)->hasSFINAEDiagnostic() && |
| 3159 | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == |
| 3160 | diag::err_typename_nested_not_found_enable_if && |
| 3161 | TemplateArgs[0].getArgument().getKind() |
| 3162 | == TemplateArgument::Expression) { |
| 3163 | Expr *FailedCond; |
| 3164 | std::string FailedDescription; |
| 3165 | std::tie(FailedCond, FailedDescription) = |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3166 | findFailedBooleanCondition( |
| 3167 | TemplateArgs[0].getSourceExpression(), |
| 3168 | /*AllowTopLevelCond=*/true); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3169 | |
| 3170 | // Remove the old SFINAE diagnostic. |
| 3171 | PartialDiagnosticAt OldDiag = |
| 3172 | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; |
| 3173 | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); |
| 3174 | |
| 3175 | // Add a new SFINAE diagnostic specifying which condition |
| 3176 | // failed. |
| 3177 | (*DeductionInfo)->addSFINAEDiagnostic( |
| 3178 | OldDiag.first, |
| 3179 | PDiag(diag::err_typename_nested_not_found_requirement) |
| 3180 | << FailedDescription |
| 3181 | << FailedCond->getSourceRange()); |
| 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3186 | return QualType(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3187 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3188 | } else if (Name.isDependent() || |
| 3189 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3190 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3191 | // This class template specialization is a dependent |
| 3192 | // type. Therefore, its canonical type is another class template |
| 3193 | // specialization type that contains all of the converted |
| 3194 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 3195 | // A<T, T> have identical types when A is declared as: |
| 3196 | // |
| 3197 | // template<typename T, typename U = T> struct A; |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 3198 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3199 | |
| 3200 | // This might work out to be a current instantiation, in which |
| 3201 | // case the canonical type needs to be the InjectedClassNameType. |
| 3202 | // |
| 3203 | // TODO: in theory this could be a simple hashtable lookup; most |
| 3204 | // changes to CurContext don't change the set of current |
| 3205 | // instantiations. |
| 3206 | if (isa<ClassTemplateDecl>(Template)) { |
| 3207 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 3208 | // If we get out to a namespace, we're done. |
| 3209 | if (Ctx->isFileContext()) break; |
| 3210 | |
| 3211 | // If this isn't a record, keep looking. |
| 3212 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 3213 | if (!Record) continue; |
| 3214 | |
| 3215 | // Look for one of the two cases with InjectedClassNameTypes |
| 3216 | // and check whether it's the same template. |
| 3217 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 3218 | !Record->getDescribedClassTemplate()) |
| 3219 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3220 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3221 | // Fetch the injected class name type and check whether its |
| 3222 | // injected type is equal to the type we just built. |
| 3223 | QualType ICNT = Context.getTypeDeclType(Record); |
| 3224 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 3225 | ->getInjectedSpecializationType(); |
| 3226 | |
| 3227 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 3228 | continue; |
| 3229 | |
| 3230 | // If so, the canonical type of this TST is the injected |
| 3231 | // class name type of the record we just found. |
| 3232 | assert(ICNT.isCanonical()); |
| 3233 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3234 | break; |
| 3235 | } |
| 3236 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3238 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3239 | // Find the class template specialization declaration that |
| 3240 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3241 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3242 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3243 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3244 | if (!Decl) { |
| 3245 | // This is the first time we have referenced this class template |
| 3246 | // specialization. Create the canonical declaration and add it to |
| 3247 | // the set of specializations. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3248 | Decl = ClassTemplateSpecializationDecl::Create( |
| 3249 | Context, ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 3250 | ClassTemplate->getDeclContext(), |
| 3251 | ClassTemplate->getTemplatedDecl()->getBeginLoc(), |
| 3252 | ClassTemplate->getLocation(), ClassTemplate, Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 3253 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 3254 | if (ClassTemplate->isOutOfLine()) |
| 3255 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 3258 | if (Decl->getSpecializationKind() == TSK_Undeclared) { |
| 3259 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3260 | TemplateArgLists.addOuterTemplateArguments(Converted); |
| 3261 | InstantiateAttrsForDecl(TemplateArgLists, ClassTemplate->getTemplatedDecl(), |
| 3262 | Decl); |
| 3263 | } |
| 3264 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 3265 | // Diagnose uses of this specialization. |
| 3266 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 3267 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3268 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3269 | assert(isa<RecordType>(CanonType) && |
| 3270 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3271 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 3272 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 3273 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3274 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3275 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3276 | // Build the fully-sugared type for this class template |
| 3277 | // specialization, which refers back to the class template |
| 3278 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 3279 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3282 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3283 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3284 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
| 3285 | SourceLocation TemplateIILoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3287 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3288 | SourceLocation RAngleLoc, |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3289 | bool IsCtorOrDtorName, bool IsClassName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3290 | if (SS.isInvalid()) |
| 3291 | return true; |
| 3292 | |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3293 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { |
| 3294 | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); |
| 3295 | |
| 3296 | // C++ [temp.res]p3: |
| 3297 | // A qualified-id that refers to a type and in which the |
| 3298 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 3299 | // shall be prefixed by the keyword typename to indicate that the |
| 3300 | // qualified-id denotes a type, forming an |
| 3301 | // elaborated-type-specifier (7.1.5.3). |
| 3302 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { |
Richard Smith | 3411fbf | 2017-02-01 21:41:18 +0000 | [diff] [blame] | 3303 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3304 | << SS.getScopeRep() << TemplateII->getName(); |
| 3305 | // Recover as if 'typename' were specified. |
| 3306 | // FIXME: This is not quite correct recovery as we don't transform SS |
| 3307 | // into the corresponding dependent form (and we don't diagnose missing |
| 3308 | // 'template' keywords within SS as a result). |
| 3309 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
| 3310 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
| 3311 | TemplateArgsIn, RAngleLoc); |
| 3312 | } |
| 3313 | |
| 3314 | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
| 3315 | // it's not actually allowed to be used as a type in most cases. Because |
| 3316 | // we annotate it before we know whether it's valid, we have to check for |
| 3317 | // this case here. |
| 3318 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3319 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 3320 | Diag(TemplateIILoc, |
| 3321 | TemplateKWLoc.isInvalid() |
| 3322 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
| 3323 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 3324 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 3325 | << 1 /*if any keyword was present, it was 'template'*/; |
| 3326 | } |
| 3327 | } |
| 3328 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3329 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3330 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3331 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3332 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3333 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3334 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3335 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3336 | QualType T |
| 3337 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 3338 | DTN->getQualifier(), |
| 3339 | DTN->getIdentifier(), |
| 3340 | TemplateArgs); |
| 3341 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3342 | TypeLocBuilder TLB; |
| 3343 | DependentTemplateSpecializationTypeLoc SpecTL |
| 3344 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3345 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 3346 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3347 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3348 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3349 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3350 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3351 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3352 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3353 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3354 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3355 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3356 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3357 | if (Result.isNull()) |
| 3358 | return true; |
| 3359 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3360 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3361 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3362 | TemplateSpecializationTypeLoc SpecTL |
| 3363 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3364 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3365 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3366 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3367 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3368 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3369 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3370 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3371 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 3372 | // constructor or destructor name (in such a case, the scope specifier |
| 3373 | // will be attached to the enclosing Decl or Expr node). |
| 3374 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3375 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 3376 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 3377 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3378 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3379 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3380 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3381 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3382 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3383 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3385 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3386 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3387 | SourceLocation TagLoc, |
| 3388 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3389 | SourceLocation TemplateKWLoc, |
| 3390 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3391 | SourceLocation TemplateLoc, |
| 3392 | SourceLocation LAngleLoc, |
| 3393 | ASTTemplateArgsPtr TemplateArgsIn, |
| 3394 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3395 | TemplateName Template = TemplateD.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3396 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3397 | // Translate the parser's template argument list in our AST format. |
| 3398 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 3399 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3401 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3402 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3403 | ElaboratedTypeKeyword Keyword |
| 3404 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3405 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3406 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 3407 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3408 | DTN->getQualifier(), |
| 3409 | DTN->getIdentifier(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3410 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3411 | |
| 3412 | // Build type-source information. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3413 | TypeLocBuilder TLB; |
| 3414 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3415 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 3416 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 3417 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3418 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3419 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3420 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3421 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3422 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3423 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3424 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3425 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3426 | |
| 3427 | if (TypeAliasTemplateDecl *TAT = |
| 3428 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 3429 | // C++0x [dcl.type.elab]p2: |
| 3430 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 3431 | // resolves to an alias template specialization, the |
| 3432 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 3433 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 3434 | << TAT << NTK_TypeAliasTemplate << TagKind; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3435 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 3436 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3437 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3438 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 3439 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 3440 | return TypeResult(true); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3441 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3442 | // Check the tag kind |
| 3443 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3444 | RecordDecl *D = RT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3445 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3446 | IdentifierInfo *Id = D->getIdentifier(); |
| 3447 | assert(Id && "templated class must have an identifier"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3448 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 3449 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 3450 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3451 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3452 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3453 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3454 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3455 | } |
| 3456 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3458 | // Provide source-location information for the template specialization. |
| 3459 | TypeLocBuilder TLB; |
| 3460 | TemplateSpecializationTypeLoc SpecTL |
| 3461 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3462 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3463 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 3464 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3465 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3466 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3467 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3469 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3470 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3471 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 3472 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3473 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3474 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3475 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3478 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 3479 | NamedDecl *PrevDecl, |
| 3480 | SourceLocation Loc, |
| 3481 | bool IsPartialSpecialization); |
| 3482 | |
| 3483 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3484 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3485 | static bool isTemplateArgumentTemplateParameter( |
| 3486 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 3487 | switch (Arg.getKind()) { |
| 3488 | case TemplateArgument::Null: |
| 3489 | case TemplateArgument::NullPtr: |
| 3490 | case TemplateArgument::Integral: |
| 3491 | case TemplateArgument::Declaration: |
| 3492 | case TemplateArgument::Pack: |
| 3493 | case TemplateArgument::TemplateExpansion: |
| 3494 | return false; |
| 3495 | |
| 3496 | case TemplateArgument::Type: { |
| 3497 | QualType Type = Arg.getAsType(); |
| 3498 | const TemplateTypeParmType *TPT = |
| 3499 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 3500 | return TPT && !Type.hasQualifiers() && |
| 3501 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 3502 | } |
| 3503 | |
| 3504 | case TemplateArgument::Expression: { |
| 3505 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 3506 | if (!DRE || !DRE->getDecl()) |
| 3507 | return false; |
| 3508 | const NonTypeTemplateParmDecl *NTTP = |
| 3509 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 3510 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 3511 | } |
| 3512 | |
| 3513 | case TemplateArgument::Template: |
| 3514 | const TemplateTemplateParmDecl *TTP = |
| 3515 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 3516 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 3517 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 3518 | } |
| 3519 | llvm_unreachable("unexpected kind of template argument"); |
| 3520 | } |
| 3521 | |
| 3522 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 3523 | ArrayRef<TemplateArgument> Args) { |
| 3524 | if (Params->size() != Args.size()) |
| 3525 | return false; |
| 3526 | |
| 3527 | unsigned Depth = Params->getDepth(); |
| 3528 | |
| 3529 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3530 | TemplateArgument Arg = Args[I]; |
| 3531 | |
| 3532 | // If the parameter is a pack expansion, the argument must be a pack |
| 3533 | // whose only element is a pack expansion. |
| 3534 | if (Params->getParam(I)->isParameterPack()) { |
| 3535 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 3536 | !Arg.pack_begin()->isPackExpansion()) |
| 3537 | return false; |
| 3538 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 3539 | } |
| 3540 | |
| 3541 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 3542 | return false; |
| 3543 | } |
| 3544 | |
| 3545 | return true; |
| 3546 | } |
| 3547 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3548 | /// Convert the parser's template argument list representation into our form. |
| 3549 | static TemplateArgumentListInfo |
| 3550 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 3551 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 3552 | TemplateId.RAngleLoc); |
| 3553 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 3554 | TemplateId.NumArgs); |
| 3555 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 3556 | return TemplateArgs; |
| 3557 | } |
| 3558 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3559 | template<typename PartialSpecDecl> |
| 3560 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 3561 | if (Partial->getDeclContext()->isDependentContext()) |
| 3562 | return; |
| 3563 | |
| 3564 | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
| 3565 | // for non-substitution-failure issues? |
| 3566 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 3567 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 3568 | return; |
| 3569 | |
| 3570 | auto *Template = Partial->getSpecializedTemplate(); |
| 3571 | S.Diag(Partial->getLocation(), |
Richard Smith | fa4a09d | 2016-12-27 20:03:09 +0000 | [diff] [blame] | 3572 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 3573 | << isa<VarTemplateDecl>(Template); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3574 | |
| 3575 | if (Info.hasSFINAEDiagnostic()) { |
| 3576 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 3577 | PartialDiagnostic::NullDiagnostic()}; |
| 3578 | Info.takeSFINAEDiagnostic(Diag); |
| 3579 | SmallString<128> SFINAEArgString; |
| 3580 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 3581 | S.Diag(Diag.first, |
| 3582 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 3583 | << SFINAEArgString; |
| 3584 | } |
| 3585 | |
| 3586 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 3587 | } |
| 3588 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3589 | static void |
| 3590 | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
| 3591 | const llvm::SmallBitVector &DeducibleParams) { |
| 3592 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3593 | if (!DeducibleParams[I]) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 3594 | NamedDecl *Param = TemplateParams->getParam(I); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3595 | if (Param->getDeclName()) |
| 3596 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3597 | << Param->getDeclName(); |
| 3598 | else |
| 3599 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3600 | << "(anonymous)"; |
| 3601 | } |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3606 | template<typename PartialSpecDecl> |
| 3607 | static void checkTemplatePartialSpecialization(Sema &S, |
| 3608 | PartialSpecDecl *Partial) { |
| 3609 | // C++1z [temp.class.spec]p8: (DR1495) |
| 3610 | // - The specialization shall be more specialized than the primary |
| 3611 | // template (14.5.5.2). |
| 3612 | checkMoreSpecializedThanPrimary(S, Partial); |
| 3613 | |
| 3614 | // C++ [temp.class.spec]p8: (DR1315) |
| 3615 | // - Each template-parameter shall appear at least once in the |
| 3616 | // template-id outside a non-deduced context. |
| 3617 | // C++1z [temp.class.spec.match]p3 (P0127R2) |
| 3618 | // If the template arguments of a partial specialization cannot be |
| 3619 | // deduced because of the structure of its template-parameter-list |
| 3620 | // and the template-id, the program is ill-formed. |
| 3621 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 3622 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3623 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 3624 | TemplateParams->getDepth(), DeducibleParams); |
| 3625 | |
| 3626 | if (!DeducibleParams.all()) { |
| 3627 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3628 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 3629 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 3630 | << (NumNonDeducible > 1) |
| 3631 | << SourceRange(Partial->getLocation(), |
| 3632 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3633 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3634 | } |
| 3635 | } |
| 3636 | |
| 3637 | void Sema::CheckTemplatePartialSpecialization( |
| 3638 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 3639 | checkTemplatePartialSpecialization(*this, Partial); |
| 3640 | } |
| 3641 | |
| 3642 | void Sema::CheckTemplatePartialSpecialization( |
| 3643 | VarTemplatePartialSpecializationDecl *Partial) { |
| 3644 | checkTemplatePartialSpecialization(*this, Partial); |
| 3645 | } |
| 3646 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3647 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
| 3648 | // C++1z [temp.param]p11: |
| 3649 | // A template parameter of a deduction guide template that does not have a |
| 3650 | // default-argument shall be deducible from the parameter-type-list of the |
| 3651 | // deduction guide template. |
| 3652 | auto *TemplateParams = TD->getTemplateParameters(); |
| 3653 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3654 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
| 3655 | for (unsigned I = 0; I != TemplateParams->size(); ++I) { |
| 3656 | // A parameter pack is deducible (to an empty pack). |
| 3657 | auto *Param = TemplateParams->getParam(I); |
| 3658 | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)) |
| 3659 | DeducibleParams[I] = true; |
| 3660 | } |
| 3661 | |
| 3662 | if (!DeducibleParams.all()) { |
| 3663 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3664 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
| 3665 | << (NumNonDeducible > 1); |
| 3666 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
| 3667 | } |
| 3668 | } |
| 3669 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3670 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3671 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 3672 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3673 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3674 | // D must be variable template id. |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 3675 | assert(D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3676 | "Variable template specialization is declared with a template it."); |
| 3677 | |
| 3678 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3679 | TemplateArgumentListInfo TemplateArgs = |
| 3680 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3681 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 3682 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 3683 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3684 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3685 | TemplateName Name = TemplateId->Template.get(); |
| 3686 | |
| 3687 | // The template-id must name a variable template. |
| 3688 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3689 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 3690 | if (!VarTemplate) { |
| 3691 | NamedDecl *FnTemplate; |
| 3692 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 3693 | FnTemplate = *OTS->begin(); |
| 3694 | else |
| 3695 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 3696 | if (FnTemplate) |
| 3697 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 3698 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3699 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 3700 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3701 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3702 | |
| 3703 | // Check for unexpanded parameter packs in any of the template arguments. |
| 3704 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 3705 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 3706 | UPPC_PartialSpecialization)) |
| 3707 | return true; |
| 3708 | |
| 3709 | // Check that the template argument list is well-formed for this |
| 3710 | // template. |
| 3711 | SmallVector<TemplateArgument, 4> Converted; |
| 3712 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 3713 | false, Converted)) |
| 3714 | return true; |
| 3715 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3716 | // Find the variable template (partial) specialization declaration that |
| 3717 | // corresponds to these arguments. |
| 3718 | if (IsPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3719 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 3720 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3721 | return true; |
| 3722 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3723 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
| 3724 | // also do them during instantiation. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3725 | bool InstantiationDependent; |
| 3726 | if (!Name.isDependent() && |
| 3727 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3728 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3729 | InstantiationDependent)) { |
| 3730 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3731 | << VarTemplate->getDeclName(); |
| 3732 | IsPartialSpecialization = false; |
| 3733 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3734 | |
| 3735 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 3736 | Converted)) { |
| 3737 | // C++ [temp.class.spec]p9b3: |
| 3738 | // |
| 3739 | // -- The argument list of the specialization shall not be identical |
| 3740 | // to the implicit argument list of the primary template. |
| 3741 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 3742 | << /*variable template*/ 1 |
| 3743 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 3744 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 3745 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 3746 | // of the primary template. |
| 3747 | return true; |
| 3748 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3751 | void *InsertPos = nullptr; |
| 3752 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3753 | |
| 3754 | if (IsPartialSpecialization) |
| 3755 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3756 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3757 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3758 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3759 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3760 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3761 | |
| 3762 | // Check whether we can declare a variable template specialization in |
| 3763 | // the current scope. |
| 3764 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 3765 | TemplateNameLoc, |
| 3766 | IsPartialSpecialization)) |
| 3767 | return true; |
| 3768 | |
| 3769 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 3770 | // Since the only prior variable template specialization with these |
| 3771 | // arguments was referenced but not declared, reuse that |
| 3772 | // declaration node as our own, updating its source location and |
| 3773 | // the list of outer template parameters to reflect our new declaration. |
| 3774 | Specialization = PrevDecl; |
| 3775 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3776 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3777 | } else if (IsPartialSpecialization) { |
| 3778 | // Create a new class template partial specialization declaration node. |
| 3779 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 3780 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3781 | VarTemplatePartialSpecializationDecl *Partial = |
| 3782 | VarTemplatePartialSpecializationDecl::Create( |
| 3783 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 3784 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3785 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3786 | |
| 3787 | if (!PrevPartial) |
| 3788 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 3789 | Specialization = Partial; |
| 3790 | |
| 3791 | // If we are providing an explicit specialization of a member variable |
| 3792 | // template specialization, make a note of that. |
| 3793 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3794 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3795 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3796 | CheckTemplatePartialSpecialization(Partial); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3797 | } else { |
| 3798 | // Create a new class template specialization declaration node for |
| 3799 | // this explicit specialization or friend declaration. |
| 3800 | Specialization = VarTemplateSpecializationDecl::Create( |
| 3801 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3802 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3803 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 3804 | |
| 3805 | if (!PrevDecl) |
| 3806 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 3807 | } |
| 3808 | |
| 3809 | // C++ [temp.expl.spec]p6: |
| 3810 | // If a template, a member template or the member of a class template is |
| 3811 | // explicitly specialized then that specialization shall be declared |
| 3812 | // before the first use of that specialization that would cause an implicit |
| 3813 | // instantiation to take place, in every translation unit in which such a |
| 3814 | // use occurs; no diagnostic is required. |
| 3815 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3816 | bool Okay = false; |
| 3817 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 3818 | // Is there any previous explicit specialization declaration? |
| 3819 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3820 | Okay = true; |
| 3821 | break; |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | if (!Okay) { |
| 3826 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3827 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3828 | << Name << Range; |
| 3829 | |
| 3830 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3831 | diag::note_instantiation_required_here) |
| 3832 | << (PrevDecl->getTemplateSpecializationKind() != |
| 3833 | TSK_ImplicitInstantiation); |
| 3834 | return true; |
| 3835 | } |
| 3836 | } |
| 3837 | |
| 3838 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 3839 | Specialization->setLexicalDeclContext(CurContext); |
| 3840 | |
| 3841 | // Add the specialization into its lexical context, so that it can |
| 3842 | // be seen when iterating through the list of declarations in that |
| 3843 | // context. However, specializations are not found by name lookup. |
| 3844 | CurContext->addDecl(Specialization); |
| 3845 | |
| 3846 | // Note that this is an explicit specialization. |
| 3847 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 3848 | |
| 3849 | if (PrevDecl) { |
| 3850 | // Check that this isn't a redefinition of this specialization, |
| 3851 | // merging with previous declarations. |
| 3852 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 3853 | forRedeclarationInCurContext()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3854 | PrevSpec.addDecl(PrevDecl); |
| 3855 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3856 | } else if (Specialization->isStaticDataMember() && |
| 3857 | Specialization->isOutOfLine()) { |
| 3858 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
| 3861 | // Link instantiations of static data members back to the template from |
| 3862 | // which they were instantiated. |
| 3863 | if (Specialization->isStaticDataMember()) |
| 3864 | Specialization->setInstantiationOfStaticDataMember( |
| 3865 | VarTemplate->getTemplatedDecl(), |
| 3866 | Specialization->getSpecializationKind()); |
| 3867 | |
| 3868 | return Specialization; |
| 3869 | } |
| 3870 | |
| 3871 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3872 | /// A partial specialization whose template arguments have matched |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3873 | /// a given template-id. |
| 3874 | struct PartialSpecMatchResult { |
| 3875 | VarTemplatePartialSpecializationDecl *Partial; |
| 3876 | TemplateArgumentList *Args; |
| 3877 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 3878 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3879 | |
| 3880 | DeclResult |
| 3881 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3882 | SourceLocation TemplateNameLoc, |
| 3883 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3884 | assert(Template && "A variable template id without template?"); |
| 3885 | |
| 3886 | // Check that the template argument list is well-formed for this template. |
| 3887 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3888 | if (CheckTemplateArgumentList( |
| 3889 | Template, TemplateNameLoc, |
| 3890 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3891 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3892 | return true; |
| 3893 | |
| 3894 | // Find the variable template specialization declaration that |
| 3895 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3896 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3897 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3898 | Converted, InsertPos)) { |
| 3899 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3900 | // If we already have a variable template specialization, return it. |
| 3901 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3902 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3903 | |
| 3904 | // This is the first time we have referenced this variable template |
| 3905 | // specialization. Create the canonical declaration and add it to |
| 3906 | // the set of specializations, based on the closest partial specialization |
| 3907 | // that it represents. That is, |
| 3908 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 3909 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3910 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3911 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 3912 | bool AmbiguousPartialSpec = false; |
| 3913 | typedef PartialSpecMatchResult MatchResult; |
| 3914 | SmallVector<MatchResult, 4> Matched; |
| 3915 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3916 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 3917 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3918 | |
| 3919 | // 1. Attempt to find the closest partial specialization that this |
| 3920 | // specializes, if any. |
| 3921 | // If any of the template arguments is dependent, then this is probably |
| 3922 | // a placeholder for an incomplete declarative context; which must be |
| 3923 | // complete by instantiation time. Thus, do not search through the partial |
| 3924 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3925 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 3926 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 3927 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3928 | bool InstantiationDependent = false; |
| 3929 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3930 | TemplateArgs, InstantiationDependent)) { |
| 3931 | |
| 3932 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3933 | Template->getPartialSpecializations(PartialSpecs); |
| 3934 | |
| 3935 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 3936 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 3937 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 3938 | |
| 3939 | if (TemplateDeductionResult Result = |
| 3940 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 3941 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3942 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3943 | FailedCandidates.addCandidate().set( |
| 3944 | DeclAccessPair::make(Template, AS_public), Partial, |
| 3945 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3946 | (void)Result; |
| 3947 | } else { |
| 3948 | Matched.push_back(PartialSpecMatchResult()); |
| 3949 | Matched.back().Partial = Partial; |
| 3950 | Matched.back().Args = Info.take(); |
| 3951 | } |
| 3952 | } |
| 3953 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3954 | if (Matched.size() >= 1) { |
| 3955 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 3956 | if (Matched.size() == 1) { |
| 3957 | // -- If exactly one matching specialization is found, the |
| 3958 | // instantiation is generated from that specialization. |
| 3959 | // We don't need to do anything for this. |
| 3960 | } else { |
| 3961 | // -- If more than one matching specialization is found, the |
| 3962 | // partial order rules (14.5.4.2) are used to determine |
| 3963 | // whether one of the specializations is more specialized |
| 3964 | // than the others. If none of the specializations is more |
| 3965 | // specialized than all of the other matching |
| 3966 | // specializations, then the use of the variable template is |
| 3967 | // ambiguous and the program is ill-formed. |
| 3968 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 3969 | PEnd = Matched.end(); |
| 3970 | P != PEnd; ++P) { |
| 3971 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 3972 | PointOfInstantiation) == |
| 3973 | P->Partial) |
| 3974 | Best = P; |
| 3975 | } |
| 3976 | |
| 3977 | // Determine if the best partial specialization is more specialized than |
| 3978 | // the others. |
| 3979 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 3980 | PEnd = Matched.end(); |
| 3981 | P != PEnd; ++P) { |
| 3982 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 3983 | P->Partial, Best->Partial, |
| 3984 | PointOfInstantiation) != Best->Partial) { |
| 3985 | AmbiguousPartialSpec = true; |
| 3986 | break; |
| 3987 | } |
| 3988 | } |
| 3989 | } |
| 3990 | |
| 3991 | // Instantiate using the best variable template partial specialization. |
| 3992 | InstantiationPattern = Best->Partial; |
| 3993 | InstantiationArgs = Best->Args; |
| 3994 | } else { |
| 3995 | // -- If no match is found, the instantiation is generated |
| 3996 | // from the primary template. |
| 3997 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 3998 | } |
| 3999 | } |
| 4000 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4001 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4002 | // Note that we do not instantiate a definition until we see an odr-use |
| 4003 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4004 | // FIXME: LateAttrs et al.? |
| 4005 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 4006 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 4007 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 4008 | if (!Decl) |
| 4009 | return true; |
| 4010 | |
| 4011 | if (AmbiguousPartialSpec) { |
| 4012 | // Partial ordering did not produce a clear winner. Complain. |
| 4013 | Decl->setInvalidDecl(); |
| 4014 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 4015 | << Decl; |
| 4016 | |
| 4017 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 4018 | for (MatchResult P : Matched) |
| 4019 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 4020 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 4021 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4022 | return true; |
| 4023 | } |
| 4024 | |
| 4025 | if (VarTemplatePartialSpecializationDecl *D = |
| 4026 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 4027 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 4028 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4029 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 4030 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4031 | assert(Decl && "No variable template specialization?"); |
| 4032 | return Decl; |
| 4033 | } |
| 4034 | |
| 4035 | ExprResult |
| 4036 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 4037 | const DeclarationNameInfo &NameInfo, |
| 4038 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 4039 | const TemplateArgumentListInfo *TemplateArgs) { |
| 4040 | |
| 4041 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 4042 | *TemplateArgs); |
| 4043 | if (Decl.isInvalid()) |
| 4044 | return ExprError(); |
| 4045 | |
| 4046 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 4047 | if (!Var->getTemplateSpecializationKind()) |
| 4048 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 4049 | NameInfo.getLoc()); |
| 4050 | |
| 4051 | // Build an ordinary singleton decl ref. |
| 4052 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4053 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
Richard Smith | ecad88d | 2018-04-26 01:08:00 +0000 | [diff] [blame] | 4056 | void Sema::diagnoseMissingTemplateArguments(TemplateName Name, |
| 4057 | SourceLocation Loc) { |
| 4058 | Diag(Loc, diag::err_template_missing_args) |
| 4059 | << (int)getTemplateNameKindForDiagnostics(Name) << Name; |
| 4060 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) { |
| 4061 | Diag(TD->getLocation(), diag::note_template_decl_here) |
| 4062 | << TD->getTemplateParameters()->getSourceRange(); |
| 4063 | } |
| 4064 | } |
| 4065 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4066 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4067 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4068 | LookupResult &R, |
| 4069 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4070 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4071 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 4072 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | // 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] | 4074 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4075 | // foo<int> could identify a single function unambiguously |
| 4076 | // This approach does NOT work, since f<int>(1); |
| 4077 | // gets resolved prior to resorting to overload resolution |
| 4078 | // i.e., template<class T> void f(double); |
| 4079 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4080 | |
| 4081 | // These should be filtered out by our callers. |
| 4082 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 4083 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 4084 | |
Richard Smith | 0410094 | 2018-04-26 02:10:22 +0000 | [diff] [blame] | 4085 | // Non-function templates require a template argument list. |
| 4086 | if (auto *TD = R.getAsSingle<TemplateDecl>()) { |
| 4087 | if (!TemplateArgs && !isa<FunctionTemplateDecl>(TD)) { |
| 4088 | diagnoseMissingTemplateArguments(TemplateName(TD), R.getNameLoc()); |
| 4089 | return ExprError(); |
| 4090 | } |
| 4091 | } |
| 4092 | |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4093 | auto AnyDependentArguments = [&]() -> bool { |
| 4094 | bool InstantiationDependent; |
| 4095 | return TemplateArgs && |
| 4096 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 4097 | *TemplateArgs, InstantiationDependent); |
| 4098 | }; |
| 4099 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4100 | // In C++1y, check variable template ids. |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4101 | if (R.getAsSingle<VarTemplateDecl>() && !AnyDependentArguments()) { |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 4102 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 4103 | R.getAsSingle<VarTemplateDecl>(), |
| 4104 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4107 | // We don't want lookup warnings at this point. |
| 4108 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4109 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4110 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4111 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4112 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4113 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4114 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4115 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 4116 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4117 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4118 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4121 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4122 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4123 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4124 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4125 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4126 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4127 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4128 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4129 | DeclContext *DC; |
| 4130 | if (!(DC = computeDeclContext(SS, false)) || |
| 4131 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4132 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 4133 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4134 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4135 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4136 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4137 | if (LookupTemplateName(R, (Scope *)nullptr, SS, QualType(), |
| 4138 | /*Entering*/false, MemberOfUnknownSpecialization, |
| 4139 | TemplateKWLoc)) |
| 4140 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4141 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4142 | if (R.isAmbiguous()) |
| 4143 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4144 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4145 | if (R.empty()) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4146 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 4147 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4148 | return ExprError(); |
| 4149 | } |
| 4150 | |
| 4151 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4152 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4153 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 4154 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4155 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 4156 | return ExprError(); |
| 4157 | } |
| 4158 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4159 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4162 | /// Form a dependent template name. |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4163 | /// |
| 4164 | /// This action forms a dependent template name given the template |
| 4165 | /// name and its (presumably dependent) scope specifier. For |
| 4166 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 4167 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 4168 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4169 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4170 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4171 | SourceLocation TemplateKWLoc, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 4172 | const UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4173 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4174 | bool EnteringContext, |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4175 | TemplateTy &Result, |
| 4176 | bool AllowInjectedClassName) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4177 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 4178 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4179 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4180 | diag::warn_cxx98_compat_template_outside_of_template : |
| 4181 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4182 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 4183 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4184 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4185 | if (SS.isSet()) |
| 4186 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 4187 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4188 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4189 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4190 | // C++0x [temp.names]p5: |
| 4191 | // If a name prefixed by the keyword template is not the name of |
| 4192 | // a template, the program is ill-formed. [Note: the keyword |
| 4193 | // template may not be applied to non-template members of class |
| 4194 | // templates. -end note ] [ Note: as is the case with the |
| 4195 | // typename prefix, the template prefix is allowed in cases |
| 4196 | // where it is not strictly necessary; i.e., when the |
| 4197 | // nested-name-specifier or the expression on the left of the -> |
| 4198 | // or . is not dependent on a template-parameter, or the use |
| 4199 | // does not appear in the scope of a template. -end note] |
| 4200 | // |
| 4201 | // Note: C++03 was more strict here, because it banned the use of |
| 4202 | // the "template" keyword prior to a template-name that was not a |
| 4203 | // dependent name. C++ DR468 relaxed this requirement (the |
| 4204 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 4205 | // 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] | 4206 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 4207 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 4208 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4209 | MemberOfUnknownSpecialization); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4210 | if (TNK == TNK_Non_template && MemberOfUnknownSpecialization) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4211 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4212 | } else if (TNK == TNK_Non_template) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4213 | // Do the lookup again to determine if this is a "nothing found" case or |
| 4214 | // a "not a template" case. FIXME: Refactor isTemplateName so we don't |
| 4215 | // need to do this. |
| 4216 | DeclarationNameInfo DNI = GetNameFromUnqualifiedId(Name); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4217 | LookupResult R(*this, DNI.getName(), Name.getBeginLoc(), |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4218 | LookupOrdinaryName); |
| 4219 | bool MOUS; |
| 4220 | if (!LookupTemplateName(R, S, SS, ObjectType.get(), EnteringContext, |
| 4221 | MOUS, TemplateKWLoc)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4222 | Diag(Name.getBeginLoc(), diag::err_no_member) |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4223 | << DNI.getName() << LookupCtx << SS.getRange(); |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4224 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4225 | } else { |
| 4226 | // We found something; return it. |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4227 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
| 4228 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4229 | Name.getKind() == UnqualifiedIdKind::IK_Identifier && |
| 4230 | Name.Identifier && LookupRD->getIdentifier() == Name.Identifier) { |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4231 | // C++14 [class.qual]p2: |
| 4232 | // In a lookup in which function names are not ignored and the |
| 4233 | // nested-name-specifier nominates a class C, if the name specified |
| 4234 | // [...] is the injected-class-name of C, [...] the name is instead |
| 4235 | // considered to name the constructor |
| 4236 | // |
| 4237 | // We don't get here if naming the constructor would be valid, so we |
| 4238 | // just reject immediately and recover by treating the |
| 4239 | // injected-class-name as naming the template. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4240 | Diag(Name.getBeginLoc(), |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4241 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4242 | << Name.Identifier |
| 4243 | << 0 /*injected-class-name used as template name*/ |
| 4244 | << 1 /*'template' keyword was used*/; |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4245 | } |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4246 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4247 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4250 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4251 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4252 | switch (Name.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4253 | case UnqualifiedIdKind::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4254 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4255 | Name.Identifier)); |
| 4256 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4257 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4258 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4259 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4260 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 4261 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4262 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4263 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 4264 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4265 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4266 | default: |
| 4267 | break; |
| 4268 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4269 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4270 | Diag(Name.getBeginLoc(), diag::err_template_kw_refers_to_non_template) |
| 4271 | << GetNameFromUnqualifiedId(Name).getName() << Name.getSourceRange() |
| 4272 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4273 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4277 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4278 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4279 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4280 | QualType ArgType; |
| 4281 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4282 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4283 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4284 | switch(Arg.getKind()) { |
| 4285 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4286 | // C++ [temp.arg.type]p1: |
| 4287 | // A template-argument for a template-parameter which is a |
| 4288 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4289 | ArgType = Arg.getAsType(); |
| 4290 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4291 | break; |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 4292 | case TemplateArgument::Template: |
| 4293 | case TemplateArgument::TemplateExpansion: { |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4294 | // We have a template type parameter but the template argument |
| 4295 | // is a template without any arguments. |
| 4296 | SourceRange SR = AL.getSourceRange(); |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 4297 | TemplateName Name = Arg.getAsTemplateOrTemplatePattern(); |
Richard Smith | ecad88d | 2018-04-26 01:08:00 +0000 | [diff] [blame] | 4298 | diagnoseMissingTemplateArguments(Name, SR.getEnd()); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4299 | return true; |
| 4300 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4301 | case TemplateArgument::Expression: { |
| 4302 | // We have a template type parameter but the template argument is an |
| 4303 | // expression; see if maybe it is missing the "typename" keyword. |
| 4304 | CXXScopeSpec SS; |
| 4305 | DeclarationNameInfo NameInfo; |
| 4306 | |
| 4307 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 4308 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4309 | NameInfo = ArgExpr->getNameInfo(); |
| 4310 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 4311 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 4312 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4313 | NameInfo = ArgExpr->getNameInfo(); |
| 4314 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 4315 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 4316 | if (ArgExpr->isImplicitAccess()) { |
| 4317 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4318 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 4319 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4322 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4323 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 4324 | LookupParsedName(Result, CurScope, &SS); |
| 4325 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 4326 | if (Result.getAsSingle<TypeDecl>() || |
| 4327 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4328 | LookupResult::NotFoundInCurrentInstantiation) { |
| 4329 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4330 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4331 | Diag(Loc, getLangOpts().MSVCCompat |
| 4332 | ? diag::ext_ms_template_type_arg_missing_typename |
| 4333 | : diag::err_template_arg_must_be_type_suggest) |
| 4334 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4335 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4336 | |
| 4337 | // Recover by synthesizing a type using the location information that we |
| 4338 | // already have. |
| 4339 | ArgType = |
| 4340 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 4341 | TypeLocBuilder TLB; |
| 4342 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 4343 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 4344 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 4345 | TL.setNameLoc(NameInfo.getLoc()); |
| 4346 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 4347 | |
| 4348 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 4349 | // properly. |
| 4350 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 4351 | TemplateArgumentLocInfo(TSI)); |
| 4352 | |
| 4353 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4354 | } |
| 4355 | } |
| 4356 | // fallthrough |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 4357 | LLVM_FALLTHROUGH; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 4358 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4359 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4360 | // We have a template type parameter but the template argument |
| 4361 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 4362 | SourceRange SR = AL.getSourceRange(); |
| 4363 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4364 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4365 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4366 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4367 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 4368 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4369 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4370 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4371 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4372 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4373 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4374 | ArgType = Context.getCanonicalType(ArgType); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4375 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4376 | // Objective-C ARC: |
| 4377 | // If an explicitly-specified template argument type is a lifetime type |
| 4378 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4379 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4380 | ArgType->isObjCLifetimeType() && |
| 4381 | !ArgType.getObjCLifetime()) { |
| 4382 | Qualifiers Qs; |
| 4383 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 4384 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 4385 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4386 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 4387 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 4388 | return false; |
| 4389 | } |
| 4390 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4391 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4392 | /// the given template type parameter. |
| 4393 | /// |
| 4394 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4395 | /// the substitution. |
| 4396 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4397 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4398 | /// for. |
| 4399 | /// |
| 4400 | /// \param TemplateLoc the location of the template name that started the |
| 4401 | /// template-id we are checking. |
| 4402 | /// |
| 4403 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4404 | /// terminates the template-id. |
| 4405 | /// |
| 4406 | /// \param Param the template template parameter whose default we are |
| 4407 | /// substituting into. |
| 4408 | /// |
| 4409 | /// \param Converted the list of template arguments provided for template |
| 4410 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4411 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4412 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4413 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4414 | TemplateDecl *Template, |
| 4415 | SourceLocation TemplateLoc, |
| 4416 | SourceLocation RAngleLoc, |
| 4417 | TemplateTypeParmDecl *Param, |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 4418 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4419 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4420 | |
| 4421 | // If the argument type is dependent, instantiate it now based |
| 4422 | // on the previously-computed template arguments. |
| 4423 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4424 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4425 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4426 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4427 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4428 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4430 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4431 | |
| 4432 | // Only substitute for the innermost template argument list. |
| 4433 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4434 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4435 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4436 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4437 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4438 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4439 | ArgType = |
| 4440 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 4441 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | return ArgType; |
| 4445 | } |
| 4446 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4447 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4448 | /// the given non-type template parameter. |
| 4449 | /// |
| 4450 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4451 | /// the substitution. |
| 4452 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4453 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4454 | /// for. |
| 4455 | /// |
| 4456 | /// \param TemplateLoc the location of the template name that started the |
| 4457 | /// template-id we are checking. |
| 4458 | /// |
| 4459 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4460 | /// terminates the template-id. |
| 4461 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4462 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4463 | /// substituting into. |
| 4464 | /// |
| 4465 | /// \param Converted the list of template arguments provided for template |
| 4466 | /// parameters that precede \p Param in the template parameter list. |
| 4467 | /// |
| 4468 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4469 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4470 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4471 | TemplateDecl *Template, |
| 4472 | SourceLocation TemplateLoc, |
| 4473 | SourceLocation RAngleLoc, |
| 4474 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4475 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4476 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4477 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4478 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4479 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4480 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4481 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4482 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4483 | |
| 4484 | // Only substitute for the innermost template argument list. |
| 4485 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4486 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4487 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4488 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4489 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4490 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 4491 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4492 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4495 | /// Substitute template arguments into the default template argument for |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4496 | /// the given template template parameter. |
| 4497 | /// |
| 4498 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4499 | /// the substitution. |
| 4500 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4501 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4502 | /// for. |
| 4503 | /// |
| 4504 | /// \param TemplateLoc the location of the template name that started the |
| 4505 | /// template-id we are checking. |
| 4506 | /// |
| 4507 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4508 | /// terminates the template-id. |
| 4509 | /// |
| 4510 | /// \param Param the template template parameter whose default we are |
| 4511 | /// substituting into. |
| 4512 | /// |
| 4513 | /// \param Converted the list of template arguments provided for template |
| 4514 | /// parameters that precede \p Param in the template parameter list. |
| 4515 | /// |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4516 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4517 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4518 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4519 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 4520 | static TemplateName |
| 4521 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4522 | TemplateDecl *Template, |
| 4523 | SourceLocation TemplateLoc, |
| 4524 | SourceLocation RAngleLoc, |
| 4525 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4526 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4527 | NestedNameSpecifierLoc &QualifierLoc) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4528 | Sema::InstantiatingTemplate Inst( |
| 4529 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 4530 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4531 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4532 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4533 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4534 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4535 | |
| 4536 | // Only substitute for the innermost template argument list. |
| 4537 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4538 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4539 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4540 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4541 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4542 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4543 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4544 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4545 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4546 | QualifierLoc = |
| 4547 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4548 | if (!QualifierLoc) |
| 4549 | return TemplateName(); |
| 4550 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4551 | |
| 4552 | return SemaRef.SubstTemplateName( |
| 4553 | QualifierLoc, |
| 4554 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 4555 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 4556 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4559 | /// If the given template parameter has a default template |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4560 | /// argument, substitute into that default template argument and |
| 4561 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4562 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4563 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 4564 | SourceLocation TemplateLoc, |
| 4565 | SourceLocation RAngleLoc, |
| 4566 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4567 | SmallVectorImpl<TemplateArgument> |
| 4568 | &Converted, |
| 4569 | bool &HasDefaultArg) { |
| 4570 | HasDefaultArg = false; |
| 4571 | |
| 4572 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4573 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4574 | return TemplateArgumentLoc(); |
| 4575 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4576 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4577 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4578 | TemplateLoc, |
| 4579 | RAngleLoc, |
| 4580 | TypeParm, |
| 4581 | Converted); |
| 4582 | if (DI) |
| 4583 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 4584 | |
| 4585 | return TemplateArgumentLoc(); |
| 4586 | } |
| 4587 | |
| 4588 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 4589 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4590 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4591 | return TemplateArgumentLoc(); |
| 4592 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4593 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4594 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4595 | TemplateLoc, |
| 4596 | RAngleLoc, |
| 4597 | NonTypeParm, |
| 4598 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4599 | if (Arg.isInvalid()) |
| 4600 | return TemplateArgumentLoc(); |
| 4601 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4602 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4603 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 4604 | } |
| 4605 | |
| 4606 | TemplateTemplateParmDecl *TempTempParm |
| 4607 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4608 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4609 | return TemplateArgumentLoc(); |
| 4610 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4611 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4612 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4613 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4614 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4615 | RAngleLoc, |
| 4616 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4617 | Converted, |
| 4618 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4619 | if (TName.isNull()) |
| 4620 | return TemplateArgumentLoc(); |
| 4621 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4622 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4623 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4624 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 4625 | } |
| 4626 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4627 | /// Convert a template-argument that we parsed as a type into a template, if |
| 4628 | /// possible. C++ permits injected-class-names to perform dual service as |
| 4629 | /// template template arguments and as template type arguments. |
| 4630 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { |
| 4631 | // Extract and step over any surrounding nested-name-specifier. |
| 4632 | NestedNameSpecifierLoc QualLoc; |
| 4633 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { |
| 4634 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
| 4635 | return TemplateArgumentLoc(); |
| 4636 | |
| 4637 | QualLoc = ETLoc.getQualifierLoc(); |
| 4638 | TLoc = ETLoc.getNamedTypeLoc(); |
| 4639 | } |
| 4640 | |
| 4641 | // If this type was written as an injected-class-name, it can be used as a |
| 4642 | // template template argument. |
| 4643 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) |
| 4644 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), |
| 4645 | QualLoc, InjLoc.getNameLoc()); |
| 4646 | |
| 4647 | // If this type was written as an injected-class-name, it may have been |
| 4648 | // converted to a RecordType during instantiation. If the RecordType is |
| 4649 | // *not* wrapped in a TemplateSpecializationType and denotes a class |
| 4650 | // template specialization, it must have come from an injected-class-name. |
| 4651 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) |
| 4652 | if (auto *CTSD = |
| 4653 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
| 4654 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), |
| 4655 | QualLoc, RecLoc.getNameLoc()); |
| 4656 | |
| 4657 | return TemplateArgumentLoc(); |
| 4658 | } |
| 4659 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4660 | /// Check that the given template argument corresponds to the given |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4661 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4662 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4664 | /// checked. |
| 4665 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4666 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4667 | /// |
| 4668 | /// \param Template The template in which the template argument resides. |
| 4669 | /// |
| 4670 | /// \param TemplateLoc The location of the template name for the template |
| 4671 | /// whose argument list we're matching. |
| 4672 | /// |
| 4673 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 4674 | /// the template argument list. |
| 4675 | /// |
| 4676 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 4677 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 4678 | /// |
| 4679 | /// \param Converted The checked, converted argument will be added to the |
| 4680 | /// end of this small vector. |
| 4681 | /// |
| 4682 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 4683 | /// explicitly written, deduced, etc. |
| 4684 | /// |
| 4685 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4686 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4687 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4688 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4689 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4690 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4691 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4692 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4693 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4694 | // Check template type parameters. |
| 4695 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4696 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4697 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4698 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4699 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4700 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4701 | // with the template arguments we've seen thus far. But if the |
| 4702 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4703 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4704 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 4705 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4706 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4707 | // FIXME: Do we need to substitute into parameters here if they're |
| 4708 | // instantiation-dependent but not dependent? |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4709 | if (NTTPType->isDependentType() && |
| 4710 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4711 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4712 | // Do substitution on the type of the non-type template parameter. |
| 4713 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4714 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4715 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4716 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4717 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4718 | |
| 4719 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4720 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4721 | NTTPType = SubstType(NTTPType, |
| 4722 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 4723 | NTTP->getLocation(), |
| 4724 | NTTP->getDeclName()); |
| 4725 | // If that worked, check the non-type template parameter type |
| 4726 | // for validity. |
| 4727 | if (!NTTPType.isNull()) |
| 4728 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 4729 | NTTP->getLocation()); |
| 4730 | if (NTTPType.isNull()) |
| 4731 | return true; |
| 4732 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4734 | switch (Arg.getArgument().getKind()) { |
| 4735 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4736 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4737 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4738 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4739 | TemplateArgument Result; |
Erich Keane | c90bb6d | 2018-05-07 17:05:20 +0000 | [diff] [blame] | 4740 | unsigned CurSFINAEErrors = NumSFINAEErrors; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4741 | ExprResult Res = |
| 4742 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 4743 | Result, CTAK); |
| 4744 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4745 | return true; |
Erich Keane | c90bb6d | 2018-05-07 17:05:20 +0000 | [diff] [blame] | 4746 | // If the current template argument causes an error, give up now. |
| 4747 | if (CurSFINAEErrors < NumSFINAEErrors) |
| 4748 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4749 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4750 | // If the resulting expression is new, then use it in place of the |
| 4751 | // old expression in the template argument. |
| 4752 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 4753 | TemplateArgument TA(Res.get()); |
| 4754 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 4755 | } |
| 4756 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4757 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4758 | break; |
| 4759 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4760 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4761 | case TemplateArgument::Declaration: |
| 4762 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4763 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4764 | // We've already checked this template argument, so just copy |
| 4765 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4766 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4767 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4769 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4770 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4771 | // We were given a template template argument. It may not be ill-formed; |
| 4772 | // see below. |
| 4773 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4774 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 4775 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4776 | // We have a template argument such as \c T::template X, which we |
| 4777 | // parsed as a template template argument. However, since we now |
| 4778 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4779 | // template name into an expression. |
| 4780 | |
| 4781 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 4782 | Arg.getTemplateNameLoc()); |
| 4783 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 4784 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4785 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4786 | // FIXME: the template-template arg was a DependentTemplateName, |
| 4787 | // so it was provided with a template keyword. However, its source |
| 4788 | // location is not stored in the template argument structure. |
| 4789 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4790 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 4791 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 4792 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4793 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4794 | // If we parsed the template argument as a pack expansion, create a |
| 4795 | // pack expansion expression. |
| 4796 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4797 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4798 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4799 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4800 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4801 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4802 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4803 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4804 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4805 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4807 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4808 | break; |
| 4809 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4810 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4811 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4812 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4813 | // therefore cannot be a non-type template argument. |
| 4814 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 4815 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4816 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4817 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4818 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4820 | case TemplateArgument::Type: { |
| 4821 | // We have a non-type template parameter but the template |
| 4822 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4824 | // C++ [temp.arg]p2: |
| 4825 | // In a template-argument, an ambiguity between a type-id and |
| 4826 | // an expression is resolved to a type-id, regardless of the |
| 4827 | // form of the corresponding template-parameter. |
| 4828 | // |
| 4829 | // We warn specifically about this case, since it can be rather |
| 4830 | // confusing for users. |
| 4831 | QualType T = Arg.getArgument().getAsType(); |
| 4832 | SourceRange SR = Arg.getSourceRange(); |
| 4833 | if (T->isFunctionType()) |
| 4834 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 4835 | else |
| 4836 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 4837 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4838 | return true; |
| 4839 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4840 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4841 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4842 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4843 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4844 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4845 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4849 | // Check template template parameters. |
| 4850 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4851 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4852 | TemplateParameterList *Params = TempParm->getTemplateParameters(); |
| 4853 | if (TempParm->isExpandedParameterPack()) |
| 4854 | Params = TempParm->getExpansionTemplateParameters(ArgumentPackIndex); |
| 4855 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4856 | // Substitute into the template parameter list of the template |
| 4857 | // template parameter, since previously-supplied template arguments |
| 4858 | // may appear within the template template parameter. |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4859 | // |
| 4860 | // FIXME: Skip this if the parameters aren't instantiation-dependent. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4861 | { |
| 4862 | // Set up a template instantiation context. |
| 4863 | LocalInstantiationScope Scope(*this); |
| 4864 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4865 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4866 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4867 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4868 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4869 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4870 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4871 | Params = SubstTemplateParams(Params, CurContext, |
| 4872 | MultiLevelTemplateArgumentList(TemplateArgs)); |
| 4873 | if (!Params) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4874 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4875 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4876 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4877 | // C++1z [temp.local]p1: (DR1004) |
| 4878 | // When [the injected-class-name] is used [...] as a template-argument for |
| 4879 | // a template template-parameter [...] it refers to the class template |
| 4880 | // itself. |
| 4881 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { |
| 4882 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
| 4883 | Arg.getTypeSourceInfo()->getTypeLoc()); |
| 4884 | if (!ConvertedArg.getArgument().isNull()) |
| 4885 | Arg = ConvertedArg; |
| 4886 | } |
| 4887 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4888 | switch (Arg.getArgument().getKind()) { |
| 4889 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4890 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4891 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4892 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4893 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 4894 | if (CheckTemplateTemplateArgument(Params, Arg)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4895 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4896 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4897 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4898 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4899 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4900 | case TemplateArgument::Expression: |
| 4901 | case TemplateArgument::Type: |
| 4902 | // We have a template template parameter but the template |
| 4903 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4904 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4905 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4906 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4907 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4908 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4909 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4910 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4911 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4912 | case TemplateArgument::NullPtr: |
| 4913 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4914 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4915 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4916 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4917 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4918 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4919 | return false; |
| 4920 | } |
| 4921 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4922 | /// Check whether the template parameter is a pack expansion, and if so, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4923 | /// determine the number of parameters produced by that expansion. For instance: |
| 4924 | /// |
| 4925 | /// \code |
| 4926 | /// template<typename ...Ts> struct A { |
| 4927 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 4928 | /// }; |
| 4929 | /// \endcode |
| 4930 | /// |
| 4931 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 4932 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4933 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4934 | if (NonTypeTemplateParmDecl *NTTP |
| 4935 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4936 | if (NTTP->isExpandedParameterPack()) |
| 4937 | return NTTP->getNumExpansionTypes(); |
| 4938 | } |
| 4939 | |
| 4940 | if (TemplateTemplateParmDecl *TTP |
| 4941 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 4942 | if (TTP->isExpandedParameterPack()) |
| 4943 | return TTP->getNumExpansionTemplateParameters(); |
| 4944 | } |
| 4945 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 4946 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4949 | /// Diagnose a missing template argument. |
| 4950 | template<typename TemplateParmDecl> |
| 4951 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 4952 | TemplateDecl *TD, |
| 4953 | const TemplateParmDecl *D, |
| 4954 | TemplateArgumentListInfo &Args) { |
| 4955 | // Dig out the most recent declaration of the template parameter; there may be |
| 4956 | // declarations of the template that are more recent than TD. |
| 4957 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 4958 | ->getTemplateParameters() |
| 4959 | ->getParam(D->getIndex())); |
| 4960 | |
| 4961 | // If there's a default argument that's not visible, diagnose that we're |
| 4962 | // missing a module import. |
| 4963 | llvm::SmallVector<Module*, 8> Modules; |
| 4964 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 4965 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 4966 | D->getDefaultArgumentLoc(), Modules, |
| 4967 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4968 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4969 | return true; |
| 4970 | } |
| 4971 | |
| 4972 | // FIXME: If there's a more recent default argument that *is* visible, |
| 4973 | // diagnose that it was declared too late. |
| 4974 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 4975 | TemplateParameterList *Params = TD->getTemplateParameters(); |
| 4976 | |
| 4977 | S.Diag(Loc, diag::err_template_arg_list_different_arity) |
| 4978 | << /*not enough args*/0 |
| 4979 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(TD)) |
| 4980 | << TD; |
| 4981 | S.Diag(TD->getLocation(), diag::note_template_decl_here) |
| 4982 | << Params->getSourceRange(); |
| 4983 | return true; |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4986 | /// Check that the given template argument list is well-formed |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4987 | /// for specializing the given template. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4988 | bool Sema::CheckTemplateArgumentList( |
| 4989 | TemplateDecl *Template, SourceLocation TemplateLoc, |
| 4990 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
| 4991 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4992 | bool UpdateArgsWithConversions) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4993 | // Make a copy of the template arguments for processing. Only make the |
| 4994 | // changes at the end when successful in matching the arguments to the |
| 4995 | // template. |
| 4996 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 4997 | |
Erich Keane | af0795b | 2017-10-24 01:39:56 +0000 | [diff] [blame] | 4998 | // Make sure we get the template parameter list from the most |
| 4999 | // recentdeclaration, since that is the only one that has is guaranteed to |
| 5000 | // have all the default template argument information. |
| 5001 | TemplateParameterList *Params = |
| 5002 | cast<TemplateDecl>(Template->getMostRecentDecl()) |
| 5003 | ->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5004 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5005 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5006 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5007 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5008 | // [...] The type and form of each template-argument specified in |
| 5009 | // a template-id shall match the type and form specified for the |
| 5010 | // corresponding parameter declared by the template in its |
| 5011 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5012 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5013 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5014 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 5015 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5016 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5017 | ParamEnd = Params->end(); |
| 5018 | Param != ParamEnd; /* increment in loop */) { |
| 5019 | // If we have an expanded parameter pack, make sure we don't have too |
| 5020 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 5021 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5022 | if (*Expansions == ArgumentPack.size()) { |
| 5023 | // We're done with this parameter pack. Pack up its arguments and add |
| 5024 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5025 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5026 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5027 | ArgumentPack.clear(); |
| 5028 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5029 | // This argument is assigned to the next parameter. |
| 5030 | ++Param; |
| 5031 | continue; |
| 5032 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 5033 | // Not enough arguments for this parameter pack. |
| 5034 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5035 | << /*not enough args*/0 |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 5036 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5037 | << Template; |
| 5038 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5039 | << Params->getSourceRange(); |
| 5040 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5041 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5042 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5043 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5044 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5045 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5046 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5047 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5048 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5049 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5050 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5051 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5052 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5053 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 5054 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5055 | // 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] | 5056 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5057 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5058 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5059 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5060 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5061 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 5062 | return true; |
| 5063 | } |
| 5064 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5065 | // We're now done with this argument. |
| 5066 | ++ArgIdx; |
| 5067 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5068 | if ((*Param)->isTemplateParameterPack()) { |
| 5069 | // The template parameter was a template parameter pack, so take the |
| 5070 | // deduced argument and place it on the argument pack. Note that we |
| 5071 | // stay on the same template parameter so that we can deduce more |
| 5072 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 5073 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5074 | } else { |
| 5075 | // Move to the next template parameter. |
| 5076 | ++Param; |
| 5077 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5078 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5079 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 5080 | // the remaining arguments, because we don't know what parameters they'll |
| 5081 | // match up with. |
| 5082 | if (PackExpansionIntoNonPack) { |
| 5083 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5084 | // If we were part way through filling in an expanded parameter pack, |
| 5085 | // fall back to just producing individual arguments. |
| 5086 | Converted.insert(Converted.end(), |
| 5087 | ArgumentPack.begin(), ArgumentPack.end()); |
| 5088 | ArgumentPack.clear(); |
| 5089 | } |
| 5090 | |
| 5091 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5092 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5093 | ++ArgIdx; |
| 5094 | } |
| 5095 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5096 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5097 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5099 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5100 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5101 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5102 | // If we're checking a partial template argument list, we're done. |
| 5103 | if (PartialTemplateArgs) { |
| 5104 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5105 | Converted.push_back( |
| 5106 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 5107 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5108 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5111 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5112 | // 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] | 5113 | if ((*Param)->isTemplateParameterPack()) { |
| 5114 | assert(!getExpandedPackSize(*Param) && |
| 5115 | "Should have dealt with this already"); |
| 5116 | |
| 5117 | // A non-expanded parameter pack before the end of the parameter list |
| 5118 | // only occurs for an ill-formed template parameter list, unless we've |
| 5119 | // got a partial argument list for a function template, so just bail out. |
| 5120 | if (Param + 1 != ParamEnd) |
| 5121 | return true; |
| 5122 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5123 | Converted.push_back( |
| 5124 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5125 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5126 | |
| 5127 | ++Param; |
| 5128 | continue; |
| 5129 | } |
| 5130 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5131 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5132 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5133 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5134 | // Retrieve the default template argument from the template |
| 5135 | // parameter. For each kind of template parameter, we substitute the |
| 5136 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5137 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5138 | // the default argument. |
| 5139 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5140 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5141 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 5142 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5143 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5144 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5145 | Template, |
| 5146 | TemplateLoc, |
| 5147 | RAngleLoc, |
| 5148 | TTP, |
| 5149 | Converted); |
| 5150 | if (!ArgType) |
| 5151 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5152 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5153 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 5154 | ArgType); |
| 5155 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5156 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5157 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5158 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 5159 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5160 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5161 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5162 | TemplateLoc, |
| 5163 | RAngleLoc, |
| 5164 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5165 | Converted); |
| 5166 | if (E.isInvalid()) |
| 5167 | return true; |
| 5168 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5169 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5170 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 5171 | } else { |
| 5172 | TemplateTemplateParmDecl *TempParm |
| 5173 | = cast<TemplateTemplateParmDecl>(*Param); |
| 5174 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5175 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5176 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 5177 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5178 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 5179 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5180 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5181 | TemplateLoc, |
| 5182 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5183 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5184 | Converted, |
| 5185 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5186 | if (Name.isNull()) |
| 5187 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5188 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5189 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 5190 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5191 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5192 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5193 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 5194 | // the default template argument. We're not actually instantiating a |
| 5195 | // template here, we just create this object to put a note into the |
| 5196 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 5197 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 5198 | SourceRange(TemplateLoc, RAngleLoc)); |
| 5199 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 5200 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5201 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5202 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 5203 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5204 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5205 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5206 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5207 | // Core issue 150 (assumed resolution): if this is a template template |
| 5208 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5209 | // template definition. |
| 5210 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5211 | NewArgs.addArgument(Arg); |
| 5212 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5213 | // Move to the next template parameter and argument. |
| 5214 | ++Param; |
| 5215 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5216 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5217 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5218 | // If we're performing a partial argument substitution, allow any trailing |
| 5219 | // pack expansions; they might be empty. This can happen even if |
| 5220 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 5221 | // still dependent). |
| 5222 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 5223 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5224 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 5225 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5226 | } |
| 5227 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5228 | // If we have any leftover arguments, then there were too many arguments. |
| 5229 | // Complain and fail. |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 5230 | if (ArgIdx < NumArgs) { |
| 5231 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 5232 | << /*too many args*/1 |
| 5233 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
| 5234 | << Template |
| 5235 | << SourceRange(NewArgs[ArgIdx].getLocation(), NewArgs.getRAngleLoc()); |
| 5236 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5237 | << Params->getSourceRange(); |
| 5238 | return true; |
| 5239 | } |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5240 | |
| 5241 | // No problems found with the new argument list, propagate changes back |
| 5242 | // to caller. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5243 | if (UpdateArgsWithConversions) |
| 5244 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5245 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5246 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5247 | } |
| 5248 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5249 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5250 | class UnnamedLocalNoLinkageFinder |
| 5251 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5252 | { |
| 5253 | Sema &S; |
| 5254 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5255 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5256 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5257 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5258 | public: |
| 5259 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 5260 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5261 | bool Visit(QualType T) { |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5262 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5263 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5264 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5265 | #define TYPE(Class, Parent) \ |
| 5266 | bool Visit##Class##Type(const Class##Type *); |
| 5267 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 5268 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5269 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 5270 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5271 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5272 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5273 | bool VisitTagDecl(const TagDecl *Tag); |
| 5274 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 5275 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 5276 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5277 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5278 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5279 | return false; |
| 5280 | } |
| 5281 | |
| 5282 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 5283 | return Visit(T->getElementType()); |
| 5284 | } |
| 5285 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5286 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5287 | return Visit(T->getPointeeType()); |
| 5288 | } |
| 5289 | |
| 5290 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5291 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5292 | return Visit(T->getPointeeType()); |
| 5293 | } |
| 5294 | |
| 5295 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5296 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5297 | return Visit(T->getPointeeType()); |
| 5298 | } |
| 5299 | |
| 5300 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5301 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5302 | return Visit(T->getPointeeType()); |
| 5303 | } |
| 5304 | |
| 5305 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5306 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5307 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 5308 | } |
| 5309 | |
| 5310 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5311 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5312 | return Visit(T->getElementType()); |
| 5313 | } |
| 5314 | |
| 5315 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5316 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5317 | return Visit(T->getElementType()); |
| 5318 | } |
| 5319 | |
| 5320 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5321 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5322 | return Visit(T->getElementType()); |
| 5323 | } |
| 5324 | |
| 5325 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5326 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5327 | return Visit(T->getElementType()); |
| 5328 | } |
| 5329 | |
| 5330 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5331 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5332 | return Visit(T->getElementType()); |
| 5333 | } |
| 5334 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 5335 | bool UnnamedLocalNoLinkageFinder::VisitDependentAddressSpaceType( |
| 5336 | const DependentAddressSpaceType *T) { |
| 5337 | return Visit(T->getPointeeType()); |
| 5338 | } |
| 5339 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5340 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 5341 | return Visit(T->getElementType()); |
| 5342 | } |
| 5343 | |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 5344 | bool UnnamedLocalNoLinkageFinder::VisitDependentVectorType( |
| 5345 | const DependentVectorType *T) { |
| 5346 | return Visit(T->getElementType()); |
| 5347 | } |
| 5348 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5349 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 5350 | return Visit(T->getElementType()); |
| 5351 | } |
| 5352 | |
| 5353 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 5354 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 5355 | for (const auto &A : T->param_types()) { |
| 5356 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5357 | return true; |
| 5358 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5359 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5360 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
| 5363 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 5364 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5365 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 5369 | const UnresolvedUsingType*) { |
| 5370 | return false; |
| 5371 | } |
| 5372 | |
| 5373 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 5374 | return false; |
| 5375 | } |
| 5376 | |
| 5377 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 5378 | return Visit(T->getUnderlyingType()); |
| 5379 | } |
| 5380 | |
| 5381 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 5382 | return false; |
| 5383 | } |
| 5384 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5385 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 5386 | const UnaryTransformType*) { |
| 5387 | return false; |
| 5388 | } |
| 5389 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5390 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 5391 | return Visit(T->getDeducedType()); |
| 5392 | } |
| 5393 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5394 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
| 5395 | const DeducedTemplateSpecializationType *T) { |
| 5396 | return Visit(T->getDeducedType()); |
| 5397 | } |
| 5398 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5399 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 5400 | return VisitTagDecl(T->getDecl()); |
| 5401 | } |
| 5402 | |
| 5403 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 5404 | return VisitTagDecl(T->getDecl()); |
| 5405 | } |
| 5406 | |
| 5407 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 5408 | const TemplateTypeParmType*) { |
| 5409 | return false; |
| 5410 | } |
| 5411 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 5412 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 5413 | const SubstTemplateTypeParmPackType *) { |
| 5414 | return false; |
| 5415 | } |
| 5416 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5417 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 5418 | const TemplateSpecializationType*) { |
| 5419 | return false; |
| 5420 | } |
| 5421 | |
| 5422 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 5423 | const InjectedClassNameType* T) { |
| 5424 | return VisitTagDecl(T->getDecl()); |
| 5425 | } |
| 5426 | |
| 5427 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 5428 | const DependentNameType* T) { |
| 5429 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5430 | } |
| 5431 | |
| 5432 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 5433 | const DependentTemplateSpecializationType* T) { |
| 5434 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5435 | } |
| 5436 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5437 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 5438 | const PackExpansionType* T) { |
| 5439 | return Visit(T->getPattern()); |
| 5440 | } |
| 5441 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5442 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 5443 | return false; |
| 5444 | } |
| 5445 | |
| 5446 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 5447 | const ObjCInterfaceType *) { |
| 5448 | return false; |
| 5449 | } |
| 5450 | |
| 5451 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 5452 | const ObjCObjectPointerType *) { |
| 5453 | return false; |
| 5454 | } |
| 5455 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5456 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 5457 | return Visit(T->getValueType()); |
| 5458 | } |
| 5459 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5460 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 5461 | return false; |
| 5462 | } |
| 5463 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5464 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 5465 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5466 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5467 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5468 | diag::warn_cxx98_compat_template_arg_local_type : |
| 5469 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5470 | << S.Context.getTypeDeclType(Tag) << SR; |
| 5471 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 5474 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5475 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5476 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5477 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 5478 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5479 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 5480 | return true; |
| 5481 | } |
| 5482 | |
| 5483 | return false; |
| 5484 | } |
| 5485 | |
| 5486 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 5487 | NestedNameSpecifier *NNS) { |
| 5488 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 5489 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5490 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5491 | switch (NNS->getKind()) { |
| 5492 | case NestedNameSpecifier::Identifier: |
| 5493 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5494 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5495 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5496 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5497 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5498 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5499 | case NestedNameSpecifier::TypeSpec: |
| 5500 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 5501 | return Visit(QualType(NNS->getAsType(), 0)); |
| 5502 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5503 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5504 | } |
| 5505 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5506 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5507 | /// template type parameter. |
| 5508 | /// |
| 5509 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 5510 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5511 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5512 | TypeSourceInfo *ArgInfo) { |
| 5513 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5514 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 5515 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 5516 | |
| 5517 | if (Arg->isVariablyModifiedType()) { |
| 5518 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5519 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5520 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5523 | // C++03 [temp.arg.type]p2: |
| 5524 | // A local type, a type with no linkage, an unnamed type or a type |
| 5525 | // compounded from any of these types shall not be used as a |
| 5526 | // template-argument for a template type-parameter. |
| 5527 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5528 | // 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] | 5529 | // a warning. |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5530 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5531 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 5532 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 5533 | } |
| 5534 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5535 | return false; |
| 5536 | } |
| 5537 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5538 | enum NullPointerValueKind { |
| 5539 | NPV_NotNullPointer, |
| 5540 | NPV_NullPointer, |
| 5541 | NPV_Error |
| 5542 | }; |
| 5543 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5544 | /// Determine whether the given template argument is a null pointer |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5545 | /// value of the appropriate type. |
| 5546 | static NullPointerValueKind |
| 5547 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5548 | QualType ParamType, Expr *Arg, |
| 5549 | Decl *Entity = nullptr) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5550 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 5551 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5552 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5553 | // dllimport'd entities aren't constant but are available inside of template |
| 5554 | // arguments. |
| 5555 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 5556 | return NPV_NotNullPointer; |
| 5557 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5558 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 5559 | llvm_unreachable( |
| 5560 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5561 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 5562 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5563 | return NPV_NotNullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5564 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5565 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5566 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 5567 | if (ArgRV.isInvalid()) |
| 5568 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5569 | Arg = ArgRV.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5570 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5571 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5572 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5573 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5574 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5575 | EvalResult.HasSideEffects) { |
| 5576 | SourceLocation DiagLoc = Arg->getExprLoc(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5577 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5578 | // If our only note is the usual "invalid subexpression" note, just point |
| 5579 | // the caret at its location rather than producing an essentially |
| 5580 | // redundant note. |
| 5581 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 5582 | diag::note_invalid_subexpr_in_const_expr) { |
| 5583 | DiagLoc = Notes[0].first; |
| 5584 | Notes.clear(); |
| 5585 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5586 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5587 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 5588 | << Arg->getType() << Arg->getSourceRange(); |
| 5589 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 5590 | S.Diag(Notes[I].first, Notes[I].second); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5591 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5592 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5593 | return NPV_Error; |
| 5594 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5595 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5596 | // C++11 [temp.arg.nontype]p1: |
| 5597 | // - an address constant expression of type std::nullptr_t |
| 5598 | if (Arg->getType()->isNullPtrType()) |
| 5599 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5600 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5601 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 5602 | // - a constant expression that evaluates to a null member pointer value |
| 5603 | // (4.11); or |
| 5604 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 5605 | (EvalResult.Val.isMemberPointer() && |
| 5606 | !EvalResult.Val.getMemberPointerDecl())) { |
| 5607 | // If our expression has an appropriate type, we've succeeded. |
| 5608 | bool ObjCLifetimeConversion; |
| 5609 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 5610 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 5611 | ObjCLifetimeConversion)) |
| 5612 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5613 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5614 | // The types didn't match, but we know we got a null pointer; complain, |
| 5615 | // then recover as if the types were correct. |
| 5616 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 5617 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 5618 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5619 | return NPV_NullPointer; |
| 5620 | } |
| 5621 | |
| 5622 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 5623 | // constant, suggest a cast to the appropriate type. |
| 5624 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 5625 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 5626 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5627 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), Code) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5628 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getEndLoc()), |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5629 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5630 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5631 | return NPV_NullPointer; |
| 5632 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5633 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5634 | // FIXME: If we ever want to support general, address-constant expressions |
| 5635 | // as non-type template arguments, we should return the ExprResult here to |
| 5636 | // be interpreted by the caller. |
| 5637 | return NPV_NotNullPointer; |
| 5638 | } |
| 5639 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5640 | /// Checks whether the given template argument is compatible with its |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5641 | /// template parameter. |
| 5642 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 5643 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 5644 | Expr *Arg, QualType ArgType) { |
| 5645 | bool ObjCLifetimeConversion; |
| 5646 | if (ParamType->isPointerType() && |
| 5647 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 5648 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 5649 | ObjCLifetimeConversion)) { |
| 5650 | // For pointer-to-object types, qualification conversions are |
| 5651 | // permitted. |
| 5652 | } else { |
| 5653 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 5654 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 5655 | // C++ [temp.arg.nontype]p5b3: |
| 5656 | // For a non-type template-parameter of type reference to |
| 5657 | // object, no conversions apply. The type referred to by the |
| 5658 | // reference may be more cv-qualified than the (otherwise |
| 5659 | // identical) type of the template- argument. The |
| 5660 | // template-parameter is bound directly to the |
| 5661 | // template-argument, which shall be an lvalue. |
| 5662 | |
| 5663 | // FIXME: Other qualifiers? |
| 5664 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 5665 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 5666 | |
| 5667 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5668 | S.Diag(Arg->getBeginLoc(), |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5669 | diag::err_template_arg_ref_bind_ignores_quals) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5670 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5671 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5672 | return true; |
| 5673 | } |
| 5674 | } |
| 5675 | } |
| 5676 | |
| 5677 | // At this point, the template argument refers to an object or |
| 5678 | // function with external linkage. We now need to check whether the |
| 5679 | // argument and parameter types are compatible. |
| 5680 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 5681 | ParamType.getNonReferenceType())) { |
| 5682 | // We can't perform this conversion or binding. |
| 5683 | if (ParamType->isReferenceType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5684 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_no_ref_bind) |
| 5685 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5686 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5687 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 5688 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5689 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5690 | return true; |
| 5691 | } |
| 5692 | } |
| 5693 | |
| 5694 | return false; |
| 5695 | } |
| 5696 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5697 | /// Checks whether the given template argument is the address |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5698 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5699 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5700 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 5701 | NonTypeTemplateParmDecl *Param, |
| 5702 | QualType ParamType, |
| 5703 | Expr *ArgIn, |
| 5704 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5705 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5706 | Expr *Arg = ArgIn; |
| 5707 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5708 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5709 | bool AddressTaken = false; |
| 5710 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5711 | if (S.getLangOpts().MicrosoftExt) { |
| 5712 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 5713 | // dereference and address-of operators. |
| 5714 | Arg = Arg->IgnoreParenCasts(); |
| 5715 | |
| 5716 | bool ExtWarnMSTemplateArg = false; |
| 5717 | UnaryOperatorKind FirstOpKind; |
| 5718 | SourceLocation FirstOpLoc; |
| 5719 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5720 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 5721 | if (UnOpKind == UO_Deref) |
| 5722 | ExtWarnMSTemplateArg = true; |
| 5723 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 5724 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 5725 | if (!AddrOpLoc.isValid()) { |
| 5726 | FirstOpKind = UnOpKind; |
| 5727 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 5728 | } |
| 5729 | } else |
| 5730 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5731 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5732 | if (FirstOpLoc.isValid()) { |
| 5733 | if (ExtWarnMSTemplateArg) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5734 | S.Diag(ArgIn->getBeginLoc(), diag::ext_ms_deref_template_argument) |
| 5735 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5736 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5737 | if (FirstOpKind == UO_AddrOf) |
| 5738 | AddressTaken = true; |
| 5739 | else if (Arg->getType()->isPointerType()) { |
| 5740 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 5741 | // constant expression. |
| 5742 | assert(FirstOpKind == UO_Deref); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5743 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 5744 | << Arg->getSourceRange(); |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5745 | } |
| 5746 | } |
| 5747 | } else { |
| 5748 | // See through any implicit casts we added to fix the type. |
| 5749 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5750 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5751 | // C++ [temp.arg.nontype]p1: |
| 5752 | // |
| 5753 | // A template-argument for a non-type, non-template |
| 5754 | // template-parameter shall be one of: [...] |
| 5755 | // |
| 5756 | // -- the address of an object or function with external |
| 5757 | // linkage, including function templates and function |
| 5758 | // template-ids but excluding non-static class members, |
| 5759 | // expressed as & id-expression where the & is optional if |
| 5760 | // the name refers to a function or array, or if the |
| 5761 | // corresponding template-parameter is a reference; or |
| 5762 | |
| 5763 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 5764 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 5765 | bool ExtraParens = false; |
| 5766 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 5767 | if (!Invalid && !ExtraParens) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5768 | S.Diag(Arg->getBeginLoc(), |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5769 | S.getLangOpts().CPlusPlus11 |
| 5770 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 5771 | : diag::ext_template_arg_extra_parens) |
| 5772 | << Arg->getSourceRange(); |
| 5773 | ExtraParens = true; |
| 5774 | } |
| 5775 | |
| 5776 | Arg = Parens->getSubExpr(); |
| 5777 | } |
| 5778 | |
| 5779 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5780 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5781 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5782 | |
| 5783 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5784 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 5785 | Arg = UnOp->getSubExpr(); |
| 5786 | AddressTaken = true; |
| 5787 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 5788 | } |
| 5789 | } |
| 5790 | |
| 5791 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5792 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5793 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5794 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5795 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5796 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 5797 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 5798 | |
| 5799 | // If our parameter has pointer type, check for a null template value. |
| 5800 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 5801 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn, |
| 5802 | Entity)) { |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5803 | case NPV_NullPointer: |
| 5804 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5805 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 5806 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5807 | return false; |
| 5808 | |
| 5809 | case NPV_Error: |
| 5810 | return true; |
| 5811 | |
| 5812 | case NPV_NotNullPointer: |
| 5813 | break; |
| 5814 | } |
| 5815 | } |
| 5816 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5817 | // Stop checking the precise nature of the argument if it is value dependent, |
| 5818 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5819 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5820 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5821 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5822 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5823 | |
| 5824 | if (isa<CXXUuidofExpr>(Arg)) { |
| 5825 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 5826 | ArgIn, Arg, ArgType)) |
| 5827 | return true; |
| 5828 | |
| 5829 | Converted = TemplateArgument(ArgIn); |
| 5830 | return false; |
| 5831 | } |
| 5832 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5833 | if (!DRE) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5834 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 5835 | << Arg->getSourceRange(); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5836 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5837 | return true; |
| 5838 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5839 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5840 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 5841 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5842 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_field) |
| 5843 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5844 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5845 | return true; |
| 5846 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5847 | |
| 5848 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5849 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5850 | if (!Method->isStatic()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5851 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_method) |
| 5852 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5853 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5854 | return true; |
| 5855 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5856 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5857 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5858 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 5859 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5860 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5861 | // A non-type template argument must refer to an object or function. |
| 5862 | if (!Func && !Var) { |
| 5863 | // We found something, but we don't know specifically what it is. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5864 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_object_or_func) |
| 5865 | << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5866 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 5867 | return true; |
| 5868 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5869 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5870 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5871 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5872 | S.Diag(Arg->getBeginLoc(), |
| 5873 | S.getLangOpts().CPlusPlus11 |
| 5874 | ? diag::warn_cxx98_compat_template_arg_object_internal |
| 5875 | : diag::ext_template_arg_object_internal) |
| 5876 | << !Func << Entity << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5877 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5878 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5879 | } else if (!Entity->hasLinkage()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5880 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_object_no_linkage) |
| 5881 | << !Func << Entity << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5882 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5883 | << !Func; |
| 5884 | return true; |
| 5885 | } |
| 5886 | |
| 5887 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5888 | // If the template parameter has pointer type, the function decays. |
| 5889 | if (ParamType->isPointerType() && !AddressTaken) |
| 5890 | ArgType = S.Context.getPointerType(Func->getType()); |
| 5891 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 5892 | // If we originally had an address-of operator, but the |
| 5893 | // parameter has reference type, complain and (if things look |
| 5894 | // like they will work) drop the address-of operator. |
| 5895 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 5896 | ParamType.getNonReferenceType())) { |
| 5897 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5898 | << ParamType; |
| 5899 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5900 | return true; |
| 5901 | } |
| 5902 | |
| 5903 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5904 | << ParamType |
| 5905 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5906 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5907 | |
| 5908 | ArgType = Func->getType(); |
| 5909 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5910 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5911 | // A value of reference type is not an object. |
| 5912 | if (Var->getType()->isReferenceType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5913 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_reference_var) |
| 5914 | << Var->getType() << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5915 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5916 | return true; |
| 5917 | } |
| 5918 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5919 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 5920 | if (Var->getTLSKind()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5921 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_thread_local) |
| 5922 | << Arg->getSourceRange(); |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5923 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 5924 | return true; |
| 5925 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5926 | |
| 5927 | // If the template parameter has pointer type, we must have taken |
| 5928 | // the address of this object. |
| 5929 | if (ParamType->isReferenceType()) { |
| 5930 | if (AddressTaken) { |
| 5931 | // If we originally had an address-of operator, but the |
| 5932 | // parameter has reference type, complain and (if things look |
| 5933 | // like they will work) drop the address-of operator. |
| 5934 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 5935 | ParamType.getNonReferenceType())) { |
| 5936 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5937 | << ParamType; |
| 5938 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5939 | return true; |
| 5940 | } |
| 5941 | |
| 5942 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5943 | << ParamType |
| 5944 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5945 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5946 | |
| 5947 | ArgType = Var->getType(); |
| 5948 | } |
| 5949 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 5950 | if (Var->getType()->isArrayType()) { |
| 5951 | // Array-to-pointer decay. |
| 5952 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 5953 | } else { |
| 5954 | // If the template parameter has pointer type but the address of |
| 5955 | // this object was not taken, complain and (possibly) recover by |
| 5956 | // taking the address of the entity. |
| 5957 | ArgType = S.Context.getPointerType(Var->getType()); |
| 5958 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5959 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 5960 | << ParamType; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5961 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5962 | return true; |
| 5963 | } |
| 5964 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5965 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 5966 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), "&"); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5967 | |
| 5968 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5969 | } |
| 5970 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5971 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5972 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5973 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 5974 | Arg, ArgType)) |
| 5975 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5976 | |
| 5977 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5978 | Converted = |
| 5979 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5980 | S.MarkAnyDeclReferenced(Arg->getBeginLoc(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5981 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5984 | /// Checks whether the given template argument is a pointer to |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5985 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5986 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 5987 | NonTypeTemplateParmDecl *Param, |
| 5988 | QualType ParamType, |
| 5989 | Expr *&ResultArg, |
| 5990 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5991 | bool Invalid = false; |
| 5992 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5993 | Expr *Arg = ResultArg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5994 | bool ObjCLifetimeConversion; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5995 | |
| 5996 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5997 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5998 | // A template-argument for a non-type, non-template |
| 5999 | // template-parameter shall be one of: [...] |
| 6000 | // |
| 6001 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6002 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6003 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6004 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 6005 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 6006 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6007 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6008 | if (!Invalid && !ExtraParens) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6009 | S.Diag(Arg->getBeginLoc(), |
| 6010 | S.getLangOpts().CPlusPlus11 |
| 6011 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 6012 | : diag::ext_template_arg_extra_parens) |
| 6013 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6014 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6015 | } |
| 6016 | |
| 6017 | Arg = Parens->getSubExpr(); |
| 6018 | } |
| 6019 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6020 | while (SubstNonTypeTemplateParmExpr *subst = |
| 6021 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 6022 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 6023 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6024 | // A pointer-to-member constant written &Class::member. |
| 6025 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6026 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6027 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 6028 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6029 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6030 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6031 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6032 | // A constant of pointer-to-member type. |
| 6033 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6034 | ValueDecl *VD = DRE->getDecl(); |
| 6035 | if (VD->getType()->isMemberPointerType()) { |
| 6036 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
| 6037 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6038 | Converted = TemplateArgument(Arg); |
| 6039 | } else { |
| 6040 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
| 6041 | Converted = TemplateArgument(VD, ParamType); |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6042 | } |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6043 | return Invalid; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6044 | } |
| 6045 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6046 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6047 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6048 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6049 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 6050 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 6051 | |
| 6052 | // Check for a null pointer value. |
| 6053 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ResultArg, |
| 6054 | Entity)) { |
| 6055 | case NPV_Error: |
| 6056 | return true; |
| 6057 | case NPV_NullPointer: |
| 6058 | S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
| 6059 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 6060 | /*isNullPtr*/true); |
| 6061 | return false; |
| 6062 | case NPV_NotNullPointer: |
| 6063 | break; |
| 6064 | } |
| 6065 | |
| 6066 | if (S.IsQualificationConversion(ResultArg->getType(), |
| 6067 | ParamType.getNonReferenceType(), false, |
| 6068 | ObjCLifetimeConversion)) { |
| 6069 | ResultArg = S.ImpCastExprToType(ResultArg, ParamType, CK_NoOp, |
| 6070 | ResultArg->getValueKind()) |
| 6071 | .get(); |
| 6072 | } else if (!S.Context.hasSameUnqualifiedType( |
| 6073 | ResultArg->getType(), ParamType.getNonReferenceType())) { |
| 6074 | // We can't perform this conversion. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6075 | S.Diag(ResultArg->getBeginLoc(), diag::err_template_arg_not_convertible) |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 6076 | << ResultArg->getType() << ParamType << ResultArg->getSourceRange(); |
| 6077 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6078 | return true; |
| 6079 | } |
| 6080 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6081 | if (!DRE) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6082 | return S.Diag(Arg->getBeginLoc(), |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6083 | diag::err_template_arg_not_pointer_to_member_form) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6084 | << Arg->getSourceRange(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6085 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6086 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 6087 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 6088 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6089 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6090 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6091 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 6092 | "Only non-static member pointers can make it here"); |
| 6093 | |
| 6094 | // Okay: this is the address of a non-static member, and therefore |
| 6095 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6096 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 6097 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6098 | } else { |
| 6099 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 6100 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6101 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6102 | return Invalid; |
| 6103 | } |
| 6104 | |
| 6105 | // 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] | 6106 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_pointer_to_member_form) |
| 6107 | << Arg->getSourceRange(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6108 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6109 | return true; |
| 6110 | } |
| 6111 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6112 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6113 | /// non-type template parameter. |
| 6114 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 6115 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6116 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6117 | /// returns the converted template argument. \p ParamType is the |
| 6118 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6119 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6120 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6121 | TemplateArgument &Converted, |
| 6122 | CheckTemplateArgumentKind CTAK) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6123 | SourceLocation StartLoc = Arg->getBeginLoc(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6124 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6125 | // If the parameter type somehow involves auto, deduce the type now. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6126 | if (getLangOpts().CPlusPlus17 && ParamType->isUndeducedType()) { |
Richard Smith | 4ae5ec8 | 2017-02-22 20:01:55 +0000 | [diff] [blame] | 6127 | // During template argument deduction, we allow 'decltype(auto)' to |
| 6128 | // match an arbitrary dependent argument. |
| 6129 | // FIXME: The language rules don't say what happens in this case. |
| 6130 | // FIXME: We get an opaque dependent type out of decltype(auto) if the |
| 6131 | // expression is merely instantiation-dependent; is this enough? |
| 6132 | if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) { |
| 6133 | auto *AT = dyn_cast<AutoType>(ParamType); |
| 6134 | if (AT && AT->isDecltypeAuto()) { |
| 6135 | Converted = TemplateArgument(Arg); |
| 6136 | return Arg; |
| 6137 | } |
| 6138 | } |
| 6139 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6140 | // When checking a deduced template argument, deduce from its type even if |
| 6141 | // the type is dependent, in order to check the types of non-type template |
| 6142 | // arguments line up properly in partial ordering. |
| 6143 | Optional<unsigned> Depth; |
| 6144 | if (CTAK != CTAK_Specified) |
| 6145 | Depth = Param->getDepth() + 1; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6146 | if (DeduceAutoType( |
| 6147 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6148 | Arg, ParamType, Depth) == DAR_Failed) { |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6149 | Diag(Arg->getExprLoc(), |
| 6150 | diag::err_non_type_template_parm_type_deduction_failure) |
| 6151 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 6152 | << Arg->getSourceRange(); |
| 6153 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6154 | return ExprError(); |
| 6155 | } |
| 6156 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 6157 | // an error. The error message normally references the parameter |
| 6158 | // declaration, but here we'll pass the argument location because that's |
| 6159 | // where the parameter type is deduced. |
| 6160 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 6161 | if (ParamType.isNull()) { |
| 6162 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6163 | return ExprError(); |
| 6164 | } |
| 6165 | } |
| 6166 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6167 | // We should have already dropped all cv-qualifiers by now. |
| 6168 | assert(!ParamType.hasQualifiers() && |
| 6169 | "non-type template parameter type cannot be qualified"); |
| 6170 | |
| 6171 | if (CTAK == CTAK_Deduced && |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 6172 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6173 | Arg->getType())) { |
Richard Smith | 957fbf1 | 2017-01-17 02:14:37 +0000 | [diff] [blame] | 6174 | // FIXME: If either type is dependent, we skip the check. This isn't |
| 6175 | // correct, since during deduction we're supposed to have replaced each |
| 6176 | // template parameter with some unique (non-dependent) placeholder. |
| 6177 | // FIXME: If the argument type contains 'auto', we carry on and fail the |
| 6178 | // type check in order to force specific types to be more specialized than |
| 6179 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to |
| 6180 | // work. |
| 6181 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
| 6182 | !Arg->getType()->getContainedAutoType()) { |
| 6183 | Converted = TemplateArgument(Arg); |
| 6184 | return Arg; |
| 6185 | } |
| 6186 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, |
| 6187 | // we should actually be checking the type of the template argument in P, |
| 6188 | // not the type of the template argument deduced from A, against the |
| 6189 | // template parameter type. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6190 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6191 | << Arg->getType() |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6192 | << ParamType.getUnqualifiedType(); |
| 6193 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6194 | return ExprError(); |
| 6195 | } |
| 6196 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6197 | // If either the parameter has a dependent type or the argument is |
| 6198 | // type-dependent, there's nothing we can check now. |
| 6199 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 6200 | // FIXME: Produce a cloned, canonical expression? |
| 6201 | Converted = TemplateArgument(Arg); |
| 6202 | return Arg; |
| 6203 | } |
| 6204 | |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6205 | // The initialization of the parameter from the argument is |
| 6206 | // a constant-evaluated context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6207 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 6208 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6209 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6210 | if (getLangOpts().CPlusPlus17) { |
| 6211 | // C++17 [temp.arg.nontype]p1: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6212 | // A template-argument for a non-type template parameter shall be |
| 6213 | // a converted constant expression of the type of the template-parameter. |
| 6214 | APValue Value; |
| 6215 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 6216 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 6217 | if (ArgResult.isInvalid()) |
| 6218 | return ExprError(); |
| 6219 | |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6220 | // For a value-dependent argument, CheckConvertedConstantExpression is |
| 6221 | // permitted (and expected) to be unable to determine a value. |
| 6222 | if (ArgResult.get()->isValueDependent()) { |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6223 | Converted = TemplateArgument(ArgResult.get()); |
| 6224 | return ArgResult; |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6227 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 6228 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6229 | // Convert the APValue to a TemplateArgument. |
| 6230 | switch (Value.getKind()) { |
| 6231 | case APValue::Uninitialized: |
| 6232 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6233 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6234 | break; |
| 6235 | case APValue::Int: |
| 6236 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6237 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6238 | break; |
| 6239 | case APValue::MemberPointer: { |
| 6240 | assert(ParamType->isMemberPointerType()); |
| 6241 | |
| 6242 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 6243 | if (!Value.getMemberPointerPath().empty()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6244 | Diag(Arg->getBeginLoc(), |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6245 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 6246 | << Value.getMemberPointerDecl() << ParamType |
| 6247 | << Arg->getSourceRange(); |
| 6248 | return ExprError(); |
| 6249 | } |
| 6250 | |
| 6251 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6252 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6253 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6254 | break; |
| 6255 | } |
| 6256 | case APValue::LValue: { |
| 6257 | // For a non-type template-parameter of pointer or reference type, |
| 6258 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6259 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 6260 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6261 | // -- a temporary object |
| 6262 | // -- a string literal |
| 6263 | // -- the result of a typeid expression, or |
Eric Christopher | 0d2c56a | 2017-03-31 01:45:39 +0000 | [diff] [blame] | 6264 | // -- a predefined __func__ variable |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6265 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 6266 | if (isa<CXXUuidofExpr>(E)) { |
Nico Weber | d60bbce | 2018-05-17 15:26:37 +0000 | [diff] [blame] | 6267 | Converted = TemplateArgument(ArgResult.get()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6268 | break; |
| 6269 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6270 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 6271 | << Arg->getSourceRange(); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6272 | return ExprError(); |
| 6273 | } |
| 6274 | auto *VD = const_cast<ValueDecl *>( |
| 6275 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 6276 | // -- a subobject |
| 6277 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 6278 | VD && VD->getType()->isArrayType() && |
| 6279 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 6280 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 6281 | // Per defect report (no number yet): |
| 6282 | // ... other than a pointer to the first element of a complete array |
| 6283 | // object. |
| 6284 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 6285 | Value.isLValueOnePastTheEnd()) { |
| 6286 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 6287 | << Value.getAsString(Context, ParamType); |
| 6288 | return ExprError(); |
| 6289 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6290 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6291 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6292 | assert((!VD || !ParamType->isNullPtrType()) && |
| 6293 | "non-null value of type nullptr_t?"); |
| 6294 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6295 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6296 | break; |
| 6297 | } |
| 6298 | case APValue::AddrLabelDiff: |
| 6299 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 6300 | case APValue::Float: |
| 6301 | case APValue::ComplexInt: |
| 6302 | case APValue::ComplexFloat: |
| 6303 | case APValue::Vector: |
| 6304 | case APValue::Array: |
| 6305 | case APValue::Struct: |
| 6306 | case APValue::Union: |
| 6307 | llvm_unreachable("invalid kind for template argument"); |
| 6308 | } |
| 6309 | |
| 6310 | return ArgResult.get(); |
| 6311 | } |
| 6312 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6313 | // C++ [temp.arg.nontype]p5: |
| 6314 | // The following conversions are performed on each expression used |
| 6315 | // as a non-type template-argument. If a non-type |
| 6316 | // template-argument cannot be converted to the type of the |
| 6317 | // corresponding template-parameter then the program is |
| 6318 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6319 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6320 | // C++11: |
| 6321 | // -- for a non-type template-parameter of integral or |
| 6322 | // enumeration type, conversions permitted in a converted |
| 6323 | // constant expression are applied. |
| 6324 | // |
| 6325 | // C++98: |
| 6326 | // -- for a non-type template-parameter of integral or |
| 6327 | // enumeration type, integral promotions (4.5) and integral |
| 6328 | // conversions (4.7) are applied. |
| 6329 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6330 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6331 | // C++ [temp.arg.nontype]p1: |
| 6332 | // A template-argument for a non-type, non-template template-parameter |
| 6333 | // shall be one of: |
| 6334 | // |
| 6335 | // -- for a non-type template-parameter of integral or enumeration |
| 6336 | // type, a converted constant expression of the type of the |
| 6337 | // template-parameter; or |
| 6338 | llvm::APSInt Value; |
| 6339 | ExprResult ArgResult = |
| 6340 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 6341 | CCEK_TemplateArg); |
| 6342 | if (ArgResult.isInvalid()) |
| 6343 | return ExprError(); |
| 6344 | |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6345 | // We can't check arbitrary value-dependent arguments. |
| 6346 | if (ArgResult.get()->isValueDependent()) { |
| 6347 | Converted = TemplateArgument(ArgResult.get()); |
| 6348 | return ArgResult; |
| 6349 | } |
| 6350 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6351 | // Widen the argument value to sizeof(parameter type). This is almost |
| 6352 | // always a no-op, except when the parameter type is bool. In |
| 6353 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 6354 | QualType IntegerType = ParamType; |
| 6355 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 6356 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 6357 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 6358 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6359 | Converted = TemplateArgument(Context, Value, |
| 6360 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6361 | return ArgResult; |
| 6362 | } |
| 6363 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6364 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 6365 | if (ArgResult.isInvalid()) |
| 6366 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6367 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6368 | |
| 6369 | QualType ArgType = Arg->getType(); |
| 6370 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6371 | // C++ [temp.arg.nontype]p1: |
| 6372 | // A template-argument for a non-type, non-template |
| 6373 | // template-parameter shall be one of: |
| 6374 | // |
| 6375 | // -- an integral constant-expression of integral or enumeration |
| 6376 | // type; or |
| 6377 | // -- the name of a non-type template-parameter; or |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6378 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6379 | if (!ArgType->isIntegralOrEnumerationType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6380 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_integral_or_enumeral) |
| 6381 | << ArgType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6382 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6383 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6384 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6385 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 6386 | QualType T; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6387 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6388 | public: |
| 6389 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 6390 | |
| 6391 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 6392 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6393 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 6394 | } |
| 6395 | } Diagnoser(ArgType); |
| 6396 | |
| 6397 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6398 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6399 | if (!Arg) |
| 6400 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6403 | // From here on out, all we care about is the unqualified form |
| 6404 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6405 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6406 | |
| 6407 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 6408 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6409 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6410 | } else if (ParamType->isBooleanType()) { |
| 6411 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6412 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6413 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 6414 | !ParamType->isEnumeralType()) { |
| 6415 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6416 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6417 | } else { |
| 6418 | // We can't perform this conversion. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6419 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 6420 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6421 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6422 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6423 | } |
| 6424 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6425 | // Add the value of this argument to the list of converted |
| 6426 | // arguments. We use the bitwidth and signedness of the template |
| 6427 | // parameter. |
| 6428 | if (Arg->isValueDependent()) { |
| 6429 | // The argument is value-dependent. Create a new |
| 6430 | // TemplateArgument with the converted expression. |
| 6431 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6432 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6435 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6436 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 6437 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6438 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6439 | if (ParamType->isBooleanType()) { |
| 6440 | // Value must be zero or one. |
| 6441 | Value = Value != 0; |
| 6442 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 6443 | if (Value.getBitWidth() != AllowedBits) |
| 6444 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6445 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6446 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6447 | llvm::APSInt OldValue = Value; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6448 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6449 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6450 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6451 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6452 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6453 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6454 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6455 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6456 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6457 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6458 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6459 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_negative) |
| 6460 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6461 | << Arg->getSourceRange(); |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6462 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6463 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6464 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6465 | // Complain if we overflowed the template parameter's type. |
| 6466 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6467 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6468 | RequiredBits = OldValue.getActiveBits(); |
| 6469 | else if (OldValue.isUnsigned()) |
| 6470 | RequiredBits = OldValue.getActiveBits() + 1; |
| 6471 | else |
| 6472 | RequiredBits = OldValue.getMinSignedBits(); |
| 6473 | if (RequiredBits > AllowedBits) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6474 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_too_large) |
| 6475 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6476 | << Arg->getSourceRange(); |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6477 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6478 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6479 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6480 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6481 | Converted = TemplateArgument(Context, Value, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6482 | ParamType->isEnumeralType() |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 6483 | ? Context.getCanonicalType(ParamType) |
| 6484 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6485 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6486 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6487 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6488 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6489 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 6490 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6491 | // Handle pointer-to-function, reference-to-function, and |
| 6492 | // pointer-to-member-function all in (roughly) the same way. |
| 6493 | if (// -- For a non-type template-parameter of type pointer to |
| 6494 | // function, only the function-to-pointer conversion (4.3) is |
| 6495 | // applied. If the template-argument represents a set of |
| 6496 | // overloaded functions (or a pointer to such), the matching |
| 6497 | // function is selected from the set (13.4). |
| 6498 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6499 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6500 | // -- For a non-type template-parameter of type reference to |
| 6501 | // function, no conversions apply. If the template-argument |
| 6502 | // represents a set of overloaded functions, the matching |
| 6503 | // function is selected from the set (13.4). |
| 6504 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6505 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6506 | // -- For a non-type template-parameter of type pointer to |
| 6507 | // member function, no conversions apply. If the |
| 6508 | // template-argument represents a set of overloaded member |
| 6509 | // functions, the matching member function is selected from |
| 6510 | // the set (13.4). |
| 6511 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6512 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6513 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6514 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6515 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6516 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6517 | true, |
| 6518 | FoundResult)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6519 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6520 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6521 | |
| 6522 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6523 | ArgType = Arg->getType(); |
| 6524 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6525 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6526 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6527 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6528 | if (!ParamType->isMemberPointerType()) { |
| 6529 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6530 | ParamType, |
| 6531 | Arg, Converted)) |
| 6532 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6533 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6534 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6535 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6536 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6537 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6538 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6539 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 6542 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6543 | // -- for a non-type template-parameter of type pointer to |
| 6544 | // object, qualification conversions (4.4) and the |
| 6545 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6546 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6547 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6548 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6549 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6550 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6551 | ParamType, |
| 6552 | Arg, Converted)) |
| 6553 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6554 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6555 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6556 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6557 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6558 | // -- For a non-type template-parameter of type reference to |
| 6559 | // object, no conversions apply. The type referred to by the |
| 6560 | // reference may be more cv-qualified than the (otherwise |
| 6561 | // identical) type of the template-argument. The |
| 6562 | // template-parameter is bound directly to the |
| 6563 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6564 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6565 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6566 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6567 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6568 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 6569 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6570 | true, |
| 6571 | FoundResult)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6572 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6573 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6574 | |
| 6575 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6576 | ArgType = Arg->getType(); |
| 6577 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6578 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6579 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6580 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6581 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6582 | ParamType, |
| 6583 | Arg, Converted)) |
| 6584 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6585 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6586 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6587 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6588 | // Deal with parameters of type std::nullptr_t. |
| 6589 | if (ParamType->isNullPtrType()) { |
| 6590 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6591 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6592 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6593 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6594 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6595 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 6596 | case NPV_NotNullPointer: |
| 6597 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 6598 | << Arg->getType() << ParamType; |
| 6599 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6600 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6601 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6602 | case NPV_Error: |
| 6603 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6604 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6605 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 6606 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 6607 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 6608 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6609 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6610 | } |
| 6611 | } |
| 6612 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6613 | // -- For a non-type template-parameter of type pointer to data |
| 6614 | // member, qualification conversions (4.4) are applied. |
| 6615 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 6616 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6617 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6618 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6619 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6620 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6623 | static void DiagnoseTemplateParameterListArityMismatch( |
| 6624 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 6625 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 6626 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6627 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6628 | /// template template parameter. |
| 6629 | /// |
| 6630 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 6631 | /// It returns true if an error occurred, and false otherwise. |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 6632 | bool Sema::CheckTemplateTemplateArgument(TemplateParameterList *Params, |
| 6633 | TemplateArgumentLoc &Arg) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6634 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6635 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 6636 | if (!Template) { |
| 6637 | // Any dependent template name is fine. |
| 6638 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 6639 | return false; |
| 6640 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6641 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6642 | if (Template->isInvalidDecl()) |
| 6643 | return true; |
| 6644 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6645 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6646 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6647 | // the name of a class template or an alias template, expressed as an |
| 6648 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6649 | // primary class templates are considered when matching the |
| 6650 | // template template argument with the corresponding parameter; |
| 6651 | // partial specializations are not considered even if their |
| 6652 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6653 | // |
| 6654 | // Note that we also allow template template parameters here, which |
| 6655 | // will happen when we are dealing with, e.g., class template |
| 6656 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6657 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6658 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6659 | !isa<TypeAliasTemplateDecl>(Template) && |
| 6660 | !isa<BuiltinTemplateDecl>(Template)) { |
| 6661 | assert(isa<FunctionTemplateDecl>(Template) && |
| 6662 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 6663 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6664 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 6665 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6668 | // C++1z [temp.arg.template]p3: (DR 150) |
| 6669 | // A template-argument matches a template template-parameter P when P |
| 6670 | // is at least as specialized as the template-argument A. |
| 6671 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 6672 | // Quick check for the common case: |
| 6673 | // If P contains a parameter pack, then A [...] matches P if each of A's |
| 6674 | // template parameters matches the corresponding template parameter in |
| 6675 | // the template-parameter-list of P. |
| 6676 | if (TemplateParameterListsAreEqual( |
| 6677 | Template->getTemplateParameters(), Params, false, |
| 6678 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 6679 | return false; |
| 6680 | |
| 6681 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 6682 | Arg.getLocation())) |
| 6683 | return false; |
| 6684 | // FIXME: Produce better diagnostics for deduction failures. |
| 6685 | } |
| 6686 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6687 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 6688 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6689 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6690 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6691 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6694 | /// Given a non-type template argument that refers to a |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6695 | /// declaration and the type of its corresponding non-type template |
| 6696 | /// parameter, produce an expression that properly refers to that |
| 6697 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6698 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6699 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 6700 | QualType ParamType, |
| 6701 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 6702 | // C++ [temp.param]p8: |
| 6703 | // |
| 6704 | // A non-type template-parameter of type "array of T" or |
| 6705 | // "function returning T" is adjusted to be of type "pointer to |
| 6706 | // T" or "pointer to function returning T", respectively. |
| 6707 | if (ParamType->isArrayType()) |
| 6708 | ParamType = Context.getArrayDecayedType(ParamType); |
| 6709 | else if (ParamType->isFunctionType()) |
| 6710 | ParamType = Context.getPointerType(ParamType); |
| 6711 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6712 | // For a NULL non-type template argument, return nullptr casted to the |
| 6713 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6714 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6715 | return ImpCastExprToType( |
| 6716 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 6717 | ParamType, |
| 6718 | ParamType->getAs<MemberPointerType>() |
| 6719 | ? CK_NullToMemberPointer |
| 6720 | : CK_NullToPointer); |
| 6721 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6722 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 6723 | "Only declaration template arguments permitted here"); |
| 6724 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6725 | ValueDecl *VD = Arg.getAsDecl(); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6726 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6727 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 6728 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 6729 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6730 | // If the value is a class member, we might have a pointer-to-member. |
| 6731 | // Determine whether the non-type template template parameter is of |
| 6732 | // pointer-to-member type. If so, we need to build an appropriate |
| 6733 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 6734 | // would refer to the member itself. |
| 6735 | if (ParamType->isMemberPointerType()) { |
| 6736 | QualType ClassType |
| 6737 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 6738 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6739 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6740 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6741 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 6742 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6743 | |
| 6744 | // The actual value-ness of this is unimportant, but for |
| 6745 | // internal consistency's sake, references to instance methods |
| 6746 | // are r-values. |
| 6747 | ExprValueKind VK = VK_LValue; |
| 6748 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 6749 | VK = VK_RValue; |
| 6750 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6751 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6752 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6753 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6754 | Loc, |
| 6755 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6756 | if (RefExpr.isInvalid()) |
| 6757 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6758 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6759 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6760 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6761 | // We might need to perform a trailing qualification conversion, since |
| 6762 | // the element type on the parameter could be more qualified than the |
| 6763 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6764 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6765 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6766 | ParamType.getUnqualifiedType(), false, |
| 6767 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6768 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6769 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6770 | assert(!RefExpr.isInvalid() && |
| 6771 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6772 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6773 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6774 | } |
| 6775 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6776 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6777 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6778 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6779 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6780 | // When the non-type template parameter is a pointer, take the |
| 6781 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6782 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6783 | if (RefExpr.isInvalid()) |
| 6784 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6785 | |
Richard Smith | fc6fca1 | 2017-01-28 00:38:35 +0000 | [diff] [blame] | 6786 | if (!Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
| 6787 | (T->isFunctionType() || T->isArrayType())) { |
| 6788 | // Decay functions and arrays unless we're forming a pointer to array. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6789 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6790 | if (RefExpr.isInvalid()) |
| 6791 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6792 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6793 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6794 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6795 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6796 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6797 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6800 | ExprValueKind VK = VK_RValue; |
| 6801 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6802 | // If the non-type template parameter has reference type, qualify the |
| 6803 | // resulting declaration reference with the extra qualifiers on the |
| 6804 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6805 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 6806 | VK = VK_LValue; |
| 6807 | T = Context.getQualifiedType(T, |
| 6808 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6809 | } else if (isa<FunctionDecl>(VD)) { |
| 6810 | // References to functions are always lvalues. |
| 6811 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6813 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6814 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6815 | } |
| 6816 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6817 | /// Construct a new expression that refers to the given |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6818 | /// integral template argument with the given source-location |
| 6819 | /// information. |
| 6820 | /// |
| 6821 | /// This routine takes care of the mapping from an integral template |
| 6822 | /// argument (which may have any integral type) to the appropriate |
| 6823 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6824 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6825 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 6826 | SourceLocation Loc) { |
| 6827 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 6828 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6829 | QualType OrigT = Arg.getIntegralType(); |
| 6830 | |
| 6831 | // If this is an enum type that we're instantiating, we need to use an integer |
| 6832 | // type the same size as the enumerator. We don't want to build an |
| 6833 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 6834 | // any integral type with C++11 enum classes, make sure we create the right |
| 6835 | // type of literal for it. |
| 6836 | QualType T = OrigT; |
| 6837 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 6838 | T = ET->getDecl()->getIntegerType(); |
| 6839 | |
| 6840 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6841 | if (T->isAnyCharacterType()) { |
| 6842 | CharacterLiteral::CharacterKind Kind; |
| 6843 | if (T->isWideCharType()) |
| 6844 | Kind = CharacterLiteral::Wide; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 6845 | else if (T->isChar8Type() && getLangOpts().Char8) |
| 6846 | Kind = CharacterLiteral::UTF8; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6847 | else if (T->isChar16Type()) |
| 6848 | Kind = CharacterLiteral::UTF16; |
| 6849 | else if (T->isChar32Type()) |
| 6850 | Kind = CharacterLiteral::UTF32; |
| 6851 | else |
| 6852 | Kind = CharacterLiteral::Ascii; |
| 6853 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6854 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 6855 | Kind, T, Loc); |
| 6856 | } else if (T->isBooleanType()) { |
| 6857 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 6858 | T, Loc); |
| 6859 | } else if (T->isNullPtrType()) { |
| 6860 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 6861 | } else { |
| 6862 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6863 | } |
| 6864 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6865 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6866 | // FIXME: This is a hack. We need a better way to handle substituted |
| 6867 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6868 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 6869 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6870 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6871 | Loc, Loc); |
| 6872 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6873 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6874 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6875 | } |
| 6876 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6877 | /// Match two template parameters within template parameter lists. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6878 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 6879 | bool Complain, |
| 6880 | Sema::TemplateParameterListEqualKind Kind, |
| 6881 | SourceLocation TemplateArgLoc) { |
| 6882 | // Check the actual kind (type, non-type, template). |
| 6883 | if (Old->getKind() != New->getKind()) { |
| 6884 | if (Complain) { |
| 6885 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 6886 | if (TemplateArgLoc.isValid()) { |
| 6887 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6888 | NextDiag = diag::note_template_param_different_kind; |
| 6889 | } |
| 6890 | S.Diag(New->getLocation(), NextDiag) |
| 6891 | << (Kind != Sema::TPL_TemplateMatch); |
| 6892 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 6893 | << (Kind != Sema::TPL_TemplateMatch); |
| 6894 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6895 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6896 | return false; |
| 6897 | } |
| 6898 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6899 | // Check that both are parameter packs or neither are parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6900 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6901 | // template template parameter, the template template parameter can have |
| 6902 | // a parameter pack where the template template argument does not. |
| 6903 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 6904 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 6905 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6906 | if (Complain) { |
| 6907 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 6908 | if (TemplateArgLoc.isValid()) { |
| 6909 | S.Diag(TemplateArgLoc, |
| 6910 | diag::err_template_arg_template_params_mismatch); |
| 6911 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 6912 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6913 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6914 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 6915 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 6916 | : 2; |
| 6917 | S.Diag(New->getLocation(), NextDiag) |
| 6918 | << ParamKind << New->isParameterPack(); |
| 6919 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 6920 | << ParamKind << Old->isParameterPack(); |
| 6921 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6922 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6923 | return false; |
| 6924 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6925 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6926 | // For non-type template parameters, check the type of the parameter. |
| 6927 | if (NonTypeTemplateParmDecl *OldNTTP |
| 6928 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 6929 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6930 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6931 | // If we are matching a template template argument to a template |
| 6932 | // template parameter and one of the non-type template parameter types |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 6933 | // is dependent, then we must wait until template instantiation time |
| 6934 | // to actually compare the arguments. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6935 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 6936 | (OldNTTP->getType()->isDependentType() || |
| 6937 | NewNTTP->getType()->isDependentType())) |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6938 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6939 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6940 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 6941 | if (Complain) { |
| 6942 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 6943 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6944 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6945 | diag::err_template_arg_template_params_mismatch); |
| 6946 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 6947 | } |
| 6948 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 6949 | << NewNTTP->getType() |
| 6950 | << (Kind != Sema::TPL_TemplateMatch); |
| 6951 | S.Diag(OldNTTP->getLocation(), |
| 6952 | diag::note_template_nontype_parm_prev_declaration) |
| 6953 | << OldNTTP->getType(); |
| 6954 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6955 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6956 | return false; |
| 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 | return true; |
| 6960 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6961 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6962 | // For template template parameters, check the template parameter types. |
| 6963 | // The template parameter lists of template template |
| 6964 | // parameters must agree. |
| 6965 | if (TemplateTemplateParmDecl *OldTTP |
| 6966 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6967 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6968 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 6969 | OldTTP->getTemplateParameters(), |
| 6970 | Complain, |
| 6971 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6972 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6973 | : Kind), |
| 6974 | TemplateArgLoc); |
| 6975 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6976 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6977 | return true; |
| 6978 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6979 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6980 | /// Diagnose a known arity mismatch when comparing template argument |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6981 | /// lists. |
| 6982 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6983 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6984 | TemplateParameterList *New, |
| 6985 | TemplateParameterList *Old, |
| 6986 | Sema::TemplateParameterListEqualKind Kind, |
| 6987 | SourceLocation TemplateArgLoc) { |
| 6988 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 6989 | if (TemplateArgLoc.isValid()) { |
| 6990 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6991 | NextDiag = diag::note_template_param_list_different_arity; |
| 6992 | } |
| 6993 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 6994 | << (New->size() > Old->size()) |
| 6995 | << (Kind != Sema::TPL_TemplateMatch) |
| 6996 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 6997 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 6998 | << (Kind != Sema::TPL_TemplateMatch) |
| 6999 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 7000 | } |
| 7001 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7002 | /// Determine whether the given template parameter lists are |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7003 | /// equivalent. |
| 7004 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7005 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7006 | /// source code as part of a new template declaration. |
| 7007 | /// |
| 7008 | /// \param Old The old template parameter list, typically found via |
| 7009 | /// name lookup of the template declared with this template parameter |
| 7010 | /// list. |
| 7011 | /// |
| 7012 | /// \param Complain If true, this routine will produce a diagnostic if |
| 7013 | /// the template parameter lists are not equivalent. |
| 7014 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7015 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7016 | /// |
| 7017 | /// \param TemplateArgLoc If this source location is valid, then we |
| 7018 | /// are actually checking the template parameter list of a template |
| 7019 | /// argument (New) against the template parameter list of its |
| 7020 | /// corresponding template template parameter (Old). We produce |
| 7021 | /// slightly different diagnostics in this scenario. |
| 7022 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7023 | /// \returns True if the template parameter lists are equal, false |
| 7024 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7025 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7026 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 7027 | TemplateParameterList *Old, |
| 7028 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7029 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7030 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7031 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 7032 | if (Complain) |
| 7033 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7034 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7035 | |
| 7036 | return false; |
| 7037 | } |
| 7038 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7039 | // C++0x [temp.arg.template]p3: |
| 7040 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7041 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7042 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7043 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7044 | // template-parameter-list of P. [...] |
| 7045 | TemplateParameterList::iterator NewParm = New->begin(); |
| 7046 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7047 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7048 | OldParmEnd = Old->end(); |
| 7049 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 7050 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 7051 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7052 | if (NewParm == NewParmEnd) { |
| 7053 | if (Complain) |
| 7054 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7055 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7056 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7057 | return false; |
| 7058 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7059 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7060 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7061 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7062 | return false; |
| 7063 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7064 | ++NewParm; |
| 7065 | continue; |
| 7066 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7067 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7068 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7069 | // [...] When P's template- parameter-list contains a template parameter |
| 7070 | // pack (14.5.3), the template parameter pack will match zero or more |
| 7071 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7072 | // template-parameter-list of A with the same type and form as the |
| 7073 | // template parameter pack in P (ignoring whether those template |
| 7074 | // parameters are template parameter packs). |
| 7075 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 7076 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7077 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7078 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7079 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7080 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7081 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7082 | // Make sure we exhausted all of the arguments. |
| 7083 | if (NewParm != NewParmEnd) { |
| 7084 | if (Complain) |
| 7085 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7086 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7087 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7088 | return false; |
| 7089 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7090 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7091 | return true; |
| 7092 | } |
| 7093 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7094 | /// Check whether a template can be declared within this scope. |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7095 | /// |
| 7096 | /// If the template declaration is valid in this scope, returns |
| 7097 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7098 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7099 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 7100 | if (!S) |
| 7101 | return false; |
| 7102 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7103 | // Find the nearest enclosing declaration scope. |
| 7104 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7105 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7106 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7107 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7108 | // C++ [temp]p4: |
| 7109 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7110 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 7111 | if (Ctx && Ctx->isExternCContext()) { |
| 7112 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 7113 | << TemplateParams->getSourceRange(); |
| 7114 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 7115 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 7116 | return true; |
| 7117 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 7118 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7119 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7120 | // C++ [temp]p2: |
| 7121 | // A template-declaration can appear only as a namespace scope or |
| 7122 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 7123 | if (Ctx) { |
| 7124 | if (Ctx->isFileContext()) |
| 7125 | return false; |
| 7126 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 7127 | // C++ [temp.mem]p2: |
| 7128 | // A local class shall not have member templates. |
| 7129 | if (RD->isLocalClass()) |
| 7130 | return Diag(TemplateParams->getTemplateLoc(), |
| 7131 | diag::err_template_inside_local_class) |
| 7132 | << TemplateParams->getSourceRange(); |
| 7133 | else |
| 7134 | return false; |
| 7135 | } |
| 7136 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7137 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7138 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7139 | diag::err_template_outside_namespace_or_class_scope) |
| 7140 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7141 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7142 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7143 | /// Determine what kind of template specialization the given declaration |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7144 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7145 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7146 | if (!D) |
| 7147 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7148 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7149 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 7150 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7151 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 7152 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7153 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 7154 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7155 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7156 | return TSK_Undeclared; |
| 7157 | } |
| 7158 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7159 | /// Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7160 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7161 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7162 | /// This routine determines whether a template specialization can be declared |
| 7163 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7164 | /// |
| 7165 | /// \param S the semantic analysis object for which this check is being |
| 7166 | /// performed. |
| 7167 | /// |
| 7168 | /// \param Specialized the entity being specialized or instantiated, which |
| 7169 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7170 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7171 | /// member class). |
| 7172 | /// |
| 7173 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 7174 | /// |
| 7175 | /// \param Loc the location of the explicit specialization or instantiation of |
| 7176 | /// this entity. |
| 7177 | /// |
| 7178 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 7179 | /// a class template. |
| 7180 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7181 | /// \returns true if there was an error that we cannot recover from, false |
| 7182 | /// otherwise. |
| 7183 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 7184 | NamedDecl *Specialized, |
| 7185 | NamedDecl *PrevDecl, |
| 7186 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7187 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7188 | // Keep these "kind" numbers in sync with the %select statements in the |
| 7189 | // various diagnostics emitted by this routine. |
| 7190 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7191 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7192 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7193 | else if (isa<VarTemplateDecl>(Specialized)) |
| 7194 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7195 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7196 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7197 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7198 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7199 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7200 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7201 | else if (isa<RecordDecl>(Specialized)) |
| 7202 | EntityKind = 7; |
| 7203 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 7204 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7205 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7206 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7207 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7208 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7209 | return true; |
| 7210 | } |
| 7211 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7212 | // C++ [temp.expl.spec]p2: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7213 | // An explicit specialization may be declared in any scope in which |
| 7214 | // the corresponding primary template may be defined. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7215 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7216 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7217 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7218 | return true; |
| 7219 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 7220 | |
| 7221 | // C++ [temp.class.spec]p6: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7222 | // A class template partial specialization may be declared in any |
| 7223 | // scope in which the primary template may be defined. |
| 7224 | DeclContext *SpecializedContext = |
| 7225 | Specialized->getDeclContext()->getRedeclContext(); |
| 7226 | DeclContext *DC = S.CurContext->getRedeclContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7227 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7228 | // Make sure that this redeclaration (or definition) occurs in the same |
| 7229 | // scope or an enclosing namespace. |
| 7230 | if (!(DC->isFileContext() ? DC->Encloses(SpecializedContext) |
| 7231 | : DC->Equals(SpecializedContext))) { |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7232 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 7233 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 7234 | << EntityKind << Specialized; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7235 | else { |
| 7236 | auto *ND = cast<NamedDecl>(SpecializedContext); |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7237 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7238 | if (S.getLangOpts().MicrosoftExt && !DC->isRecord()) |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7239 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 7240 | S.Diag(Loc, Diag) << EntityKind << Specialized |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7241 | << ND << isa<CXXRecordDecl>(ND); |
| 7242 | } |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7243 | |
| 7244 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7245 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7246 | // Don't allow specializing in the wrong class during error recovery. |
| 7247 | // Otherwise, things can go horribly wrong. |
| 7248 | if (DC->isRecord()) |
| 7249 | return true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7250 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7251 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7252 | return false; |
| 7253 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7254 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7255 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 7256 | if (!E->isTypeDependent()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7257 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7258 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7259 | Checker.TraverseStmt(E); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7260 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7261 | return E->getSourceRange(); |
| 7262 | return Checker.MatchLoc; |
| 7263 | } |
| 7264 | |
| 7265 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 7266 | if (!TL.getType()->isDependentType()) |
| 7267 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7268 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7269 | Checker.TraverseTypeLoc(TL); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7270 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7271 | return TL.getSourceRange(); |
| 7272 | return Checker.MatchLoc; |
| 7273 | } |
| 7274 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7275 | /// Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7276 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7277 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7278 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 7279 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7280 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 7281 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7282 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7283 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 7284 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7285 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7286 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7287 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7288 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7289 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7290 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7291 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7292 | |
| 7293 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7294 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 7295 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7296 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 7297 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7298 | |
| 7299 | // Strip off any implicit casts we added as part of type checking. |
| 7300 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 7301 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7302 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7303 | // C++ [temp.class.spec]p8: |
| 7304 | // A non-type argument is non-specialized if it is the name of a |
| 7305 | // non-type parameter. All other non-type arguments are |
| 7306 | // specialized. |
| 7307 | // |
| 7308 | // Below, we check the two conditions that only apply to |
| 7309 | // specialized non-type arguments, so skip any non-specialized |
| 7310 | // arguments. |
| 7311 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7312 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7313 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7314 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7315 | // C++ [temp.class.spec]p9: |
| 7316 | // Within the argument list of a class template partial |
| 7317 | // specialization, the following restrictions apply: |
| 7318 | // -- A partially specialized non-type argument expression |
| 7319 | // shall not involve a template parameter of the partial |
| 7320 | // specialization except when the argument expression is a |
| 7321 | // simple identifier. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7322 | // -- The type of a template parameter corresponding to a |
| 7323 | // specialized non-type argument shall not be dependent on a |
| 7324 | // parameter of the specialization. |
| 7325 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
| 7326 | // We implement a compromise between the original rules and DR1315: |
| 7327 | // -- A specialized non-type template argument shall not be |
| 7328 | // type-dependent and the corresponding template parameter |
| 7329 | // shall have a non-dependent type. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7330 | SourceRange ParamUseRange = |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7331 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7332 | if (ParamUseRange.isValid()) { |
| 7333 | if (IsDefaultArgument) { |
| 7334 | S.Diag(TemplateNameLoc, |
| 7335 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 7336 | S.Diag(ParamUseRange.getBegin(), |
| 7337 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 7338 | << ParamUseRange; |
| 7339 | } else { |
| 7340 | S.Diag(ParamUseRange.getBegin(), |
| 7341 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 7342 | << ParamUseRange; |
| 7343 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7344 | return true; |
| 7345 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7346 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7347 | ParamUseRange = findTemplateParameter( |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7348 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7349 | if (ParamUseRange.isValid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7350 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getBeginLoc(), |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7351 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7352 | << Param->getType(); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7353 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7354 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 7355 | << ParamUseRange; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7356 | return true; |
| 7357 | } |
| 7358 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7359 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7360 | return false; |
| 7361 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7362 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7363 | /// Check the non-type template arguments of a class template |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7364 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 7365 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7366 | /// \param TemplateNameLoc the location of the template name. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7367 | /// \param PrimaryTemplate the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7368 | /// template. |
| 7369 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7370 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7371 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7372 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7373 | /// \returns \c true if there was an error, \c false otherwise. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7374 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 7375 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 7376 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 7377 | // We have to be conservative when checking a template in a dependent |
| 7378 | // context. |
| 7379 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 7380 | return false; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7381 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7382 | TemplateParameterList *TemplateParams = |
| 7383 | PrimaryTemplate->getTemplateParameters(); |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7384 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7385 | NonTypeTemplateParmDecl *Param |
| 7386 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 7387 | if (!Param) |
| 7388 | continue; |
| 7389 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7390 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 7391 | Param, &TemplateArgs[I], |
| 7392 | 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7393 | return true; |
| 7394 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7395 | |
| 7396 | return false; |
| 7397 | } |
| 7398 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7399 | DeclResult Sema::ActOnClassTemplateSpecialization( |
| 7400 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
| 7401 | SourceLocation ModulePrivateLoc, TemplateIdAnnotation &TemplateId, |
| 7402 | const ParsedAttributesView &Attr, |
| 7403 | MultiTemplateParamsArg TemplateParameterLists, SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7404 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 7405 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7406 | CXXScopeSpec &SS = TemplateId.SS; |
| 7407 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7408 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 7409 | // store the location of the outermost template keyword in the declaration. |
| 7410 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7411 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 7412 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 7413 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 7414 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7415 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7416 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7417 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7418 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7419 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 7420 | |
| 7421 | if (!ClassTemplate) { |
| 7422 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7423 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7424 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 7425 | return true; |
| 7426 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7427 | |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7428 | bool isMemberSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7429 | bool isPartialSpecialization = false; |
| 7430 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7431 | // Check the validity of the template headers that introduce this |
| 7432 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7433 | // FIXME: We probably shouldn't complain about these headers for |
| 7434 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7435 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 7436 | TemplateParameterList *TemplateParams = |
| 7437 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7438 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7439 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7440 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7441 | if (Invalid) |
| 7442 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7443 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7444 | if (TemplateParams && TemplateParams->size() > 0) { |
| 7445 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7446 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 7447 | if (TUK == TUK_Friend) { |
| 7448 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 7449 | << SourceRange(LAngleLoc, RAngleLoc); |
| 7450 | return true; |
| 7451 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7452 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7453 | // C++ [temp.class.spec]p10: |
| 7454 | // The template parameter list of a specialization shall not |
| 7455 | // contain default template argument values. |
| 7456 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7457 | Decl *Param = TemplateParams->getParam(I); |
| 7458 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 7459 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7460 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7461 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 7462 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7463 | } |
| 7464 | } else if (NonTypeTemplateParmDecl *NTTP |
| 7465 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 7466 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7467 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7468 | diag::err_default_arg_in_partial_spec) |
| 7469 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7470 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7471 | } |
| 7472 | } else { |
| 7473 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7474 | if (TTP->hasDefaultArgument()) { |
| 7475 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7476 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7477 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7478 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7479 | } |
| 7480 | } |
| 7481 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7482 | } else if (TemplateParams) { |
| 7483 | if (TUK == TUK_Friend) |
| 7484 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7485 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7486 | SourceRange(TemplateParams->getTemplateLoc(), |
| 7487 | TemplateParams->getRAngleLoc())) |
| 7488 | << SourceRange(LAngleLoc, RAngleLoc); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7489 | } else { |
| 7490 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7491 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7492 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7493 | // Check that the specialization uses the same tag kind as the |
| 7494 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7495 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7496 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7497 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7498 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7499 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7500 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7501 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7502 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7503 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7504 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7505 | diag::note_previous_use); |
| 7506 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7507 | } |
| 7508 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7509 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7510 | TemplateArgumentListInfo TemplateArgs = |
| 7511 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7512 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7513 | // Check for unexpanded parameter packs in any of the template arguments. |
| 7514 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7515 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7516 | UPPC_PartialSpecialization)) |
| 7517 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7518 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7519 | // Check that the template argument list is well-formed for this |
| 7520 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7521 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7522 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7523 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7524 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7525 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7526 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7527 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7528 | if (isPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7529 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 7530 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7531 | return true; |
| 7532 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7533 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
| 7534 | // also do it during instantiation. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 7535 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7536 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7537 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7538 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7539 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 7540 | << ClassTemplate->getDeclName(); |
| 7541 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7542 | } |
| 7543 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7544 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7545 | void *InsertPos = nullptr; |
| 7546 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7547 | |
| 7548 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7549 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7550 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7551 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7552 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7553 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7554 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7555 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7556 | // Check whether we can declare a class template specialization in |
| 7557 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7558 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7559 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 7560 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7561 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7562 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7563 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7564 | // The canonical type |
| 7565 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 7566 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7567 | // Build the canonical type that describes the converted template |
| 7568 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7569 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7570 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7571 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7572 | |
| 7573 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7574 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 7575 | // C++ [temp.class.spec]p9b3: |
| 7576 | // |
| 7577 | // -- The argument list of the specialization shall not be identical |
| 7578 | // to the implicit argument list of the primary template. |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 7579 | // |
| 7580 | // This rule has since been removed, because it's redundant given DR1495, |
| 7581 | // but we keep it because it produces better diagnostics and recovery. |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7582 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 7583 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 7584 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7585 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 7586 | ClassTemplate->getIdentifier(), |
| 7587 | TemplateNameLoc, |
| 7588 | Attr, |
| 7589 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7590 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 7591 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7592 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7593 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7594 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7595 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7596 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7597 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 7598 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7599 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7600 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7601 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7602 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 7603 | TemplateParams, |
| 7604 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7605 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7606 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7607 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 7608 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7609 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7610 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7611 | Partial->setTemplateParameterListsInfo( |
| 7612 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7613 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7614 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7615 | if (!PrevPartial) |
| 7616 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7617 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 7618 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7619 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 7620 | // template specialization, make a note of that. |
| 7621 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 7622 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7623 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7624 | CheckTemplatePartialSpecialization(Partial); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7625 | } else { |
| 7626 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7627 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7628 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7629 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7630 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7631 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7632 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7633 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7634 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7635 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7636 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 7637 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7638 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7639 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7640 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7641 | if (!PrevDecl) |
| 7642 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7643 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7644 | if (CurContext->isDependentContext()) { |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7645 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7646 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7647 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7648 | } else { |
| 7649 | CanonType = Context.getTypeDeclType(Specialization); |
| 7650 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7651 | } |
| 7652 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7653 | // C++ [temp.expl.spec]p6: |
| 7654 | // 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] | 7655 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7656 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7657 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7658 | // use occurs; no diagnostic is required. |
| 7659 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7660 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7661 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7662 | // Is there any previous explicit specialization declaration? |
| 7663 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7664 | Okay = true; |
| 7665 | break; |
| 7666 | } |
| 7667 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7668 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7669 | if (!Okay) { |
| 7670 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 7671 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 7672 | << Context.getTypeDeclType(Specialization) << Range; |
| 7673 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7674 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7675 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7676 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7677 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7678 | return true; |
| 7679 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7680 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7681 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7682 | // If this is not a friend, note that this is an explicit specialization. |
| 7683 | if (TUK != TUK_Friend) |
| 7684 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7685 | |
| 7686 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7687 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7688 | RecordDecl *Def = Specialization->getDefinition(); |
| 7689 | NamedDecl *Hidden = nullptr; |
| 7690 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 7691 | SkipBody->ShouldSkip = true; |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 7692 | makeMergedDefinitionVisible(Hidden); |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7693 | // From here on out, treat this as just a redeclaration. |
| 7694 | TUK = TUK_Declaration; |
| 7695 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7696 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 7697 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7698 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 7699 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7700 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7701 | } |
| 7702 | } |
| 7703 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 7704 | ProcessDeclAttributeList(S, Specialization, Attr); |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 7705 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 7706 | // Add alignment attributes if necessary; these attributes are checked when |
| 7707 | // the ASTContext lays out the structure. |
| 7708 | if (TUK == TUK_Definition) { |
| 7709 | AddAlignmentAttributesForRecord(Specialization); |
| 7710 | AddMsStructLayoutForRecord(Specialization); |
| 7711 | } |
| 7712 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 7713 | if (ModulePrivateLoc.isValid()) |
| 7714 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 7715 | << (isPartialSpecialization? 1 : 0) |
| 7716 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7717 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 7718 | // Build the fully-sugared type for this class template |
| 7719 | // specialization as the user wrote in the specialization |
| 7720 | // itself. This means that we'll pretty-print the type retrieved |
| 7721 | // from the specialization's declaration the way that the user |
| 7722 | // actually wrote the specialization, rather than formatting the |
| 7723 | // name based on the "canonical" representation used to store the |
| 7724 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7725 | TypeSourceInfo *WrittenTy |
| 7726 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7727 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7728 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7729 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7730 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7731 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7732 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 7733 | // C++ [temp.expl.spec]p9: |
| 7734 | // A template explicit specialization is in the scope of the |
| 7735 | // namespace in which the template was defined. |
| 7736 | // |
| 7737 | // We actually implement this paragraph where we set the semantic |
| 7738 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 7739 | // but we also maintain the lexical context where the actual |
| 7740 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7741 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7742 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7743 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7744 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7745 | Specialization->startDefinition(); |
| 7746 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7747 | if (TUK == TUK_Friend) { |
| 7748 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 7749 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 7750 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7751 | /*FIXME:*/KWLoc); |
| 7752 | Friend->setAccess(AS_public); |
| 7753 | CurContext->addDecl(Friend); |
| 7754 | } else { |
| 7755 | // Add the specialization into its lexical context, so that it can |
| 7756 | // be seen when iterating through the list of declarations in that |
| 7757 | // context. However, specializations are not found by name lookup. |
| 7758 | CurContext->addDecl(Specialization); |
| 7759 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7760 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7761 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7762 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7763 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7764 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7765 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7766 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 7767 | ActOnDocumentableDecl(NewDecl); |
| 7768 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7769 | } |
| 7770 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7771 | /// Strips various properties off an implicit instantiation |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7772 | /// that has just been explicitly specialized. |
| 7773 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7774 | D->dropAttr<DLLImportAttr>(); |
| 7775 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7776 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7777 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7778 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7779 | } |
| 7780 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7781 | /// Compute the diagnostic location for an explicit instantiation |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7782 | // declaration or definition. |
| 7783 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7784 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7785 | // Explicit instantiations following a specialization have no effect and |
| 7786 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 7787 | // until a valid name loc is found. |
| 7788 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7789 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 7790 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7791 | PrevDiagLoc = Prev->getLocation(); |
| 7792 | } |
| 7793 | assert(PrevDiagLoc.isValid() && |
| 7794 | "Explicit instantiation without point of instantiation?"); |
| 7795 | return PrevDiagLoc; |
| 7796 | } |
| 7797 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7798 | /// Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7799 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7800 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7801 | /// new specialization/instantiation will have any effect. |
| 7802 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7803 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7804 | /// instantiation. |
| 7805 | /// |
| 7806 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 7807 | /// |
| 7808 | /// \param PrevDecl the previous declaration of the entity. |
| 7809 | /// |
| 7810 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 7811 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7812 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7813 | /// declaration was instantiated (either implicitly or explicitly). |
| 7814 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7815 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7816 | /// specialization or instantiation has no effect and should be ignored. |
| 7817 | /// |
| 7818 | /// \returns true if there was an error that should prevent the introduction of |
| 7819 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7820 | bool |
| 7821 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 7822 | TemplateSpecializationKind NewTSK, |
| 7823 | NamedDecl *PrevDecl, |
| 7824 | TemplateSpecializationKind PrevTSK, |
| 7825 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7826 | bool &HasNoEffect) { |
| 7827 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7828 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7829 | switch (NewTSK) { |
| 7830 | case TSK_Undeclared: |
| 7831 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 7832 | assert( |
| 7833 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 7834 | "previous declaration must be implicit!"); |
| 7835 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7836 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7837 | case TSK_ExplicitSpecialization: |
| 7838 | switch (PrevTSK) { |
| 7839 | case TSK_Undeclared: |
| 7840 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7841 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7842 | // explicitly specialized or has merely been mentioned without any |
| 7843 | // instantiation. |
| 7844 | return false; |
| 7845 | |
| 7846 | case TSK_ImplicitInstantiation: |
| 7847 | if (PrevPointOfInstantiation.isInvalid()) { |
| 7848 | // The declaration itself has not actually been instantiated, so it is |
| 7849 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7850 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7851 | return false; |
| 7852 | } |
| 7853 | // Fall through |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 7854 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7855 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7856 | case TSK_ExplicitInstantiationDeclaration: |
| 7857 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7858 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 7859 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7860 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7861 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7862 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7863 | // 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] | 7864 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7865 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7866 | // implicit instantiation to take place, in every translation unit in |
| 7867 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7868 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7869 | // Is there any previous explicit specialization declaration? |
| 7870 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 7871 | return false; |
| 7872 | } |
| 7873 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7874 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7875 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7876 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7877 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7878 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7879 | return true; |
| 7880 | } |
Galina Kistanova | 1d36e83 | 2017-06-08 18:20:32 +0000 | [diff] [blame] | 7881 | llvm_unreachable("The switch over PrevTSK must be exhaustive."); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7882 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7883 | case TSK_ExplicitInstantiationDeclaration: |
| 7884 | switch (PrevTSK) { |
| 7885 | case TSK_ExplicitInstantiationDeclaration: |
| 7886 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7887 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7888 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7889 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7890 | case TSK_Undeclared: |
| 7891 | case TSK_ImplicitInstantiation: |
| 7892 | // We're explicitly instantiating something that may have already been |
| 7893 | // implicitly instantiated; that's fine. |
| 7894 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7895 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7896 | case TSK_ExplicitSpecialization: |
| 7897 | // C++0x [temp.explicit]p4: |
| 7898 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7899 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7900 | // specialization for that template, the explicit instantiation has no |
| 7901 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7902 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7903 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7904 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7905 | case TSK_ExplicitInstantiationDefinition: |
| 7906 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7907 | // If an entity is the subject of both an explicit instantiation |
| 7908 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7909 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7910 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7911 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7912 | |
| 7913 | // Explicit instantiations following a specialization have no effect and |
| 7914 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 7915 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7916 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 7917 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7918 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7919 | return false; |
| 7920 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7921 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7922 | case TSK_ExplicitInstantiationDefinition: |
| 7923 | switch (PrevTSK) { |
| 7924 | case TSK_Undeclared: |
| 7925 | case TSK_ImplicitInstantiation: |
| 7926 | // We're explicitly instantiating something that may have already been |
| 7927 | // implicitly instantiated; that's fine. |
| 7928 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7929 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7930 | case TSK_ExplicitSpecialization: |
| 7931 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 7932 | // For a given set of template parameters, if an explicit |
| 7933 | // instantiation of a template appears after a declaration of |
| 7934 | // an explicit specialization for that template, the explicit |
| 7935 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 7936 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7937 | << PrevDecl; |
| 7938 | Diag(PrevDecl->getLocation(), |
| 7939 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7940 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7941 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7942 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7943 | case TSK_ExplicitInstantiationDeclaration: |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 7944 | // We're explicitly instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7945 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7946 | |
| 7947 | // C++0x [temp.explicit]p4: |
| 7948 | // For a given set of template parameters, if an explicit instantiation |
| 7949 | // of a template appears after a declaration of an explicit |
| 7950 | // specialization for that template, the explicit instantiation has no |
| 7951 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7952 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7953 | // Is there any previous explicit specialization declaration? |
| 7954 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7955 | HasNoEffect = true; |
| 7956 | break; |
| 7957 | } |
| 7958 | } |
| 7959 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7960 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7961 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7962 | case TSK_ExplicitInstantiationDefinition: |
| 7963 | // C++0x [temp.spec]p5: |
| 7964 | // For a given template and a given set of template-arguments, |
| 7965 | // - an explicit instantiation definition shall appear at most once |
| 7966 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7967 | |
| 7968 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 7969 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 7970 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7971 | : diag::err_explicit_instantiation_duplicate) |
| 7972 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7973 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7974 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7975 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7976 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7977 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7978 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7979 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7980 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7981 | } |
| 7982 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7983 | /// Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7984 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7985 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7986 | /// The only possible way to get a dependent function template specialization |
| 7987 | /// is with a friend declaration, like so: |
| 7988 | /// |
| 7989 | /// \code |
| 7990 | /// template \<class T> void foo(T); |
| 7991 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7992 | /// friend void foo<>(T); |
| 7993 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7994 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7995 | /// |
| 7996 | /// There really isn't any useful analysis we can do here, so we |
| 7997 | /// just store the information. |
| 7998 | bool |
| 7999 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 8000 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 8001 | LookupResult &Previous) { |
| 8002 | // Remove anything from Previous that isn't a function template in |
| 8003 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8004 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8005 | LookupResult::Filter F = Previous.makeFilter(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8006 | enum DiscardReason { NotAFunctionTemplate, NotAMemberOfEnclosing }; |
| 8007 | SmallVector<std::pair<DiscardReason, Decl *>, 8> DiscardedCandidates; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8008 | while (F.hasNext()) { |
| 8009 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8010 | if (!isa<FunctionTemplateDecl>(D)) { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8011 | F.erase(); |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8012 | DiscardedCandidates.push_back(std::make_pair(NotAFunctionTemplate, D)); |
| 8013 | continue; |
| 8014 | } |
| 8015 | |
| 8016 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8017 | D->getDeclContext()->getRedeclContext())) { |
| 8018 | F.erase(); |
| 8019 | DiscardedCandidates.push_back(std::make_pair(NotAMemberOfEnclosing, D)); |
| 8020 | continue; |
| 8021 | } |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8022 | } |
| 8023 | F.done(); |
| 8024 | |
Erik Pilkington | 0b75dc5 | 2018-07-19 20:40:20 +0000 | [diff] [blame] | 8025 | if (Previous.empty()) { |
| 8026 | Diag(FD->getLocation(), |
| 8027 | diag::err_dependent_function_template_spec_no_match); |
| 8028 | for (auto &P : DiscardedCandidates) |
| 8029 | Diag(P.second->getLocation(), |
| 8030 | diag::note_dependent_function_template_spec_discard_reason) |
| 8031 | << P.first; |
| 8032 | return true; |
| 8033 | } |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8034 | |
| 8035 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 8036 | ExplicitTemplateArgs); |
| 8037 | return false; |
| 8038 | } |
| 8039 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8040 | /// Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8041 | /// specialization. |
| 8042 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8043 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8044 | /// explicit function template specialization. On successful completion, |
| 8045 | /// the function declaration \p FD will become a function template |
| 8046 | /// specialization. |
| 8047 | /// |
| 8048 | /// \param FD the function declaration, which will be updated to become a |
| 8049 | /// function template specialization. |
| 8050 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8051 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 8052 | /// if any. Note that this may be valid info even when 0 arguments are |
| 8053 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 8054 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8055 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8056 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8057 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8058 | bool Sema::CheckFunctionTemplateSpecialization( |
| 8059 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 8060 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8061 | // The set of function template specializations that could match this |
| 8062 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8063 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8064 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 8065 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8066 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8067 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 8068 | ConvertedTemplateArgs; |
| 8069 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8070 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8071 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8072 | I != E; ++I) { |
| 8073 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 8074 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8075 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8076 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8077 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8078 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8079 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8080 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8081 | // When matching a constexpr member function template specialization |
| 8082 | // against the primary template, we don't yet know whether the |
| 8083 | // specialization has an implicit 'const' (because we don't know whether |
| 8084 | // it will be a static member function until we know which template it |
| 8085 | // specializes), so adjust it now assuming it specializes this template. |
| 8086 | QualType FT = FD->getType(); |
| 8087 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8088 | CXXMethodDecl *OldMD = |
| 8089 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8090 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8091 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8092 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8093 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8094 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8095 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8096 | } |
| 8097 | } |
| 8098 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8099 | TemplateArgumentListInfo Args; |
| 8100 | if (ExplicitTemplateArgs) |
| 8101 | Args = *ExplicitTemplateArgs; |
| 8102 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8103 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8104 | // A trailing template-argument can be left unspecified in the |
| 8105 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8106 | // provided it can be deduced from the function argument type. |
| 8107 | // Perform template argument deduction to determine whether we may be |
| 8108 | // specializing this template. |
| 8109 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8110 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8111 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 8112 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 8113 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8114 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 8115 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8116 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8117 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8118 | FailedCandidates.addCandidate().set( |
| 8119 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8120 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8121 | (void)TDK; |
| 8122 | continue; |
| 8123 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8124 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8125 | // Target attributes are part of the cuda function signature, so |
| 8126 | // the deduced template's cuda target must match that of the |
| 8127 | // specialization. Given that C++ template deduction does not |
| 8128 | // take target attributes into account, we reject candidates |
| 8129 | // here that have a different target. |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8130 | if (LangOpts.CUDA && |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8131 | IdentifyCUDATarget(Specialization, |
| 8132 | /* IgnoreImplicitHDAttributes = */ true) != |
| 8133 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8134 | FailedCandidates.addCandidate().set( |
| 8135 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8136 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8137 | continue; |
| 8138 | } |
| 8139 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8140 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8141 | if (ExplicitTemplateArgs) |
| 8142 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8143 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8144 | } |
| 8145 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8146 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 8147 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8148 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 8149 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8150 | FD->getLocation(), |
| 8151 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 8152 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8153 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8154 | PDiag(diag::note_function_template_spec_matched)); |
| 8155 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8156 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8157 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8158 | |
| 8159 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 8160 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 8161 | |
| 8162 | FunctionTemplateSpecializationInfo *SpecInfo |
| 8163 | = Specialization->getTemplateSpecializationInfo(); |
| 8164 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8165 | |
| 8166 | // Note: do not overwrite location info if previous template |
| 8167 | // specialization kind was explicit. |
| 8168 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8169 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8170 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8171 | Specialization->setLexicalDeclContext(FD->getLexicalDeclContext()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8172 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 8173 | // function can differ from the template declaration with respect to |
| 8174 | // the constexpr specifier. |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8175 | // FIXME: We need an update record for this AST mutation. |
| 8176 | // FIXME: What if there are multiple such prior declarations (for instance, |
| 8177 | // from different modules)? |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8178 | Specialization->setConstexpr(FD->isConstexpr()); |
| 8179 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8180 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8181 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8182 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8183 | |
| 8184 | // If this is a friend declaration, then we're not really declaring |
| 8185 | // an explicit specialization. |
| 8186 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8187 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8188 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8189 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8190 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8191 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8192 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8193 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8194 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8195 | |
| 8196 | // C++ [temp.expl.spec]p6: |
| 8197 | // 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] | 8198 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8199 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8200 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8201 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8202 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8203 | if (!isFriend && |
| 8204 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8205 | TSK_ExplicitSpecialization, |
| 8206 | Specialization, |
| 8207 | SpecInfo->getTemplateSpecializationKind(), |
| 8208 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8209 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8210 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8211 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8212 | // Mark the prior declaration as an explicit specialization, so that later |
| 8213 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8214 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8215 | // Since explicit specializations do not inherit '=delete' from their |
| 8216 | // primary function template - check if the 'specialization' that was |
| 8217 | // implicitly generated (during template argument deduction for partial |
| 8218 | // ordering) from the most specialized of all the function templates that |
| 8219 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 8220 | // first check that it was implicitly generated during template argument |
| 8221 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 8222 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 8223 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 8224 | !Specialization->getCanonicalDecl()->isReferenced()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8225 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8226 | assert( |
| 8227 | Specialization->getCanonicalDecl() == Specialization && |
| 8228 | "This must be the only existing declaration of this specialization"); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8229 | // FIXME: We need an update record for this AST mutation. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8230 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8231 | } |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8232 | // FIXME: We need an update record for this AST mutation. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8233 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8234 | MarkUnusedFileScopedDecl(Specialization); |
| 8235 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8236 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8237 | // Turn the given function declaration into a function template |
| 8238 | // specialization, with the template arguments from the previous |
| 8239 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8240 | // Take copies of (semantic and syntactic) template argument lists. |
| 8241 | const TemplateArgumentList* TemplArgs = new (Context) |
| 8242 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8243 | FD->setFunctionTemplateSpecialization( |
| 8244 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 8245 | SpecInfo->getTemplateSpecializationKind(), |
| 8246 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8247 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8248 | // A function template specialization inherits the target attributes |
| 8249 | // of its template. (We require the attributes explicitly in the |
| 8250 | // code to match, but a template may have implicit attributes by |
| 8251 | // virtue e.g. of being constexpr, and it passes these implicit |
| 8252 | // attributes on to its specializations.) |
| 8253 | if (LangOpts.CUDA) |
| 8254 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 8255 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8256 | // The "previous declaration" for this function template specialization is |
| 8257 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8258 | Previous.clear(); |
| 8259 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8260 | return false; |
| 8261 | } |
| 8262 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8263 | /// Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8264 | /// specialization. |
| 8265 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8266 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8267 | /// explicit member function specialization. On successful completion, |
| 8268 | /// the function declaration \p FD will become a member function |
| 8269 | /// specialization. |
| 8270 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8271 | /// \param Member the member declaration, which will be updated to become a |
| 8272 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8273 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8274 | /// \param Previous the set of declarations, one of which may be specialized |
| 8275 | /// by this function specialization; the set will be modified to contain the |
| 8276 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8277 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8278 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8279 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8280 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8281 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8282 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8283 | NamedDecl *Instantiation = nullptr; |
| 8284 | NamedDecl *InstantiatedFrom = nullptr; |
| 8285 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8286 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8287 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8288 | // Nowhere to look anyway. |
| 8289 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8290 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8291 | I != E; ++I) { |
| 8292 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 8293 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 8294 | QualType Adjusted = Function->getType(); |
| 8295 | if (!hasExplicitCallingConv(Adjusted)) |
| 8296 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
Richard Smith | 4576a77 | 2018-09-10 06:35:32 +0000 | [diff] [blame^] | 8297 | // This doesn't handle deduced return types, but both function |
| 8298 | // declarations should be undeduced at this point. |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 8299 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8300 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8301 | Instantiation = Method; |
| 8302 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8303 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8304 | break; |
| 8305 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8306 | } |
| 8307 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8308 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8309 | VarDecl *PrevVar; |
| 8310 | if (Previous.isSingleResult() && |
| 8311 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8312 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8313 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8314 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8315 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8316 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8317 | } |
| 8318 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8319 | CXXRecordDecl *PrevRecord; |
| 8320 | if (Previous.isSingleResult() && |
| 8321 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8322 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8323 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8324 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8325 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8326 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8327 | } else if (isa<EnumDecl>(Member)) { |
| 8328 | EnumDecl *PrevEnum; |
| 8329 | if (Previous.isSingleResult() && |
| 8330 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8331 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8332 | Instantiation = PrevEnum; |
| 8333 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 8334 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 8335 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8336 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8337 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8338 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8339 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8340 | // specializations are always out-of-line, the caller will complain about |
| 8341 | // this mismatch later. |
| 8342 | return false; |
| 8343 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8344 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8345 | // A member specialization in a friend declaration isn't really declaring |
| 8346 | // an explicit specialization, just identifying a specific (possibly implicit) |
| 8347 | // specialization. Don't change the template specialization kind. |
| 8348 | // |
| 8349 | // FIXME: Is this really valid? Other compilers reject. |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8350 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 8351 | // Preserve instantiation information. |
| 8352 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 8353 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 8354 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 8355 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8356 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 8357 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 8358 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 8359 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8360 | } |
| 8361 | |
| 8362 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8363 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8364 | return false; |
| 8365 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8366 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8367 | // Make sure that this is a specialization of a member. |
| 8368 | if (!InstantiatedFrom) { |
| 8369 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 8370 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8371 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 8372 | return true; |
| 8373 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8374 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8375 | // C++ [temp.expl.spec]p6: |
| 8376 | // 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] | 8377 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8378 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8379 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8380 | // use occurs; no diagnostic is required. |
| 8381 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8382 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8383 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8384 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 8385 | TSK_ExplicitSpecialization, |
| 8386 | Instantiation, |
| 8387 | MSInfo->getTemplateSpecializationKind(), |
| 8388 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8389 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8390 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8391 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8392 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8393 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8394 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8395 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8396 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8397 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 8398 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8399 | // Note that this member specialization is an "instantiation of" the |
| 8400 | // corresponding member of the original template. |
| 8401 | if (auto *MemberFunction = dyn_cast<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8402 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 8403 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 8404 | TSK_ImplicitInstantiation) { |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8405 | // Explicit specializations of member functions of class templates do not |
| 8406 | // inherit '=delete' from the member function they are specializing. |
| 8407 | if (InstantiationFunction->isDeleted()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8408 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8409 | assert(InstantiationFunction->getCanonicalDecl() == |
| 8410 | InstantiationFunction); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8411 | // FIXME: We need an update record for this AST mutation. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 8412 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8413 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8414 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8415 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8416 | MemberFunction->setInstantiationOfMemberFunction( |
| 8417 | cast<CXXMethodDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8418 | } else if (auto *MemberVar = dyn_cast<VarDecl>(Member)) { |
| 8419 | MemberVar->setInstantiationOfStaticDataMember( |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8420 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8421 | } else if (auto *MemberClass = dyn_cast<CXXRecordDecl>(Member)) { |
| 8422 | MemberClass->setInstantiationOfMemberClass( |
| 8423 | cast<CXXRecordDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8424 | } else if (auto *MemberEnum = dyn_cast<EnumDecl>(Member)) { |
| 8425 | MemberEnum->setInstantiationOfMemberEnum( |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8426 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8427 | } else { |
| 8428 | llvm_unreachable("unknown member specialization kind"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8429 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8430 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8431 | // Save the caller the trouble of having to figure out which declaration |
| 8432 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8433 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8434 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8435 | return false; |
| 8436 | } |
| 8437 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8438 | /// Complete the explicit specialization of a member of a class template by |
| 8439 | /// updating the instantiated member to be marked as an explicit specialization. |
| 8440 | /// |
| 8441 | /// \param OrigD The member declaration instantiated from the template. |
| 8442 | /// \param Loc The location of the explicit specialization of the member. |
| 8443 | template<typename DeclT> |
| 8444 | static void completeMemberSpecializationImpl(Sema &S, DeclT *OrigD, |
| 8445 | SourceLocation Loc) { |
| 8446 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 8447 | return; |
| 8448 | |
| 8449 | // FIXME: Inform AST mutation listeners of this AST mutation. |
| 8450 | // FIXME: If there are multiple in-class declarations of the member (from |
| 8451 | // multiple modules, or a declaration and later definition of a member type), |
| 8452 | // should we update all of them? |
| 8453 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
| 8454 | OrigD->setLocation(Loc); |
| 8455 | } |
| 8456 | |
| 8457 | void Sema::CompleteMemberSpecialization(NamedDecl *Member, |
| 8458 | LookupResult &Previous) { |
| 8459 | NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl()); |
| 8460 | if (Instantiation == Member) |
| 8461 | return; |
| 8462 | |
| 8463 | if (auto *Function = dyn_cast<CXXMethodDecl>(Instantiation)) |
| 8464 | completeMemberSpecializationImpl(*this, Function, Member->getLocation()); |
| 8465 | else if (auto *Var = dyn_cast<VarDecl>(Instantiation)) |
| 8466 | completeMemberSpecializationImpl(*this, Var, Member->getLocation()); |
| 8467 | else if (auto *Record = dyn_cast<CXXRecordDecl>(Instantiation)) |
| 8468 | completeMemberSpecializationImpl(*this, Record, Member->getLocation()); |
| 8469 | else if (auto *Enum = dyn_cast<EnumDecl>(Instantiation)) |
| 8470 | completeMemberSpecializationImpl(*this, Enum, Member->getLocation()); |
| 8471 | else |
| 8472 | llvm_unreachable("unknown member specialization kind"); |
| 8473 | } |
| 8474 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8475 | /// Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8476 | /// |
| 8477 | /// \returns true if a serious error occurs, false otherwise. |
| 8478 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8479 | SourceLocation InstLoc, |
| 8480 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8481 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 8482 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8483 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8484 | if (CurContext->isRecord()) { |
| 8485 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 8486 | << D; |
| 8487 | return true; |
| 8488 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8489 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8490 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8491 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8492 | // template. If the name declared in the explicit instantiation is an |
| 8493 | // unqualified name, the explicit instantiation shall appear in the |
| 8494 | // namespace where its template is declared or, if that namespace is inline |
| 8495 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8496 | // |
| 8497 | // 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] | 8498 | if (WasQualifiedName) { |
| 8499 | if (CurContext->Encloses(OrigContext)) |
| 8500 | return false; |
| 8501 | } else { |
| 8502 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 8503 | return false; |
| 8504 | } |
| 8505 | |
| 8506 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 8507 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8508 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8509 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8510 | diag::err_explicit_instantiation_out_of_scope : |
| 8511 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8512 | << D << NS; |
| 8513 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8514 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8515 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8516 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 8517 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 8518 | << D << NS; |
| 8519 | } else |
| 8520 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8521 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8522 | diag::err_explicit_instantiation_must_be_global : |
| 8523 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 8524 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8525 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8526 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8527 | } |
| 8528 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8529 | /// Determine whether the given scope specifier has a template-id in it. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8530 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 8531 | if (!SS.isSet()) |
| 8532 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8533 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8534 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8535 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8536 | // or a static data member of a class template specialization, the name of |
| 8537 | // the class template specialization in the qualified-id for the member |
| 8538 | // name shall be a simple-template-id. |
| 8539 | // |
| 8540 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8541 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 8542 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8543 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8544 | if (isa<TemplateSpecializationType>(T)) |
| 8545 | return true; |
| 8546 | |
| 8547 | return false; |
| 8548 | } |
| 8549 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8550 | /// Make a dllexport or dllimport attr on a class template specialization take |
| 8551 | /// effect. |
| 8552 | static void dllExportImportClassTemplateSpecialization( |
| 8553 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 8554 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 8555 | assert(A && "dllExportImportClassTemplateSpecialization called " |
| 8556 | "on Def without dllexport or dllimport"); |
| 8557 | |
| 8558 | // We reject explicit instantiations in class scope, so there should |
| 8559 | // never be any delayed exported classes to worry about. |
| 8560 | assert(S.DelayedDllExportClasses.empty() && |
| 8561 | "delayed exports present at explicit instantiation"); |
| 8562 | S.checkClassLevelDLLAttribute(Def); |
| 8563 | |
| 8564 | // Propagate attribute to base class templates. |
| 8565 | for (auto &B : Def->bases()) { |
| 8566 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 8567 | B.getType()->getAsCXXRecordDecl())) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8568 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getBeginLoc()); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8569 | } |
| 8570 | |
| 8571 | S.referenceDLLExportedClassMethods(); |
| 8572 | } |
| 8573 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8574 | // Explicit instantiation of a class template specialization |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8575 | DeclResult Sema::ActOnExplicitInstantiation( |
| 8576 | Scope *S, SourceLocation ExternLoc, SourceLocation TemplateLoc, |
| 8577 | unsigned TagSpec, SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 8578 | TemplateTy TemplateD, SourceLocation TemplateNameLoc, |
| 8579 | SourceLocation LAngleLoc, ASTTemplateArgsPtr TemplateArgsIn, |
| 8580 | SourceLocation RAngleLoc, const ParsedAttributesView &Attr) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8581 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 8582 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8583 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8584 | // Check that the specialization uses the same tag kind as the |
| 8585 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8586 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 8587 | assert(Kind != TTK_Enum && |
| 8588 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8589 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8590 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 8591 | |
| 8592 | if (!ClassTemplate) { |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 8593 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 8594 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8595 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8596 | return true; |
| 8597 | } |
| 8598 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 8599 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 8600 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 8601 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8602 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8603 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8604 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8605 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8606 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8607 | diag::note_previous_use); |
| 8608 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 8609 | } |
| 8610 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8611 | // C++0x [temp.explicit]p2: |
| 8612 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8613 | // definition and an explicit instantiation declaration. An explicit |
| 8614 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8615 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 8616 | ? TSK_ExplicitInstantiationDefinition |
| 8617 | : TSK_ExplicitInstantiationDeclaration; |
| 8618 | |
| 8619 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 8620 | // Check for dllexport class template instantiation declarations. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8621 | for (const ParsedAttr &AL : Attr) { |
| 8622 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8623 | Diag(ExternLoc, |
| 8624 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8625 | Diag(AL.getLoc(), diag::note_attribute); |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8626 | break; |
| 8627 | } |
| 8628 | } |
| 8629 | |
| 8630 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 8631 | Diag(ExternLoc, |
| 8632 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8633 | Diag(A->getLocation(), diag::note_attribute); |
| 8634 | } |
| 8635 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8636 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8637 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 8638 | // instantiation declarations for most purposes. |
| 8639 | bool DLLImportExplicitInstantiationDef = false; |
| 8640 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 8641 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 8642 | // Check for dllimport class template instantiation definitions. |
| 8643 | bool DLLImport = |
| 8644 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8645 | for (const ParsedAttr &AL : Attr) { |
| 8646 | if (AL.getKind() == ParsedAttr::AT_DLLImport) |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8647 | DLLImport = true; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 8648 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8649 | // dllexport trumps dllimport here. |
| 8650 | DLLImport = false; |
| 8651 | break; |
| 8652 | } |
| 8653 | } |
| 8654 | if (DLLImport) { |
| 8655 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 8656 | DLLImportExplicitInstantiationDef = true; |
| 8657 | } |
| 8658 | } |
| 8659 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8660 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8661 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 8662 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8663 | |
| 8664 | // Check that the template argument list is well-formed for this |
| 8665 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8666 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8667 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 8668 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8669 | return true; |
| 8670 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8671 | // Find the class template specialization declaration that |
| 8672 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8673 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8674 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 8675 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8676 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8677 | TemplateSpecializationKind PrevDecl_TSK |
| 8678 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 8679 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8680 | // C++0x [temp.explicit]p2: |
| 8681 | // [...] An explicit instantiation shall appear in an enclosing |
| 8682 | // namespace of its template. [...] |
| 8683 | // |
| 8684 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8685 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 8686 | SS.isSet())) |
| 8687 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8688 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8689 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8690 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8691 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8692 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8693 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8694 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8695 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8696 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8697 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8698 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8699 | // Even though HasNoEffect == true means that this explicit instantiation |
| 8700 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 8701 | |
| 8702 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 8703 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8704 | // Since the only prior class template specialization with these |
| 8705 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8706 | // declaration node as our own, updating the source location |
| 8707 | // for the template name to reflect our new declaration. |
| 8708 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8709 | Specialization = PrevDecl; |
| 8710 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8711 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8712 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8713 | |
| 8714 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 8715 | DLLImportExplicitInstantiationDef) { |
| 8716 | // The new specialization might add a dllimport attribute. |
| 8717 | HasNoEffect = false; |
| 8718 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8719 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8720 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8721 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8722 | // Create a new class template specialization declaration node for |
| 8723 | // this explicit specialization. |
| 8724 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 8725 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8726 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 8727 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8728 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 8729 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8730 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 8731 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8732 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8733 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8734 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8735 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8736 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8737 | } |
| 8738 | |
| 8739 | // Build the fully-sugared type for this explicit instantiation as |
| 8740 | // the user wrote in the explicit instantiation itself. This means |
| 8741 | // that we'll pretty-print the type retrieved from the |
| 8742 | // specialization's declaration the way that the user actually wrote |
| 8743 | // the explicit instantiation, rather than formatting the name based |
| 8744 | // on the "canonical" representation used to store the template |
| 8745 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 8746 | TypeSourceInfo *WrittenTy |
| 8747 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 8748 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8749 | Context.getTypeDeclType(Specialization)); |
| 8750 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8751 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8752 | // Set source locations for keywords. |
| 8753 | Specialization->setExternLoc(ExternLoc); |
| 8754 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 8755 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8756 | |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8757 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8758 | ProcessDeclAttributeList(S, Specialization, Attr); |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 8759 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8760 | // Add the explicit instantiation into its lexical context. However, |
| 8761 | // since explicit instantiations are never found by name lookup, we |
| 8762 | // just put it into the declaration context directly. |
| 8763 | Specialization->setLexicalDeclContext(CurContext); |
| 8764 | CurContext->addDecl(Specialization); |
| 8765 | |
| 8766 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 8767 | if (HasNoEffect) { |
| 8768 | // Set the template specialization kind. |
| 8769 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8770 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 8771 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8772 | |
| 8773 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8774 | // A definition of a class template or class member template |
| 8775 | // shall be in scope at the point of the explicit instantiation of |
| 8776 | // the class template or class member template. |
| 8777 | // |
| 8778 | // This check comes when we actually try to perform the |
| 8779 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8780 | ClassTemplateSpecializationDecl *Def |
| 8781 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8782 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8783 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8784 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8785 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8786 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8787 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 8788 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8789 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8790 | // Instantiate the members of this class template specialization. |
| 8791 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8792 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8793 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8794 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8795 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 8796 | // TSK_ExplicitInstantiationDefinition |
| 8797 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8798 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 8799 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8800 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8801 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8802 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 8803 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
Shoaib Meenai | ab3f96c | 2016-11-09 23:52:20 +0000 | [diff] [blame] | 8804 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8805 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 8806 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 8807 | // attribute to a template with a previous instantiation declaration. |
| 8808 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8809 | auto *A = cast<InheritableAttr>( |
| 8810 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 8811 | A->setInherited(true); |
| 8812 | Def->addAttr(A); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8813 | dllExportImportClassTemplateSpecialization(*this, Def); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8814 | } |
| 8815 | } |
| 8816 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8817 | // Fix a TSK_ImplicitInstantiation followed by a |
| 8818 | // TSK_ExplicitInstantiationDefinition |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8819 | bool NewlyDLLExported = |
| 8820 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
| 8821 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8822 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8823 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 8824 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 8825 | // attribute to a template with a previous implicit instantiation. |
| 8826 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to |
| 8827 | // avoid potentially strange codegen behavior. For example, if we extend |
| 8828 | // this conditional to dllimport, and we have a source file calling a |
| 8829 | // method on an implicitly instantiated template class instance and then |
| 8830 | // declaring a dllimport explicit instantiation definition for the same |
| 8831 | // template class, the codegen for the method call will not respect the |
| 8832 | // dllimport, while it will with cl. The Def will already have the DLL |
| 8833 | // attribute, since the Def and Specialization will be the same in the |
| 8834 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the |
| 8835 | // attribute to the Specialization; we just need to make it take effect. |
| 8836 | assert(Def == Specialization && |
| 8837 | "Def and Specialization should match for implicit instantiation"); |
| 8838 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 8839 | } |
| 8840 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8841 | // Set the template specialization kind. Make sure it is set before |
| 8842 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 8843 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8844 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8845 | } else { |
| 8846 | |
| 8847 | // Set the template specialization kind. |
| 8848 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8849 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8850 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8851 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8852 | } |
| 8853 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8854 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8855 | DeclResult |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 8856 | Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation ExternLoc, |
| 8857 | SourceLocation TemplateLoc, unsigned TagSpec, |
| 8858 | SourceLocation KWLoc, CXXScopeSpec &SS, |
| 8859 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 8860 | const ParsedAttributesView &Attr) { |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8861 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 8862 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8863 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8864 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8865 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 8866 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8867 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 8868 | SourceLocation(), false, TypeResult(), |
Akira Hatanaka | 12ddcee | 2017-06-26 18:46:12 +0000 | [diff] [blame] | 8869 | /*IsTypeSpecifier*/false, |
| 8870 | /*IsTemplateParamOrArg*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8871 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 8872 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8873 | if (!TagD) |
| 8874 | return true; |
| 8875 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8876 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8877 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8878 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 8879 | if (Tag->isInvalidDecl()) |
| 8880 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8881 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8882 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 8883 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 8884 | if (!Pattern) { |
| 8885 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 8886 | << Context.getTypeDeclType(Record); |
| 8887 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 8888 | return true; |
| 8889 | } |
| 8890 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8891 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8892 | // If the explicit instantiation is for a class or member class, the |
| 8893 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8894 | // simple-template-id. |
| 8895 | // |
| 8896 | // C++98 has the same restriction, just worded differently. |
| 8897 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8898 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8899 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8900 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8901 | // C++0x [temp.explicit]p2: |
| 8902 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8903 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8904 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 8905 | TemplateSpecializationKind TSK |
| 8906 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8907 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8908 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8909 | // C++0x [temp.explicit]p2: |
| 8910 | // [...] An explicit instantiation shall appear in an enclosing |
| 8911 | // namespace of its template. [...] |
| 8912 | // |
| 8913 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8914 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8915 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8916 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8917 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8918 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8919 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8920 | PrevDecl = Record; |
| 8921 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8922 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8923 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8924 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8925 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8926 | PrevDecl, |
| 8927 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8928 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8929 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8930 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8931 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8932 | return TagD; |
| 8933 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8934 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8935 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8936 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8937 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8938 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8939 | // 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] | 8940 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8941 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8942 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8943 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 8944 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 8945 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8946 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 8947 | << Pattern; |
| 8948 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8949 | } else { |
| 8950 | if (InstantiateClass(NameLoc, Record, Def, |
| 8951 | getTemplateInstantiationArgs(Record), |
| 8952 | TSK)) |
| 8953 | return true; |
| 8954 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8955 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8956 | if (!RecordDef) |
| 8957 | return true; |
| 8958 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8959 | } |
| 8960 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8961 | // Instantiate all of the members of the class. |
| 8962 | InstantiateClassMembers(NameLoc, RecordDef, |
| 8963 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8964 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8965 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8966 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 8967 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8968 | // FIXME: We don't have any representation for explicit instantiations of |
| 8969 | // member classes. Such a representation is not needed for compilation, but it |
| 8970 | // should be available for clients that want to see all of the declarations in |
| 8971 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8972 | return TagD; |
| 8973 | } |
| 8974 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8975 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 8976 | SourceLocation ExternLoc, |
| 8977 | SourceLocation TemplateLoc, |
| 8978 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8979 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8980 | // TODO: check if/when DNInfo should replace Name. |
| 8981 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 8982 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8983 | if (!Name) { |
| 8984 | if (!D.isInvalidType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8985 | Diag(D.getDeclSpec().getBeginLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8986 | diag::err_explicit_instantiation_requires_name) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8987 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8988 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8989 | return true; |
| 8990 | } |
| 8991 | |
| 8992 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 8993 | // we find one that is. |
| 8994 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 8995 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 8996 | S = S->getParent(); |
| 8997 | |
| 8998 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8999 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 9000 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9001 | if (R.isNull()) |
| 9002 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9003 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9004 | // C++ [dcl.stc]p1: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9005 | // A storage-class-specifier shall not be specified in [...] an explicit |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9006 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9007 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9008 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 9009 | << Name; |
| 9010 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9011 | } else if (D.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9012 | != DeclSpec::SCS_unspecified) { |
| 9013 | // Complain about then remove the storage class specifier. |
| 9014 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 9015 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9016 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9017 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9018 | } |
| 9019 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 9020 | // C++0x [temp.explicit]p1: |
| 9021 | // [...] An explicit instantiation of a function template shall not use the |
| 9022 | // inline or constexpr specifiers. |
| 9023 | // Presumably, this also applies to member functions of class templates as |
| 9024 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9025 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9026 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9027 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9028 | diag::err_explicit_instantiation_inline : |
| 9029 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9030 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9031 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9032 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 9033 | // not already specified. |
| 9034 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 9035 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9036 | |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 9037 | // A deduction guide is not on the list of entities that can be explicitly |
| 9038 | // instantiated. |
| 9039 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9040 | Diag(D.getDeclSpec().getBeginLoc(), diag::err_deduction_guide_specialized) |
| 9041 | << /*explicit instantiation*/ 0; |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 9042 | return true; |
| 9043 | } |
| 9044 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9045 | // C++0x [temp.explicit]p2: |
| 9046 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9047 | // definition and an explicit instantiation declaration. An explicit |
| 9048 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9049 | TemplateSpecializationKind TSK |
| 9050 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 9051 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9052 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9053 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9054 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9055 | |
| 9056 | if (!R->isFunctionType()) { |
| 9057 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9058 | // A [...] static data member of a class template can be explicitly |
| 9059 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9060 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9061 | // C++1y [temp.explicit]p1: |
| 9062 | // A [...] variable [...] template specialization can be explicitly |
| 9063 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9064 | if (Previous.isAmbiguous()) |
| 9065 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9066 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 9067 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9068 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9069 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9070 | if (!PrevTemplate) { |
| 9071 | if (!Prev || !Prev->isStaticDataMember()) { |
| 9072 | // We expect to see a data data member here. |
| 9073 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 9074 | << Name; |
| 9075 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9076 | P != PEnd; ++P) |
| 9077 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 9078 | return true; |
| 9079 | } |
| 9080 | |
| 9081 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 9082 | // FIXME: Check for explicit specialization? |
| 9083 | Diag(D.getIdentifierLoc(), |
| 9084 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 9085 | << Prev; |
| 9086 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 9087 | // FIXME: Can we provide a note showing where this was declared? |
| 9088 | return true; |
| 9089 | } |
| 9090 | } else { |
| 9091 | // Explicitly instantiate a variable template. |
| 9092 | |
| 9093 | // C++1y [dcl.spec.auto]p6: |
| 9094 | // ... A program that uses auto or decltype(auto) in a context not |
| 9095 | // explicitly allowed in this section is ill-formed. |
| 9096 | // |
| 9097 | // This includes auto-typed variable template instantiations. |
| 9098 | if (R->isUndeducedType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9099 | Diag(T->getTypeLoc().getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9100 | diag::err_auto_not_allowed_var_inst); |
| 9101 | return true; |
| 9102 | } |
| 9103 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9104 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9105 | // C++1y [temp.explicit]p3: |
| 9106 | // If the explicit instantiation is for a variable, the unqualified-id |
| 9107 | // in the declaration shall be a template-id. |
| 9108 | Diag(D.getIdentifierLoc(), |
| 9109 | diag::err_explicit_instantiation_without_template_id) |
| 9110 | << PrevTemplate; |
| 9111 | Diag(PrevTemplate->getLocation(), |
| 9112 | diag::note_explicit_instantiation_here); |
| 9113 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9114 | } |
| 9115 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9116 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9117 | TemplateArgumentListInfo TemplateArgs = |
| 9118 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9119 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9120 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 9121 | D.getIdentifierLoc(), TemplateArgs); |
| 9122 | if (Res.isInvalid()) |
| 9123 | return true; |
| 9124 | |
| 9125 | // Ignore access control bits, we don't need them for redeclaration |
| 9126 | // checking. |
| 9127 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9128 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9129 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9130 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9131 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9132 | // or a static data member of a class template specialization, the name of |
| 9133 | // the class template specialization in the qualified-id for the member |
| 9134 | // name shall be a simple-template-id. |
| 9135 | // |
| 9136 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9137 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 9138 | // This does not apply to variable template specializations, where the |
| 9139 | // template-id is in the unqualified-id instead. |
| 9140 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9141 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9142 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9143 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9144 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9145 | // Check the scope of this explicit instantiation. |
| 9146 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9147 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9148 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 9149 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 9150 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9151 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9152 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9153 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9154 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9155 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9156 | if (!HasNoEffect) { |
| 9157 | // Instantiate static data member or variable template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9158 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 9159 | if (PrevTemplate) { |
| 9160 | // Merge attributes. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9161 | ProcessDeclAttributeList(S, Prev, D.getDeclSpec().getAttributes()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9162 | } |
| 9163 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9164 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 9165 | } |
| 9166 | |
| 9167 | // Check the new variable specialization against the parsed input. |
| 9168 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9169 | Diag(T->getTypeLoc().getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9170 | diag::err_invalid_var_template_spec_type) |
| 9171 | << 0 << PrevTemplate << R << Prev->getType(); |
| 9172 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 9173 | << 2 << PrevTemplate->getDeclName(); |
| 9174 | return true; |
| 9175 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9176 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9177 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9178 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9179 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9180 | |
| 9181 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 9182 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9183 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9184 | TemplateArgumentListInfo TemplateArgs; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9185 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9186 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9187 | HasExplicitTemplateArgs = true; |
| 9188 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9189 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9190 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9191 | // A [...] function [...] can be explicitly instantiated from its template. |
| 9192 | // A member function [...] of a class template can be explicitly |
| 9193 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9194 | // template. |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9195 | UnresolvedSet<8> TemplateMatches; |
| 9196 | FunctionDecl *NonTemplateMatch = nullptr; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9197 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9198 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9199 | P != PEnd; ++P) { |
| 9200 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9201 | if (!HasExplicitTemplateArgs) { |
| 9202 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 9203 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 9204 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 9205 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9206 | if (Method->getPrimaryTemplate()) { |
| 9207 | TemplateMatches.addDecl(Method, P.getAccess()); |
| 9208 | } else { |
| 9209 | // FIXME: Can this assert ever happen? Needs a test. |
| 9210 | assert(!NonTemplateMatch && "Multiple NonTemplateMatches"); |
| 9211 | NonTemplateMatch = Method; |
| 9212 | } |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9213 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9214 | } |
| 9215 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9216 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9217 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 9218 | if (!FunTmpl) |
| 9219 | continue; |
| 9220 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9221 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9222 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9223 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9224 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9225 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 9226 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9227 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9228 | // Keep track of almost-matches. |
| 9229 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9230 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9231 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9232 | (void)TDK; |
| 9233 | continue; |
| 9234 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9235 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 9236 | // Target attributes are part of the cuda function signature, so |
| 9237 | // the cuda target of the instantiated function must match that of its |
| 9238 | // template. Given that C++ template deduction does not take |
| 9239 | // target attributes into account, we reject candidates here that |
| 9240 | // have a different target. |
| 9241 | if (LangOpts.CUDA && |
| 9242 | IdentifyCUDATarget(Specialization, |
| 9243 | /* IgnoreImplicitHDAttributes = */ true) != |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9244 | IdentifyCUDATarget(D.getDeclSpec().getAttributes())) { |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 9245 | FailedCandidates.addCandidate().set( |
| 9246 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 9247 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 9248 | continue; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 9249 | } |
| 9250 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9251 | TemplateMatches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9252 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9253 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9254 | FunctionDecl *Specialization = NonTemplateMatch; |
| 9255 | if (!Specialization) { |
| 9256 | // Find the most specialized function template specialization. |
| 9257 | UnresolvedSetIterator Result = getMostSpecialized( |
| 9258 | TemplateMatches.begin(), TemplateMatches.end(), FailedCandidates, |
| 9259 | D.getIdentifierLoc(), |
| 9260 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 9261 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 9262 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9263 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9264 | if (Result == TemplateMatches.end()) |
| 9265 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9266 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9267 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 9268 | Specialization = cast<FunctionDecl>(*Result); |
| 9269 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9270 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 9271 | // C++11 [except.spec]p4 |
| 9272 | // In an explicit instantiation an exception-specification may be specified, |
| 9273 | // but is not required. |
| 9274 | // If an exception-specification is specified in an explicit instantiation |
| 9275 | // directive, it shall be compatible with the exception-specifications of |
| 9276 | // other declarations of that function. |
| 9277 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 9278 | if (FPT->hasExceptionSpec()) { |
| 9279 | unsigned DiagID = |
| 9280 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 9281 | if (getLangOpts().MicrosoftExt) |
| 9282 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 9283 | bool Result = CheckEquivalentExceptionSpec( |
| 9284 | PDiag(DiagID) << Specialization->getType(), |
| 9285 | PDiag(diag::note_explicit_instantiation_here), |
| 9286 | Specialization->getType()->getAs<FunctionProtoType>(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9287 | Specialization->getLocation(), FPT, D.getBeginLoc()); |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 9288 | // In Microsoft mode, mismatching exception specifications just cause a |
| 9289 | // warning. |
| 9290 | if (!getLangOpts().MicrosoftExt && Result) |
| 9291 | return true; |
| 9292 | } |
| 9293 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9294 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9295 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9296 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 9297 | << Specialization |
| 9298 | << (Specialization->getTemplateSpecializationKind() == |
| 9299 | TSK_ExplicitSpecialization); |
| 9300 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 9301 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9302 | } |
| 9303 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9304 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 9305 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 9306 | PrevDecl = Specialization; |
| 9307 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9308 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9309 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9310 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9311 | PrevDecl, |
| 9312 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9313 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9314 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9315 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9316 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9317 | // FIXME: We may still want to build some representation of this |
| 9318 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9319 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9320 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9321 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 9322 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 9323 | ProcessDeclAttributeList(S, Specialization, D.getDeclSpec().getAttributes()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9324 | |
Hans Wennborg | b8304a6 | 2017-11-29 23:44:11 +0000 | [diff] [blame] | 9325 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 9326 | // instantiation declarations. |
| 9327 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 9328 | Specialization->hasAttr<DLLImportAttr>() && |
| 9329 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 9330 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 9331 | |
| 9332 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 9333 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 9334 | if (Specialization->isDefined()) { |
| 9335 | // Let the ASTConsumer know that this function has been explicitly |
| 9336 | // instantiated now, and its linkage might have changed. |
| 9337 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 9338 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 9339 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9340 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9341 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9342 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9343 | // or a static data member of a class template specialization, the name of |
| 9344 | // the class template specialization in the qualified-id for the member |
| 9345 | // name shall be a simple-template-id. |
| 9346 | // |
| 9347 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9348 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9349 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9350 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9351 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9352 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9353 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9354 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9355 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9356 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9357 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9358 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9359 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9360 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9361 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9362 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9363 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9364 | } |
| 9365 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9366 | TypeResult |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 9367 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9368 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 9369 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 9370 | // This has to hold, because SS is expected to be defined. |
| 9371 | assert(Name && "Expected a name in a dependent tag"); |
| 9372 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9373 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9374 | if (!NNS) |
| 9375 | return true; |
| 9376 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9377 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 9378 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9379 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 9380 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9381 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9382 | return true; |
| 9383 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9384 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9385 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9386 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9387 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9388 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9389 | // Create type-source location information for this type. |
| 9390 | TypeLocBuilder TLB; |
| 9391 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9392 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9393 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 9394 | TL.setNameLoc(NameLoc); |
| 9395 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9396 | } |
| 9397 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9398 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9399 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 9400 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 9401 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9402 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9403 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9404 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9405 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9406 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9407 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9408 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9409 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9410 | << FixItHint::CreateRemoval(TypenameLoc); |
| 9411 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9412 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9413 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 9414 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 9415 | if (T.isNull()) |
| 9416 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9417 | |
| 9418 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9419 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9420 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9421 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9422 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9423 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9424 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9425 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9426 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9427 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9428 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9429 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9430 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9431 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9432 | } |
| 9433 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9434 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9435 | Sema::ActOnTypenameType(Scope *S, |
| 9436 | SourceLocation TypenameLoc, |
| 9437 | const CXXScopeSpec &SS, |
| 9438 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9439 | TemplateTy TemplateIn, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9440 | IdentifierInfo *TemplateII, |
| 9441 | SourceLocation TemplateIILoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9442 | SourceLocation LAngleLoc, |
| 9443 | ASTTemplateArgsPtr TemplateArgsIn, |
| 9444 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9445 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9446 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9447 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9448 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9449 | diag::ext_typename_outside_of_template) |
| 9450 | << FixItHint::CreateRemoval(TypenameLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9451 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9452 | // Strangely, non-type results are not ignored by this lookup, so the |
| 9453 | // program is ill-formed if it finds an injected-class-name. |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 9454 | if (TypenameLoc.isValid()) { |
| 9455 | auto *LookupRD = |
| 9456 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
| 9457 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 9458 | Diag(TemplateIILoc, |
| 9459 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9460 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 9461 | << (TemplateKWLoc.isValid() ? 1 : 0 /*'template'/'typename' keyword*/); |
| 9462 | } |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9463 | } |
| 9464 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9465 | // Translate the parser's template argument list in our AST format. |
| 9466 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 9467 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9468 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9469 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9470 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 9471 | // Construct a dependent template specialization type. |
| 9472 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9473 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9474 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 9475 | DTN->getQualifier(), |
| 9476 | DTN->getIdentifier(), |
| 9477 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9478 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9479 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9480 | TypeLocBuilder Builder; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9481 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9482 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9483 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 9484 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 9485 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9486 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9487 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9488 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9489 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9490 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9491 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 9492 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9493 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9494 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9495 | if (T.isNull()) |
| 9496 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9497 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9498 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9499 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9500 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9501 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9502 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9503 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9504 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9505 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9506 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9507 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9508 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9509 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 9510 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9511 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9512 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9513 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9514 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 9515 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 9516 | } |
| 9517 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9518 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9519 | /// Determine whether this failed name lookup should be treated as being |
| 9520 | /// disabled by a usage of std::enable_if. |
| 9521 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9522 | SourceRange &CondRange, Expr *&Cond) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9523 | // We must be looking for a ::type... |
| 9524 | if (!II.isStr("type")) |
| 9525 | return false; |
| 9526 | |
| 9527 | // ... within an explicitly-written template specialization... |
| 9528 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 9529 | return false; |
| 9530 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9531 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 9532 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 9533 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9534 | return false; |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 9535 | const TemplateSpecializationType *EnableIfTST = EnableIfTSTLoc.getTypePtr(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9536 | |
| 9537 | // ... which names a complete class template declaration... |
| 9538 | const TemplateDecl *EnableIfDecl = |
| 9539 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 9540 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 9541 | return false; |
| 9542 | |
| 9543 | // ... called "enable_if". |
| 9544 | const IdentifierInfo *EnableIfII = |
| 9545 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 9546 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 9547 | return false; |
| 9548 | |
| 9549 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9550 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9551 | |
| 9552 | // Dig out the condition. |
| 9553 | Cond = nullptr; |
| 9554 | if (EnableIfTSTLoc.getArgLoc(0).getArgument().getKind() |
| 9555 | != TemplateArgument::Expression) |
| 9556 | return true; |
| 9557 | |
| 9558 | Cond = EnableIfTSTLoc.getArgLoc(0).getSourceExpression(); |
| 9559 | |
| 9560 | // Ignore Boolean literals; they add no value. |
| 9561 | if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts())) |
| 9562 | Cond = nullptr; |
| 9563 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9564 | return true; |
| 9565 | } |
| 9566 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9567 | /// Build the type that describes a C++ typename specifier, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9568 | /// e.g., "typename T::type". |
| 9569 | QualType |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9570 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9571 | SourceLocation KeywordLoc, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9572 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9573 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9574 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9575 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9576 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9577 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9578 | DeclContext *Ctx = computeDeclContext(SS); |
| 9579 | if (!Ctx) { |
| 9580 | // If the nested-name-specifier is dependent and couldn't be |
| 9581 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9582 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9583 | return Context.getDependentNameType(Keyword, |
| 9584 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9585 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 9586 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9587 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9588 | // If the nested-name-specifier refers to the current instantiation, |
| 9589 | // the "typename" keyword itself is superfluous. In C++03, the |
| 9590 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 9591 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 9592 | // 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] | 9593 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9594 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 9595 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9596 | |
| 9597 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9598 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 9599 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9600 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9601 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9602 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9603 | case LookupResult::NotFound: { |
| 9604 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 9605 | // a more specific diagnostic. |
| 9606 | SourceRange CondRange; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9607 | Expr *Cond = nullptr; |
| 9608 | if (isEnableIf(QualifierLoc, II, CondRange, Cond)) { |
| 9609 | // If we have a condition, narrow it down to the specific failed |
| 9610 | // condition. |
| 9611 | if (Cond) { |
| 9612 | Expr *FailedCond; |
| 9613 | std::string FailedDescription; |
| 9614 | std::tie(FailedCond, FailedDescription) = |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 9615 | findFailedBooleanCondition(Cond, /*AllowTopLevelCond=*/true); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9616 | |
| 9617 | Diag(FailedCond->getExprLoc(), |
| 9618 | diag::err_typename_nested_not_found_requirement) |
| 9619 | << FailedDescription |
| 9620 | << FailedCond->getSourceRange(); |
| 9621 | return QualType(); |
| 9622 | } |
| 9623 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9624 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9625 | << Ctx << CondRange; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9626 | return QualType(); |
| 9627 | } |
| 9628 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 9629 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9630 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9631 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9632 | |
| 9633 | case LookupResult::FoundUnresolvedValue: { |
| 9634 | // We found a using declaration that is a value. Most likely, the using |
| 9635 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9636 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9637 | IILoc); |
| 9638 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 9639 | << Name << Ctx << FullRange; |
| 9640 | if (UnresolvedUsingValueDecl *Using |
| 9641 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 9642 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9643 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 9644 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 9645 | } |
| 9646 | } |
| 9647 | // Fall through to create a dependent typename type, from which we can recover |
| 9648 | // better. |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 9649 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9650 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 9651 | case LookupResult::NotFoundInCurrentInstantiation: |
| 9652 | // Okay, it's a member of an unknown instantiation. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9653 | return Context.getDependentNameType(Keyword, |
| 9654 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9655 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9656 | |
| 9657 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9658 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9659 | // C++ [class.qual]p2: |
| 9660 | // In a lookup in which function names are not ignored and the |
| 9661 | // nested-name-specifier nominates a class C, if the name specified |
| 9662 | // after the nested-name-specifier, when looked up in C, is the |
| 9663 | // injected-class-name of C [...] then the name is instead considered |
| 9664 | // to name the constructor of class C. |
| 9665 | // |
| 9666 | // Unlike in an elaborated-type-specifier, function names are not ignored |
| 9667 | // in typename-specifier lookup. However, they are ignored in all the |
| 9668 | // contexts where we form a typename type with no keyword (that is, in |
| 9669 | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). |
| 9670 | // |
| 9671 | // FIXME: That's not strictly true: mem-initializer-id lookup does not |
| 9672 | // ignore functions, but that appears to be an oversight. |
| 9673 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
| 9674 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
| 9675 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
| 9676 | FoundRD->isInjectedClassName() && |
| 9677 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
| 9678 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9679 | << &II << 1 << 0 /*'typename' keyword used*/; |
| 9680 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9681 | // We found a type. Build an ElaboratedType, since the |
| 9682 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 9683 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9684 | return Context.getElaboratedType(Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9685 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9686 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9687 | } |
| 9688 | |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9689 | // C++ [dcl.type.simple]p2: |
| 9690 | // A type-specifier of the form |
| 9691 | // typename[opt] nested-name-specifier[opt] template-name |
| 9692 | // is a placeholder for a deduced class type [...]. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 9693 | if (getLangOpts().CPlusPlus17) { |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9694 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
| 9695 | return Context.getElaboratedType( |
| 9696 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
| 9697 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
| 9698 | QualType(), false)); |
| 9699 | } |
| 9700 | } |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 9701 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9702 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 9703 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9704 | break; |
| 9705 | |
| 9706 | case LookupResult::FoundOverloaded: |
| 9707 | DiagID = diag::err_typename_nested_not_type; |
| 9708 | Referenced = *Result.begin(); |
| 9709 | break; |
| 9710 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 9711 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9712 | return QualType(); |
| 9713 | } |
| 9714 | |
| 9715 | // If we get here, it's because name lookup did not find a |
| 9716 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9717 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9718 | IILoc); |
| 9719 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9720 | if (Referenced) |
| 9721 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 9722 | << Name; |
| 9723 | return QualType(); |
| 9724 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9725 | |
| 9726 | namespace { |
| 9727 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 9728 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9729 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9730 | SourceLocation Loc; |
| 9731 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9732 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9733 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 9734 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9735 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9736 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9737 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9738 | DeclarationName Entity) |
| 9739 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9740 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9741 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9742 | /// Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9743 | /// transformed. |
| 9744 | /// |
| 9745 | /// For the purposes of type reconstruction, a type has already been |
| 9746 | /// transformed if it is NULL or if it is not dependent. |
| 9747 | bool AlreadyTransformed(QualType T) { |
| 9748 | return T.isNull() || !T->isDependentType(); |
| 9749 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9750 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9751 | /// Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9752 | /// rebuilt. |
| 9753 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9754 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9755 | /// Returns the name of the entity whose type is being rebuilt. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9756 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9757 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9758 | /// Sets the "base" location and entity when that |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 9759 | /// information is known based on another transformation. |
| 9760 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 9761 | this->Loc = Loc; |
| 9762 | this->Entity = Entity; |
| 9763 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9764 | |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 9765 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 9766 | // Lambdas never need to be transformed. |
| 9767 | return E; |
| 9768 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9769 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 9770 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9771 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9772 | /// Rebuilds a type within the context of the current instantiation. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9773 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9774 | /// 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] | 9775 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9776 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9777 | /// partial specialization thereof). This routine will rebuild that type now |
| 9778 | /// that we have entered the declarator's scope, which may produce different |
| 9779 | /// canonical types, e.g., |
| 9780 | /// |
| 9781 | /// \code |
| 9782 | /// template<typename T> |
| 9783 | /// struct X { |
| 9784 | /// typedef T* pointer; |
| 9785 | /// pointer data(); |
| 9786 | /// }; |
| 9787 | /// |
| 9788 | /// template<typename T> |
| 9789 | /// typename X<T>::pointer X<T>::data() { ... } |
| 9790 | /// \endcode |
| 9791 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 9792 | /// 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] | 9793 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 9794 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9795 | /// 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] | 9796 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 9797 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9798 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 9799 | SourceLocation Loc, |
| 9800 | DeclarationName Name) { |
| 9801 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9802 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9803 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9804 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 9805 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 9806 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9807 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9808 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9809 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 9810 | DeclarationName()); |
| 9811 | return Rebuilder.TransformExpr(E); |
| 9812 | } |
| 9813 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9814 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9815 | if (SS.isInvalid()) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9816 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9817 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9818 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9819 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 9820 | DeclarationName()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9821 | NestedNameSpecifierLoc Rebuilt |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9822 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9823 | if (!Rebuilt) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9824 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9825 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9826 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9827 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9828 | } |
| 9829 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9830 | /// 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] | 9831 | /// instantiation. |
| 9832 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 9833 | TemplateParameterList *Params) { |
| 9834 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 9835 | Decl *Param = Params->getParam(I); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9836 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9837 | // There is nothing to rebuild in a type parameter. |
| 9838 | if (isa<TemplateTypeParmDecl>(Param)) |
| 9839 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9840 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9841 | // Rebuild the template parameter list of a template template parameter. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9842 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9843 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 9844 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 9845 | TTP->getTemplateParameters())) |
| 9846 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9847 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9848 | continue; |
| 9849 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9850 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9851 | // Rebuild the type of a non-type template parameter. |
| 9852 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9853 | TypeSourceInfo *NewTSI |
| 9854 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 9855 | NTTP->getLocation(), |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9856 | NTTP->getDeclName()); |
| 9857 | if (!NewTSI) |
| 9858 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9859 | |
Erik Pilkington | 9f9462a | 2018-08-07 22:59:02 +0000 | [diff] [blame] | 9860 | if (NewTSI->getType()->isUndeducedType()) { |
| 9861 | // C++17 [temp.dep.expr]p3: |
| 9862 | // An id-expression is type-dependent if it contains |
| 9863 | // - an identifier associated by name lookup with a non-type |
| 9864 | // template-parameter declared with a type that contains a |
| 9865 | // placeholder type (7.1.7.4), |
| 9866 | NewTSI = SubstAutoTypeSourceInfo(NewTSI, Context.DependentTy); |
| 9867 | } |
| 9868 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9869 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 9870 | NTTP->setTypeSourceInfo(NewTSI); |
| 9871 | NTTP->setType(NewTSI->getType()); |
| 9872 | } |
| 9873 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9874 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9875 | return false; |
| 9876 | } |
| 9877 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9878 | /// Produces a formatted string that describes the binding of |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9879 | /// template parameters to template arguments. |
| 9880 | std::string |
| 9881 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9882 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9883 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9884 | } |
| 9885 | |
| 9886 | std::string |
| 9887 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9888 | const TemplateArgument *Args, |
| 9889 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9890 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9891 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9892 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9893 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9894 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9895 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9896 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9897 | if (I >= NumArgs) |
| 9898 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9899 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9900 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9901 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9902 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9903 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9904 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9905 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9906 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9907 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9908 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9909 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9910 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9911 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 9912 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9913 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9914 | |
| 9915 | Out << ']'; |
| 9916 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9917 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9918 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9919 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 9920 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9921 | if (!FD) |
| 9922 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9923 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9924 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9925 | |
| 9926 | // Take tokens to avoid allocations |
| 9927 | LPT->Toks.swap(Toks); |
| 9928 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9929 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9930 | |
| 9931 | FD->setLateTemplateParsed(true); |
| 9932 | } |
| 9933 | |
| 9934 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 9935 | if (!FD) |
| 9936 | return; |
| 9937 | FD->setLateTemplateParsed(false); |
| 9938 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9939 | |
| 9940 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 9941 | DeclContext *DC = CurContext; |
| 9942 | |
| 9943 | while (DC) { |
| 9944 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 9945 | const FunctionDecl *FD = RD->isLocalClass(); |
| 9946 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 9947 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 9948 | return false; |
| 9949 | |
| 9950 | DC = DC->getParent(); |
| 9951 | } |
| 9952 | return false; |
| 9953 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9954 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 9955 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9956 | /// Walk the path from which a declaration was instantiated, and check |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9957 | /// that every explicit specialization along that path is visible. This enforces |
| 9958 | /// C++ [temp.expl.spec]/6: |
| 9959 | /// |
| 9960 | /// If a template, a member template or a member of a class template is |
| 9961 | /// explicitly specialized then that specialization shall be declared before |
| 9962 | /// the first use of that specialization that would cause an implicit |
| 9963 | /// instantiation to take place, in every translation unit in which such a |
| 9964 | /// use occurs; no diagnostic is required. |
| 9965 | /// |
| 9966 | /// and also C++ [temp.class.spec]/1: |
| 9967 | /// |
| 9968 | /// A partial specialization shall be declared before the first use of a |
| 9969 | /// class template specialization that would make use of the partial |
| 9970 | /// specialization as the result of an implicit or explicit instantiation |
| 9971 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 9972 | /// required. |
| 9973 | class ExplicitSpecializationVisibilityChecker { |
| 9974 | Sema &S; |
| 9975 | SourceLocation Loc; |
| 9976 | llvm::SmallVector<Module *, 8> Modules; |
| 9977 | |
| 9978 | public: |
| 9979 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 9980 | : S(S), Loc(Loc) {} |
| 9981 | |
| 9982 | void check(NamedDecl *ND) { |
| 9983 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 9984 | return checkImpl(FD); |
| 9985 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 9986 | return checkImpl(RD); |
| 9987 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 9988 | return checkImpl(VD); |
| 9989 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 9990 | return checkImpl(ED); |
| 9991 | } |
| 9992 | |
| 9993 | private: |
| 9994 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 9995 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 9996 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 9997 | const bool Recover = true; |
| 9998 | |
| 9999 | // If we got a custom set of modules (because only a subset of the |
| 10000 | // declarations are interesting), use them, otherwise let |
| 10001 | // diagnoseMissingImport intelligently pick some. |
| 10002 | if (Modules.empty()) |
| 10003 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 10004 | else |
| 10005 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 10006 | } |
| 10007 | |
| 10008 | // Check a specific declaration. There are three problematic cases: |
| 10009 | // |
| 10010 | // 1) The declaration is an explicit specialization of a template |
| 10011 | // specialization. |
| 10012 | // 2) The declaration is an explicit specialization of a member of an |
| 10013 | // templated class. |
| 10014 | // 3) The declaration is an instantiation of a template, and that template |
| 10015 | // is an explicit specialization of a member of a templated class. |
| 10016 | // |
| 10017 | // We don't need to go any deeper than that, as the instantiation of the |
| 10018 | // surrounding class / etc is not triggered by whatever triggered this |
| 10019 | // instantiation, and thus should be checked elsewhere. |
| 10020 | template<typename SpecDecl> |
| 10021 | void checkImpl(SpecDecl *Spec) { |
| 10022 | bool IsHiddenExplicitSpecialization = false; |
| 10023 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 10024 | IsHiddenExplicitSpecialization = |
| 10025 | Spec->getMemberSpecializationInfo() |
| 10026 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 10027 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10028 | } else { |
| 10029 | checkInstantiated(Spec); |
| 10030 | } |
| 10031 | |
| 10032 | if (IsHiddenExplicitSpecialization) |
| 10033 | diagnose(Spec->getMostRecentDecl(), false); |
| 10034 | } |
| 10035 | |
| 10036 | void checkInstantiated(FunctionDecl *FD) { |
| 10037 | if (auto *TD = FD->getPrimaryTemplate()) |
| 10038 | checkTemplate(TD); |
| 10039 | } |
| 10040 | |
| 10041 | void checkInstantiated(CXXRecordDecl *RD) { |
| 10042 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 10043 | if (!SD) |
| 10044 | return; |
| 10045 | |
| 10046 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10047 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 10048 | checkTemplate(TD); |
| 10049 | else if (auto *TD = |
| 10050 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 10051 | if (!S.hasVisibleDeclaration(TD)) |
| 10052 | diagnose(TD, true); |
| 10053 | checkTemplate(TD); |
| 10054 | } |
| 10055 | } |
| 10056 | |
| 10057 | void checkInstantiated(VarDecl *RD) { |
| 10058 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 10059 | if (!SD) |
| 10060 | return; |
| 10061 | |
| 10062 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10063 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 10064 | checkTemplate(TD); |
| 10065 | else if (auto *TD = |
| 10066 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 10067 | if (!S.hasVisibleDeclaration(TD)) |
| 10068 | diagnose(TD, true); |
| 10069 | checkTemplate(TD); |
| 10070 | } |
| 10071 | } |
| 10072 | |
| 10073 | void checkInstantiated(EnumDecl *FD) {} |
| 10074 | |
| 10075 | template<typename TemplDecl> |
| 10076 | void checkTemplate(TemplDecl *TD) { |
| 10077 | if (TD->isMemberSpecialization()) { |
| 10078 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 10079 | diagnose(TD->getMostRecentDecl(), false); |
| 10080 | } |
| 10081 | } |
| 10082 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 10083 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10084 | |
| 10085 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 10086 | if (!getLangOpts().Modules) |
| 10087 | return; |
| 10088 | |
| 10089 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 10090 | } |
| 10091 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10092 | /// Check whether a template partial specialization that we've discovered |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10093 | /// is hidden, and produce suitable diagnostics if so. |
| 10094 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 10095 | NamedDecl *Spec) { |
| 10096 | llvm::SmallVector<Module *, 8> Modules; |
| 10097 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 10098 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 10099 | MissingImportKind::PartialSpecialization, |
| 10100 | /*Recover*/true); |
| 10101 | } |