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 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 197 | LookupResult R(*this, TName, Name.getLocStart(), 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) { |
| 542 | Diag(NameInfo.getLocStart(), diag::err_template_kw_missing) |
| 543 | << "" << NameInfo.getName().getAsString() |
| 544 | << SourceRange(Less, Greater); |
| 545 | return; |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | // Try to correct the name by looking for templates and C++ named casts. |
| 549 | struct TemplateCandidateFilter : CorrectionCandidateCallback { |
| 550 | TemplateCandidateFilter() { |
| 551 | WantTypeSpecifiers = false; |
| 552 | WantExpressionKeywords = false; |
| 553 | WantRemainingKeywords = false; |
| 554 | WantCXXNamedCasts = true; |
| 555 | }; |
| 556 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| 557 | if (auto *ND = Candidate.getCorrectionDecl()) |
| 558 | return isAcceptableTemplateName(ND->getASTContext(), ND, true); |
| 559 | return Candidate.isKeyword(); |
| 560 | } |
| 561 | }; |
| 562 | |
| 563 | DeclarationName Name = NameInfo.getName(); |
| 564 | if (TypoCorrection Corrected = |
| 565 | CorrectTypo(NameInfo, LookupKind, S, &SS, |
| 566 | llvm::make_unique<TemplateCandidateFilter>(), |
| 567 | CTK_ErrorRecovery, LookupCtx)) { |
| 568 | auto *ND = Corrected.getFoundDecl(); |
| 569 | if (ND) |
| 570 | ND = isAcceptableTemplateName(Context, ND, |
| 571 | /*AllowFunctionTemplates*/ true); |
| 572 | if (ND || Corrected.isKeyword()) { |
| 573 | if (LookupCtx) { |
| 574 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 575 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| 576 | Name.getAsString() == CorrectedStr; |
| 577 | diagnoseTypo(Corrected, |
| 578 | PDiag(diag::err_non_template_in_member_template_id_suggest) |
| 579 | << Name << LookupCtx << DroppedSpecifier |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 580 | << SS.getRange(), false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 581 | } else { |
| 582 | diagnoseTypo(Corrected, |
| 583 | PDiag(diag::err_non_template_in_template_id_suggest) |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 584 | << Name, false); |
Richard Smith | 42bc73a | 2017-05-10 02:30:28 +0000 | [diff] [blame] | 585 | } |
| 586 | if (Found) |
| 587 | Diag(Found->getLocation(), |
| 588 | diag::note_non_template_in_template_id_found); |
| 589 | return; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) |
| 594 | << Name << SourceRange(Less, Greater); |
| 595 | if (Found) |
| 596 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); |
| 597 | } |
| 598 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 599 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 600 | /// was just parsed. This is only possible with an explicit scope |
| 601 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 602 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 603 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 604 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 605 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 606 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 607 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 608 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 610 | // C++11 [expr.prim.general]p12: |
| 611 | // An id-expression that denotes a non-static data member or non-static |
| 612 | // member function of a class can only be used: |
| 613 | // (...) |
| 614 | // - if that id-expression denotes a non-static data member and it |
| 615 | // appears in an unevaluated operand. |
| 616 | // |
| 617 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 618 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 619 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 620 | // always a MemberExpr. |
| 621 | bool MightBeCxx11UnevalField = |
| 622 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 623 | |
Akira Hatanaka | d644e02 | 2016-12-16 03:19:41 +0000 | [diff] [blame] | 624 | // Check if the nested name specifier is an enum type. |
| 625 | bool IsEnum = false; |
| 626 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
| 627 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
| 628 | |
| 629 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 630 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 631 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 632 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 633 | // Since the 'this' expression is synthesized, we don't need to |
| 634 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 635 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 636 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 637 | return CXXDependentScopeMemberExpr::Create( |
| 638 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 639 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 640 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 643 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 644 | } |
| 645 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 646 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 647 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 648 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 649 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 650 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 651 | return DependentScopeDeclRefExpr::Create( |
| 652 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 653 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 656 | |
| 657 | /// Determine whether we would be unable to instantiate this template (because |
| 658 | /// it either has no definition, or is in the process of being instantiated). |
| 659 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 660 | NamedDecl *Instantiation, |
| 661 | bool InstantiatedFromMember, |
| 662 | const NamedDecl *Pattern, |
| 663 | const NamedDecl *PatternDef, |
| 664 | TemplateSpecializationKind TSK, |
| 665 | bool Complain /*= true*/) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 666 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 667 | isa<VarDecl>(Instantiation)); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 668 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 669 | bool IsEntityBeingDefined = false; |
| 670 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 671 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 672 | |
| 673 | if (PatternDef && !IsEntityBeingDefined) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 674 | NamedDecl *SuggestedDef = nullptr; |
| 675 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 676 | /*OnlyNeedComplete*/false)) { |
| 677 | // If we're allowed to diagnose this and recover, do so. |
| 678 | bool Recover = Complain && !isSFINAEContext(); |
| 679 | if (Complain) |
| 680 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 681 | Sema::MissingImportKind::Definition, Recover); |
| 682 | return !Recover; |
| 683 | } |
| 684 | return false; |
| 685 | } |
| 686 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 687 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 688 | return true; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 689 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 690 | llvm::Optional<unsigned> Note; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 691 | QualType InstantiationTy; |
| 692 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 693 | InstantiationTy = Context.getTypeDeclType(TD); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 694 | if (PatternDef) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 695 | Diag(PointOfInstantiation, |
| 696 | diag::err_template_instantiate_within_definition) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 697 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 698 | << InstantiationTy; |
| 699 | // Not much point in noting the template declaration here, since |
| 700 | // we're lexically inside it. |
| 701 | Instantiation->setInvalidDecl(); |
| 702 | } else if (InstantiatedFromMember) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 703 | if (isa<FunctionDecl>(Instantiation)) { |
| 704 | Diag(PointOfInstantiation, |
| 705 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 706 | << /*member function*/ 1 << Instantiation->getDeclName() |
| 707 | << Instantiation->getDeclContext(); |
| 708 | Note = diag::note_explicit_instantiation_here; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 709 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 710 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 711 | Diag(PointOfInstantiation, |
| 712 | diag::err_implicit_instantiate_member_undefined) |
| 713 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 714 | Note = diag::note_member_declared_at; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 715 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 716 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 717 | if (isa<FunctionDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 718 | Diag(PointOfInstantiation, |
| 719 | diag::err_explicit_instantiation_undefined_func_template) |
| 720 | << Pattern; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 721 | Note = diag::note_explicit_instantiation_here; |
| 722 | } else if (isa<TagDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 723 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 724 | << (TSK != TSK_ImplicitInstantiation) |
| 725 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 726 | Note = diag::note_template_decl_here; |
| 727 | } else { |
| 728 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 729 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 730 | Diag(PointOfInstantiation, |
| 731 | diag::err_explicit_instantiation_undefined_var_template) |
| 732 | << Instantiation; |
| 733 | Instantiation->setInvalidDecl(); |
| 734 | } else |
| 735 | Diag(PointOfInstantiation, |
| 736 | diag::err_explicit_instantiation_undefined_member) |
| 737 | << /*static data member*/ 2 << Instantiation->getDeclName() |
| 738 | << Instantiation->getDeclContext(); |
| 739 | Note = diag::note_explicit_instantiation_here; |
| 740 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 741 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 742 | if (Note) // Diagnostics were emitted. |
| 743 | Diag(Pattern->getLocation(), Note.getValue()); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 744 | |
| 745 | // In general, Instantiation isn't marked invalid to get more than one |
| 746 | // error for multiple undefined instantiations. But the code that does |
| 747 | // explicit declaration -> explicit definition conversion can't handle |
| 748 | // invalid declarations, so mark as invalid in that case. |
| 749 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 750 | Instantiation->setInvalidDecl(); |
| 751 | return true; |
| 752 | } |
| 753 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 754 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 755 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 756 | /// declaration at location Loc. Returns true to indicate that this is |
| 757 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 758 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 759 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 760 | |
| 761 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 762 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 763 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 764 | |
| 765 | // C++ [temp.local]p4: |
| 766 | // A template-parameter shall not be redeclared within its |
| 767 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 769 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 770 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 773 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 774 | /// the parameter D to reference the templated declaration and return a pointer |
| 775 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 776 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 777 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 778 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 779 | return Temp; |
| 780 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 781 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 784 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 785 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 786 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 787 | "Only template template arguments can be pack expansions here"); |
| 788 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 789 | "Template template argument pack expansion without packs"); |
| 790 | ParsedTemplateArgument Result(*this); |
| 791 | Result.EllipsisLoc = EllipsisLoc; |
| 792 | return Result; |
| 793 | } |
| 794 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 795 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 796 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 798 | switch (Arg.getKind()) { |
| 799 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 800 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 801 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 802 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 803 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 804 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 805 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 806 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 807 | case ParsedTemplateArgument::NonType: { |
| 808 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 809 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 810 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 811 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 812 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 813 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 814 | TemplateArgument TArg; |
| 815 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 816 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 817 | else |
| 818 | TArg = Template; |
| 819 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 820 | Arg.getScopeSpec().getWithLocInContext( |
| 821 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 822 | Arg.getLocation(), |
| 823 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 826 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 827 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 828 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 829 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 830 | /// Translates template arguments as provided by the parser |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 831 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 832 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 833 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 834 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 835 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 836 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 837 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 838 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 839 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 840 | SourceLocation Loc, |
| 841 | IdentifierInfo *Name) { |
| 842 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 843 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 844 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 845 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 846 | } |
| 847 | |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 848 | /// Convert a parsed type into a parsed template argument. This is mostly |
| 849 | /// trivial, except that we may have parsed a C++17 deduced class template |
| 850 | /// specialization type, in which case we should form a template template |
| 851 | /// argument instead of a type template argument. |
| 852 | ParsedTemplateArgument Sema::ActOnTemplateTypeArgument(TypeResult ParsedType) { |
| 853 | TypeSourceInfo *TInfo; |
| 854 | QualType T = GetTypeFromParser(ParsedType.get(), &TInfo); |
| 855 | if (T.isNull()) |
| 856 | return ParsedTemplateArgument(); |
| 857 | assert(TInfo && "template argument with no location"); |
| 858 | |
| 859 | // If we might have formed a deduced template specialization type, convert |
| 860 | // it to a template template argument. |
| 861 | if (getLangOpts().CPlusPlus17) { |
| 862 | TypeLoc TL = TInfo->getTypeLoc(); |
| 863 | SourceLocation EllipsisLoc; |
| 864 | if (auto PET = TL.getAs<PackExpansionTypeLoc>()) { |
| 865 | EllipsisLoc = PET.getEllipsisLoc(); |
| 866 | TL = PET.getPatternLoc(); |
| 867 | } |
| 868 | |
| 869 | CXXScopeSpec SS; |
| 870 | if (auto ET = TL.getAs<ElaboratedTypeLoc>()) { |
| 871 | SS.Adopt(ET.getQualifierLoc()); |
| 872 | TL = ET.getNamedTypeLoc(); |
| 873 | } |
| 874 | |
| 875 | if (auto DTST = TL.getAs<DeducedTemplateSpecializationTypeLoc>()) { |
| 876 | TemplateName Name = DTST.getTypePtr()->getTemplateName(); |
| 877 | if (SS.isSet()) |
| 878 | Name = Context.getQualifiedTemplateName(SS.getScopeRep(), |
| 879 | /*HasTemplateKeyword*/ false, |
| 880 | Name.getAsTemplateDecl()); |
| 881 | ParsedTemplateArgument Result(SS, TemplateTy::make(Name), |
| 882 | DTST.getTemplateNameLoc()); |
| 883 | if (EllipsisLoc.isValid()) |
| 884 | Result = Result.getTemplatePackExpansion(EllipsisLoc); |
| 885 | return Result; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | // This is a normal type template argument. Note, if the type template |
| 890 | // argument is an injected-class-name for a template, it has a dual nature |
| 891 | // and can be used as either a type or a template. We handle that in |
| 892 | // convertTypeTemplateArgumentToTemplate. |
| 893 | return ParsedTemplateArgument(ParsedTemplateArgument::Type, |
| 894 | ParsedType.get().getAsOpaquePtr(), |
| 895 | TInfo->getTypeLoc().getLocStart()); |
| 896 | } |
| 897 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 898 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 899 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 900 | /// the keyword "typename" was used to declare the type parameter |
| 901 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 902 | /// "class" or "typename" keyword. ParamName is the name of the |
| 903 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 904 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 905 | /// If the type parameter has a default argument, it will be added |
| 906 | /// later via ActOnTypeParameterDefault. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 907 | NamedDecl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 908 | SourceLocation EllipsisLoc, |
| 909 | SourceLocation KeyLoc, |
| 910 | IdentifierInfo *ParamName, |
| 911 | SourceLocation ParamNameLoc, |
| 912 | unsigned Depth, unsigned Position, |
| 913 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 914 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | assert(S->isTemplateParamScope() && |
| 916 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 917 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 918 | SourceLocation Loc = ParamNameLoc; |
| 919 | if (!ParamName) |
| 920 | Loc = KeyLoc; |
| 921 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 922 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 923 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 924 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 925 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 926 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 927 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 928 | |
| 929 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 930 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 931 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 932 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 933 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 934 | IdResolver.AddDecl(Param); |
| 935 | } |
| 936 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 937 | // C++0x [temp.param]p9: |
| 938 | // A default template-argument may be specified for any kind of |
| 939 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 940 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 941 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 942 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 945 | // Handle the default argument, if provided. |
| 946 | if (DefaultArg) { |
| 947 | TypeSourceInfo *DefaultTInfo; |
| 948 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 949 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 950 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 951 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 952 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 953 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 954 | UPPC_DefaultArgument)) |
| 955 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 956 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 957 | // Check the template argument itself. |
| 958 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 959 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 960 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 961 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 962 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 963 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 964 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 965 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 966 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 969 | /// Check that the type of a non-type template parameter is |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 970 | /// well-formed. |
| 971 | /// |
| 972 | /// \returns the (possibly-promoted) parameter type if valid; |
| 973 | /// otherwise, produces a diagnostic and returns a NULL type. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 974 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
| 975 | SourceLocation Loc) { |
| 976 | if (TSI->getType()->isUndeducedType()) { |
| 977 | // C++1z [temp.dep.expr]p3: |
| 978 | // An id-expression is type-dependent if it contains |
| 979 | // - an identifier associated by name lookup with a non-type |
| 980 | // template-parameter declared with a type that contains a |
| 981 | // placeholder type (7.1.7.4), |
| 982 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
| 983 | } |
| 984 | |
| 985 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
| 986 | } |
| 987 | |
| 988 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
| 989 | SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 990 | // We don't allow variably-modified types as the type of non-type template |
| 991 | // parameters. |
| 992 | if (T->isVariablyModifiedType()) { |
| 993 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 994 | << T; |
| 995 | return QualType(); |
| 996 | } |
| 997 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 998 | // C++ [temp.param]p4: |
| 999 | // |
| 1000 | // A non-type template-parameter shall have one of the following |
| 1001 | // (optionally cv-qualified) types: |
| 1002 | // |
| 1003 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1004 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1006 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1008 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1009 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1010 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 1011 | // -- std::nullptr_t. |
| 1012 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1013 | // If T is a dependent type, we can't do the check now, so we |
| 1014 | // assume that it is well-formed. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1015 | T->isDependentType() || |
| 1016 | // Allow use of auto in template parameter declarations. |
| 1017 | T->isUndeducedType()) { |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 1018 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 1019 | // are ignored when determining its type. |
| 1020 | return T.getUnqualifiedType(); |
| 1021 | } |
| 1022 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1023 | // C++ [temp.param]p8: |
| 1024 | // |
| 1025 | // A non-type template-parameter of type "array of T" or |
| 1026 | // "function returning T" is adjusted to be of type "pointer to |
| 1027 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1028 | else if (T->isArrayType() || T->isFunctionType()) |
| 1029 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1030 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1031 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 1032 | << T; |
| 1033 | |
| 1034 | return QualType(); |
| 1035 | } |
| 1036 | |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1037 | NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1038 | unsigned Depth, |
| 1039 | unsigned Position, |
| 1040 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1041 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1042 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1043 | |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1044 | // Check that we have valid decl-specifiers specified. |
| 1045 | auto CheckValidDeclSpecifiers = [this, &D] { |
| 1046 | // C++ [temp.param] |
| 1047 | // p1 |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 1048 | // template-parameter: |
| 1049 | // ... |
| 1050 | // parameter-declaration |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1051 | // p2 |
| 1052 | // ... A storage class shall not be specified in a template-parameter |
| 1053 | // declaration. |
| 1054 | // [dcl.typedef]p1: |
| 1055 | // The typedef specifier [...] shall not be used in the decl-specifier-seq |
| 1056 | // of a parameter-declaration |
| 1057 | const DeclSpec &DS = D.getDeclSpec(); |
| 1058 | auto EmitDiag = [this](SourceLocation Loc) { |
| 1059 | Diag(Loc, diag::err_invalid_decl_specifier_in_nontype_parm) |
| 1060 | << FixItHint::CreateRemoval(Loc); |
| 1061 | }; |
| 1062 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) |
| 1063 | EmitDiag(DS.getStorageClassSpecLoc()); |
| 1064 | |
Sam McCall | 1371cba | 2017-12-22 07:09:51 +0000 | [diff] [blame] | 1065 | if (DS.getThreadStorageClassSpec() != TSCS_unspecified) |
Faisal Vali | a223d1c | 2017-12-22 03:50:55 +0000 | [diff] [blame] | 1066 | EmitDiag(DS.getThreadStorageClassSpecLoc()); |
| 1067 | |
| 1068 | // [dcl.inline]p1: |
| 1069 | // The inline specifier can be applied only to the declaration or |
| 1070 | // definition of a variable or function. |
| 1071 | |
| 1072 | if (DS.isInlineSpecified()) |
| 1073 | EmitDiag(DS.getInlineSpecLoc()); |
| 1074 | |
| 1075 | // [dcl.constexpr]p1: |
| 1076 | // The constexpr specifier shall be applied only to the definition of a |
| 1077 | // variable or variable template or the declaration of a function or |
| 1078 | // function template. |
| 1079 | |
| 1080 | if (DS.isConstexprSpecified()) |
| 1081 | EmitDiag(DS.getConstexprSpecLoc()); |
| 1082 | |
| 1083 | // [dcl.fct.spec]p1: |
| 1084 | // Function-specifiers can be used only in function declarations. |
| 1085 | |
| 1086 | if (DS.isVirtualSpecified()) |
| 1087 | EmitDiag(DS.getVirtualSpecLoc()); |
| 1088 | |
| 1089 | if (DS.isExplicitSpecified()) |
| 1090 | EmitDiag(DS.getExplicitSpecLoc()); |
| 1091 | |
| 1092 | if (DS.isNoreturnSpecified()) |
| 1093 | EmitDiag(DS.getNoreturnSpecLoc()); |
| 1094 | }; |
| 1095 | |
| 1096 | CheckValidDeclSpecifiers(); |
| 1097 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1098 | if (TInfo->getType()->isUndeducedType()) { |
| 1099 | Diag(D.getIdentifierLoc(), |
| 1100 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 1101 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
| 1102 | } |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1103 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1104 | assert(S->isTemplateParamScope() && |
| 1105 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1106 | bool Invalid = false; |
| 1107 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 1108 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1109 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1110 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 1111 | Invalid = true; |
| 1112 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1113 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1114 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1115 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1116 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 1117 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1118 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 1119 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1120 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1121 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1122 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1124 | if (Invalid) |
| 1125 | Param->setInvalidDecl(); |
| 1126 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1127 | if (ParamName) { |
| 1128 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 1129 | ParamName); |
| 1130 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1131 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1132 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1133 | IdResolver.AddDecl(Param); |
| 1134 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1135 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1136 | // C++0x [temp.param]p9: |
| 1137 | // A default template-argument may be specified for any kind of |
| 1138 | // template-parameter that is not a template parameter pack. |
| 1139 | if (Default && IsParameterPack) { |
| 1140 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1141 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1144 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1145 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1146 | // Check for unexpanded parameter packs. |
| 1147 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 1148 | return Param; |
| 1149 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1150 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 1151 | ExprResult DefaultRes = |
| 1152 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1153 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1154 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1155 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1156 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1157 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1158 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1159 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1160 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1161 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1162 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1163 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1164 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1165 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1166 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1167 | /// has been parsed. S is the current scope. |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 1168 | NamedDecl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1169 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1170 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1171 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1172 | IdentifierInfo *Name, |
| 1173 | SourceLocation NameLoc, |
| 1174 | unsigned Depth, |
| 1175 | unsigned Position, |
| 1176 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1177 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1178 | assert(S->isTemplateParamScope() && |
| 1179 | "Template template parameter not in template parameter scope!"); |
| 1180 | |
| 1181 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1182 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1183 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 1184 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1185 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 1186 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1187 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1188 | Param->setAccess(AS_public); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1189 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1190 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1191 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1192 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 1193 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 1194 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1195 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1196 | IdResolver.AddDecl(Param); |
| 1197 | } |
| 1198 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1199 | if (Params->size() == 0) { |
| 1200 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 1201 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 1202 | Param->setInvalidDecl(); |
| 1203 | } |
| 1204 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1205 | // C++0x [temp.param]p9: |
| 1206 | // A default template-argument may be specified for any kind of |
| 1207 | // template-parameter that is not a template parameter pack. |
| 1208 | if (IsParameterPack && !Default.isInvalid()) { |
| 1209 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 1210 | Default = ParsedTemplateArgument(); |
| 1211 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1212 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1213 | if (!Default.isInvalid()) { |
| 1214 | // Check only that we have a template template argument. We don't want to |
| 1215 | // try to check well-formedness now, because our template template parameter |
| 1216 | // might have dependent types in its template parameters, which we wouldn't |
| 1217 | // be able to match now. |
| 1218 | // |
| 1219 | // If none of the template template parameter's template arguments mention |
| 1220 | // other template parameters, we could actually perform more checking here. |
| 1221 | // However, it isn't worth doing. |
| 1222 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 1223 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 1224 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1225 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1226 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 1227 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1228 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1229 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1230 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 1231 | DefaultArg.getArgument().getAsTemplate(), |
| 1232 | UPPC_DefaultArgument)) |
| 1233 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1234 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1235 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1236 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1237 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1238 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1241 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 1242 | /// constrained by RequiresClause, that contains the template parameters in |
| 1243 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1244 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1245 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 1246 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1248 | SourceLocation LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1249 | ArrayRef<NamedDecl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1250 | SourceLocation RAngleLoc, |
| 1251 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1252 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1253 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1254 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1255 | return TemplateParameterList::Create( |
| 1256 | Context, TemplateLoc, LAngleLoc, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 1257 | llvm::makeArrayRef(Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 1258 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1259 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1260 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1261 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 1262 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1263 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1266 | DeclResult |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 1267 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1268 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1269 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 1270 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1271 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 1272 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1273 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1274 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1275 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1276 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1278 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1279 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1280 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1281 | |
| 1282 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1283 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1284 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1285 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1286 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 1287 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1288 | |
| 1289 | // There is no such thing as an unnamed class template. |
| 1290 | if (!Name) { |
| 1291 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1292 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1295 | // Find any previous declaration with this name. For a friend with no |
| 1296 | // scope explicitly specified, we only look for tag declarations (per |
| 1297 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1298 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1299 | LookupResult Previous(*this, Name, NameLoc, |
| 1300 | (SS.isEmpty() && TUK == TUK_Friend) |
| 1301 | ? LookupTagName : LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1302 | forRedeclarationInCurContext()); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1303 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 1304 | SemanticContext = computeDeclContext(SS, true); |
| 1305 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1306 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 1307 | // in the AST, and historically we have just ignored such friend |
| 1308 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1309 | Diag(NameLoc, TUK == TUK_Friend |
| 1310 | ? diag::warn_template_qualified_friend_ignored |
| 1311 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1312 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1313 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1314 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1316 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 1317 | return true; |
| 1318 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1319 | // If we're adding a template to a dependent context, we may need to |
| 1320 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 1321 | // now that we know what the current instantiation is. |
| 1322 | if (SemanticContext->isDependentContext()) { |
| 1323 | ContextRAII SavedContext(*this, SemanticContext); |
| 1324 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 1325 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1326 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 1327 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc, false); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1328 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1329 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1330 | } else { |
| 1331 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 1332 | |
| 1333 | // C++14 [class.mem]p14: |
| 1334 | // If T is the name of a class, then each of the following shall have a |
| 1335 | // name different from T: |
| 1336 | // -- every member template of class T |
| 1337 | if (TUK != TUK_Friend && |
| 1338 | DiagnoseClassNameShadow(SemanticContext, |
| 1339 | DeclarationNameInfo(Name, NameLoc))) |
| 1340 | return true; |
| 1341 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1342 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1343 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1345 | if (Previous.isAmbiguous()) |
| 1346 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1347 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1348 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1349 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1350 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1351 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1352 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1353 | // Maybe we will complain about the shadowed template parameter. |
| 1354 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1355 | // Just pretend that we didn't see the previous declaration. |
| 1356 | PrevDecl = nullptr; |
| 1357 | } |
| 1358 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1359 | // If there is a previous declaration with the same name, check |
| 1360 | // whether this is a valid redeclaration. |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1361 | ClassTemplateDecl *PrevClassTemplate = |
| 1362 | dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1363 | |
| 1364 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1365 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1366 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1367 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1368 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1369 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1370 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1371 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1372 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1373 | PrevClassTemplate |
| 1374 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1375 | ->getSpecializedTemplate(); |
| 1376 | } |
| 1377 | } |
| 1378 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1379 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1380 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1381 | // [...] When looking for a prior declaration of a class or a function |
| 1382 | // 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] | 1383 | // function is neither a qualified name nor a template-id, scopes outside |
| 1384 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1385 | if (!SS.isSet()) { |
| 1386 | DeclContext *OutermostContext = CurContext; |
| 1387 | while (!OutermostContext->isFileContext()) |
| 1388 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1389 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 1390 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1391 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1392 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1393 | SemanticContext = PrevDecl->getDeclContext(); |
| 1394 | } else { |
| 1395 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1396 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1397 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1398 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1399 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Check that the chosen semantic context doesn't already contain a |
| 1402 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1403 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1404 | DeclContext *LookupContext = SemanticContext; |
| 1405 | while (LookupContext->isTransparentContext()) |
| 1406 | LookupContext = LookupContext->getLookupParent(); |
| 1407 | LookupQualifiedName(Previous, LookupContext); |
| 1408 | |
| 1409 | if (Previous.isAmbiguous()) |
| 1410 | return true; |
| 1411 | |
| 1412 | if (Previous.begin() != Previous.end()) |
| 1413 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1414 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1415 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1416 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1417 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1418 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1419 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1420 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1421 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1422 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1423 | if (SS.isEmpty() && |
| 1424 | !(PrevClassTemplate && |
| 1425 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1426 | SemanticContext->getRedeclContext()))) { |
| 1427 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1428 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1429 | diag::note_using_decl_target); |
| 1430 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1431 | // Recover by ignoring the old declaration. |
| 1432 | PrevDecl = PrevClassTemplate = nullptr; |
| 1433 | } |
| 1434 | } |
| 1435 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1436 | // TODO Memory management; associated constraints are not always stored. |
| 1437 | Expr *const CurAC = formAssociatedConstraints(TemplateParams, nullptr); |
| 1438 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1439 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1440 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1441 | // for a friend in a dependent context: the template parameter list itself |
| 1442 | // could be dependent. |
| 1443 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1444 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1445 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1446 | /*Complain=*/true, |
| 1447 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1448 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1449 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1450 | // Check for matching associated constraints on redeclarations. |
| 1451 | const Expr *const PrevAC = PrevClassTemplate->getAssociatedConstraints(); |
| 1452 | const bool RedeclACMismatch = [&] { |
| 1453 | if (!(CurAC || PrevAC)) |
| 1454 | return false; // Nothing to check; no mismatch. |
| 1455 | if (CurAC && PrevAC) { |
| 1456 | llvm::FoldingSetNodeID CurACInfo, PrevACInfo; |
| 1457 | CurAC->Profile(CurACInfo, Context, /*Canonical=*/true); |
| 1458 | PrevAC->Profile(PrevACInfo, Context, /*Canonical=*/true); |
| 1459 | if (CurACInfo == PrevACInfo) |
| 1460 | return false; // All good; no mismatch. |
| 1461 | } |
| 1462 | return true; |
| 1463 | }(); |
| 1464 | |
| 1465 | if (RedeclACMismatch) { |
| 1466 | Diag(CurAC ? CurAC->getLocStart() : NameLoc, |
| 1467 | diag::err_template_different_associated_constraints); |
| 1468 | Diag(PrevAC ? PrevAC->getLocStart() : PrevClassTemplate->getLocation(), |
| 1469 | diag::note_template_prev_declaration) << /*declaration*/0; |
| 1470 | return true; |
| 1471 | } |
| 1472 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1473 | // C++ [temp.class]p4: |
| 1474 | // In a redeclaration, partial specialization, explicit |
| 1475 | // specialization or explicit instantiation of a class template, |
| 1476 | // the class-key shall agree in kind with the original class |
| 1477 | // template declaration (7.1.5.3). |
| 1478 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1479 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1480 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1482 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1483 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1484 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1485 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1488 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1489 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1490 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1491 | // If we have a prior definition that is not visible, treat this as |
| 1492 | // simply making that previous definition visible. |
| 1493 | NamedDecl *Hidden = nullptr; |
| 1494 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1495 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1496 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1497 | assert(Tmpl && "original definition of a class template is not a " |
| 1498 | "class template?"); |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 1499 | makeMergedDefinitionVisible(Hidden); |
| 1500 | makeMergedDefinitionVisible(Tmpl); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1501 | return Def; |
| 1502 | } |
| 1503 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1504 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1505 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1506 | // FIXME: Would it make sense to try to "forget" the previous |
| 1507 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1508 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1509 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1510 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1511 | } else if (PrevDecl) { |
| 1512 | // C++ [temp]p5: |
| 1513 | // A class template shall not have the same name as any other |
| 1514 | // template, class, function, object, enumeration, enumerator, |
| 1515 | // namespace, or type in the same scope (3.3), except as specified |
| 1516 | // in (14.5.4). |
| 1517 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1518 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1519 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1522 | // Check the template parameter list of this declaration, possibly |
| 1523 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1524 | // template declaration. Skip this check for a friend in a dependent |
| 1525 | // context, because the template parameter list might be dependent. |
| 1526 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1527 | CheckTemplateParameterList( |
| 1528 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1529 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1530 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1531 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1532 | SemanticContext->isDependentContext()) |
| 1533 | ? TPC_ClassTemplateMember |
| 1534 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1535 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1536 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1538 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1539 | // 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] | 1540 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1541 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1542 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1543 | : diag::err_member_decl_does_not_match) |
| 1544 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1545 | Invalid = true; |
| 1546 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1549 | // If this is a templated friend in a dependent context we should not put it |
| 1550 | // on the redecl chain. In some cases, the templated friend can be the most |
| 1551 | // recent declaration tricking the template instantiator to make substitutions |
| 1552 | // there. |
| 1553 | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
| 1554 | bool ShouldAddRedecl |
| 1555 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
| 1556 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1558 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1559 | PrevClassTemplate && ShouldAddRedecl ? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1560 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1561 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1562 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1563 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1564 | NewClass->setTemplateParameterListsInfo( |
| 1565 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1566 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1567 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1568 | // Add alignment attributes if necessary; these attributes are checked when |
| 1569 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1570 | if (TUK == TUK_Definition) { |
| 1571 | AddAlignmentAttributesForRecord(NewClass); |
| 1572 | AddMsStructLayoutForRecord(NewClass); |
| 1573 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1574 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1575 | // Attach the associated constraints when the declaration will not be part of |
| 1576 | // a decl chain. |
| 1577 | Expr *const ACtoAttach = |
| 1578 | PrevClassTemplate && ShouldAddRedecl ? nullptr : CurAC; |
| 1579 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1580 | ClassTemplateDecl *NewTemplate |
| 1581 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1582 | DeclarationName(Name), TemplateParams, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1583 | NewClass, ACtoAttach); |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1584 | |
| 1585 | if (ShouldAddRedecl) |
| 1586 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
| 1587 | |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1588 | NewClass->setDescribedClassTemplate(NewTemplate); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1589 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1590 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1591 | NewTemplate->setModulePrivate(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1592 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1593 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1594 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1595 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1596 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1597 | (void)T; |
| 1598 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1599 | // 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] | 1600 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1601 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1602 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1603 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1604 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1605 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1606 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1607 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1609 | // Set the lexical context of these templates |
| 1610 | NewClass->setLexicalDeclContext(CurContext); |
| 1611 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1612 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1613 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1614 | NewClass->startDefinition(); |
| 1615 | |
| 1616 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1617 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1618 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1619 | if (PrevClassTemplate) |
| 1620 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1621 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1622 | AddPushedVisibilityAttribute(NewClass); |
| 1623 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1624 | if (TUK != TUK_Friend) { |
| 1625 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1626 | Scope *Outer = S; |
| 1627 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1628 | Outer = Outer->getParent(); |
| 1629 | PushOnScopeChains(NewTemplate, Outer); |
| 1630 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1631 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1632 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1633 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1634 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1635 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1636 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1637 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1638 | // Friend templates are visible in fairly strange ways. |
| 1639 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1640 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1641 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1642 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1643 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1644 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1645 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1646 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1647 | FriendDecl *Friend = FriendDecl::Create( |
| 1648 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1649 | Friend->setAccess(AS_public); |
| 1650 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1651 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1652 | |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1653 | if (PrevClassTemplate) |
| 1654 | CheckRedeclarationModuleOwnership(NewTemplate, PrevClassTemplate); |
| 1655 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1656 | if (Invalid) { |
| 1657 | NewTemplate->setInvalidDecl(); |
| 1658 | NewClass->setInvalidDecl(); |
| 1659 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1660 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1661 | ActOnDocumentableDecl(NewTemplate); |
| 1662 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1663 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1666 | namespace { |
| 1667 | /// Transform to convert portions of a constructor declaration into the |
| 1668 | /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1. |
| 1669 | struct ConvertConstructorToDeductionGuideTransform { |
| 1670 | ConvertConstructorToDeductionGuideTransform(Sema &S, |
| 1671 | ClassTemplateDecl *Template) |
| 1672 | : SemaRef(S), Template(Template) {} |
| 1673 | |
| 1674 | Sema &SemaRef; |
| 1675 | ClassTemplateDecl *Template; |
| 1676 | |
| 1677 | DeclContext *DC = Template->getDeclContext(); |
| 1678 | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
| 1679 | DeclarationName DeductionGuideName = |
| 1680 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
| 1681 | |
| 1682 | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
| 1683 | |
| 1684 | // Index adjustment to apply to convert depth-1 template parameters into |
| 1685 | // depth-0 template parameters. |
| 1686 | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
| 1687 | |
| 1688 | /// Transform a constructor declaration into a deduction guide. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1689 | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
| 1690 | CXXConstructorDecl *CD) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1691 | SmallVector<TemplateArgument, 16> SubstArgs; |
| 1692 | |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1693 | LocalInstantiationScope Scope(SemaRef); |
| 1694 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1695 | // C++ [over.match.class.deduct]p1: |
| 1696 | // -- For each constructor of the class template designated by the |
| 1697 | // template-name, a function template with the following properties: |
| 1698 | |
| 1699 | // -- The template parameters are the template parameters of the class |
| 1700 | // template followed by the template parameters (including default |
| 1701 | // template arguments) of the constructor, if any. |
| 1702 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 1703 | if (FTD) { |
| 1704 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
| 1705 | SmallVector<NamedDecl *, 16> AllParams; |
| 1706 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
| 1707 | AllParams.insert(AllParams.begin(), |
| 1708 | TemplateParams->begin(), TemplateParams->end()); |
| 1709 | SubstArgs.reserve(InnerParams->size()); |
| 1710 | |
| 1711 | // Later template parameters could refer to earlier ones, so build up |
| 1712 | // a list of substituted template arguments as we go. |
| 1713 | for (NamedDecl *Param : *InnerParams) { |
| 1714 | MultiLevelTemplateArgumentList Args; |
| 1715 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1716 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1717 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
| 1718 | if (!NewParam) |
| 1719 | return nullptr; |
| 1720 | AllParams.push_back(NewParam); |
| 1721 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
| 1722 | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
| 1723 | } |
| 1724 | TemplateParams = TemplateParameterList::Create( |
| 1725 | SemaRef.Context, InnerParams->getTemplateLoc(), |
| 1726 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
| 1727 | /*FIXME: RequiresClause*/ nullptr); |
| 1728 | } |
| 1729 | |
| 1730 | // If we built a new template-parameter-list, track that we need to |
| 1731 | // substitute references to the old parameters into references to the |
| 1732 | // new ones. |
| 1733 | MultiLevelTemplateArgumentList Args; |
| 1734 | if (FTD) { |
| 1735 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1736 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1739 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1740 | .getAsAdjusted<FunctionProtoTypeLoc>(); |
| 1741 | assert(FPTL && "no prototype for constructor declaration"); |
| 1742 | |
| 1743 | // Transform the type of the function, adjusting the return type and |
| 1744 | // replacing references to the old parameters with references to the |
| 1745 | // new ones. |
| 1746 | TypeLocBuilder TLB; |
| 1747 | SmallVector<ParmVarDecl*, 8> Params; |
| 1748 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args); |
| 1749 | if (NewType.isNull()) |
| 1750 | return nullptr; |
| 1751 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
| 1752 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1753 | return buildDeductionGuide(TemplateParams, CD->isExplicit(), NewTInfo, |
| 1754 | CD->getLocStart(), CD->getLocation(), |
| 1755 | CD->getLocEnd()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | /// Build a deduction guide with the specified parameter types. |
| 1759 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
| 1760 | SourceLocation Loc = Template->getLocation(); |
| 1761 | |
| 1762 | // Build the requested type. |
| 1763 | FunctionProtoType::ExtProtoInfo EPI; |
| 1764 | EPI.HasTrailingReturn = true; |
| 1765 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
| 1766 | DeductionGuideName, EPI); |
| 1767 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
| 1768 | |
| 1769 | FunctionProtoTypeLoc FPTL = |
| 1770 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
| 1771 | |
| 1772 | // Build the parameters, needed during deduction / substitution. |
| 1773 | SmallVector<ParmVarDecl*, 4> Params; |
| 1774 | for (auto T : ParamTypes) { |
| 1775 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
| 1776 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
| 1777 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
| 1778 | NewParam->setScopeInfo(0, Params.size()); |
| 1779 | FPTL.setParam(Params.size(), NewParam); |
| 1780 | Params.push_back(NewParam); |
| 1781 | } |
| 1782 | |
| 1783 | return buildDeductionGuide(Template->getTemplateParameters(), false, TSI, |
| 1784 | Loc, Loc, Loc); |
| 1785 | } |
| 1786 | |
| 1787 | private: |
| 1788 | /// Transform a constructor template parameter into a deduction guide template |
| 1789 | /// parameter, rebuilding any internal references to earlier parameters and |
| 1790 | /// renumbering as we go. |
| 1791 | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
| 1792 | MultiLevelTemplateArgumentList &Args) { |
| 1793 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
| 1794 | // TemplateTypeParmDecl's index cannot be changed after creation, so |
| 1795 | // substitute it directly. |
| 1796 | auto *NewTTP = TemplateTypeParmDecl::Create( |
| 1797 | SemaRef.Context, DC, TTP->getLocStart(), TTP->getLocation(), |
| 1798 | /*Depth*/0, Depth1IndexAdjustment + TTP->getIndex(), |
| 1799 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
| 1800 | TTP->isParameterPack()); |
| 1801 | if (TTP->hasDefaultArgument()) { |
| 1802 | TypeSourceInfo *InstantiatedDefaultArg = |
| 1803 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
| 1804 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
| 1805 | if (InstantiatedDefaultArg) |
| 1806 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
| 1807 | } |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1808 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
| 1809 | NewTTP); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1810 | return NewTTP; |
| 1811 | } |
| 1812 | |
| 1813 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
| 1814 | return transformTemplateParameterImpl(TTP, Args); |
| 1815 | |
| 1816 | return transformTemplateParameterImpl( |
| 1817 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
| 1818 | } |
| 1819 | template<typename TemplateParmDecl> |
| 1820 | TemplateParmDecl * |
| 1821 | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
| 1822 | MultiLevelTemplateArgumentList &Args) { |
| 1823 | // Ask the template instantiator to do the heavy lifting for us, then adjust |
| 1824 | // the index of the parameter once it's done. |
| 1825 | auto *NewParam = |
| 1826 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
| 1827 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
| 1828 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
| 1829 | return NewParam; |
| 1830 | } |
| 1831 | |
| 1832 | QualType transformFunctionProtoType(TypeLocBuilder &TLB, |
| 1833 | FunctionProtoTypeLoc TL, |
| 1834 | SmallVectorImpl<ParmVarDecl*> &Params, |
| 1835 | MultiLevelTemplateArgumentList &Args) { |
| 1836 | SmallVector<QualType, 4> ParamTypes; |
| 1837 | const FunctionProtoType *T = TL.getTypePtr(); |
| 1838 | |
| 1839 | // -- The types of the function parameters are those of the constructor. |
| 1840 | for (auto *OldParam : TL.getParams()) { |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1841 | ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1842 | if (!NewParam) |
| 1843 | return QualType(); |
| 1844 | ParamTypes.push_back(NewParam->getType()); |
| 1845 | Params.push_back(NewParam); |
| 1846 | } |
| 1847 | |
| 1848 | // -- The return type is the class template specialization designated by |
| 1849 | // the template-name and template arguments corresponding to the |
| 1850 | // template parameters obtained from the class template. |
| 1851 | // |
| 1852 | // We use the injected-class-name type of the primary template instead. |
| 1853 | // This has the convenient property that it is different from any type that |
| 1854 | // the user can write in a deduction-guide (because they cannot enter the |
| 1855 | // context of the template), so implicit deduction guides can never collide |
| 1856 | // with explicit ones. |
| 1857 | QualType ReturnType = DeducedType; |
| 1858 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
| 1859 | |
| 1860 | // Resolving a wording defect, we also inherit the variadicness of the |
| 1861 | // constructor. |
| 1862 | FunctionProtoType::ExtProtoInfo EPI; |
| 1863 | EPI.Variadic = T->isVariadic(); |
| 1864 | EPI.HasTrailingReturn = true; |
| 1865 | |
| 1866 | QualType Result = SemaRef.BuildFunctionType( |
| 1867 | ReturnType, ParamTypes, TL.getLocStart(), DeductionGuideName, EPI); |
| 1868 | if (Result.isNull()) |
| 1869 | return QualType(); |
| 1870 | |
| 1871 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
| 1872 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
| 1873 | NewTL.setLParenLoc(TL.getLParenLoc()); |
| 1874 | NewTL.setRParenLoc(TL.getRParenLoc()); |
| 1875 | NewTL.setExceptionSpecRange(SourceRange()); |
| 1876 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
| 1877 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I) |
| 1878 | NewTL.setParam(I, Params[I]); |
| 1879 | |
| 1880 | return Result; |
| 1881 | } |
| 1882 | |
| 1883 | ParmVarDecl * |
| 1884 | transformFunctionTypeParam(ParmVarDecl *OldParam, |
| 1885 | MultiLevelTemplateArgumentList &Args) { |
| 1886 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
Richard Smith | 479ba8e | 2017-04-20 01:15:31 +0000 | [diff] [blame] | 1887 | TypeSourceInfo *NewDI; |
| 1888 | if (!Args.getNumLevels()) |
| 1889 | NewDI = OldDI; |
| 1890 | else if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { |
| 1891 | // Expand out the one and only element in each inner pack. |
| 1892 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); |
| 1893 | NewDI = |
| 1894 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, |
| 1895 | OldParam->getLocation(), OldParam->getDeclName()); |
| 1896 | if (!NewDI) return nullptr; |
| 1897 | NewDI = |
| 1898 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), |
| 1899 | PackTL.getTypePtr()->getNumExpansions()); |
| 1900 | } else |
| 1901 | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
| 1902 | OldParam->getDeclName()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1903 | if (!NewDI) |
| 1904 | return nullptr; |
| 1905 | |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1906 | // Canonicalize the type. This (for instance) replaces references to |
| 1907 | // typedef members of the current instantiations with the definitions of |
| 1908 | // those typedefs, avoiding triggering instantiation of the deduced type |
| 1909 | // during deduction. |
| 1910 | // FIXME: It would be preferable to retain type sugar and source |
| 1911 | // information here (and handle this in substitution instead). |
| 1912 | NewDI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 1913 | SemaRef.Context.getCanonicalType(NewDI->getType()), |
| 1914 | OldParam->getLocation()); |
| 1915 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1916 | // Resolving a wording defect, we also inherit default arguments from the |
| 1917 | // constructor. |
| 1918 | ExprResult NewDefArg; |
| 1919 | if (OldParam->hasDefaultArg()) { |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1920 | NewDefArg = Args.getNumLevels() |
| 1921 | ? SemaRef.SubstExpr(OldParam->getDefaultArg(), Args) |
| 1922 | : OldParam->getDefaultArg(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1923 | if (NewDefArg.isInvalid()) |
| 1924 | return nullptr; |
| 1925 | } |
| 1926 | |
| 1927 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
| 1928 | OldParam->getInnerLocStart(), |
| 1929 | OldParam->getLocation(), |
| 1930 | OldParam->getIdentifier(), |
| 1931 | NewDI->getType(), |
| 1932 | NewDI, |
| 1933 | OldParam->getStorageClass(), |
| 1934 | NewDefArg.get()); |
| 1935 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
| 1936 | OldParam->getFunctionScopeIndex()); |
| 1937 | return NewParam; |
| 1938 | } |
| 1939 | |
| 1940 | NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams, |
| 1941 | bool Explicit, TypeSourceInfo *TInfo, |
| 1942 | SourceLocation LocStart, SourceLocation Loc, |
| 1943 | SourceLocation LocEnd) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1944 | DeclarationNameInfo Name(DeductionGuideName, Loc); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1945 | ArrayRef<ParmVarDecl *> Params = |
| 1946 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
| 1947 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1948 | // Build the implicit deduction guide template. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1949 | auto *Guide = |
| 1950 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, Explicit, |
| 1951 | Name, TInfo->getType(), TInfo, LocEnd); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1952 | Guide->setImplicit(); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1953 | Guide->setParams(Params); |
| 1954 | |
| 1955 | for (auto *Param : Params) |
| 1956 | Param->setDeclContext(Guide); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1957 | |
| 1958 | auto *GuideTemplate = FunctionTemplateDecl::Create( |
| 1959 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
| 1960 | GuideTemplate->setImplicit(); |
| 1961 | Guide->setDescribedFunctionTemplate(GuideTemplate); |
| 1962 | |
| 1963 | if (isa<CXXRecordDecl>(DC)) { |
| 1964 | Guide->setAccess(AS_public); |
| 1965 | GuideTemplate->setAccess(AS_public); |
| 1966 | } |
| 1967 | |
| 1968 | DC->addDecl(GuideTemplate); |
| 1969 | return GuideTemplate; |
| 1970 | } |
| 1971 | }; |
| 1972 | } |
| 1973 | |
| 1974 | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
| 1975 | SourceLocation Loc) { |
| 1976 | DeclContext *DC = Template->getDeclContext(); |
| 1977 | if (DC->isDependentContext()) |
| 1978 | return; |
| 1979 | |
| 1980 | ConvertConstructorToDeductionGuideTransform Transform( |
| 1981 | *this, cast<ClassTemplateDecl>(Template)); |
| 1982 | if (!isCompleteType(Loc, Transform.DeducedType)) |
| 1983 | return; |
| 1984 | |
| 1985 | // Check whether we've already declared deduction guides for this template. |
| 1986 | // FIXME: Consider storing a flag on the template to indicate this. |
| 1987 | auto Existing = DC->lookup(Transform.DeductionGuideName); |
| 1988 | for (auto *D : Existing) |
| 1989 | if (D->isImplicit()) |
| 1990 | return; |
| 1991 | |
| 1992 | // In case we were expanding a pack when we attempted to declare deduction |
| 1993 | // guides, turn off pack expansion for everything we're about to do. |
| 1994 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
| 1995 | // Create a template instantiation record to track the "instantiation" of |
| 1996 | // constructors into deduction guides. |
| 1997 | // FIXME: Add a kind for this to give more meaningful diagnostics. But can |
| 1998 | // this substitution process actually fail? |
| 1999 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
Volodymyr Sapsai | 2f649f3 | 2018-05-14 22:49:44 +0000 | [diff] [blame] | 2000 | if (BuildingDeductionGuides.isInvalid()) |
| 2001 | return; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2002 | |
| 2003 | // Convert declared constructors into deduction guide templates. |
| 2004 | // FIXME: Skip constructors for which deduction must necessarily fail (those |
| 2005 | // for which some class template parameter without a default argument never |
| 2006 | // appears in a deduced context). |
| 2007 | bool AddedAny = false; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2008 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
| 2009 | D = D->getUnderlyingDecl(); |
| 2010 | if (D->isInvalidDecl() || D->isImplicit()) |
| 2011 | continue; |
| 2012 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 2013 | |
| 2014 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2015 | auto *CD = |
| 2016 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2017 | // Class-scope explicit specializations (MS extension) do not result in |
| 2018 | // deduction guides. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2019 | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization())) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2020 | continue; |
| 2021 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2022 | Transform.transformConstructor(FTD, CD); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2023 | AddedAny = true; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2026 | // C++17 [over.match.class.deduct] |
| 2027 | // -- If C is not defined or does not declare any constructors, an |
| 2028 | // additional function template derived as above from a hypothetical |
| 2029 | // constructor C(). |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2030 | if (!AddedAny) |
| 2031 | Transform.buildSimpleDeductionGuide(None); |
| 2032 | |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 2033 | // -- An additional function template derived as above from a hypothetical |
| 2034 | // constructor C(C), called the copy deduction candidate. |
| 2035 | cast<CXXDeductionGuideDecl>( |
| 2036 | cast<FunctionTemplateDecl>( |
| 2037 | Transform.buildSimpleDeductionGuide(Transform.DeducedType)) |
| 2038 | ->getTemplatedDecl()) |
| 2039 | ->setIsCopyDeductionCandidate(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2042 | /// Diagnose the presence of a default template argument on a |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2043 | /// template parameter, which is ill-formed in certain contexts. |
| 2044 | /// |
| 2045 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2046 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2047 | Sema::TemplateParamListContext TPC, |
| 2048 | SourceLocation ParamLoc, |
| 2049 | SourceRange DefArgRange) { |
| 2050 | switch (TPC) { |
| 2051 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2052 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2053 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2054 | return false; |
| 2055 | |
| 2056 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2057 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2058 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2059 | // A default template-argument shall not be specified in a |
| 2060 | // function template declaration or a function template |
| 2061 | // definition [...] |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2062 | // If a friend function template declaration specifies a default |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 2063 | // template-argument, that declaration shall be a definition and shall be |
| 2064 | // the only declaration of the function template in the translation unit. |
| 2065 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2066 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2067 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 2068 | : diag::ext_template_parameter_default_in_function_template) |
| 2069 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2070 | return false; |
| 2071 | |
| 2072 | case Sema::TPC_ClassTemplateMember: |
| 2073 | // C++0x [temp.param]p9: |
| 2074 | // A default template-argument shall not be specified in the |
| 2075 | // template-parameter-lists of the definition of a member of a |
| 2076 | // class template that appears outside of the member's class. |
| 2077 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 2078 | << DefArgRange; |
| 2079 | return true; |
| 2080 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 2081 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2082 | case Sema::TPC_FriendFunctionTemplate: |
| 2083 | // C++ [temp.param]p9: |
| 2084 | // A default template-argument shall not be specified in a |
| 2085 | // friend template declaration. |
| 2086 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 2087 | << DefArgRange; |
| 2088 | return true; |
| 2089 | |
| 2090 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 2091 | // for friend function templates if there is only a single |
| 2092 | // declaration (and it is a definition). Strange! |
| 2093 | } |
| 2094 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2095 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2098 | /// Check for unexpanded parameter packs within the template parameters |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2099 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 2100 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 2101 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2102 | // A template template parameter which is a parameter pack is also a pack |
| 2103 | // expansion. |
| 2104 | if (TTP->isParameterPack()) |
| 2105 | return false; |
| 2106 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2107 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 2108 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 2109 | NamedDecl *P = Params->getParam(I); |
| 2110 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2111 | if (!NTTP->isParameterPack() && |
| 2112 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2113 | NTTP->getTypeSourceInfo(), |
| 2114 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 2115 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2117 | continue; |
| 2118 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2119 | |
| 2120 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2121 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 2122 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 2123 | return true; |
| 2124 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2125 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2126 | return false; |
| 2127 | } |
| 2128 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2129 | /// Checks the validity of a template parameter list, possibly |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2130 | /// considering the template parameter list from a previous |
| 2131 | /// declaration. |
| 2132 | /// |
| 2133 | /// If an "old" template parameter list is provided, it must be |
| 2134 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 2135 | /// template parameter list. |
| 2136 | /// |
| 2137 | /// \param NewParams Template parameter list for a new template |
| 2138 | /// declaration. This template parameter list will be updated with any |
| 2139 | /// default arguments that are carried through from the previous |
| 2140 | /// template parameter list. |
| 2141 | /// |
| 2142 | /// \param OldParams If provided, template parameter list from a |
| 2143 | /// previous declaration of the same template. Default template |
| 2144 | /// arguments will be merged from the old template parameter list to |
| 2145 | /// the new template parameter list. |
| 2146 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2147 | /// \param TPC Describes the context in which we are checking the given |
| 2148 | /// template parameter list. |
| 2149 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2150 | /// \returns true if an error occurred, false otherwise. |
| 2151 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2152 | TemplateParameterList *OldParams, |
| 2153 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2154 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2156 | // C++ [temp.param]p10: |
| 2157 | // The set of default template-arguments available for use with a |
| 2158 | // template declaration or definition is obtained by merging the |
| 2159 | // default arguments from the definition (if in scope) and all |
| 2160 | // declarations in scope in the same way default function |
| 2161 | // arguments are (8.3.6). |
| 2162 | bool SawDefaultArgument = false; |
| 2163 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2164 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 2165 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 2166 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2167 | if (OldParams) |
| 2168 | OldParam = OldParams->begin(); |
| 2169 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2170 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2171 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2172 | NewParamEnd = NewParams->end(); |
| 2173 | NewParam != NewParamEnd; ++NewParam) { |
| 2174 | // Variables used to diagnose redundant default arguments |
| 2175 | bool RedundantDefaultArg = false; |
| 2176 | SourceLocation OldDefaultLoc; |
| 2177 | SourceLocation NewDefaultLoc; |
| 2178 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2179 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2180 | bool MissingDefaultArg = false; |
| 2181 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2182 | // Variable used to diagnose non-final parameter packs |
| 2183 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2184 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2185 | if (TemplateTypeParmDecl *NewTypeParm |
| 2186 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2187 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2188 | if (NewTypeParm->hasDefaultArgument() && |
| 2189 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2190 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2191 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2192 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2193 | NewTypeParm->removeDefaultArgument(); |
| 2194 | |
| 2195 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2196 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2197 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 2198 | if (NewTypeParm->isParameterPack()) { |
| 2199 | assert(!NewTypeParm->hasDefaultArgument() && |
| 2200 | "Parameter packs can't have a default argument!"); |
| 2201 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2202 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2203 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2204 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2205 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2206 | SawDefaultArgument = true; |
| 2207 | RedundantDefaultArg = true; |
| 2208 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2209 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 2210 | // Merge the default argument from the old declaration to the |
| 2211 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2212 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2213 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2214 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 2215 | SawDefaultArgument = true; |
| 2216 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2217 | } else if (SawDefaultArgument) |
| 2218 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2219 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2220 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2221 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2222 | if (!NewNonTypeParm->isParameterPack() && |
| 2223 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2224 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2225 | UPPC_NonTypeTemplateParameterType)) { |
| 2226 | Invalid = true; |
| 2227 | continue; |
| 2228 | } |
| 2229 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2230 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2231 | if (NewNonTypeParm->hasDefaultArgument() && |
| 2232 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2233 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2234 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2235 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2238 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2239 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2240 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2241 | if (NewNonTypeParm->isParameterPack()) { |
| 2242 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 2243 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2244 | if (!NewNonTypeParm->isPackExpansion()) |
| 2245 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2246 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 2247 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2248 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2249 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2250 | SawDefaultArgument = true; |
| 2251 | RedundantDefaultArg = true; |
| 2252 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2253 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 2254 | // Merge the default argument from the old declaration to the |
| 2255 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2256 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2257 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2258 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 2259 | SawDefaultArgument = true; |
| 2260 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2261 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2263 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2264 | TemplateTemplateParmDecl *NewTemplateParm |
| 2265 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2266 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2267 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 2268 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2269 | Invalid = true; |
| 2270 | continue; |
| 2271 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2272 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2273 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2274 | if (NewTemplateParm->hasDefaultArgument() && |
| 2275 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2276 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2277 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2278 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2279 | |
| 2280 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2281 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2282 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2283 | if (NewTemplateParm->isParameterPack()) { |
| 2284 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 2285 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2286 | if (!NewTemplateParm->isPackExpansion()) |
| 2287 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2288 | } else if (OldTemplateParm && |
| 2289 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 2290 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2291 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 2292 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2293 | SawDefaultArgument = true; |
| 2294 | RedundantDefaultArg = true; |
| 2295 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2296 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 2297 | // Merge the default argument from the old declaration to the |
| 2298 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2299 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2300 | PreviousDefaultArgLoc |
| 2301 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2302 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 2303 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2304 | PreviousDefaultArgLoc |
| 2305 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2306 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2310 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2311 | // If a template parameter of a primary class template or alias template |
| 2312 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2313 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2314 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 2315 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2316 | Diag((*NewParam)->getLocation(), |
| 2317 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 2318 | Invalid = true; |
| 2319 | } |
| 2320 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2321 | if (RedundantDefaultArg) { |
| 2322 | // C++ [temp.param]p12: |
| 2323 | // A template-parameter shall not be given default arguments |
| 2324 | // by two different declarations in the same scope. |
| 2325 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 2326 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 2327 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 2328 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2329 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2330 | // If a template-parameter of a class template has a default |
| 2331 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 2332 | // have a default template-argument supplied or be a template parameter |
| 2333 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2335 | diag::err_template_param_default_arg_missing); |
| 2336 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 2337 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2338 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | // If we have an old template parameter list that we're merging |
| 2342 | // in, move on to the next parameter. |
| 2343 | if (OldParams) |
| 2344 | ++OldParam; |
| 2345 | } |
| 2346 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2347 | // We were missing some default arguments at the end of the list, so remove |
| 2348 | // all of the default arguments. |
| 2349 | if (RemoveDefaultArguments) { |
| 2350 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2351 | NewParamEnd = NewParams->end(); |
| 2352 | NewParam != NewParamEnd; ++NewParam) { |
| 2353 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 2354 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2355 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2356 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 2357 | NTTP->removeDefaultArgument(); |
| 2358 | else |
| 2359 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 2360 | } |
| 2361 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2362 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2363 | return Invalid; |
| 2364 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2365 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2366 | namespace { |
| 2367 | |
| 2368 | /// A class which looks for a use of a certain level of template |
| 2369 | /// parameter. |
| 2370 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 2371 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 2372 | |
| 2373 | unsigned Depth; |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2374 | |
| 2375 | // Whether we're looking for a use of a template parameter that makes the |
| 2376 | // overall construct type-dependent / a dependent type. This is strictly |
| 2377 | // best-effort for now; we may fail to match at all for a dependent type |
| 2378 | // in some cases if this is set. |
| 2379 | bool IgnoreNonTypeDependent; |
| 2380 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2381 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2382 | SourceLocation MatchLoc; |
| 2383 | |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2384 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
| 2385 | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
| 2386 | Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2387 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2388 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2389 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
| 2390 | NamedDecl *ND = Params->getParam(0); |
| 2391 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 2392 | Depth = PD->getDepth(); |
| 2393 | } else if (NonTypeTemplateParmDecl *PD = |
| 2394 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 2395 | Depth = PD->getDepth(); |
| 2396 | } else { |
| 2397 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 2398 | } |
| 2399 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2400 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2401 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 2402 | if (ParmDepth >= Depth) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2403 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2404 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2405 | return true; |
| 2406 | } |
| 2407 | return false; |
| 2408 | } |
| 2409 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2410 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 2411 | // Prune out non-type-dependent expressions if requested. This can |
| 2412 | // sometimes result in us failing to find a template parameter reference |
| 2413 | // (if a value-dependent expression creates a dependent type), but this |
| 2414 | // mode is best-effort only. |
| 2415 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 2416 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 2417 | return true; |
| 2418 | return super::TraverseStmt(S, Q); |
| 2419 | } |
| 2420 | |
| 2421 | bool TraverseTypeLoc(TypeLoc TL) { |
| 2422 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 2423 | !TL.getType()->isDependentType()) |
| 2424 | return true; |
| 2425 | return super::TraverseTypeLoc(TL); |
| 2426 | } |
| 2427 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2428 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2429 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 2430 | } |
| 2431 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2432 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2433 | // For a best-effort search, keep looking until we find a location. |
| 2434 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | bool TraverseTemplateName(TemplateName N) { |
| 2438 | if (TemplateTemplateParmDecl *PD = |
| 2439 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2440 | if (Matches(PD->getDepth())) |
| 2441 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2442 | return super::TraverseTemplateName(N); |
| 2443 | } |
| 2444 | |
| 2445 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 2446 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2447 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 2448 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2449 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2450 | return super::VisitDeclRefExpr(E); |
| 2451 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2452 | |
| 2453 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 2454 | return TraverseType(T->getReplacementType()); |
| 2455 | } |
| 2456 | |
| 2457 | bool |
| 2458 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 2459 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 2460 | } |
| 2461 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 2462 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 2463 | return TraverseType(T->getInjectedSpecializationType()); |
| 2464 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2465 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2466 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2467 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2468 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2469 | /// list. |
| 2470 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2471 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2472 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2473 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2474 | return Checker.Match; |
| 2475 | } |
| 2476 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2477 | // Find the source range corresponding to the named type in the given |
| 2478 | // nested-name-specifier, if any. |
| 2479 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 2480 | QualType T, |
| 2481 | const CXXScopeSpec &SS) { |
| 2482 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 2483 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 2484 | if (const Type *CurType = NNS->getAsType()) { |
| 2485 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 2486 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 2487 | } else |
| 2488 | break; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2489 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2490 | NNSLoc = NNSLoc.getPrefix(); |
| 2491 | } |
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 | return SourceRange(); |
| 2494 | } |
| 2495 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2496 | /// Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2497 | /// specifier, returning the template parameter list that applies to the |
| 2498 | /// name. |
| 2499 | /// |
| 2500 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 2501 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2502 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2503 | /// \param DeclLoc The location of the declaration itself. |
| 2504 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2505 | /// \param SS the scope specifier that will be matched to the given template |
| 2506 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 2507 | /// being declared. |
| 2508 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2509 | /// \param TemplateId The template-id following the scope specifier, if there |
| 2510 | /// is one. Used to check for a missing 'template<>'. |
| 2511 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2512 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 2513 | /// innermost template parameter lists. |
| 2514 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2515 | /// \param IsFriend Whether to apply the slightly different rules for |
| 2516 | /// matching template parameters to scope specifiers in friend |
| 2517 | /// declarations. |
| 2518 | /// |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2519 | /// \param IsMemberSpecialization will be set true if the scope specifier |
| 2520 | /// denotes a fully-specialized type, and therefore this is a declaration of |
| 2521 | /// a member specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2522 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2524 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2525 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2527 | /// 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] | 2528 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2529 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 2530 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2531 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2532 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2533 | bool &IsMemberSpecialization, bool &Invalid) { |
| 2534 | IsMemberSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2535 | Invalid = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2536 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2537 | // The sequence of nested types to which we will match up the template |
| 2538 | // parameter lists. We first build this list by starting with the type named |
| 2539 | // 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] | 2540 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2541 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2542 | if (SS.getScopeRep()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2543 | if (CXXRecordDecl *Record |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2544 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 2545 | T = Context.getTypeDeclType(Record); |
| 2546 | else |
| 2547 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 2548 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2550 | // If we found an explicit specialization that prevents us from needing |
| 2551 | // 'template<>' headers, this will be set to the location of that |
| 2552 | // explicit specialization. |
| 2553 | SourceLocation ExplicitSpecLoc; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2554 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2555 | while (!T.isNull()) { |
| 2556 | NestedTypes.push_back(T); |
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 | // Retrieve the parent of a record type. |
| 2559 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2560 | // If this type is an explicit specialization, we're done. |
| 2561 | if (ClassTemplateSpecializationDecl *Spec |
| 2562 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2563 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2564 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 2565 | ExplicitSpecLoc = Spec->getLocation(); |
| 2566 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 2567 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2568 | } else if (Record->getTemplateSpecializationKind() |
| 2569 | == TSK_ExplicitSpecialization) { |
| 2570 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2571 | break; |
| 2572 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2573 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2574 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 2575 | T = Context.getTypeDeclType(Parent); |
| 2576 | else |
| 2577 | T = QualType(); |
| 2578 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2581 | if (const TemplateSpecializationType *TST |
| 2582 | = T->getAs<TemplateSpecializationType>()) { |
| 2583 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 2584 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 2585 | T = Context.getTypeDeclType(Parent); |
| 2586 | else |
| 2587 | T = QualType(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2588 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2589 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2590 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2591 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2592 | // Look one step prior in a dependent template specialization type. |
| 2593 | if (const DependentTemplateSpecializationType *DependentTST |
| 2594 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2595 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 2596 | T = QualType(NNS->getAsType(), 0); |
| 2597 | else |
| 2598 | T = QualType(); |
| 2599 | continue; |
| 2600 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2601 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2602 | // Look one step prior in a dependent name type. |
| 2603 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 2604 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 2605 | T = QualType(NNS->getAsType(), 0); |
| 2606 | else |
| 2607 | T = QualType(); |
| 2608 | continue; |
| 2609 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2610 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2611 | // Retrieve the parent of an enumeration type. |
| 2612 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 2613 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 2614 | // check here. |
| 2615 | EnumDecl *Enum = EnumT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2616 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2617 | // Get to the parent type. |
| 2618 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 2619 | T = Context.getTypeDeclType(Parent); |
| 2620 | else |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2621 | T = QualType(); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2622 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2623 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2624 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2625 | T = QualType(); |
| 2626 | } |
| 2627 | // Reverse the nested types list, since we want to traverse from the outermost |
| 2628 | // to the innermost while checking template-parameter-lists. |
| 2629 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2630 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2631 | // C++0x [temp.expl.spec]p17: |
| 2632 | // A member or a member template may be nested within many |
| 2633 | // enclosing class templates. In an explicit specialization for |
| 2634 | // such a member, the member declaration shall be preceded by a |
| 2635 | // template<> for each enclosing class template that is |
| 2636 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2637 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2638 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2639 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2640 | if (SawNonEmptyTemplateParameterList) { |
| 2641 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 2642 | << !Recovery << Range; |
| 2643 | Invalid = true; |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2644 | IsMemberSpecialization = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2645 | return true; |
| 2646 | } |
| 2647 | |
| 2648 | return false; |
| 2649 | }; |
| 2650 | |
| 2651 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 2652 | // Check that we can have an explicit specialization here. |
| 2653 | if (CheckExplicitSpecialization(Range, true)) |
| 2654 | return true; |
| 2655 | |
| 2656 | // We don't have a template header, but we should. |
| 2657 | SourceLocation ExpectedTemplateLoc; |
| 2658 | if (!ParamLists.empty()) |
| 2659 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 2660 | else |
| 2661 | ExpectedTemplateLoc = DeclStartLoc; |
| 2662 | |
| 2663 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 2664 | << Range |
| 2665 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 2666 | return false; |
| 2667 | }; |
| 2668 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2669 | unsigned ParamIdx = 0; |
| 2670 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 2671 | ++TypeIdx) { |
| 2672 | T = NestedTypes[TypeIdx]; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2673 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2674 | // Whether we expect a 'template<>' header. |
| 2675 | bool NeedEmptyTemplateHeader = false; |
| 2676 | |
| 2677 | // Whether we expect a template header with parameters. |
| 2678 | bool NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2679 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2680 | // For a dependent type, the set of template parameters that we |
| 2681 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2682 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2683 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2684 | // C++0x [temp.expl.spec]p15: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2685 | // A member or a member template may be nested within many enclosing |
| 2686 | // class templates. In an explicit specialization for such a member, the |
| 2687 | // member declaration shall be preceded by a template<> for each |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2688 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2689 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2690 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 2691 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 2692 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 2693 | NeedNonemptyTemplateHeader = true; |
| 2694 | } else if (Record->isDependentType()) { |
| 2695 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2696 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2697 | ->getTemplateParameters(); |
| 2698 | NeedNonemptyTemplateHeader = true; |
| 2699 | } |
| 2700 | } else if (ClassTemplateSpecializationDecl *Spec |
| 2701 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 2702 | // C++0x [temp.expl.spec]p4: |
| 2703 | // Members of an explicitly specialized class template are defined |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2704 | // in the same manner as members of normal classes, and not using |
| 2705 | // the template<> syntax. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2706 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 2707 | NeedEmptyTemplateHeader = true; |
| 2708 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 2709 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2710 | } else if (Record->getTemplateSpecializationKind()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2711 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2712 | != TSK_ExplicitSpecialization && |
| 2713 | TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2714 | IsMemberSpecialization = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2715 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2716 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2717 | } |
| 2718 | } else if (const TemplateSpecializationType *TST |
| 2719 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 2720 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2721 | ExpectedTemplateParams = Template->getTemplateParameters(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2722 | NeedNonemptyTemplateHeader = true; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2723 | } |
| 2724 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2725 | // FIXME: We actually could/should check the template arguments here |
| 2726 | // against the corresponding template parameter list. |
| 2727 | NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2730 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2731 | // In an explicit specialization declaration for a member of a class |
| 2732 | // template or a member template that ap- pears in namespace scope, the |
| 2733 | // member template and some of its enclosing class templates may remain |
| 2734 | // unspecialized, except that the declaration shall not explicitly |
| 2735 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2736 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2737 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2738 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2739 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2740 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2741 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2742 | } else |
| 2743 | SawNonEmptyTemplateParameterList = true; |
| 2744 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2745 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2746 | if (NeedEmptyTemplateHeader) { |
| 2747 | // 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] | 2748 | // here, then it's a member specialization. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2749 | if (TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2750 | IsMemberSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2751 | |
| 2752 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2753 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2754 | // The header has template parameters when it shouldn't. Complain. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2755 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2756 | diag::err_template_param_list_matches_nontemplate) |
| 2757 | << T |
| 2758 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2759 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2760 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2761 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2762 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2763 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2764 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2765 | // Consume this template header. |
| 2766 | ++ParamIdx; |
| 2767 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2768 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2769 | |
| 2770 | if (!IsFriend) |
| 2771 | if (DiagnoseMissingExplicitSpecialization( |
| 2772 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2773 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2774 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2775 | continue; |
| 2776 | } |
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 | if (NeedNonemptyTemplateHeader) { |
| 2779 | // In friend declarations we can have template-ids which don't |
| 2780 | // depend on the corresponding template parameter lists. But |
| 2781 | // assume that empty parameter lists are supposed to match this |
| 2782 | // template-id. |
| 2783 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2784 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2785 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2786 | ExpectedTemplateParams = nullptr; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2787 | else |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2788 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2790 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2791 | if (ParamIdx < ParamLists.size()) { |
| 2792 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2793 | if (ExpectedTemplateParams && |
| 2794 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2795 | ExpectedTemplateParams, |
| 2796 | true, TPL_TemplateMatch)) |
| 2797 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2798 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2799 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2800 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2801 | TPC_ClassTemplateMember)) |
| 2802 | Invalid = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2804 | ++ParamIdx; |
| 2805 | continue; |
| 2806 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2807 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2808 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2809 | << T |
| 2810 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2811 | Invalid = true; |
| 2812 | continue; |
| 2813 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2814 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2816 | // If there were at least as many template-ids as there were template |
| 2817 | // parameter lists, then there are no template parameter lists remaining for |
| 2818 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2819 | if (ParamIdx >= ParamLists.size()) { |
| 2820 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2821 | // We don't have a template header for the declaration itself, but we |
| 2822 | // should. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2823 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2824 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2825 | |
| 2826 | // Fabricate an empty template parameter list for the invented header. |
| 2827 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2828 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2829 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2832 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2833 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2834 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2835 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2836 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2837 | bool HasAnyExplicitSpecHeader = false; |
| 2838 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2839 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2840 | if (ParamLists[I]->size() == 0) |
| 2841 | HasAnyExplicitSpecHeader = true; |
| 2842 | else |
| 2843 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2844 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2846 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2847 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2848 | : diag::err_template_spec_extra_headers) |
| 2849 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2850 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2851 | |
| 2852 | // If there was a specialization somewhere, such that 'template<>' is |
| 2853 | // not required, and there were any 'template<>' headers, note where the |
| 2854 | // specialization occurred. |
| 2855 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2856 | Diag(ExplicitSpecLoc, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2857 | diag::note_explicit_template_spec_does_not_need_header) |
| 2858 | << NestedTypes.back(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2859 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2860 | // We have a template parameter list with no corresponding scope, which |
| 2861 | // means that the resulting template declaration can't be instantiated |
| 2862 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2863 | if (!AllExplicitSpecHeaders) |
| 2864 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2865 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2867 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2868 | // In an explicit specialization declaration for a member of a class |
| 2869 | // template or a member template that ap- pears in namespace scope, the |
| 2870 | // member template and some of its enclosing class templates may remain |
| 2871 | // unspecialized, except that the declaration shall not explicitly |
| 2872 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2873 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2874 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2875 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2876 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2877 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2878 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2879 | // Return the last template parameter list, which corresponds to the |
| 2880 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2881 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2884 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2885 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2886 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2887 | << (isa<FunctionTemplateDecl>(Template) |
| 2888 | ? 0 |
| 2889 | : isa<ClassTemplateDecl>(Template) |
| 2890 | ? 1 |
| 2891 | : isa<VarTemplateDecl>(Template) |
| 2892 | ? 2 |
| 2893 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2894 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2895 | return; |
| 2896 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2897 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2898 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2899 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2900 | IEnd = OST->end(); |
| 2901 | I != IEnd; ++I) |
| 2902 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2903 | << 0 << (*I)->getDeclName(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2904 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2905 | return; |
| 2906 | } |
| 2907 | } |
| 2908 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2909 | static QualType |
| 2910 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2911 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2912 | SourceLocation TemplateLoc, |
| 2913 | TemplateArgumentListInfo &TemplateArgs) { |
| 2914 | ASTContext &Context = SemaRef.getASTContext(); |
| 2915 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2916 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2917 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2918 | // S<T, 0, ..., N-1>. |
| 2919 | |
| 2920 | // C++14 [inteseq.intseq]p1: |
| 2921 | // T shall be an integer type. |
| 2922 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2923 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2924 | diag::err_integer_sequence_integral_element_type); |
| 2925 | return QualType(); |
| 2926 | } |
| 2927 | |
| 2928 | // C++14 [inteseq.make]p1: |
| 2929 | // If N is negative the program is ill-formed. |
| 2930 | TemplateArgument NumArgsArg = Converted[2]; |
| 2931 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2932 | if (NumArgs < 0) { |
| 2933 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2934 | diag::err_integer_sequence_negative_length); |
| 2935 | return QualType(); |
| 2936 | } |
| 2937 | |
| 2938 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2939 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2940 | // The type argument gets reused as the first template argument in the |
| 2941 | // synthetic template argument list. |
| 2942 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2943 | // Expand N into 0 ... N-1. |
| 2944 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2945 | I < NumArgs; ++I) { |
| 2946 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2947 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2948 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2949 | } |
| 2950 | // The first template argument will be reused as the template decl that |
| 2951 | // our synthetic template arguments will be applied to. |
| 2952 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2953 | TemplateLoc, SyntheticTemplateArgs); |
| 2954 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2955 | |
| 2956 | case BTK__type_pack_element: |
| 2957 | // Specializations of |
| 2958 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2959 | // are treated like T_Index. |
| 2960 | assert(Converted.size() == 2 && |
| 2961 | "__type_pack_element should be given an index and a parameter pack"); |
| 2962 | |
| 2963 | // If the Index is out of bounds, the program is ill-formed. |
| 2964 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2965 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2966 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2967 | "type std::size_t, and hence be non-negative"); |
| 2968 | if (Index >= Ts.pack_size()) { |
| 2969 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2970 | diag::err_type_pack_element_out_of_bounds); |
| 2971 | return QualType(); |
| 2972 | } |
| 2973 | |
| 2974 | // We simply return the type at index `Index`. |
| 2975 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2976 | return Nth->getAsType(); |
| 2977 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2978 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2979 | } |
| 2980 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 2981 | /// Determine whether this alias template is "enable_if_t". |
| 2982 | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { |
| 2983 | return AliasTemplate->getName().equals("enable_if_t"); |
| 2984 | } |
| 2985 | |
| 2986 | /// Collect all of the separable terms in the given condition, which |
| 2987 | /// might be a conjunction. |
| 2988 | /// |
| 2989 | /// FIXME: The right answer is to convert the logical expression into |
| 2990 | /// disjunctive normal form, so we can find the first failed term |
| 2991 | /// within each possible clause. |
| 2992 | static void collectConjunctionTerms(Expr *Clause, |
| 2993 | SmallVectorImpl<Expr *> &Terms) { |
| 2994 | if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { |
| 2995 | if (BinOp->getOpcode() == BO_LAnd) { |
| 2996 | collectConjunctionTerms(BinOp->getLHS(), Terms); |
| 2997 | collectConjunctionTerms(BinOp->getRHS(), Terms); |
| 2998 | } |
| 2999 | |
| 3000 | return; |
| 3001 | } |
| 3002 | |
| 3003 | Terms.push_back(Clause); |
| 3004 | } |
| 3005 | |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3006 | // The ranges-v3 library uses an odd pattern of a top-level "||" with |
| 3007 | // a left-hand side that is value-dependent but never true. Identify |
| 3008 | // the idiom and ignore that term. |
| 3009 | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { |
| 3010 | // Top-level '||'. |
| 3011 | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); |
| 3012 | if (!BinOp) return Cond; |
| 3013 | |
| 3014 | if (BinOp->getOpcode() != BO_LOr) return Cond; |
| 3015 | |
| 3016 | // With an inner '==' that has a literal on the right-hand side. |
| 3017 | Expr *LHS = BinOp->getLHS(); |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3018 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3019 | if (!InnerBinOp) return Cond; |
| 3020 | |
| 3021 | if (InnerBinOp->getOpcode() != BO_EQ || |
| 3022 | !isa<IntegerLiteral>(InnerBinOp->getRHS())) |
| 3023 | return Cond; |
| 3024 | |
| 3025 | // If the inner binary operation came from a macro expansion named |
| 3026 | // CONCEPT_REQUIRES or CONCEPT_REQUIRES_, return the right-hand side |
| 3027 | // of the '||', which is the real, user-provided condition. |
Douglas Gregor | c0fe1f2 | 2017-07-05 21:12:37 +0000 | [diff] [blame] | 3028 | SourceLocation Loc = InnerBinOp->getExprLoc(); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3029 | if (!Loc.isMacroID()) return Cond; |
| 3030 | |
| 3031 | StringRef MacroName = PP.getImmediateMacroName(Loc); |
| 3032 | if (MacroName == "CONCEPT_REQUIRES" || MacroName == "CONCEPT_REQUIRES_") |
| 3033 | return BinOp->getRHS(); |
| 3034 | |
| 3035 | return Cond; |
| 3036 | } |
| 3037 | |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3038 | std::pair<Expr *, std::string> |
| 3039 | Sema::findFailedBooleanCondition(Expr *Cond, bool AllowTopLevelCond) { |
| 3040 | Cond = lookThroughRangesV3Condition(PP, Cond); |
Douglas Gregor | bb33f57 | 2017-07-05 20:20:15 +0000 | [diff] [blame] | 3041 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3042 | // Separate out all of the terms in a conjunction. |
| 3043 | SmallVector<Expr *, 4> Terms; |
| 3044 | collectConjunctionTerms(Cond, Terms); |
| 3045 | |
| 3046 | // Determine which term failed. |
| 3047 | Expr *FailedCond = nullptr; |
| 3048 | for (Expr *Term : Terms) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3049 | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); |
| 3050 | |
| 3051 | // Literals are uninteresting. |
| 3052 | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || |
| 3053 | isa<IntegerLiteral>(TermAsWritten)) |
| 3054 | continue; |
| 3055 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3056 | // The initialization of the parameter from the argument is |
| 3057 | // a constant-evaluated context. |
| 3058 | EnterExpressionEvaluationContext ConstantEvaluated( |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3059 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3060 | |
| 3061 | bool Succeeded; |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3062 | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3063 | !Succeeded) { |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3064 | FailedCond = TermAsWritten; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3065 | break; |
| 3066 | } |
| 3067 | } |
| 3068 | |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3069 | if (!FailedCond) { |
| 3070 | if (!AllowTopLevelCond) |
| 3071 | return { nullptr, "" }; |
| 3072 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3073 | FailedCond = Cond->IgnoreParenImpCasts(); |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3074 | } |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3075 | |
| 3076 | std::string Description; |
| 3077 | { |
| 3078 | llvm::raw_string_ostream Out(Description); |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3079 | FailedCond->printPretty(Out, nullptr, getPrintingPolicy()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3080 | } |
| 3081 | return { FailedCond, Description }; |
| 3082 | } |
| 3083 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3084 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 3085 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3086 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3087 | DependentTemplateName *DTN |
| 3088 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3089 | if (DTN && DTN->isIdentifier()) |
| 3090 | // When building a template-id where the template-name is dependent, |
| 3091 | // assume the template is a type template. Either our assumption is |
| 3092 | // correct, or the code is ill-formed and will be diagnosed when the |
| 3093 | // dependent name is substituted. |
| 3094 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 3095 | DTN->getQualifier(), |
| 3096 | DTN->getIdentifier(), |
| 3097 | TemplateArgs); |
| 3098 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3099 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 3100 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 3101 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3102 | // We might have a substituted template template parameter pack. If so, |
| 3103 | // build a template specialization type for it. |
| 3104 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 3105 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3106 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 3107 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 3108 | << Name; |
| 3109 | NoteAllFoundTemplates(Name); |
| 3110 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3111 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3112 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3113 | // Check that the template argument list is well-formed for this |
| 3114 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3115 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3116 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3117 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3118 | return QualType(); |
| 3119 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3120 | QualType CanonType; |
| 3121 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3122 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3123 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 3124 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3125 | // Find the canonical type for this type alias template specialization. |
| 3126 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 3127 | if (Pattern->isInvalidDecl()) |
| 3128 | return QualType(); |
| 3129 | |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3130 | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, |
| 3131 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3132 | |
| 3133 | // Only substitute for the innermost template argument list. |
| 3134 | MultiLevelTemplateArgumentList TemplateArgLists; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3135 | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 3136 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 3137 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 3138 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3139 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3140 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3141 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3142 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3143 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3144 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3145 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 3146 | TemplateArgLists, AliasTemplate->getLocation(), |
| 3147 | AliasTemplate->getDeclName()); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3148 | if (CanonType.isNull()) { |
| 3149 | // If this was enable_if and we failed to find the nested type |
| 3150 | // within enable_if in a SFINAE context, dig out the specific |
| 3151 | // enable_if condition that failed and present that instead. |
| 3152 | if (isEnableIfAliasTemplate(AliasTemplate)) { |
| 3153 | if (auto DeductionInfo = isSFINAEContext()) { |
| 3154 | if (*DeductionInfo && |
| 3155 | (*DeductionInfo)->hasSFINAEDiagnostic() && |
| 3156 | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == |
| 3157 | diag::err_typename_nested_not_found_enable_if && |
| 3158 | TemplateArgs[0].getArgument().getKind() |
| 3159 | == TemplateArgument::Expression) { |
| 3160 | Expr *FailedCond; |
| 3161 | std::string FailedDescription; |
| 3162 | std::tie(FailedCond, FailedDescription) = |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 3163 | findFailedBooleanCondition( |
| 3164 | TemplateArgs[0].getSourceExpression(), |
| 3165 | /*AllowTopLevelCond=*/true); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3166 | |
| 3167 | // Remove the old SFINAE diagnostic. |
| 3168 | PartialDiagnosticAt OldDiag = |
| 3169 | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; |
| 3170 | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); |
| 3171 | |
| 3172 | // Add a new SFINAE diagnostic specifying which condition |
| 3173 | // failed. |
| 3174 | (*DeductionInfo)->addSFINAEDiagnostic( |
| 3175 | OldDiag.first, |
| 3176 | PDiag(diag::err_typename_nested_not_found_requirement) |
| 3177 | << FailedDescription |
| 3178 | << FailedCond->getSourceRange()); |
| 3179 | } |
| 3180 | } |
| 3181 | } |
| 3182 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3183 | return QualType(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 3184 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3185 | } else if (Name.isDependent() || |
| 3186 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3187 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3188 | // This class template specialization is a dependent |
| 3189 | // type. Therefore, its canonical type is another class template |
| 3190 | // specialization type that contains all of the converted |
| 3191 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 3192 | // A<T, T> have identical types when A is declared as: |
| 3193 | // |
| 3194 | // template<typename T, typename U = T> struct A; |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 3195 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3196 | |
| 3197 | // This might work out to be a current instantiation, in which |
| 3198 | // case the canonical type needs to be the InjectedClassNameType. |
| 3199 | // |
| 3200 | // TODO: in theory this could be a simple hashtable lookup; most |
| 3201 | // changes to CurContext don't change the set of current |
| 3202 | // instantiations. |
| 3203 | if (isa<ClassTemplateDecl>(Template)) { |
| 3204 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 3205 | // If we get out to a namespace, we're done. |
| 3206 | if (Ctx->isFileContext()) break; |
| 3207 | |
| 3208 | // If this isn't a record, keep looking. |
| 3209 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 3210 | if (!Record) continue; |
| 3211 | |
| 3212 | // Look for one of the two cases with InjectedClassNameTypes |
| 3213 | // and check whether it's the same template. |
| 3214 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 3215 | !Record->getDescribedClassTemplate()) |
| 3216 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3217 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3218 | // Fetch the injected class name type and check whether its |
| 3219 | // injected type is equal to the type we just built. |
| 3220 | QualType ICNT = Context.getTypeDeclType(Record); |
| 3221 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 3222 | ->getInjectedSpecializationType(); |
| 3223 | |
| 3224 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 3225 | continue; |
| 3226 | |
| 3227 | // If so, the canonical type of this TST is the injected |
| 3228 | // class name type of the record we just found. |
| 3229 | assert(ICNT.isCanonical()); |
| 3230 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3231 | break; |
| 3232 | } |
| 3233 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3234 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3235 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3236 | // Find the class template specialization declaration that |
| 3237 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3238 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3239 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3240 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3241 | if (!Decl) { |
| 3242 | // This is the first time we have referenced this class template |
| 3243 | // specialization. Create the canonical declaration and add it to |
| 3244 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 3246 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 3247 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 3248 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3249 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3250 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3251 | Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 3252 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 3253 | if (ClassTemplate->isOutOfLine()) |
| 3254 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 3257 | if (Decl->getSpecializationKind() == TSK_Undeclared) { |
| 3258 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3259 | TemplateArgLists.addOuterTemplateArguments(Converted); |
| 3260 | InstantiateAttrsForDecl(TemplateArgLists, ClassTemplate->getTemplatedDecl(), |
| 3261 | Decl); |
| 3262 | } |
| 3263 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 3264 | // Diagnose uses of this specialization. |
| 3265 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 3266 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3267 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3268 | assert(isa<RecordType>(CanonType) && |
| 3269 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 3270 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 3271 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 3272 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3273 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3275 | // Build the fully-sugared type for this class template |
| 3276 | // specialization, which refers back to the class template |
| 3277 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 3278 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3281 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3282 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3283 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
| 3284 | SourceLocation TemplateIILoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3286 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3287 | SourceLocation RAngleLoc, |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3288 | bool IsCtorOrDtorName, bool IsClassName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3289 | if (SS.isInvalid()) |
| 3290 | return true; |
| 3291 | |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3292 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { |
| 3293 | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); |
| 3294 | |
| 3295 | // C++ [temp.res]p3: |
| 3296 | // A qualified-id that refers to a type and in which the |
| 3297 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 3298 | // shall be prefixed by the keyword typename to indicate that the |
| 3299 | // qualified-id denotes a type, forming an |
| 3300 | // elaborated-type-specifier (7.1.5.3). |
| 3301 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { |
Richard Smith | 3411fbf | 2017-02-01 21:41:18 +0000 | [diff] [blame] | 3302 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3303 | << SS.getScopeRep() << TemplateII->getName(); |
| 3304 | // Recover as if 'typename' were specified. |
| 3305 | // FIXME: This is not quite correct recovery as we don't transform SS |
| 3306 | // into the corresponding dependent form (and we don't diagnose missing |
| 3307 | // 'template' keywords within SS as a result). |
| 3308 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
| 3309 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
| 3310 | TemplateArgsIn, RAngleLoc); |
| 3311 | } |
| 3312 | |
| 3313 | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
| 3314 | // it's not actually allowed to be used as a type in most cases. Because |
| 3315 | // we annotate it before we know whether it's valid, we have to check for |
| 3316 | // this case here. |
| 3317 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3318 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 3319 | Diag(TemplateIILoc, |
| 3320 | TemplateKWLoc.isInvalid() |
| 3321 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
| 3322 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 3323 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 3324 | << 1 /*if any keyword was present, it was 'template'*/; |
| 3325 | } |
| 3326 | } |
| 3327 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3328 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3329 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3330 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3331 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3332 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3333 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3334 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3335 | QualType T |
| 3336 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 3337 | DTN->getQualifier(), |
| 3338 | DTN->getIdentifier(), |
| 3339 | TemplateArgs); |
| 3340 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3341 | TypeLocBuilder TLB; |
| 3342 | DependentTemplateSpecializationTypeLoc SpecTL |
| 3343 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3344 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 3345 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3346 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3347 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3348 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3349 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 3350 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3351 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3352 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3353 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3354 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3355 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3356 | if (Result.isNull()) |
| 3357 | return true; |
| 3358 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3359 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3360 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3361 | TemplateSpecializationTypeLoc SpecTL |
| 3362 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3363 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3364 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3365 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3366 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3367 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3368 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3369 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 3370 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 3371 | // constructor or destructor name (in such a case, the scope specifier |
| 3372 | // will be attached to the enclosing Decl or Expr node). |
| 3373 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3374 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 3375 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 3376 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3377 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3378 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3379 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3380 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3381 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3382 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3384 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3385 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3386 | SourceLocation TagLoc, |
| 3387 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3388 | SourceLocation TemplateKWLoc, |
| 3389 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3390 | SourceLocation TemplateLoc, |
| 3391 | SourceLocation LAngleLoc, |
| 3392 | ASTTemplateArgsPtr TemplateArgsIn, |
| 3393 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3394 | TemplateName Template = TemplateD.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3395 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3396 | // Translate the parser's template argument list in our AST format. |
| 3397 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 3398 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3399 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3400 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3401 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3402 | ElaboratedTypeKeyword Keyword |
| 3403 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3405 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 3406 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3407 | DTN->getQualifier(), |
| 3408 | DTN->getIdentifier(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3409 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3410 | |
| 3411 | // Build type-source information. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3412 | TypeLocBuilder TLB; |
| 3413 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3414 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 3415 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 3416 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3417 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3418 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3419 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3420 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3421 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3422 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3423 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3424 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3425 | |
| 3426 | if (TypeAliasTemplateDecl *TAT = |
| 3427 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 3428 | // C++0x [dcl.type.elab]p2: |
| 3429 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 3430 | // resolves to an alias template specialization, the |
| 3431 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 3432 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 3433 | << TAT << NTK_TypeAliasTemplate << TagKind; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3434 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 3435 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3436 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3437 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 3438 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 3439 | return TypeResult(true); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3440 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3441 | // Check the tag kind |
| 3442 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3443 | RecordDecl *D = RT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3444 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3445 | IdentifierInfo *Id = D->getIdentifier(); |
| 3446 | assert(Id && "templated class must have an identifier"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3447 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 3448 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 3449 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3450 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3451 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3452 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3453 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3454 | } |
| 3455 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3456 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3457 | // Provide source-location information for the template specialization. |
| 3458 | TypeLocBuilder TLB; |
| 3459 | TemplateSpecializationTypeLoc SpecTL |
| 3460 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3461 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3462 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 3463 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3464 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3465 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3466 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3468 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3469 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3470 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 3471 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3472 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3473 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3474 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3477 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 3478 | NamedDecl *PrevDecl, |
| 3479 | SourceLocation Loc, |
| 3480 | bool IsPartialSpecialization); |
| 3481 | |
| 3482 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3483 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3484 | static bool isTemplateArgumentTemplateParameter( |
| 3485 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 3486 | switch (Arg.getKind()) { |
| 3487 | case TemplateArgument::Null: |
| 3488 | case TemplateArgument::NullPtr: |
| 3489 | case TemplateArgument::Integral: |
| 3490 | case TemplateArgument::Declaration: |
| 3491 | case TemplateArgument::Pack: |
| 3492 | case TemplateArgument::TemplateExpansion: |
| 3493 | return false; |
| 3494 | |
| 3495 | case TemplateArgument::Type: { |
| 3496 | QualType Type = Arg.getAsType(); |
| 3497 | const TemplateTypeParmType *TPT = |
| 3498 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 3499 | return TPT && !Type.hasQualifiers() && |
| 3500 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 3501 | } |
| 3502 | |
| 3503 | case TemplateArgument::Expression: { |
| 3504 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 3505 | if (!DRE || !DRE->getDecl()) |
| 3506 | return false; |
| 3507 | const NonTypeTemplateParmDecl *NTTP = |
| 3508 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 3509 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 3510 | } |
| 3511 | |
| 3512 | case TemplateArgument::Template: |
| 3513 | const TemplateTemplateParmDecl *TTP = |
| 3514 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 3515 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 3516 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 3517 | } |
| 3518 | llvm_unreachable("unexpected kind of template argument"); |
| 3519 | } |
| 3520 | |
| 3521 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 3522 | ArrayRef<TemplateArgument> Args) { |
| 3523 | if (Params->size() != Args.size()) |
| 3524 | return false; |
| 3525 | |
| 3526 | unsigned Depth = Params->getDepth(); |
| 3527 | |
| 3528 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3529 | TemplateArgument Arg = Args[I]; |
| 3530 | |
| 3531 | // If the parameter is a pack expansion, the argument must be a pack |
| 3532 | // whose only element is a pack expansion. |
| 3533 | if (Params->getParam(I)->isParameterPack()) { |
| 3534 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 3535 | !Arg.pack_begin()->isPackExpansion()) |
| 3536 | return false; |
| 3537 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 3538 | } |
| 3539 | |
| 3540 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 3541 | return false; |
| 3542 | } |
| 3543 | |
| 3544 | return true; |
| 3545 | } |
| 3546 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3547 | /// Convert the parser's template argument list representation into our form. |
| 3548 | static TemplateArgumentListInfo |
| 3549 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 3550 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 3551 | TemplateId.RAngleLoc); |
| 3552 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 3553 | TemplateId.NumArgs); |
| 3554 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 3555 | return TemplateArgs; |
| 3556 | } |
| 3557 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3558 | template<typename PartialSpecDecl> |
| 3559 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 3560 | if (Partial->getDeclContext()->isDependentContext()) |
| 3561 | return; |
| 3562 | |
| 3563 | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
| 3564 | // for non-substitution-failure issues? |
| 3565 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 3566 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 3567 | return; |
| 3568 | |
| 3569 | auto *Template = Partial->getSpecializedTemplate(); |
| 3570 | S.Diag(Partial->getLocation(), |
Richard Smith | fa4a09d | 2016-12-27 20:03:09 +0000 | [diff] [blame] | 3571 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 3572 | << isa<VarTemplateDecl>(Template); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3573 | |
| 3574 | if (Info.hasSFINAEDiagnostic()) { |
| 3575 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 3576 | PartialDiagnostic::NullDiagnostic()}; |
| 3577 | Info.takeSFINAEDiagnostic(Diag); |
| 3578 | SmallString<128> SFINAEArgString; |
| 3579 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 3580 | S.Diag(Diag.first, |
| 3581 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 3582 | << SFINAEArgString; |
| 3583 | } |
| 3584 | |
| 3585 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 3586 | } |
| 3587 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3588 | static void |
| 3589 | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
| 3590 | const llvm::SmallBitVector &DeducibleParams) { |
| 3591 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3592 | if (!DeducibleParams[I]) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 3593 | NamedDecl *Param = TemplateParams->getParam(I); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3594 | if (Param->getDeclName()) |
| 3595 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3596 | << Param->getDeclName(); |
| 3597 | else |
| 3598 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3599 | << "(anonymous)"; |
| 3600 | } |
| 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3605 | template<typename PartialSpecDecl> |
| 3606 | static void checkTemplatePartialSpecialization(Sema &S, |
| 3607 | PartialSpecDecl *Partial) { |
| 3608 | // C++1z [temp.class.spec]p8: (DR1495) |
| 3609 | // - The specialization shall be more specialized than the primary |
| 3610 | // template (14.5.5.2). |
| 3611 | checkMoreSpecializedThanPrimary(S, Partial); |
| 3612 | |
| 3613 | // C++ [temp.class.spec]p8: (DR1315) |
| 3614 | // - Each template-parameter shall appear at least once in the |
| 3615 | // template-id outside a non-deduced context. |
| 3616 | // C++1z [temp.class.spec.match]p3 (P0127R2) |
| 3617 | // If the template arguments of a partial specialization cannot be |
| 3618 | // deduced because of the structure of its template-parameter-list |
| 3619 | // and the template-id, the program is ill-formed. |
| 3620 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 3621 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3622 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 3623 | TemplateParams->getDepth(), DeducibleParams); |
| 3624 | |
| 3625 | if (!DeducibleParams.all()) { |
| 3626 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3627 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 3628 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 3629 | << (NumNonDeducible > 1) |
| 3630 | << SourceRange(Partial->getLocation(), |
| 3631 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3632 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3633 | } |
| 3634 | } |
| 3635 | |
| 3636 | void Sema::CheckTemplatePartialSpecialization( |
| 3637 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 3638 | checkTemplatePartialSpecialization(*this, Partial); |
| 3639 | } |
| 3640 | |
| 3641 | void Sema::CheckTemplatePartialSpecialization( |
| 3642 | VarTemplatePartialSpecializationDecl *Partial) { |
| 3643 | checkTemplatePartialSpecialization(*this, Partial); |
| 3644 | } |
| 3645 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3646 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
| 3647 | // C++1z [temp.param]p11: |
| 3648 | // A template parameter of a deduction guide template that does not have a |
| 3649 | // default-argument shall be deducible from the parameter-type-list of the |
| 3650 | // deduction guide template. |
| 3651 | auto *TemplateParams = TD->getTemplateParameters(); |
| 3652 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3653 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
| 3654 | for (unsigned I = 0; I != TemplateParams->size(); ++I) { |
| 3655 | // A parameter pack is deducible (to an empty pack). |
| 3656 | auto *Param = TemplateParams->getParam(I); |
| 3657 | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)) |
| 3658 | DeducibleParams[I] = true; |
| 3659 | } |
| 3660 | |
| 3661 | if (!DeducibleParams.all()) { |
| 3662 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3663 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
| 3664 | << (NumNonDeducible > 1); |
| 3665 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
| 3666 | } |
| 3667 | } |
| 3668 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3669 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3670 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 3671 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3672 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3673 | // D must be variable template id. |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 3674 | assert(D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3675 | "Variable template specialization is declared with a template it."); |
| 3676 | |
| 3677 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3678 | TemplateArgumentListInfo TemplateArgs = |
| 3679 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3680 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 3681 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 3682 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3683 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3684 | TemplateName Name = TemplateId->Template.get(); |
| 3685 | |
| 3686 | // The template-id must name a variable template. |
| 3687 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3688 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 3689 | if (!VarTemplate) { |
| 3690 | NamedDecl *FnTemplate; |
| 3691 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 3692 | FnTemplate = *OTS->begin(); |
| 3693 | else |
| 3694 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 3695 | if (FnTemplate) |
| 3696 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 3697 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3698 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 3699 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3700 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3701 | |
| 3702 | // Check for unexpanded parameter packs in any of the template arguments. |
| 3703 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 3704 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 3705 | UPPC_PartialSpecialization)) |
| 3706 | return true; |
| 3707 | |
| 3708 | // Check that the template argument list is well-formed for this |
| 3709 | // template. |
| 3710 | SmallVector<TemplateArgument, 4> Converted; |
| 3711 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 3712 | false, Converted)) |
| 3713 | return true; |
| 3714 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3715 | // Find the variable template (partial) specialization declaration that |
| 3716 | // corresponds to these arguments. |
| 3717 | if (IsPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3718 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 3719 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3720 | return true; |
| 3721 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3722 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
| 3723 | // also do them during instantiation. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3724 | bool InstantiationDependent; |
| 3725 | if (!Name.isDependent() && |
| 3726 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3727 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3728 | InstantiationDependent)) { |
| 3729 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3730 | << VarTemplate->getDeclName(); |
| 3731 | IsPartialSpecialization = false; |
| 3732 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3733 | |
| 3734 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 3735 | Converted)) { |
| 3736 | // C++ [temp.class.spec]p9b3: |
| 3737 | // |
| 3738 | // -- The argument list of the specialization shall not be identical |
| 3739 | // to the implicit argument list of the primary template. |
| 3740 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 3741 | << /*variable template*/ 1 |
| 3742 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 3743 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 3744 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 3745 | // of the primary template. |
| 3746 | return true; |
| 3747 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3750 | void *InsertPos = nullptr; |
| 3751 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3752 | |
| 3753 | if (IsPartialSpecialization) |
| 3754 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3755 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3756 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3757 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3758 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3759 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3760 | |
| 3761 | // Check whether we can declare a variable template specialization in |
| 3762 | // the current scope. |
| 3763 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 3764 | TemplateNameLoc, |
| 3765 | IsPartialSpecialization)) |
| 3766 | return true; |
| 3767 | |
| 3768 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 3769 | // Since the only prior variable template specialization with these |
| 3770 | // arguments was referenced but not declared, reuse that |
| 3771 | // declaration node as our own, updating its source location and |
| 3772 | // the list of outer template parameters to reflect our new declaration. |
| 3773 | Specialization = PrevDecl; |
| 3774 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3775 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3776 | } else if (IsPartialSpecialization) { |
| 3777 | // Create a new class template partial specialization declaration node. |
| 3778 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 3779 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3780 | VarTemplatePartialSpecializationDecl *Partial = |
| 3781 | VarTemplatePartialSpecializationDecl::Create( |
| 3782 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 3783 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3784 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3785 | |
| 3786 | if (!PrevPartial) |
| 3787 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 3788 | Specialization = Partial; |
| 3789 | |
| 3790 | // If we are providing an explicit specialization of a member variable |
| 3791 | // template specialization, make a note of that. |
| 3792 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3793 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3794 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3795 | CheckTemplatePartialSpecialization(Partial); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3796 | } else { |
| 3797 | // Create a new class template specialization declaration node for |
| 3798 | // this explicit specialization or friend declaration. |
| 3799 | Specialization = VarTemplateSpecializationDecl::Create( |
| 3800 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3801 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3802 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 3803 | |
| 3804 | if (!PrevDecl) |
| 3805 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 3806 | } |
| 3807 | |
| 3808 | // C++ [temp.expl.spec]p6: |
| 3809 | // If a template, a member template or the member of a class template is |
| 3810 | // explicitly specialized then that specialization shall be declared |
| 3811 | // before the first use of that specialization that would cause an implicit |
| 3812 | // instantiation to take place, in every translation unit in which such a |
| 3813 | // use occurs; no diagnostic is required. |
| 3814 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3815 | bool Okay = false; |
| 3816 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 3817 | // Is there any previous explicit specialization declaration? |
| 3818 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3819 | Okay = true; |
| 3820 | break; |
| 3821 | } |
| 3822 | } |
| 3823 | |
| 3824 | if (!Okay) { |
| 3825 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3826 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3827 | << Name << Range; |
| 3828 | |
| 3829 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3830 | diag::note_instantiation_required_here) |
| 3831 | << (PrevDecl->getTemplateSpecializationKind() != |
| 3832 | TSK_ImplicitInstantiation); |
| 3833 | return true; |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 3838 | Specialization->setLexicalDeclContext(CurContext); |
| 3839 | |
| 3840 | // Add the specialization into its lexical context, so that it can |
| 3841 | // be seen when iterating through the list of declarations in that |
| 3842 | // context. However, specializations are not found by name lookup. |
| 3843 | CurContext->addDecl(Specialization); |
| 3844 | |
| 3845 | // Note that this is an explicit specialization. |
| 3846 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 3847 | |
| 3848 | if (PrevDecl) { |
| 3849 | // Check that this isn't a redefinition of this specialization, |
| 3850 | // merging with previous declarations. |
| 3851 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 3852 | forRedeclarationInCurContext()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3853 | PrevSpec.addDecl(PrevDecl); |
| 3854 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3855 | } else if (Specialization->isStaticDataMember() && |
| 3856 | Specialization->isOutOfLine()) { |
| 3857 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | // Link instantiations of static data members back to the template from |
| 3861 | // which they were instantiated. |
| 3862 | if (Specialization->isStaticDataMember()) |
| 3863 | Specialization->setInstantiationOfStaticDataMember( |
| 3864 | VarTemplate->getTemplatedDecl(), |
| 3865 | Specialization->getSpecializationKind()); |
| 3866 | |
| 3867 | return Specialization; |
| 3868 | } |
| 3869 | |
| 3870 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3871 | /// A partial specialization whose template arguments have matched |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3872 | /// a given template-id. |
| 3873 | struct PartialSpecMatchResult { |
| 3874 | VarTemplatePartialSpecializationDecl *Partial; |
| 3875 | TemplateArgumentList *Args; |
| 3876 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 3877 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3878 | |
| 3879 | DeclResult |
| 3880 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3881 | SourceLocation TemplateNameLoc, |
| 3882 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3883 | assert(Template && "A variable template id without template?"); |
| 3884 | |
| 3885 | // Check that the template argument list is well-formed for this template. |
| 3886 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3887 | if (CheckTemplateArgumentList( |
| 3888 | Template, TemplateNameLoc, |
| 3889 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3890 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3891 | return true; |
| 3892 | |
| 3893 | // Find the variable template specialization declaration that |
| 3894 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3895 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3896 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3897 | Converted, InsertPos)) { |
| 3898 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3899 | // If we already have a variable template specialization, return it. |
| 3900 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3901 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3902 | |
| 3903 | // This is the first time we have referenced this variable template |
| 3904 | // specialization. Create the canonical declaration and add it to |
| 3905 | // the set of specializations, based on the closest partial specialization |
| 3906 | // that it represents. That is, |
| 3907 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 3908 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3909 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3910 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 3911 | bool AmbiguousPartialSpec = false; |
| 3912 | typedef PartialSpecMatchResult MatchResult; |
| 3913 | SmallVector<MatchResult, 4> Matched; |
| 3914 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3915 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 3916 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3917 | |
| 3918 | // 1. Attempt to find the closest partial specialization that this |
| 3919 | // specializes, if any. |
| 3920 | // If any of the template arguments is dependent, then this is probably |
| 3921 | // a placeholder for an incomplete declarative context; which must be |
| 3922 | // complete by instantiation time. Thus, do not search through the partial |
| 3923 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3924 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 3925 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 3926 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3927 | bool InstantiationDependent = false; |
| 3928 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3929 | TemplateArgs, InstantiationDependent)) { |
| 3930 | |
| 3931 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3932 | Template->getPartialSpecializations(PartialSpecs); |
| 3933 | |
| 3934 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 3935 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 3936 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 3937 | |
| 3938 | if (TemplateDeductionResult Result = |
| 3939 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 3940 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3941 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3942 | FailedCandidates.addCandidate().set( |
| 3943 | DeclAccessPair::make(Template, AS_public), Partial, |
| 3944 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3945 | (void)Result; |
| 3946 | } else { |
| 3947 | Matched.push_back(PartialSpecMatchResult()); |
| 3948 | Matched.back().Partial = Partial; |
| 3949 | Matched.back().Args = Info.take(); |
| 3950 | } |
| 3951 | } |
| 3952 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3953 | if (Matched.size() >= 1) { |
| 3954 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 3955 | if (Matched.size() == 1) { |
| 3956 | // -- If exactly one matching specialization is found, the |
| 3957 | // instantiation is generated from that specialization. |
| 3958 | // We don't need to do anything for this. |
| 3959 | } else { |
| 3960 | // -- If more than one matching specialization is found, the |
| 3961 | // partial order rules (14.5.4.2) are used to determine |
| 3962 | // whether one of the specializations is more specialized |
| 3963 | // than the others. If none of the specializations is more |
| 3964 | // specialized than all of the other matching |
| 3965 | // specializations, then the use of the variable template is |
| 3966 | // ambiguous and the program is ill-formed. |
| 3967 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 3968 | PEnd = Matched.end(); |
| 3969 | P != PEnd; ++P) { |
| 3970 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 3971 | PointOfInstantiation) == |
| 3972 | P->Partial) |
| 3973 | Best = P; |
| 3974 | } |
| 3975 | |
| 3976 | // Determine if the best partial specialization is more specialized than |
| 3977 | // the others. |
| 3978 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 3979 | PEnd = Matched.end(); |
| 3980 | P != PEnd; ++P) { |
| 3981 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 3982 | P->Partial, Best->Partial, |
| 3983 | PointOfInstantiation) != Best->Partial) { |
| 3984 | AmbiguousPartialSpec = true; |
| 3985 | break; |
| 3986 | } |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | // Instantiate using the best variable template partial specialization. |
| 3991 | InstantiationPattern = Best->Partial; |
| 3992 | InstantiationArgs = Best->Args; |
| 3993 | } else { |
| 3994 | // -- If no match is found, the instantiation is generated |
| 3995 | // from the primary template. |
| 3996 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 3997 | } |
| 3998 | } |
| 3999 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4000 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4001 | // Note that we do not instantiate a definition until we see an odr-use |
| 4002 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4003 | // FIXME: LateAttrs et al.? |
| 4004 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 4005 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 4006 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 4007 | if (!Decl) |
| 4008 | return true; |
| 4009 | |
| 4010 | if (AmbiguousPartialSpec) { |
| 4011 | // Partial ordering did not produce a clear winner. Complain. |
| 4012 | Decl->setInvalidDecl(); |
| 4013 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 4014 | << Decl; |
| 4015 | |
| 4016 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 4017 | for (MatchResult P : Matched) |
| 4018 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 4019 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 4020 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4021 | return true; |
| 4022 | } |
| 4023 | |
| 4024 | if (VarTemplatePartialSpecializationDecl *D = |
| 4025 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 4026 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 4027 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4028 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 4029 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4030 | assert(Decl && "No variable template specialization?"); |
| 4031 | return Decl; |
| 4032 | } |
| 4033 | |
| 4034 | ExprResult |
| 4035 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 4036 | const DeclarationNameInfo &NameInfo, |
| 4037 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 4038 | const TemplateArgumentListInfo *TemplateArgs) { |
| 4039 | |
| 4040 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 4041 | *TemplateArgs); |
| 4042 | if (Decl.isInvalid()) |
| 4043 | return ExprError(); |
| 4044 | |
| 4045 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 4046 | if (!Var->getTemplateSpecializationKind()) |
| 4047 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 4048 | NameInfo.getLoc()); |
| 4049 | |
| 4050 | // Build an ordinary singleton decl ref. |
| 4051 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4052 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4053 | } |
| 4054 | |
Richard Smith | ecad88d | 2018-04-26 01:08:00 +0000 | [diff] [blame] | 4055 | void Sema::diagnoseMissingTemplateArguments(TemplateName Name, |
| 4056 | SourceLocation Loc) { |
| 4057 | Diag(Loc, diag::err_template_missing_args) |
| 4058 | << (int)getTemplateNameKindForDiagnostics(Name) << Name; |
| 4059 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) { |
| 4060 | Diag(TD->getLocation(), diag::note_template_decl_here) |
| 4061 | << TD->getTemplateParameters()->getSourceRange(); |
| 4062 | } |
| 4063 | } |
| 4064 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4065 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4066 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4067 | LookupResult &R, |
| 4068 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4069 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4070 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 4071 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4072 | // 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] | 4073 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4074 | // foo<int> could identify a single function unambiguously |
| 4075 | // This approach does NOT work, since f<int>(1); |
| 4076 | // gets resolved prior to resorting to overload resolution |
| 4077 | // i.e., template<class T> void f(double); |
| 4078 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4079 | |
| 4080 | // These should be filtered out by our callers. |
| 4081 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 4082 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 4083 | |
Richard Smith | 0410094 | 2018-04-26 02:10:22 +0000 | [diff] [blame] | 4084 | // Non-function templates require a template argument list. |
| 4085 | if (auto *TD = R.getAsSingle<TemplateDecl>()) { |
| 4086 | if (!TemplateArgs && !isa<FunctionTemplateDecl>(TD)) { |
| 4087 | diagnoseMissingTemplateArguments(TemplateName(TD), R.getNameLoc()); |
| 4088 | return ExprError(); |
| 4089 | } |
| 4090 | } |
| 4091 | |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4092 | auto AnyDependentArguments = [&]() -> bool { |
| 4093 | bool InstantiationDependent; |
| 4094 | return TemplateArgs && |
| 4095 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 4096 | *TemplateArgs, InstantiationDependent); |
| 4097 | }; |
| 4098 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4099 | // In C++1y, check variable template ids. |
Richard Smith | 0bf96f9 | 2018-04-25 22:58:55 +0000 | [diff] [blame] | 4100 | if (R.getAsSingle<VarTemplateDecl>() && !AnyDependentArguments()) { |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 4101 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 4102 | R.getAsSingle<VarTemplateDecl>(), |
| 4103 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4106 | // We don't want lookup warnings at this point. |
| 4107 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4108 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4109 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4110 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4111 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4112 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4113 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4114 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 4115 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4116 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4117 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4120 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4121 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4122 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4123 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4124 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4125 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4126 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 4127 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4128 | DeclContext *DC; |
| 4129 | if (!(DC = computeDeclContext(SS, false)) || |
| 4130 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4131 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 4132 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4133 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4134 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4135 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4136 | if (LookupTemplateName(R, (Scope *)nullptr, SS, QualType(), |
| 4137 | /*Entering*/false, MemberOfUnknownSpecialization, |
| 4138 | TemplateKWLoc)) |
| 4139 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4141 | if (R.isAmbiguous()) |
| 4142 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4143 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4144 | if (R.empty()) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4145 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 4146 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4147 | return ExprError(); |
| 4148 | } |
| 4149 | |
| 4150 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4151 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4152 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 4153 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4154 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 4155 | return ExprError(); |
| 4156 | } |
| 4157 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4158 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4161 | /// Form a dependent template name. |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4162 | /// |
| 4163 | /// This action forms a dependent template name given the template |
| 4164 | /// name and its (presumably dependent) scope specifier. For |
| 4165 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 4166 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 4167 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4168 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4169 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4170 | SourceLocation TemplateKWLoc, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 4171 | const UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4172 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4173 | bool EnteringContext, |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4174 | TemplateTy &Result, |
| 4175 | bool AllowInjectedClassName) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4176 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 4177 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4178 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4179 | diag::warn_cxx98_compat_template_outside_of_template : |
| 4180 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 4182 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4183 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4184 | if (SS.isSet()) |
| 4185 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 4186 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4187 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 4188 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4189 | // C++0x [temp.names]p5: |
| 4190 | // If a name prefixed by the keyword template is not the name of |
| 4191 | // a template, the program is ill-formed. [Note: the keyword |
| 4192 | // template may not be applied to non-template members of class |
| 4193 | // templates. -end note ] [ Note: as is the case with the |
| 4194 | // typename prefix, the template prefix is allowed in cases |
| 4195 | // where it is not strictly necessary; i.e., when the |
| 4196 | // nested-name-specifier or the expression on the left of the -> |
| 4197 | // or . is not dependent on a template-parameter, or the use |
| 4198 | // does not appear in the scope of a template. -end note] |
| 4199 | // |
| 4200 | // Note: C++03 was more strict here, because it banned the use of |
| 4201 | // the "template" keyword prior to a template-name that was not a |
| 4202 | // dependent name. C++ DR468 relaxed this requirement (the |
| 4203 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 4204 | // 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] | 4205 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 4206 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 4207 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 4208 | MemberOfUnknownSpecialization); |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4209 | if (TNK == TNK_Non_template && MemberOfUnknownSpecialization) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4210 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4211 | } else if (TNK == TNK_Non_template) { |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 4212 | // Do the lookup again to determine if this is a "nothing found" case or |
| 4213 | // a "not a template" case. FIXME: Refactor isTemplateName so we don't |
| 4214 | // need to do this. |
| 4215 | DeclarationNameInfo DNI = GetNameFromUnqualifiedId(Name); |
| 4216 | LookupResult R(*this, DNI.getName(), Name.getLocStart(), |
| 4217 | LookupOrdinaryName); |
| 4218 | bool MOUS; |
| 4219 | if (!LookupTemplateName(R, S, SS, ObjectType.get(), EnteringContext, |
| 4220 | MOUS, TemplateKWLoc)) |
| 4221 | Diag(Name.getLocStart(), diag::err_no_member) |
| 4222 | << DNI.getName() << LookupCtx << SS.getRange(); |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4223 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 4224 | } else { |
| 4225 | // We found something; return it. |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4226 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
| 4227 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4228 | Name.getKind() == UnqualifiedIdKind::IK_Identifier && |
| 4229 | Name.Identifier && LookupRD->getIdentifier() == Name.Identifier) { |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 4230 | // C++14 [class.qual]p2: |
| 4231 | // In a lookup in which function names are not ignored and the |
| 4232 | // nested-name-specifier nominates a class C, if the name specified |
| 4233 | // [...] is the injected-class-name of C, [...] the name is instead |
| 4234 | // considered to name the constructor |
| 4235 | // |
| 4236 | // We don't get here if naming the constructor would be valid, so we |
| 4237 | // just reject immediately and recover by treating the |
| 4238 | // injected-class-name as naming the template. |
| 4239 | Diag(Name.getLocStart(), |
| 4240 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 4241 | << Name.Identifier << 0 /*injected-class-name used as template name*/ |
| 4242 | << 1 /*'template' keyword was used*/; |
| 4243 | } |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4244 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4245 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 4248 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4250 | switch (Name.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4251 | case UnqualifiedIdKind::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4252 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4253 | Name.Identifier)); |
| 4254 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4255 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4256 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 4257 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 4258 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 4259 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4260 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 4261 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 4262 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 4263 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4264 | default: |
| 4265 | break; |
| 4266 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4268 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 4269 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4270 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 4271 | << 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 | /// Diagnose an arity mismatch in the |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4923 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 4924 | SourceLocation TemplateLoc, |
| 4925 | TemplateArgumentListInfo &TemplateArgs) { |
| 4926 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 4927 | unsigned NumParams = Params->size(); |
| 4928 | unsigned NumArgs = TemplateArgs.size(); |
| 4929 | |
| 4930 | SourceRange Range; |
| 4931 | if (NumArgs > NumParams) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4932 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4933 | TemplateArgs.getRAngleLoc()); |
| 4934 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 4935 | << (NumArgs > NumParams) |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 4936 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4937 | << Template << Range; |
| 4938 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 4939 | << Params->getSourceRange(); |
| 4940 | return true; |
| 4941 | } |
| 4942 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4943 | /// Check whether the template parameter is a pack expansion, and if so, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4944 | /// determine the number of parameters produced by that expansion. For instance: |
| 4945 | /// |
| 4946 | /// \code |
| 4947 | /// template<typename ...Ts> struct A { |
| 4948 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 4949 | /// }; |
| 4950 | /// \endcode |
| 4951 | /// |
| 4952 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 4953 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4954 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4955 | if (NonTypeTemplateParmDecl *NTTP |
| 4956 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4957 | if (NTTP->isExpandedParameterPack()) |
| 4958 | return NTTP->getNumExpansionTypes(); |
| 4959 | } |
| 4960 | |
| 4961 | if (TemplateTemplateParmDecl *TTP |
| 4962 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 4963 | if (TTP->isExpandedParameterPack()) |
| 4964 | return TTP->getNumExpansionTemplateParameters(); |
| 4965 | } |
| 4966 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 4967 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4970 | /// Diagnose a missing template argument. |
| 4971 | template<typename TemplateParmDecl> |
| 4972 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 4973 | TemplateDecl *TD, |
| 4974 | const TemplateParmDecl *D, |
| 4975 | TemplateArgumentListInfo &Args) { |
| 4976 | // Dig out the most recent declaration of the template parameter; there may be |
| 4977 | // declarations of the template that are more recent than TD. |
| 4978 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 4979 | ->getTemplateParameters() |
| 4980 | ->getParam(D->getIndex())); |
| 4981 | |
| 4982 | // If there's a default argument that's not visible, diagnose that we're |
| 4983 | // missing a module import. |
| 4984 | llvm::SmallVector<Module*, 8> Modules; |
| 4985 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 4986 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 4987 | D->getDefaultArgumentLoc(), Modules, |
| 4988 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4989 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4990 | return true; |
| 4991 | } |
| 4992 | |
| 4993 | // FIXME: If there's a more recent default argument that *is* visible, |
| 4994 | // diagnose that it was declared too late. |
| 4995 | |
| 4996 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 4997 | } |
| 4998 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4999 | /// Check that the given template argument list is well-formed |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5000 | /// for specializing the given template. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5001 | bool Sema::CheckTemplateArgumentList( |
| 5002 | TemplateDecl *Template, SourceLocation TemplateLoc, |
| 5003 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
| 5004 | SmallVectorImpl<TemplateArgument> &Converted, |
| 5005 | bool UpdateArgsWithConversions) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5006 | // Make a copy of the template arguments for processing. Only make the |
| 5007 | // changes at the end when successful in matching the arguments to the |
| 5008 | // template. |
| 5009 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 5010 | |
Erich Keane | af0795b | 2017-10-24 01:39:56 +0000 | [diff] [blame] | 5011 | // Make sure we get the template parameter list from the most |
| 5012 | // recentdeclaration, since that is the only one that has is guaranteed to |
| 5013 | // have all the default template argument information. |
| 5014 | TemplateParameterList *Params = |
| 5015 | cast<TemplateDecl>(Template->getMostRecentDecl()) |
| 5016 | ->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5017 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5018 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5019 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5020 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5021 | // [...] The type and form of each template-argument specified in |
| 5022 | // a template-id shall match the type and form specified for the |
| 5023 | // corresponding parameter declared by the template in its |
| 5024 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5025 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5026 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5027 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 5028 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5029 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5030 | ParamEnd = Params->end(); |
| 5031 | Param != ParamEnd; /* increment in loop */) { |
| 5032 | // If we have an expanded parameter pack, make sure we don't have too |
| 5033 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 5034 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5035 | if (*Expansions == ArgumentPack.size()) { |
| 5036 | // We're done with this parameter pack. Pack up its arguments and add |
| 5037 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5038 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5039 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5040 | ArgumentPack.clear(); |
| 5041 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5042 | // This argument is assigned to the next parameter. |
| 5043 | ++Param; |
| 5044 | continue; |
| 5045 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 5046 | // Not enough arguments for this parameter pack. |
| 5047 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 5048 | << false |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 5049 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5050 | << Template; |
| 5051 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5052 | << Params->getSourceRange(); |
| 5053 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5054 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5055 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5056 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5057 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5058 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5059 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5060 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5061 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5062 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5063 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5064 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5065 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5066 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 5067 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5068 | // 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] | 5069 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5070 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5071 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5072 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5073 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 5074 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 5075 | return true; |
| 5076 | } |
| 5077 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5078 | // We're now done with this argument. |
| 5079 | ++ArgIdx; |
| 5080 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5081 | if ((*Param)->isTemplateParameterPack()) { |
| 5082 | // The template parameter was a template parameter pack, so take the |
| 5083 | // deduced argument and place it on the argument pack. Note that we |
| 5084 | // stay on the same template parameter so that we can deduce more |
| 5085 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 5086 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5087 | } else { |
| 5088 | // Move to the next template parameter. |
| 5089 | ++Param; |
| 5090 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5091 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 5092 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 5093 | // the remaining arguments, because we don't know what parameters they'll |
| 5094 | // match up with. |
| 5095 | if (PackExpansionIntoNonPack) { |
| 5096 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5097 | // If we were part way through filling in an expanded parameter pack, |
| 5098 | // fall back to just producing individual arguments. |
| 5099 | Converted.insert(Converted.end(), |
| 5100 | ArgumentPack.begin(), ArgumentPack.end()); |
| 5101 | ArgumentPack.clear(); |
| 5102 | } |
| 5103 | |
| 5104 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5105 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5106 | ++ArgIdx; |
| 5107 | } |
| 5108 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5109 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5110 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5111 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5112 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5113 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5114 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5115 | // If we're checking a partial template argument list, we're done. |
| 5116 | if (PartialTemplateArgs) { |
| 5117 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5118 | Converted.push_back( |
| 5119 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 5120 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5121 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 5122 | } |
| 5123 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5124 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5125 | // 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] | 5126 | if ((*Param)->isTemplateParameterPack()) { |
| 5127 | assert(!getExpandedPackSize(*Param) && |
| 5128 | "Should have dealt with this already"); |
| 5129 | |
| 5130 | // A non-expanded parameter pack before the end of the parameter list |
| 5131 | // only occurs for an ill-formed template parameter list, unless we've |
| 5132 | // got a partial argument list for a function template, so just bail out. |
| 5133 | if (Param + 1 != ParamEnd) |
| 5134 | return true; |
| 5135 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5136 | Converted.push_back( |
| 5137 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5138 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5139 | |
| 5140 | ++Param; |
| 5141 | continue; |
| 5142 | } |
| 5143 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5144 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5145 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5146 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5147 | // Retrieve the default template argument from the template |
| 5148 | // parameter. For each kind of template parameter, we substitute the |
| 5149 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5150 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5151 | // the default argument. |
| 5152 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5153 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5154 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 5155 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5156 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5157 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5158 | Template, |
| 5159 | TemplateLoc, |
| 5160 | RAngleLoc, |
| 5161 | TTP, |
| 5162 | Converted); |
| 5163 | if (!ArgType) |
| 5164 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5165 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5166 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 5167 | ArgType); |
| 5168 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5169 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5170 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5171 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 5172 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5173 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5174 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5175 | TemplateLoc, |
| 5176 | RAngleLoc, |
| 5177 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5178 | Converted); |
| 5179 | if (E.isInvalid()) |
| 5180 | return true; |
| 5181 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5182 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5183 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 5184 | } else { |
| 5185 | TemplateTemplateParmDecl *TempParm |
| 5186 | = cast<TemplateTemplateParmDecl>(*Param); |
| 5187 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 5188 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 5189 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 5190 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5191 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 5192 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5193 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5194 | TemplateLoc, |
| 5195 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5196 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5197 | Converted, |
| 5198 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5199 | if (Name.isNull()) |
| 5200 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5201 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 5202 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 5203 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5204 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5205 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5206 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 5207 | // the default template argument. We're not actually instantiating a |
| 5208 | // template here, we just create this object to put a note into the |
| 5209 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 5210 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 5211 | SourceRange(TemplateLoc, RAngleLoc)); |
| 5212 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 5213 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5214 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 5215 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 5216 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 5217 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 5218 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5219 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5220 | // Core issue 150 (assumed resolution): if this is a template template |
| 5221 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5222 | // template definition. |
| 5223 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5224 | NewArgs.addArgument(Arg); |
| 5225 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 5226 | // Move to the next template parameter and argument. |
| 5227 | ++Param; |
| 5228 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5229 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5230 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5231 | // If we're performing a partial argument substitution, allow any trailing |
| 5232 | // pack expansions; they might be empty. This can happen even if |
| 5233 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 5234 | // still dependent). |
| 5235 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 5236 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5237 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 5238 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 5241 | // If we have any leftover arguments, then there were too many arguments. |
| 5242 | // Complain and fail. |
| 5243 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 5244 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 5245 | |
| 5246 | // No problems found with the new argument list, propagate changes back |
| 5247 | // to caller. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 5248 | if (UpdateArgsWithConversions) |
| 5249 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5250 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5251 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5252 | } |
| 5253 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5254 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5255 | class UnnamedLocalNoLinkageFinder |
| 5256 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5257 | { |
| 5258 | Sema &S; |
| 5259 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5260 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5261 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5262 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5263 | public: |
| 5264 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 5265 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5266 | bool Visit(QualType T) { |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5267 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5268 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5269 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5270 | #define TYPE(Class, Parent) \ |
| 5271 | bool Visit##Class##Type(const Class##Type *); |
| 5272 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 5273 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5274 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 5275 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5276 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5277 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5278 | bool VisitTagDecl(const TagDecl *Tag); |
| 5279 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 5280 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 5281 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5282 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5283 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5284 | return false; |
| 5285 | } |
| 5286 | |
| 5287 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 5288 | return Visit(T->getElementType()); |
| 5289 | } |
| 5290 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5291 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5292 | return Visit(T->getPointeeType()); |
| 5293 | } |
| 5294 | |
| 5295 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5296 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5297 | return Visit(T->getPointeeType()); |
| 5298 | } |
| 5299 | |
| 5300 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5301 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5302 | return Visit(T->getPointeeType()); |
| 5303 | } |
| 5304 | |
| 5305 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5306 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5307 | return Visit(T->getPointeeType()); |
| 5308 | } |
| 5309 | |
| 5310 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5311 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5312 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 5313 | } |
| 5314 | |
| 5315 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5316 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5317 | return Visit(T->getElementType()); |
| 5318 | } |
| 5319 | |
| 5320 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5321 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5322 | return Visit(T->getElementType()); |
| 5323 | } |
| 5324 | |
| 5325 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5326 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5327 | return Visit(T->getElementType()); |
| 5328 | } |
| 5329 | |
| 5330 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5331 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5332 | return Visit(T->getElementType()); |
| 5333 | } |
| 5334 | |
| 5335 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5336 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5337 | return Visit(T->getElementType()); |
| 5338 | } |
| 5339 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 5340 | bool UnnamedLocalNoLinkageFinder::VisitDependentAddressSpaceType( |
| 5341 | const DependentAddressSpaceType *T) { |
| 5342 | return Visit(T->getPointeeType()); |
| 5343 | } |
| 5344 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5345 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 5346 | return Visit(T->getElementType()); |
| 5347 | } |
| 5348 | |
| 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) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5627 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 5628 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 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) { |
| 5668 | S.Diag(Arg->getLocStart(), |
| 5669 | diag::err_template_arg_ref_bind_ignores_quals) |
| 5670 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 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()) |
| 5684 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 5685 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 5686 | else |
| 5687 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 5688 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 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) |
| 5734 | S.Diag(ArgIn->getLocStart(), 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); |
| 5743 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5744 | << Arg->getSourceRange(); |
| 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) { |
| 5768 | S.Diag(Arg->getLocStart(), |
| 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) { |
| 5834 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5835 | << Arg->getSourceRange(); |
| 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)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5842 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 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()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5851 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 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. |
| 5864 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 5865 | << Arg->getSourceRange(); |
| 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) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5872 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5873 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 5874 | diag::ext_template_arg_object_internal) |
| 5875 | << !Func << Entity << Arg->getSourceRange(); |
| 5876 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5877 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5878 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5879 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 5880 | << !Func << Entity << Arg->getSourceRange(); |
| 5881 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5882 | << !Func; |
| 5883 | return true; |
| 5884 | } |
| 5885 | |
| 5886 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5887 | // If the template parameter has pointer type, the function decays. |
| 5888 | if (ParamType->isPointerType() && !AddressTaken) |
| 5889 | ArgType = S.Context.getPointerType(Func->getType()); |
| 5890 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 5891 | // If we originally had an address-of operator, but the |
| 5892 | // parameter has reference type, complain and (if things look |
| 5893 | // like they will work) drop the address-of operator. |
| 5894 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 5895 | ParamType.getNonReferenceType())) { |
| 5896 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5897 | << ParamType; |
| 5898 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5899 | return true; |
| 5900 | } |
| 5901 | |
| 5902 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5903 | << ParamType |
| 5904 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5905 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5906 | |
| 5907 | ArgType = Func->getType(); |
| 5908 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5909 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5910 | // A value of reference type is not an object. |
| 5911 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5912 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5913 | diag::err_template_arg_reference_var) |
| 5914 | << Var->getType() << Arg->getSourceRange(); |
| 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()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5921 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 5922 | << Arg->getSourceRange(); |
| 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)) { |
| 5959 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 5960 | << ParamType; |
| 5961 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5962 | return true; |
| 5963 | } |
| 5964 | |
| 5965 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 5966 | << ParamType |
| 5967 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 5968 | |
| 5969 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5970 | } |
| 5971 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5972 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5973 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5974 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 5975 | Arg, ArgType)) |
| 5976 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5977 | |
| 5978 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5979 | Converted = |
| 5980 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 5981 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5982 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5985 | /// Checks whether the given template argument is a pointer to |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5986 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5987 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 5988 | NonTypeTemplateParmDecl *Param, |
| 5989 | QualType ParamType, |
| 5990 | Expr *&ResultArg, |
| 5991 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5992 | bool Invalid = false; |
| 5993 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5994 | Expr *Arg = ResultArg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5995 | bool ObjCLifetimeConversion; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5996 | |
| 5997 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5998 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5999 | // A template-argument for a non-type, non-template |
| 6000 | // template-parameter shall be one of: [...] |
| 6001 | // |
| 6002 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6003 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6004 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6005 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 6006 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 6007 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6008 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6009 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6010 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6011 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6012 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 6013 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6014 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 6015 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
| 6018 | Arg = Parens->getSubExpr(); |
| 6019 | } |
| 6020 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6021 | while (SubstNonTypeTemplateParmExpr *subst = |
| 6022 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 6023 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 6024 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6025 | // A pointer-to-member constant written &Class::member. |
| 6026 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6027 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6028 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 6029 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6030 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6031 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6032 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6033 | // A constant of pointer-to-member type. |
| 6034 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6035 | ValueDecl *VD = DRE->getDecl(); |
| 6036 | if (VD->getType()->isMemberPointerType()) { |
| 6037 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
| 6038 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6039 | Converted = TemplateArgument(Arg); |
| 6040 | } else { |
| 6041 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
| 6042 | Converted = TemplateArgument(VD, ParamType); |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6043 | } |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6044 | return Invalid; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6045 | } |
| 6046 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6047 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6048 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 6049 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6050 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 6051 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 6052 | |
| 6053 | // Check for a null pointer value. |
| 6054 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ResultArg, |
| 6055 | Entity)) { |
| 6056 | case NPV_Error: |
| 6057 | return true; |
| 6058 | case NPV_NullPointer: |
| 6059 | S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
| 6060 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 6061 | /*isNullPtr*/true); |
| 6062 | return false; |
| 6063 | case NPV_NotNullPointer: |
| 6064 | break; |
| 6065 | } |
| 6066 | |
| 6067 | if (S.IsQualificationConversion(ResultArg->getType(), |
| 6068 | ParamType.getNonReferenceType(), false, |
| 6069 | ObjCLifetimeConversion)) { |
| 6070 | ResultArg = S.ImpCastExprToType(ResultArg, ParamType, CK_NoOp, |
| 6071 | ResultArg->getValueKind()) |
| 6072 | .get(); |
| 6073 | } else if (!S.Context.hasSameUnqualifiedType( |
| 6074 | ResultArg->getType(), ParamType.getNonReferenceType())) { |
| 6075 | // We can't perform this conversion. |
| 6076 | S.Diag(ResultArg->getLocStart(), diag::err_template_arg_not_convertible) |
| 6077 | << ResultArg->getType() << ParamType << ResultArg->getSourceRange(); |
| 6078 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6079 | return true; |
| 6080 | } |
| 6081 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6082 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6083 | return S.Diag(Arg->getLocStart(), |
| 6084 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6085 | << Arg->getSourceRange(); |
| 6086 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6087 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 6088 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 6089 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6090 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 6091 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6092 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 6093 | "Only non-static member pointers can make it here"); |
| 6094 | |
| 6095 | // Okay: this is the address of a non-static member, and therefore |
| 6096 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6097 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 6098 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6099 | } else { |
| 6100 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 6101 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6102 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6103 | return Invalid; |
| 6104 | } |
| 6105 | |
| 6106 | // We found something else, but we don't know specifically what it is. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6107 | S.Diag(Arg->getLocStart(), |
| 6108 | diag::err_template_arg_not_pointer_to_member_form) |
| 6109 | << Arg->getSourceRange(); |
| 6110 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 6111 | return true; |
| 6112 | } |
| 6113 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6114 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6115 | /// non-type template parameter. |
| 6116 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 6117 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6118 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6119 | /// returns the converted template argument. \p ParamType is the |
| 6120 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6121 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6122 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6123 | TemplateArgument &Converted, |
| 6124 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6125 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6126 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6127 | // If the parameter type somehow involves auto, deduce the type now. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6128 | if (getLangOpts().CPlusPlus17 && ParamType->isUndeducedType()) { |
Richard Smith | 4ae5ec8 | 2017-02-22 20:01:55 +0000 | [diff] [blame] | 6129 | // During template argument deduction, we allow 'decltype(auto)' to |
| 6130 | // match an arbitrary dependent argument. |
| 6131 | // FIXME: The language rules don't say what happens in this case. |
| 6132 | // FIXME: We get an opaque dependent type out of decltype(auto) if the |
| 6133 | // expression is merely instantiation-dependent; is this enough? |
| 6134 | if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) { |
| 6135 | auto *AT = dyn_cast<AutoType>(ParamType); |
| 6136 | if (AT && AT->isDecltypeAuto()) { |
| 6137 | Converted = TemplateArgument(Arg); |
| 6138 | return Arg; |
| 6139 | } |
| 6140 | } |
| 6141 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6142 | // When checking a deduced template argument, deduce from its type even if |
| 6143 | // the type is dependent, in order to check the types of non-type template |
| 6144 | // arguments line up properly in partial ordering. |
| 6145 | Optional<unsigned> Depth; |
| 6146 | if (CTAK != CTAK_Specified) |
| 6147 | Depth = Param->getDepth() + 1; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6148 | if (DeduceAutoType( |
| 6149 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6150 | Arg, ParamType, Depth) == DAR_Failed) { |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 6151 | Diag(Arg->getExprLoc(), |
| 6152 | diag::err_non_type_template_parm_type_deduction_failure) |
| 6153 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 6154 | << Arg->getSourceRange(); |
| 6155 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6156 | return ExprError(); |
| 6157 | } |
| 6158 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 6159 | // an error. The error message normally references the parameter |
| 6160 | // declaration, but here we'll pass the argument location because that's |
| 6161 | // where the parameter type is deduced. |
| 6162 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 6163 | if (ParamType.isNull()) { |
| 6164 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6165 | return ExprError(); |
| 6166 | } |
| 6167 | } |
| 6168 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6169 | // We should have already dropped all cv-qualifiers by now. |
| 6170 | assert(!ParamType.hasQualifiers() && |
| 6171 | "non-type template parameter type cannot be qualified"); |
| 6172 | |
| 6173 | if (CTAK == CTAK_Deduced && |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 6174 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6175 | Arg->getType())) { |
Richard Smith | 957fbf1 | 2017-01-17 02:14:37 +0000 | [diff] [blame] | 6176 | // FIXME: If either type is dependent, we skip the check. This isn't |
| 6177 | // correct, since during deduction we're supposed to have replaced each |
| 6178 | // template parameter with some unique (non-dependent) placeholder. |
| 6179 | // FIXME: If the argument type contains 'auto', we carry on and fail the |
| 6180 | // type check in order to force specific types to be more specialized than |
| 6181 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to |
| 6182 | // work. |
| 6183 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
| 6184 | !Arg->getType()->getContainedAutoType()) { |
| 6185 | Converted = TemplateArgument(Arg); |
| 6186 | return Arg; |
| 6187 | } |
| 6188 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, |
| 6189 | // we should actually be checking the type of the template argument in P, |
| 6190 | // not the type of the template argument deduced from A, against the |
| 6191 | // template parameter type. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6192 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6193 | << Arg->getType() |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6194 | << ParamType.getUnqualifiedType(); |
| 6195 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6196 | return ExprError(); |
| 6197 | } |
| 6198 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 6199 | // If either the parameter has a dependent type or the argument is |
| 6200 | // type-dependent, there's nothing we can check now. |
| 6201 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 6202 | // FIXME: Produce a cloned, canonical expression? |
| 6203 | Converted = TemplateArgument(Arg); |
| 6204 | return Arg; |
| 6205 | } |
| 6206 | |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6207 | // The initialization of the parameter from the argument is |
| 6208 | // a constant-evaluated context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6209 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 6210 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 6211 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 6212 | if (getLangOpts().CPlusPlus17) { |
| 6213 | // C++17 [temp.arg.nontype]p1: |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6214 | // A template-argument for a non-type template parameter shall be |
| 6215 | // a converted constant expression of the type of the template-parameter. |
| 6216 | APValue Value; |
| 6217 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 6218 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 6219 | if (ArgResult.isInvalid()) |
| 6220 | return ExprError(); |
| 6221 | |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6222 | // For a value-dependent argument, CheckConvertedConstantExpression is |
| 6223 | // permitted (and expected) to be unable to determine a value. |
| 6224 | if (ArgResult.get()->isValueDependent()) { |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6225 | Converted = TemplateArgument(ArgResult.get()); |
| 6226 | return ArgResult; |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 6227 | } |
| 6228 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6229 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 6230 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6231 | // Convert the APValue to a TemplateArgument. |
| 6232 | switch (Value.getKind()) { |
| 6233 | case APValue::Uninitialized: |
| 6234 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6235 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6236 | break; |
| 6237 | case APValue::Int: |
| 6238 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6239 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6240 | break; |
| 6241 | case APValue::MemberPointer: { |
| 6242 | assert(ParamType->isMemberPointerType()); |
| 6243 | |
| 6244 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 6245 | if (!Value.getMemberPointerPath().empty()) { |
| 6246 | Diag(Arg->getLocStart(), |
| 6247 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 6248 | << Value.getMemberPointerDecl() << ParamType |
| 6249 | << Arg->getSourceRange(); |
| 6250 | return ExprError(); |
| 6251 | } |
| 6252 | |
| 6253 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6254 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6255 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6256 | break; |
| 6257 | } |
| 6258 | case APValue::LValue: { |
| 6259 | // For a non-type template-parameter of pointer or reference type, |
| 6260 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6261 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 6262 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6263 | // -- a temporary object |
| 6264 | // -- a string literal |
| 6265 | // -- the result of a typeid expression, or |
Eric Christopher | 0d2c56a | 2017-03-31 01:45:39 +0000 | [diff] [blame] | 6266 | // -- a predefined __func__ variable |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6267 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 6268 | if (isa<CXXUuidofExpr>(E)) { |
Nico Weber | d60bbce | 2018-05-17 15:26:37 +0000 | [diff] [blame] | 6269 | Converted = TemplateArgument(ArgResult.get()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6270 | break; |
| 6271 | } |
| 6272 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 6273 | << Arg->getSourceRange(); |
| 6274 | return ExprError(); |
| 6275 | } |
| 6276 | auto *VD = const_cast<ValueDecl *>( |
| 6277 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 6278 | // -- a subobject |
| 6279 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 6280 | VD && VD->getType()->isArrayType() && |
| 6281 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 6282 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 6283 | // Per defect report (no number yet): |
| 6284 | // ... other than a pointer to the first element of a complete array |
| 6285 | // object. |
| 6286 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 6287 | Value.isLValueOnePastTheEnd()) { |
| 6288 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 6289 | << Value.getAsString(Context, ParamType); |
| 6290 | return ExprError(); |
| 6291 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6292 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6293 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6294 | assert((!VD || !ParamType->isNullPtrType()) && |
| 6295 | "non-null value of type nullptr_t?"); |
| 6296 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6297 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 6298 | break; |
| 6299 | } |
| 6300 | case APValue::AddrLabelDiff: |
| 6301 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 6302 | case APValue::Float: |
| 6303 | case APValue::ComplexInt: |
| 6304 | case APValue::ComplexFloat: |
| 6305 | case APValue::Vector: |
| 6306 | case APValue::Array: |
| 6307 | case APValue::Struct: |
| 6308 | case APValue::Union: |
| 6309 | llvm_unreachable("invalid kind for template argument"); |
| 6310 | } |
| 6311 | |
| 6312 | return ArgResult.get(); |
| 6313 | } |
| 6314 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6315 | // C++ [temp.arg.nontype]p5: |
| 6316 | // The following conversions are performed on each expression used |
| 6317 | // as a non-type template-argument. If a non-type |
| 6318 | // template-argument cannot be converted to the type of the |
| 6319 | // corresponding template-parameter then the program is |
| 6320 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6321 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6322 | // C++11: |
| 6323 | // -- for a non-type template-parameter of integral or |
| 6324 | // enumeration type, conversions permitted in a converted |
| 6325 | // constant expression are applied. |
| 6326 | // |
| 6327 | // C++98: |
| 6328 | // -- for a non-type template-parameter of integral or |
| 6329 | // enumeration type, integral promotions (4.5) and integral |
| 6330 | // conversions (4.7) are applied. |
| 6331 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6332 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6333 | // C++ [temp.arg.nontype]p1: |
| 6334 | // A template-argument for a non-type, non-template template-parameter |
| 6335 | // shall be one of: |
| 6336 | // |
| 6337 | // -- for a non-type template-parameter of integral or enumeration |
| 6338 | // type, a converted constant expression of the type of the |
| 6339 | // template-parameter; or |
| 6340 | llvm::APSInt Value; |
| 6341 | ExprResult ArgResult = |
| 6342 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 6343 | CCEK_TemplateArg); |
| 6344 | if (ArgResult.isInvalid()) |
| 6345 | return ExprError(); |
| 6346 | |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 6347 | // We can't check arbitrary value-dependent arguments. |
| 6348 | if (ArgResult.get()->isValueDependent()) { |
| 6349 | Converted = TemplateArgument(ArgResult.get()); |
| 6350 | return ArgResult; |
| 6351 | } |
| 6352 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6353 | // Widen the argument value to sizeof(parameter type). This is almost |
| 6354 | // always a no-op, except when the parameter type is bool. In |
| 6355 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 6356 | QualType IntegerType = ParamType; |
| 6357 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 6358 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 6359 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 6360 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6361 | Converted = TemplateArgument(Context, Value, |
| 6362 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 6363 | return ArgResult; |
| 6364 | } |
| 6365 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6366 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 6367 | if (ArgResult.isInvalid()) |
| 6368 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6369 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6370 | |
| 6371 | QualType ArgType = Arg->getType(); |
| 6372 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6373 | // C++ [temp.arg.nontype]p1: |
| 6374 | // A template-argument for a non-type, non-template |
| 6375 | // template-parameter shall be one of: |
| 6376 | // |
| 6377 | // -- an integral constant-expression of integral or enumeration |
| 6378 | // type; or |
| 6379 | // -- the name of a non-type template-parameter; or |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6380 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6381 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6382 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6383 | diag::err_template_arg_not_integral_or_enumeral) |
| 6384 | << ArgType << Arg->getSourceRange(); |
| 6385 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6386 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6387 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6388 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 6389 | QualType T; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6390 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6391 | public: |
| 6392 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 6393 | |
| 6394 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 6395 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 6396 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 6397 | } |
| 6398 | } Diagnoser(ArgType); |
| 6399 | |
| 6400 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6401 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 6402 | if (!Arg) |
| 6403 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6406 | // From here on out, all we care about is the unqualified form |
| 6407 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6408 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6409 | |
| 6410 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 6411 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6412 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6413 | } else if (ParamType->isBooleanType()) { |
| 6414 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6415 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6416 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 6417 | !ParamType->isEnumeralType()) { |
| 6418 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6419 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6420 | } else { |
| 6421 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6422 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6423 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 6424 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6425 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6426 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6427 | } |
| 6428 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6429 | // Add the value of this argument to the list of converted |
| 6430 | // arguments. We use the bitwidth and signedness of the template |
| 6431 | // parameter. |
| 6432 | if (Arg->isValueDependent()) { |
| 6433 | // The argument is value-dependent. Create a new |
| 6434 | // TemplateArgument with the converted expression. |
| 6435 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6436 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6437 | } |
| 6438 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6439 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6440 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 6441 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6442 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6443 | if (ParamType->isBooleanType()) { |
| 6444 | // Value must be zero or one. |
| 6445 | Value = Value != 0; |
| 6446 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 6447 | if (Value.getBitWidth() != AllowedBits) |
| 6448 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6449 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6450 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6451 | llvm::APSInt OldValue = Value; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6452 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6453 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6454 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6455 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6456 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6457 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6458 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6459 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6460 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6461 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6462 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6463 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6464 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6465 | << Arg->getSourceRange(); |
| 6466 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6467 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6468 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6469 | // Complain if we overflowed the template parameter's type. |
| 6470 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6471 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6472 | RequiredBits = OldValue.getActiveBits(); |
| 6473 | else if (OldValue.isUnsigned()) |
| 6474 | RequiredBits = OldValue.getActiveBits() + 1; |
| 6475 | else |
| 6476 | RequiredBits = OldValue.getMinSignedBits(); |
| 6477 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6478 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6479 | diag::warn_template_arg_too_large) |
| 6480 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6481 | << Arg->getSourceRange(); |
| 6482 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6483 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6484 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6485 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6486 | Converted = TemplateArgument(Context, Value, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6487 | ParamType->isEnumeralType() |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 6488 | ? Context.getCanonicalType(ParamType) |
| 6489 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6490 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6491 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6492 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6493 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6494 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 6495 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6496 | // Handle pointer-to-function, reference-to-function, and |
| 6497 | // pointer-to-member-function all in (roughly) the same way. |
| 6498 | if (// -- For a non-type template-parameter of type pointer to |
| 6499 | // function, only the function-to-pointer conversion (4.3) is |
| 6500 | // applied. If the template-argument represents a set of |
| 6501 | // overloaded functions (or a pointer to such), the matching |
| 6502 | // function is selected from the set (13.4). |
| 6503 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6504 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6505 | // -- For a non-type template-parameter of type reference to |
| 6506 | // function, no conversions apply. If the template-argument |
| 6507 | // represents a set of overloaded functions, the matching |
| 6508 | // function is selected from the set (13.4). |
| 6509 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6510 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6511 | // -- For a non-type template-parameter of type pointer to |
| 6512 | // member function, no conversions apply. If the |
| 6513 | // template-argument represents a set of overloaded member |
| 6514 | // functions, the matching member function is selected from |
| 6515 | // the set (13.4). |
| 6516 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6517 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6518 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6519 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6520 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6521 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6522 | true, |
| 6523 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6524 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6525 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6526 | |
| 6527 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6528 | ArgType = Arg->getType(); |
| 6529 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6530 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6531 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6532 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6533 | if (!ParamType->isMemberPointerType()) { |
| 6534 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6535 | ParamType, |
| 6536 | Arg, Converted)) |
| 6537 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6538 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6539 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6540 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6541 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6542 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6543 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6544 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6545 | } |
| 6546 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 6547 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6548 | // -- for a non-type template-parameter of type pointer to |
| 6549 | // object, qualification conversions (4.4) and the |
| 6550 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6551 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6552 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6553 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6554 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6555 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6556 | ParamType, |
| 6557 | Arg, Converted)) |
| 6558 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6559 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6560 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6561 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6562 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6563 | // -- For a non-type template-parameter of type reference to |
| 6564 | // object, no conversions apply. The type referred to by the |
| 6565 | // reference may be more cv-qualified than the (otherwise |
| 6566 | // identical) type of the template-argument. The |
| 6567 | // template-parameter is bound directly to the |
| 6568 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6569 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6570 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6571 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6572 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6573 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 6574 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6575 | true, |
| 6576 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6577 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6578 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6579 | |
| 6580 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6581 | ArgType = Arg->getType(); |
| 6582 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6583 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6584 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6585 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6586 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6587 | ParamType, |
| 6588 | Arg, Converted)) |
| 6589 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6590 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6591 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6592 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6593 | // Deal with parameters of type std::nullptr_t. |
| 6594 | if (ParamType->isNullPtrType()) { |
| 6595 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6596 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6597 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6598 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6599 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6600 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 6601 | case NPV_NotNullPointer: |
| 6602 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 6603 | << Arg->getType() << ParamType; |
| 6604 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6605 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6606 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6607 | case NPV_Error: |
| 6608 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6609 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6610 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 6611 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 6612 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 6613 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6614 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6615 | } |
| 6616 | } |
| 6617 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6618 | // -- For a non-type template-parameter of type pointer to data |
| 6619 | // member, qualification conversions (4.4) are applied. |
| 6620 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 6621 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6622 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6623 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6624 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6625 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6628 | static void DiagnoseTemplateParameterListArityMismatch( |
| 6629 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 6630 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 6631 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6632 | /// Check a template argument against its corresponding |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6633 | /// template template parameter. |
| 6634 | /// |
| 6635 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 6636 | /// It returns true if an error occurred, and false otherwise. |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 6637 | bool Sema::CheckTemplateTemplateArgument(TemplateParameterList *Params, |
| 6638 | TemplateArgumentLoc &Arg) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6639 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6640 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 6641 | if (!Template) { |
| 6642 | // Any dependent template name is fine. |
| 6643 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 6644 | return false; |
| 6645 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6646 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6647 | if (Template->isInvalidDecl()) |
| 6648 | return true; |
| 6649 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6650 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6651 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6652 | // the name of a class template or an alias template, expressed as an |
| 6653 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6654 | // primary class templates are considered when matching the |
| 6655 | // template template argument with the corresponding parameter; |
| 6656 | // partial specializations are not considered even if their |
| 6657 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6658 | // |
| 6659 | // Note that we also allow template template parameters here, which |
| 6660 | // will happen when we are dealing with, e.g., class template |
| 6661 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6662 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6663 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6664 | !isa<TypeAliasTemplateDecl>(Template) && |
| 6665 | !isa<BuiltinTemplateDecl>(Template)) { |
| 6666 | assert(isa<FunctionTemplateDecl>(Template) && |
| 6667 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 6668 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6669 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 6670 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6671 | } |
| 6672 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6673 | // C++1z [temp.arg.template]p3: (DR 150) |
| 6674 | // A template-argument matches a template template-parameter P when P |
| 6675 | // is at least as specialized as the template-argument A. |
| 6676 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 6677 | // Quick check for the common case: |
| 6678 | // If P contains a parameter pack, then A [...] matches P if each of A's |
| 6679 | // template parameters matches the corresponding template parameter in |
| 6680 | // the template-parameter-list of P. |
| 6681 | if (TemplateParameterListsAreEqual( |
| 6682 | Template->getTemplateParameters(), Params, false, |
| 6683 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 6684 | return false; |
| 6685 | |
| 6686 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 6687 | Arg.getLocation())) |
| 6688 | return false; |
| 6689 | // FIXME: Produce better diagnostics for deduction failures. |
| 6690 | } |
| 6691 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6692 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 6693 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6694 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6695 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6696 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6697 | } |
| 6698 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6699 | /// Given a non-type template argument that refers to a |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6700 | /// declaration and the type of its corresponding non-type template |
| 6701 | /// parameter, produce an expression that properly refers to that |
| 6702 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6703 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6704 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 6705 | QualType ParamType, |
| 6706 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 6707 | // C++ [temp.param]p8: |
| 6708 | // |
| 6709 | // A non-type template-parameter of type "array of T" or |
| 6710 | // "function returning T" is adjusted to be of type "pointer to |
| 6711 | // T" or "pointer to function returning T", respectively. |
| 6712 | if (ParamType->isArrayType()) |
| 6713 | ParamType = Context.getArrayDecayedType(ParamType); |
| 6714 | else if (ParamType->isFunctionType()) |
| 6715 | ParamType = Context.getPointerType(ParamType); |
| 6716 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6717 | // For a NULL non-type template argument, return nullptr casted to the |
| 6718 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6719 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6720 | return ImpCastExprToType( |
| 6721 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 6722 | ParamType, |
| 6723 | ParamType->getAs<MemberPointerType>() |
| 6724 | ? CK_NullToMemberPointer |
| 6725 | : CK_NullToPointer); |
| 6726 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6727 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 6728 | "Only declaration template arguments permitted here"); |
| 6729 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6730 | ValueDecl *VD = Arg.getAsDecl(); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6731 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6732 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 6733 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 6734 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6735 | // If the value is a class member, we might have a pointer-to-member. |
| 6736 | // Determine whether the non-type template template parameter is of |
| 6737 | // pointer-to-member type. If so, we need to build an appropriate |
| 6738 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 6739 | // would refer to the member itself. |
| 6740 | if (ParamType->isMemberPointerType()) { |
| 6741 | QualType ClassType |
| 6742 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 6743 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6744 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6745 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6746 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 6747 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6748 | |
| 6749 | // The actual value-ness of this is unimportant, but for |
| 6750 | // internal consistency's sake, references to instance methods |
| 6751 | // are r-values. |
| 6752 | ExprValueKind VK = VK_LValue; |
| 6753 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 6754 | VK = VK_RValue; |
| 6755 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6756 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6757 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6758 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6759 | Loc, |
| 6760 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6761 | if (RefExpr.isInvalid()) |
| 6762 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6763 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6764 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6765 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6766 | // We might need to perform a trailing qualification conversion, since |
| 6767 | // the element type on the parameter could be more qualified than the |
| 6768 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6769 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6770 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6771 | ParamType.getUnqualifiedType(), false, |
| 6772 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6773 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6774 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6775 | assert(!RefExpr.isInvalid() && |
| 6776 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6777 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6778 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6779 | } |
| 6780 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6781 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6782 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6783 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6784 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6785 | // When the non-type template parameter is a pointer, take the |
| 6786 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6787 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6788 | if (RefExpr.isInvalid()) |
| 6789 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6790 | |
Richard Smith | fc6fca1 | 2017-01-28 00:38:35 +0000 | [diff] [blame] | 6791 | if (!Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
| 6792 | (T->isFunctionType() || T->isArrayType())) { |
| 6793 | // Decay functions and arrays unless we're forming a pointer to array. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6794 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6795 | if (RefExpr.isInvalid()) |
| 6796 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6797 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6798 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6799 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6800 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6801 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6802 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6803 | } |
| 6804 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6805 | ExprValueKind VK = VK_RValue; |
| 6806 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6807 | // If the non-type template parameter has reference type, qualify the |
| 6808 | // resulting declaration reference with the extra qualifiers on the |
| 6809 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6810 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 6811 | VK = VK_LValue; |
| 6812 | T = Context.getQualifiedType(T, |
| 6813 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6814 | } else if (isa<FunctionDecl>(VD)) { |
| 6815 | // References to functions are always lvalues. |
| 6816 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6817 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6818 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6819 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6822 | /// Construct a new expression that refers to the given |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6823 | /// integral template argument with the given source-location |
| 6824 | /// information. |
| 6825 | /// |
| 6826 | /// This routine takes care of the mapping from an integral template |
| 6827 | /// argument (which may have any integral type) to the appropriate |
| 6828 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6829 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6830 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 6831 | SourceLocation Loc) { |
| 6832 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 6833 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6834 | QualType OrigT = Arg.getIntegralType(); |
| 6835 | |
| 6836 | // If this is an enum type that we're instantiating, we need to use an integer |
| 6837 | // type the same size as the enumerator. We don't want to build an |
| 6838 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 6839 | // any integral type with C++11 enum classes, make sure we create the right |
| 6840 | // type of literal for it. |
| 6841 | QualType T = OrigT; |
| 6842 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 6843 | T = ET->getDecl()->getIntegerType(); |
| 6844 | |
| 6845 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6846 | if (T->isAnyCharacterType()) { |
| 6847 | CharacterLiteral::CharacterKind Kind; |
| 6848 | if (T->isWideCharType()) |
| 6849 | Kind = CharacterLiteral::Wide; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 6850 | else if (T->isChar8Type() && getLangOpts().Char8) |
| 6851 | Kind = CharacterLiteral::UTF8; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6852 | else if (T->isChar16Type()) |
| 6853 | Kind = CharacterLiteral::UTF16; |
| 6854 | else if (T->isChar32Type()) |
| 6855 | Kind = CharacterLiteral::UTF32; |
| 6856 | else |
| 6857 | Kind = CharacterLiteral::Ascii; |
| 6858 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6859 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 6860 | Kind, T, Loc); |
| 6861 | } else if (T->isBooleanType()) { |
| 6862 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 6863 | T, Loc); |
| 6864 | } else if (T->isNullPtrType()) { |
| 6865 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 6866 | } else { |
| 6867 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6868 | } |
| 6869 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6870 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6871 | // FIXME: This is a hack. We need a better way to handle substituted |
| 6872 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6873 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 6874 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6875 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6876 | Loc, Loc); |
| 6877 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6878 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6879 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6880 | } |
| 6881 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6882 | /// Match two template parameters within template parameter lists. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6883 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 6884 | bool Complain, |
| 6885 | Sema::TemplateParameterListEqualKind Kind, |
| 6886 | SourceLocation TemplateArgLoc) { |
| 6887 | // Check the actual kind (type, non-type, template). |
| 6888 | if (Old->getKind() != New->getKind()) { |
| 6889 | if (Complain) { |
| 6890 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 6891 | if (TemplateArgLoc.isValid()) { |
| 6892 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6893 | NextDiag = diag::note_template_param_different_kind; |
| 6894 | } |
| 6895 | S.Diag(New->getLocation(), NextDiag) |
| 6896 | << (Kind != Sema::TPL_TemplateMatch); |
| 6897 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 6898 | << (Kind != Sema::TPL_TemplateMatch); |
| 6899 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6900 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6901 | return false; |
| 6902 | } |
| 6903 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6904 | // Check that both are parameter packs or neither are parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6905 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6906 | // template template parameter, the template template parameter can have |
| 6907 | // a parameter pack where the template template argument does not. |
| 6908 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 6909 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 6910 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6911 | if (Complain) { |
| 6912 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 6913 | if (TemplateArgLoc.isValid()) { |
| 6914 | S.Diag(TemplateArgLoc, |
| 6915 | diag::err_template_arg_template_params_mismatch); |
| 6916 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 6917 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6918 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6919 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 6920 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 6921 | : 2; |
| 6922 | S.Diag(New->getLocation(), NextDiag) |
| 6923 | << ParamKind << New->isParameterPack(); |
| 6924 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 6925 | << ParamKind << Old->isParameterPack(); |
| 6926 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6927 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6928 | return false; |
| 6929 | } |
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 | // For non-type template parameters, check the type of the parameter. |
| 6932 | if (NonTypeTemplateParmDecl *OldNTTP |
| 6933 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 6934 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6935 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6936 | // If we are matching a template template argument to a template |
| 6937 | // template parameter and one of the non-type template parameter types |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 6938 | // is dependent, then we must wait until template instantiation time |
| 6939 | // to actually compare the arguments. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6940 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
Richard Smith | 1389418 | 2017-04-13 21:37:24 +0000 | [diff] [blame] | 6941 | (OldNTTP->getType()->isDependentType() || |
| 6942 | NewNTTP->getType()->isDependentType())) |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6943 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6944 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6945 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 6946 | if (Complain) { |
| 6947 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 6948 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6949 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6950 | diag::err_template_arg_template_params_mismatch); |
| 6951 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 6952 | } |
| 6953 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 6954 | << NewNTTP->getType() |
| 6955 | << (Kind != Sema::TPL_TemplateMatch); |
| 6956 | S.Diag(OldNTTP->getLocation(), |
| 6957 | diag::note_template_nontype_parm_prev_declaration) |
| 6958 | << OldNTTP->getType(); |
| 6959 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6960 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6961 | return false; |
| 6962 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6963 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6964 | return true; |
| 6965 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6966 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6967 | // For template template parameters, check the template parameter types. |
| 6968 | // The template parameter lists of template template |
| 6969 | // parameters must agree. |
| 6970 | if (TemplateTemplateParmDecl *OldTTP |
| 6971 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6972 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6973 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 6974 | OldTTP->getTemplateParameters(), |
| 6975 | Complain, |
| 6976 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6977 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6978 | : Kind), |
| 6979 | TemplateArgLoc); |
| 6980 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6981 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6982 | return true; |
| 6983 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6984 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6985 | /// Diagnose a known arity mismatch when comparing template argument |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6986 | /// lists. |
| 6987 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6988 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6989 | TemplateParameterList *New, |
| 6990 | TemplateParameterList *Old, |
| 6991 | Sema::TemplateParameterListEqualKind Kind, |
| 6992 | SourceLocation TemplateArgLoc) { |
| 6993 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 6994 | if (TemplateArgLoc.isValid()) { |
| 6995 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6996 | NextDiag = diag::note_template_param_list_different_arity; |
| 6997 | } |
| 6998 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 6999 | << (New->size() > Old->size()) |
| 7000 | << (Kind != Sema::TPL_TemplateMatch) |
| 7001 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 7002 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 7003 | << (Kind != Sema::TPL_TemplateMatch) |
| 7004 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 7005 | } |
| 7006 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7007 | /// Determine whether the given template parameter lists are |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7008 | /// equivalent. |
| 7009 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7010 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7011 | /// source code as part of a new template declaration. |
| 7012 | /// |
| 7013 | /// \param Old The old template parameter list, typically found via |
| 7014 | /// name lookup of the template declared with this template parameter |
| 7015 | /// list. |
| 7016 | /// |
| 7017 | /// \param Complain If true, this routine will produce a diagnostic if |
| 7018 | /// the template parameter lists are not equivalent. |
| 7019 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7020 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7021 | /// |
| 7022 | /// \param TemplateArgLoc If this source location is valid, then we |
| 7023 | /// are actually checking the template parameter list of a template |
| 7024 | /// argument (New) against the template parameter list of its |
| 7025 | /// corresponding template template parameter (Old). We produce |
| 7026 | /// slightly different diagnostics in this scenario. |
| 7027 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7028 | /// \returns True if the template parameter lists are equal, false |
| 7029 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7030 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7031 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 7032 | TemplateParameterList *Old, |
| 7033 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 7034 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 7035 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7036 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 7037 | if (Complain) |
| 7038 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7039 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7040 | |
| 7041 | return false; |
| 7042 | } |
| 7043 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 7044 | // C++0x [temp.arg.template]p3: |
| 7045 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7046 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7047 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7048 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7049 | // template-parameter-list of P. [...] |
| 7050 | TemplateParameterList::iterator NewParm = New->begin(); |
| 7051 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7052 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7053 | OldParmEnd = Old->end(); |
| 7054 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 7055 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 7056 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7057 | if (NewParm == NewParmEnd) { |
| 7058 | if (Complain) |
| 7059 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7060 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7061 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7062 | return false; |
| 7063 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7064 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7065 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7066 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7067 | return false; |
| 7068 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7069 | ++NewParm; |
| 7070 | continue; |
| 7071 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7072 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7073 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 7074 | // [...] When P's template- parameter-list contains a template parameter |
| 7075 | // pack (14.5.3), the template parameter pack will match zero or more |
| 7076 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7077 | // template-parameter-list of A with the same type and form as the |
| 7078 | // template parameter pack in P (ignoring whether those template |
| 7079 | // parameters are template parameter packs). |
| 7080 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 7081 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7082 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7083 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7084 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7085 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7086 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7087 | // Make sure we exhausted all of the arguments. |
| 7088 | if (NewParm != NewParmEnd) { |
| 7089 | if (Complain) |
| 7090 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7091 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7092 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 7093 | return false; |
| 7094 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7095 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7096 | return true; |
| 7097 | } |
| 7098 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7099 | /// Check whether a template can be declared within this scope. |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7100 | /// |
| 7101 | /// If the template declaration is valid in this scope, returns |
| 7102 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7103 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7104 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 7105 | if (!S) |
| 7106 | return false; |
| 7107 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7108 | // Find the nearest enclosing declaration scope. |
| 7109 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7110 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7111 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7112 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7113 | // C++ [temp]p4: |
| 7114 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7115 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 7116 | if (Ctx && Ctx->isExternCContext()) { |
| 7117 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 7118 | << TemplateParams->getSourceRange(); |
| 7119 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 7120 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 7121 | return true; |
| 7122 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 7123 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7124 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 7125 | // C++ [temp]p2: |
| 7126 | // A template-declaration can appear only as a namespace scope or |
| 7127 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 7128 | if (Ctx) { |
| 7129 | if (Ctx->isFileContext()) |
| 7130 | return false; |
| 7131 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 7132 | // C++ [temp.mem]p2: |
| 7133 | // A local class shall not have member templates. |
| 7134 | if (RD->isLocalClass()) |
| 7135 | return Diag(TemplateParams->getTemplateLoc(), |
| 7136 | diag::err_template_inside_local_class) |
| 7137 | << TemplateParams->getSourceRange(); |
| 7138 | else |
| 7139 | return false; |
| 7140 | } |
| 7141 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7142 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7143 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7144 | diag::err_template_outside_namespace_or_class_scope) |
| 7145 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 7146 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7147 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7148 | /// Determine what kind of template specialization the given declaration |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7149 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7150 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7151 | if (!D) |
| 7152 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7153 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7154 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 7155 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7156 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 7157 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7158 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 7159 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7160 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7161 | return TSK_Undeclared; |
| 7162 | } |
| 7163 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7164 | /// Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7165 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7166 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7167 | /// This routine determines whether a template specialization can be declared |
| 7168 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7169 | /// |
| 7170 | /// \param S the semantic analysis object for which this check is being |
| 7171 | /// performed. |
| 7172 | /// |
| 7173 | /// \param Specialized the entity being specialized or instantiated, which |
| 7174 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7175 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7176 | /// member class). |
| 7177 | /// |
| 7178 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 7179 | /// |
| 7180 | /// \param Loc the location of the explicit specialization or instantiation of |
| 7181 | /// this entity. |
| 7182 | /// |
| 7183 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 7184 | /// a class template. |
| 7185 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7186 | /// \returns true if there was an error that we cannot recover from, false |
| 7187 | /// otherwise. |
| 7188 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 7189 | NamedDecl *Specialized, |
| 7190 | NamedDecl *PrevDecl, |
| 7191 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7192 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7193 | // Keep these "kind" numbers in sync with the %select statements in the |
| 7194 | // various diagnostics emitted by this routine. |
| 7195 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7196 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7197 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7198 | else if (isa<VarTemplateDecl>(Specialized)) |
| 7199 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 7200 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7201 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7202 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7203 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7204 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7205 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7206 | else if (isa<RecordDecl>(Specialized)) |
| 7207 | EntityKind = 7; |
| 7208 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 7209 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7210 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7211 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7212 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7213 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7214 | return true; |
| 7215 | } |
| 7216 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7217 | // C++ [temp.expl.spec]p2: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7218 | // An explicit specialization may be declared in any scope in which |
| 7219 | // the corresponding primary template may be defined. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7220 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7221 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7222 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7223 | return true; |
| 7224 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 7225 | |
| 7226 | // C++ [temp.class.spec]p6: |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7227 | // A class template partial specialization may be declared in any |
| 7228 | // scope in which the primary template may be defined. |
| 7229 | DeclContext *SpecializedContext = |
| 7230 | Specialized->getDeclContext()->getRedeclContext(); |
| 7231 | DeclContext *DC = S.CurContext->getRedeclContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7232 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7233 | // Make sure that this redeclaration (or definition) occurs in the same |
| 7234 | // scope or an enclosing namespace. |
| 7235 | if (!(DC->isFileContext() ? DC->Encloses(SpecializedContext) |
| 7236 | : DC->Equals(SpecializedContext))) { |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7237 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 7238 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 7239 | << EntityKind << Specialized; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7240 | else { |
| 7241 | auto *ND = cast<NamedDecl>(SpecializedContext); |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7242 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7243 | if (S.getLangOpts().MicrosoftExt && !DC->isRecord()) |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 7244 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 7245 | S.Diag(Loc, Diag) << EntityKind << Specialized |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7246 | << ND << isa<CXXRecordDecl>(ND); |
| 7247 | } |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 7248 | |
| 7249 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7250 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 7251 | // Don't allow specializing in the wrong class during error recovery. |
| 7252 | // Otherwise, things can go horribly wrong. |
| 7253 | if (DC->isRecord()) |
| 7254 | return true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7255 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7256 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7257 | return false; |
| 7258 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7259 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7260 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 7261 | if (!E->isTypeDependent()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7262 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7263 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7264 | Checker.TraverseStmt(E); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7265 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7266 | return E->getSourceRange(); |
| 7267 | return Checker.MatchLoc; |
| 7268 | } |
| 7269 | |
| 7270 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 7271 | if (!TL.getType()->isDependentType()) |
| 7272 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7273 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7274 | Checker.TraverseTypeLoc(TL); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7275 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7276 | return TL.getSourceRange(); |
| 7277 | return Checker.MatchLoc; |
| 7278 | } |
| 7279 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7280 | /// Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7281 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7282 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7283 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 7284 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7285 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 7286 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7287 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7288 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 7289 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7290 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7291 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7292 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7293 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7294 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7295 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7296 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 7297 | |
| 7298 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7299 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 7300 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7301 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 7302 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7303 | |
| 7304 | // Strip off any implicit casts we added as part of type checking. |
| 7305 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 7306 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7307 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7308 | // C++ [temp.class.spec]p8: |
| 7309 | // A non-type argument is non-specialized if it is the name of a |
| 7310 | // non-type parameter. All other non-type arguments are |
| 7311 | // specialized. |
| 7312 | // |
| 7313 | // Below, we check the two conditions that only apply to |
| 7314 | // specialized non-type arguments, so skip any non-specialized |
| 7315 | // arguments. |
| 7316 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 7317 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7318 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7319 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7320 | // C++ [temp.class.spec]p9: |
| 7321 | // Within the argument list of a class template partial |
| 7322 | // specialization, the following restrictions apply: |
| 7323 | // -- A partially specialized non-type argument expression |
| 7324 | // shall not involve a template parameter of the partial |
| 7325 | // specialization except when the argument expression is a |
| 7326 | // simple identifier. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7327 | // -- The type of a template parameter corresponding to a |
| 7328 | // specialized non-type argument shall not be dependent on a |
| 7329 | // parameter of the specialization. |
| 7330 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
| 7331 | // We implement a compromise between the original rules and DR1315: |
| 7332 | // -- A specialized non-type template argument shall not be |
| 7333 | // type-dependent and the corresponding template parameter |
| 7334 | // shall have a non-dependent type. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7335 | SourceRange ParamUseRange = |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7336 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7337 | if (ParamUseRange.isValid()) { |
| 7338 | if (IsDefaultArgument) { |
| 7339 | S.Diag(TemplateNameLoc, |
| 7340 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 7341 | S.Diag(ParamUseRange.getBegin(), |
| 7342 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 7343 | << ParamUseRange; |
| 7344 | } else { |
| 7345 | S.Diag(ParamUseRange.getBegin(), |
| 7346 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 7347 | << ParamUseRange; |
| 7348 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7349 | return true; |
| 7350 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7351 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7352 | ParamUseRange = findTemplateParameter( |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7353 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7354 | if (ParamUseRange.isValid()) { |
| 7355 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 7356 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7357 | << Param->getType(); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7358 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7359 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 7360 | << ParamUseRange; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7361 | return true; |
| 7362 | } |
| 7363 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7364 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7365 | return false; |
| 7366 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7367 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7368 | /// Check the non-type template arguments of a class template |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7369 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 7370 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7371 | /// \param TemplateNameLoc the location of the template name. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7372 | /// \param PrimaryTemplate the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7373 | /// template. |
| 7374 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7375 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7376 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7377 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7378 | /// \returns \c true if there was an error, \c false otherwise. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7379 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 7380 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 7381 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 7382 | // We have to be conservative when checking a template in a dependent |
| 7383 | // context. |
| 7384 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 7385 | return false; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7386 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7387 | TemplateParameterList *TemplateParams = |
| 7388 | PrimaryTemplate->getTemplateParameters(); |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7389 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7390 | NonTypeTemplateParmDecl *Param |
| 7391 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 7392 | if (!Param) |
| 7393 | continue; |
| 7394 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7395 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 7396 | Param, &TemplateArgs[I], |
| 7397 | 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7398 | return true; |
| 7399 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7400 | |
| 7401 | return false; |
| 7402 | } |
| 7403 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7404 | DeclResult |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 7405 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7406 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7407 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 7408 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7409 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7410 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7411 | MultiTemplateParamsArg |
| 7412 | TemplateParameterLists, |
| 7413 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7414 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 7415 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7416 | CXXScopeSpec &SS = TemplateId.SS; |
| 7417 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7418 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 7419 | // store the location of the outermost template keyword in the declaration. |
| 7420 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7421 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 7422 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 7423 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 7424 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7425 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7426 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7427 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7428 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7429 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 7430 | |
| 7431 | if (!ClassTemplate) { |
| 7432 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7433 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7434 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 7435 | return true; |
| 7436 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7437 | |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7438 | bool isMemberSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7439 | bool isPartialSpecialization = false; |
| 7440 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7441 | // Check the validity of the template headers that introduce this |
| 7442 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7443 | // FIXME: We probably shouldn't complain about these headers for |
| 7444 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7445 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 7446 | TemplateParameterList *TemplateParams = |
| 7447 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7448 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7449 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7450 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7451 | if (Invalid) |
| 7452 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7453 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7454 | if (TemplateParams && TemplateParams->size() > 0) { |
| 7455 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7456 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 7457 | if (TUK == TUK_Friend) { |
| 7458 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 7459 | << SourceRange(LAngleLoc, RAngleLoc); |
| 7460 | return true; |
| 7461 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7462 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7463 | // C++ [temp.class.spec]p10: |
| 7464 | // The template parameter list of a specialization shall not |
| 7465 | // contain default template argument values. |
| 7466 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7467 | Decl *Param = TemplateParams->getParam(I); |
| 7468 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 7469 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7470 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7471 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 7472 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7473 | } |
| 7474 | } else if (NonTypeTemplateParmDecl *NTTP |
| 7475 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 7476 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7477 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7478 | diag::err_default_arg_in_partial_spec) |
| 7479 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7480 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7481 | } |
| 7482 | } else { |
| 7483 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7484 | if (TTP->hasDefaultArgument()) { |
| 7485 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7486 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7487 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7488 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7489 | } |
| 7490 | } |
| 7491 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7492 | } else if (TemplateParams) { |
| 7493 | if (TUK == TUK_Friend) |
| 7494 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7495 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7496 | SourceRange(TemplateParams->getTemplateLoc(), |
| 7497 | TemplateParams->getRAngleLoc())) |
| 7498 | << SourceRange(LAngleLoc, RAngleLoc); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7499 | } else { |
| 7500 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7501 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7502 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7503 | // Check that the specialization uses the same tag kind as the |
| 7504 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7505 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7506 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7507 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7508 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7509 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7510 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7511 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7512 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7513 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7514 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7515 | diag::note_previous_use); |
| 7516 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7517 | } |
| 7518 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7519 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7520 | TemplateArgumentListInfo TemplateArgs = |
| 7521 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7522 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7523 | // Check for unexpanded parameter packs in any of the template arguments. |
| 7524 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7525 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7526 | UPPC_PartialSpecialization)) |
| 7527 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7528 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7529 | // Check that the template argument list is well-formed for this |
| 7530 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7531 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7532 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7533 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7534 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7535 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7536 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7537 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7538 | if (isPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7539 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 7540 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7541 | return true; |
| 7542 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7543 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
| 7544 | // also do it during instantiation. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 7545 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7546 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7547 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7548 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7549 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 7550 | << ClassTemplate->getDeclName(); |
| 7551 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7552 | } |
| 7553 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7554 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7555 | void *InsertPos = nullptr; |
| 7556 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7557 | |
| 7558 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7559 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7560 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7561 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7562 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7563 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7564 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7565 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7566 | // Check whether we can declare a class template specialization in |
| 7567 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7568 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7569 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 7570 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7571 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7572 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7573 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7574 | // The canonical type |
| 7575 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 7576 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7577 | // Build the canonical type that describes the converted template |
| 7578 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7579 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7580 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7581 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7582 | |
| 7583 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7584 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 7585 | // C++ [temp.class.spec]p9b3: |
| 7586 | // |
| 7587 | // -- The argument list of the specialization shall not be identical |
| 7588 | // to the implicit argument list of the primary template. |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 7589 | // |
| 7590 | // This rule has since been removed, because it's redundant given DR1495, |
| 7591 | // but we keep it because it produces better diagnostics and recovery. |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7592 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 7593 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 7594 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7595 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 7596 | ClassTemplate->getIdentifier(), |
| 7597 | TemplateNameLoc, |
| 7598 | Attr, |
| 7599 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7600 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 7601 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7602 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7603 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7604 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7605 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7606 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7607 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 7608 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7609 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7610 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7611 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7612 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 7613 | TemplateParams, |
| 7614 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7615 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7616 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7617 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 7618 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7619 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7620 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7621 | Partial->setTemplateParameterListsInfo( |
| 7622 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7623 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7624 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7625 | if (!PrevPartial) |
| 7626 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7627 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 7628 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7629 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 7630 | // template specialization, make a note of that. |
| 7631 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 7632 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7633 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7634 | CheckTemplatePartialSpecialization(Partial); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7635 | } else { |
| 7636 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7637 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7638 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7639 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7640 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7641 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7642 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7643 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7644 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7645 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7646 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 7647 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7648 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7649 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7650 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7651 | if (!PrevDecl) |
| 7652 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7653 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7654 | if (CurContext->isDependentContext()) { |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7655 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7656 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7657 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7658 | } else { |
| 7659 | CanonType = Context.getTypeDeclType(Specialization); |
| 7660 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7661 | } |
| 7662 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7663 | // C++ [temp.expl.spec]p6: |
| 7664 | // 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] | 7665 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7666 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7667 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7668 | // use occurs; no diagnostic is required. |
| 7669 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7670 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7671 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7672 | // Is there any previous explicit specialization declaration? |
| 7673 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7674 | Okay = true; |
| 7675 | break; |
| 7676 | } |
| 7677 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7678 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7679 | if (!Okay) { |
| 7680 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 7681 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 7682 | << Context.getTypeDeclType(Specialization) << Range; |
| 7683 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7684 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7685 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7686 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7687 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7688 | return true; |
| 7689 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7690 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7691 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7692 | // If this is not a friend, note that this is an explicit specialization. |
| 7693 | if (TUK != TUK_Friend) |
| 7694 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7695 | |
| 7696 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7697 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7698 | RecordDecl *Def = Specialization->getDefinition(); |
| 7699 | NamedDecl *Hidden = nullptr; |
| 7700 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 7701 | SkipBody->ShouldSkip = true; |
Richard Smith | 858e0e0 | 2017-05-11 23:11:16 +0000 | [diff] [blame] | 7702 | makeMergedDefinitionVisible(Hidden); |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7703 | // From here on out, treat this as just a redeclaration. |
| 7704 | TUK = TUK_Declaration; |
| 7705 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7706 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 7707 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7708 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 7709 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7710 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7711 | } |
| 7712 | } |
| 7713 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 7714 | if (Attr) |
| 7715 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7716 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 7717 | // Add alignment attributes if necessary; these attributes are checked when |
| 7718 | // the ASTContext lays out the structure. |
| 7719 | if (TUK == TUK_Definition) { |
| 7720 | AddAlignmentAttributesForRecord(Specialization); |
| 7721 | AddMsStructLayoutForRecord(Specialization); |
| 7722 | } |
| 7723 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 7724 | if (ModulePrivateLoc.isValid()) |
| 7725 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 7726 | << (isPartialSpecialization? 1 : 0) |
| 7727 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7728 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 7729 | // Build the fully-sugared type for this class template |
| 7730 | // specialization as the user wrote in the specialization |
| 7731 | // itself. This means that we'll pretty-print the type retrieved |
| 7732 | // from the specialization's declaration the way that the user |
| 7733 | // actually wrote the specialization, rather than formatting the |
| 7734 | // name based on the "canonical" representation used to store the |
| 7735 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7736 | TypeSourceInfo *WrittenTy |
| 7737 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7738 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7739 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7740 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7741 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7742 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7743 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 7744 | // C++ [temp.expl.spec]p9: |
| 7745 | // A template explicit specialization is in the scope of the |
| 7746 | // namespace in which the template was defined. |
| 7747 | // |
| 7748 | // We actually implement this paragraph where we set the semantic |
| 7749 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 7750 | // but we also maintain the lexical context where the actual |
| 7751 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7752 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7753 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7754 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7755 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7756 | Specialization->startDefinition(); |
| 7757 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7758 | if (TUK == TUK_Friend) { |
| 7759 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 7760 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 7761 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7762 | /*FIXME:*/KWLoc); |
| 7763 | Friend->setAccess(AS_public); |
| 7764 | CurContext->addDecl(Friend); |
| 7765 | } else { |
| 7766 | // Add the specialization into its lexical context, so that it can |
| 7767 | // be seen when iterating through the list of declarations in that |
| 7768 | // context. However, specializations are not found by name lookup. |
| 7769 | CurContext->addDecl(Specialization); |
| 7770 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7771 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7772 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7773 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7774 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7775 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7776 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7777 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 7778 | ActOnDocumentableDecl(NewDecl); |
| 7779 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7780 | } |
| 7781 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7782 | /// Strips various properties off an implicit instantiation |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7783 | /// that has just been explicitly specialized. |
| 7784 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7785 | D->dropAttr<DLLImportAttr>(); |
| 7786 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7787 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7788 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7789 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7790 | } |
| 7791 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7792 | /// Compute the diagnostic location for an explicit instantiation |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7793 | // declaration or definition. |
| 7794 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7795 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7796 | // Explicit instantiations following a specialization have no effect and |
| 7797 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 7798 | // until a valid name loc is found. |
| 7799 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7800 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 7801 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7802 | PrevDiagLoc = Prev->getLocation(); |
| 7803 | } |
| 7804 | assert(PrevDiagLoc.isValid() && |
| 7805 | "Explicit instantiation without point of instantiation?"); |
| 7806 | return PrevDiagLoc; |
| 7807 | } |
| 7808 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7809 | /// Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7810 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7811 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7812 | /// new specialization/instantiation will have any effect. |
| 7813 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7814 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7815 | /// instantiation. |
| 7816 | /// |
| 7817 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 7818 | /// |
| 7819 | /// \param PrevDecl the previous declaration of the entity. |
| 7820 | /// |
| 7821 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 7822 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7823 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7824 | /// declaration was instantiated (either implicitly or explicitly). |
| 7825 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7826 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7827 | /// specialization or instantiation has no effect and should be ignored. |
| 7828 | /// |
| 7829 | /// \returns true if there was an error that should prevent the introduction of |
| 7830 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7831 | bool |
| 7832 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 7833 | TemplateSpecializationKind NewTSK, |
| 7834 | NamedDecl *PrevDecl, |
| 7835 | TemplateSpecializationKind PrevTSK, |
| 7836 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7837 | bool &HasNoEffect) { |
| 7838 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7839 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7840 | switch (NewTSK) { |
| 7841 | case TSK_Undeclared: |
| 7842 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 7843 | assert( |
| 7844 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 7845 | "previous declaration must be implicit!"); |
| 7846 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7847 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7848 | case TSK_ExplicitSpecialization: |
| 7849 | switch (PrevTSK) { |
| 7850 | case TSK_Undeclared: |
| 7851 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7852 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7853 | // explicitly specialized or has merely been mentioned without any |
| 7854 | // instantiation. |
| 7855 | return false; |
| 7856 | |
| 7857 | case TSK_ImplicitInstantiation: |
| 7858 | if (PrevPointOfInstantiation.isInvalid()) { |
| 7859 | // The declaration itself has not actually been instantiated, so it is |
| 7860 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7861 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7862 | return false; |
| 7863 | } |
| 7864 | // Fall through |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 7865 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7866 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7867 | case TSK_ExplicitInstantiationDeclaration: |
| 7868 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7869 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 7870 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7871 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7872 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7873 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7874 | // 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] | 7875 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7876 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7877 | // implicit instantiation to take place, in every translation unit in |
| 7878 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7879 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7880 | // Is there any previous explicit specialization declaration? |
| 7881 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 7882 | return false; |
| 7883 | } |
| 7884 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7885 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7886 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7887 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7888 | << (PrevTSK != TSK_ImplicitInstantiation); |
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 | return true; |
| 7891 | } |
Galina Kistanova | 1d36e83 | 2017-06-08 18:20:32 +0000 | [diff] [blame] | 7892 | llvm_unreachable("The switch over PrevTSK must be exhaustive."); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7893 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7894 | case TSK_ExplicitInstantiationDeclaration: |
| 7895 | switch (PrevTSK) { |
| 7896 | case TSK_ExplicitInstantiationDeclaration: |
| 7897 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7898 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7899 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7900 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7901 | case TSK_Undeclared: |
| 7902 | case TSK_ImplicitInstantiation: |
| 7903 | // We're explicitly instantiating something that may have already been |
| 7904 | // implicitly instantiated; that's fine. |
| 7905 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7906 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7907 | case TSK_ExplicitSpecialization: |
| 7908 | // C++0x [temp.explicit]p4: |
| 7909 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7910 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7911 | // specialization for that template, the explicit instantiation has no |
| 7912 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7913 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7914 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7915 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7916 | case TSK_ExplicitInstantiationDefinition: |
| 7917 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7918 | // If an entity is the subject of both an explicit instantiation |
| 7919 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7920 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7921 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7922 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7923 | |
| 7924 | // Explicit instantiations following a specialization have no effect and |
| 7925 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 7926 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7927 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 7928 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7929 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7930 | return false; |
| 7931 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7932 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7933 | case TSK_ExplicitInstantiationDefinition: |
| 7934 | switch (PrevTSK) { |
| 7935 | case TSK_Undeclared: |
| 7936 | case TSK_ImplicitInstantiation: |
| 7937 | // We're explicitly instantiating something that may have already been |
| 7938 | // implicitly instantiated; that's fine. |
| 7939 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7940 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7941 | case TSK_ExplicitSpecialization: |
| 7942 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 7943 | // For a given set of template parameters, if an explicit |
| 7944 | // instantiation of a template appears after a declaration of |
| 7945 | // an explicit specialization for that template, the explicit |
| 7946 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 7947 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7948 | << PrevDecl; |
| 7949 | Diag(PrevDecl->getLocation(), |
| 7950 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7951 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7952 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7953 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7954 | case TSK_ExplicitInstantiationDeclaration: |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 7955 | // We're explicitly instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7956 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7957 | |
| 7958 | // C++0x [temp.explicit]p4: |
| 7959 | // For a given set of template parameters, if an explicit instantiation |
| 7960 | // of a template appears after a declaration of an explicit |
| 7961 | // specialization for that template, the explicit instantiation has no |
| 7962 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7963 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7964 | // Is there any previous explicit specialization declaration? |
| 7965 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7966 | HasNoEffect = true; |
| 7967 | break; |
| 7968 | } |
| 7969 | } |
| 7970 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7971 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7972 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7973 | case TSK_ExplicitInstantiationDefinition: |
| 7974 | // C++0x [temp.spec]p5: |
| 7975 | // For a given template and a given set of template-arguments, |
| 7976 | // - an explicit instantiation definition shall appear at most once |
| 7977 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7978 | |
| 7979 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 7980 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 7981 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7982 | : diag::err_explicit_instantiation_duplicate) |
| 7983 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7984 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7985 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7986 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7987 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7988 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7989 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7990 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7991 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7992 | } |
| 7993 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7994 | /// Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7995 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7996 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7997 | /// The only possible way to get a dependent function template specialization |
| 7998 | /// is with a friend declaration, like so: |
| 7999 | /// |
| 8000 | /// \code |
| 8001 | /// template \<class T> void foo(T); |
| 8002 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8003 | /// friend void foo<>(T); |
| 8004 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 8005 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8006 | /// |
| 8007 | /// There really isn't any useful analysis we can do here, so we |
| 8008 | /// just store the information. |
| 8009 | bool |
| 8010 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 8011 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 8012 | LookupResult &Previous) { |
| 8013 | // Remove anything from Previous that isn't a function template in |
| 8014 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8015 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8016 | LookupResult::Filter F = Previous.makeFilter(); |
| 8017 | while (F.hasNext()) { |
| 8018 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 8019 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8020 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 8021 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 8022 | F.erase(); |
| 8023 | } |
| 8024 | F.done(); |
| 8025 | |
| 8026 | // Should this be diagnosed here? |
| 8027 | if (Previous.empty()) return true; |
| 8028 | |
| 8029 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 8030 | ExplicitTemplateArgs); |
| 8031 | return false; |
| 8032 | } |
| 8033 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8034 | /// Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8035 | /// specialization. |
| 8036 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8037 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8038 | /// explicit function template specialization. On successful completion, |
| 8039 | /// the function declaration \p FD will become a function template |
| 8040 | /// specialization. |
| 8041 | /// |
| 8042 | /// \param FD the function declaration, which will be updated to become a |
| 8043 | /// function template specialization. |
| 8044 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8045 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 8046 | /// if any. Note that this may be valid info even when 0 arguments are |
| 8047 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 8048 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8049 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8050 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8051 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8052 | bool Sema::CheckFunctionTemplateSpecialization( |
| 8053 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 8054 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8055 | // The set of function template specializations that could match this |
| 8056 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8057 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8058 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 8059 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8060 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8061 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 8062 | ConvertedTemplateArgs; |
| 8063 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8064 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8065 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8066 | I != E; ++I) { |
| 8067 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 8068 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8069 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8070 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8071 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8072 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8073 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8074 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8075 | // When matching a constexpr member function template specialization |
| 8076 | // against the primary template, we don't yet know whether the |
| 8077 | // specialization has an implicit 'const' (because we don't know whether |
| 8078 | // it will be a static member function until we know which template it |
| 8079 | // specializes), so adjust it now assuming it specializes this template. |
| 8080 | QualType FT = FD->getType(); |
| 8081 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8082 | CXXMethodDecl *OldMD = |
| 8083 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8084 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 8085 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8086 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8087 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8088 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8089 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 8090 | } |
| 8091 | } |
| 8092 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8093 | TemplateArgumentListInfo Args; |
| 8094 | if (ExplicitTemplateArgs) |
| 8095 | Args = *ExplicitTemplateArgs; |
| 8096 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8097 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8098 | // A trailing template-argument can be left unspecified in the |
| 8099 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8100 | // provided it can be deduced from the function argument type. |
| 8101 | // Perform template argument deduction to determine whether we may be |
| 8102 | // specializing this template. |
| 8103 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8104 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8105 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 8106 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 8107 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8108 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 8109 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8110 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8111 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8112 | FailedCandidates.addCandidate().set( |
| 8113 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8114 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8115 | (void)TDK; |
| 8116 | continue; |
| 8117 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8118 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8119 | // Target attributes are part of the cuda function signature, so |
| 8120 | // the deduced template's cuda target must match that of the |
| 8121 | // specialization. Given that C++ template deduction does not |
| 8122 | // take target attributes into account, we reject candidates |
| 8123 | // here that have a different target. |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8124 | if (LangOpts.CUDA && |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8125 | IdentifyCUDATarget(Specialization, |
| 8126 | /* IgnoreImplicitHDAttributes = */ true) != |
| 8127 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8128 | FailedCandidates.addCandidate().set( |
| 8129 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8130 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8131 | continue; |
| 8132 | } |
| 8133 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8134 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8135 | if (ExplicitTemplateArgs) |
| 8136 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8137 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8138 | } |
| 8139 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8140 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 8141 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8142 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 8143 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8144 | FD->getLocation(), |
| 8145 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 8146 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8147 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8148 | PDiag(diag::note_function_template_spec_matched)); |
| 8149 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8150 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8151 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8152 | |
| 8153 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 8154 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 8155 | |
| 8156 | FunctionTemplateSpecializationInfo *SpecInfo |
| 8157 | = Specialization->getTemplateSpecializationInfo(); |
| 8158 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8159 | |
| 8160 | // Note: do not overwrite location info if previous template |
| 8161 | // specialization kind was explicit. |
| 8162 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8163 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 8164 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8165 | Specialization->setLexicalDeclContext(FD->getLexicalDeclContext()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8166 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 8167 | // function can differ from the template declaration with respect to |
| 8168 | // the constexpr specifier. |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8169 | // FIXME: We need an update record for this AST mutation. |
| 8170 | // FIXME: What if there are multiple such prior declarations (for instance, |
| 8171 | // from different modules)? |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 8172 | Specialization->setConstexpr(FD->isConstexpr()); |
| 8173 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8174 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8175 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8176 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8177 | |
| 8178 | // If this is a friend declaration, then we're not really declaring |
| 8179 | // an explicit specialization. |
| 8180 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8181 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8182 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8183 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8184 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8185 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8186 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8187 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8188 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8189 | |
| 8190 | // C++ [temp.expl.spec]p6: |
| 8191 | // 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] | 8192 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8193 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8194 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8195 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8196 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8197 | if (!isFriend && |
| 8198 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8199 | TSK_ExplicitSpecialization, |
| 8200 | Specialization, |
| 8201 | SpecInfo->getTemplateSpecializationKind(), |
| 8202 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8203 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8204 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8205 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8206 | // Mark the prior declaration as an explicit specialization, so that later |
| 8207 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8208 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8209 | // Since explicit specializations do not inherit '=delete' from their |
| 8210 | // primary function template - check if the 'specialization' that was |
| 8211 | // implicitly generated (during template argument deduction for partial |
| 8212 | // ordering) from the most specialized of all the function templates that |
| 8213 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 8214 | // first check that it was implicitly generated during template argument |
| 8215 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 8216 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 8217 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 8218 | !Specialization->getCanonicalDecl()->isReferenced()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8219 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8220 | assert( |
| 8221 | Specialization->getCanonicalDecl() == Specialization && |
| 8222 | "This must be the only existing declaration of this specialization"); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8223 | // FIXME: We need an update record for this AST mutation. |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 8224 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8225 | } |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 8226 | // FIXME: We need an update record for this AST mutation. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 8227 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8228 | MarkUnusedFileScopedDecl(Specialization); |
| 8229 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8230 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8231 | // Turn the given function declaration into a function template |
| 8232 | // specialization, with the template arguments from the previous |
| 8233 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 8234 | // Take copies of (semantic and syntactic) template argument lists. |
| 8235 | const TemplateArgumentList* TemplArgs = new (Context) |
| 8236 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 8237 | FD->setFunctionTemplateSpecialization( |
| 8238 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 8239 | SpecInfo->getTemplateSpecializationKind(), |
| 8240 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8241 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8242 | // A function template specialization inherits the target attributes |
| 8243 | // of its template. (We require the attributes explicitly in the |
| 8244 | // code to match, but a template may have implicit attributes by |
| 8245 | // virtue e.g. of being constexpr, and it passes these implicit |
| 8246 | // attributes on to its specializations.) |
| 8247 | if (LangOpts.CUDA) |
| 8248 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 8249 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8250 | // The "previous declaration" for this function template specialization is |
| 8251 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8252 | Previous.clear(); |
| 8253 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 8254 | return false; |
| 8255 | } |
| 8256 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8257 | /// Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8258 | /// specialization. |
| 8259 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8260 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8261 | /// explicit member function specialization. On successful completion, |
| 8262 | /// the function declaration \p FD will become a member function |
| 8263 | /// specialization. |
| 8264 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8265 | /// \param Member the member declaration, which will be updated to become a |
| 8266 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8267 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8268 | /// \param Previous the set of declarations, one of which may be specialized |
| 8269 | /// by this function specialization; the set will be modified to contain the |
| 8270 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8271 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8272 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8273 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8274 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8275 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8276 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8277 | NamedDecl *Instantiation = nullptr; |
| 8278 | NamedDecl *InstantiatedFrom = nullptr; |
| 8279 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8280 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8281 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8282 | // Nowhere to look anyway. |
| 8283 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8284 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8285 | I != E; ++I) { |
| 8286 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 8287 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 8288 | QualType Adjusted = Function->getType(); |
| 8289 | if (!hasExplicitCallingConv(Adjusted)) |
| 8290 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 8291 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8292 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8293 | Instantiation = Method; |
| 8294 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8295 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8296 | break; |
| 8297 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8298 | } |
| 8299 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8300 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8301 | VarDecl *PrevVar; |
| 8302 | if (Previous.isSingleResult() && |
| 8303 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8304 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8305 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8306 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8307 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8308 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8309 | } |
| 8310 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8311 | CXXRecordDecl *PrevRecord; |
| 8312 | if (Previous.isSingleResult() && |
| 8313 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8314 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8315 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8316 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8317 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8318 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8319 | } else if (isa<EnumDecl>(Member)) { |
| 8320 | EnumDecl *PrevEnum; |
| 8321 | if (Previous.isSingleResult() && |
| 8322 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8323 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8324 | Instantiation = PrevEnum; |
| 8325 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 8326 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 8327 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8328 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8329 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8330 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8331 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8332 | // specializations are always out-of-line, the caller will complain about |
| 8333 | // this mismatch later. |
| 8334 | return false; |
| 8335 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8336 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8337 | // A member specialization in a friend declaration isn't really declaring |
| 8338 | // an explicit specialization, just identifying a specific (possibly implicit) |
| 8339 | // specialization. Don't change the template specialization kind. |
| 8340 | // |
| 8341 | // FIXME: Is this really valid? Other compilers reject. |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8342 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 8343 | // Preserve instantiation information. |
| 8344 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 8345 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 8346 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 8347 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8348 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 8349 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 8350 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 8351 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8352 | } |
| 8353 | |
| 8354 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8355 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 8356 | return false; |
| 8357 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8358 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8359 | // Make sure that this is a specialization of a member. |
| 8360 | if (!InstantiatedFrom) { |
| 8361 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 8362 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8363 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 8364 | return true; |
| 8365 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8366 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8367 | // C++ [temp.expl.spec]p6: |
| 8368 | // 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] | 8369 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8370 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8371 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8372 | // use occurs; no diagnostic is required. |
| 8373 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8374 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8375 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8376 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 8377 | TSK_ExplicitSpecialization, |
| 8378 | Instantiation, |
| 8379 | MSInfo->getTemplateSpecializationKind(), |
| 8380 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8381 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8382 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8383 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8384 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8385 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8386 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8387 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8388 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8389 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 8390 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8391 | // Note that this member specialization is an "instantiation of" the |
| 8392 | // corresponding member of the original template. |
| 8393 | if (auto *MemberFunction = dyn_cast<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8394 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 8395 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 8396 | TSK_ImplicitInstantiation) { |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8397 | // Explicit specializations of member functions of class templates do not |
| 8398 | // inherit '=delete' from the member function they are specializing. |
| 8399 | if (InstantiationFunction->isDeleted()) { |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8400 | // FIXME: This assert will not hold in the presence of modules. |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8401 | assert(InstantiationFunction->getCanonicalDecl() == |
| 8402 | InstantiationFunction); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8403 | // FIXME: We need an update record for this AST mutation. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 8404 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8405 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8406 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8407 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8408 | MemberFunction->setInstantiationOfMemberFunction( |
| 8409 | cast<CXXMethodDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8410 | } else if (auto *MemberVar = dyn_cast<VarDecl>(Member)) { |
| 8411 | MemberVar->setInstantiationOfStaticDataMember( |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8412 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8413 | } else if (auto *MemberClass = dyn_cast<CXXRecordDecl>(Member)) { |
| 8414 | MemberClass->setInstantiationOfMemberClass( |
| 8415 | cast<CXXRecordDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8416 | } else if (auto *MemberEnum = dyn_cast<EnumDecl>(Member)) { |
| 8417 | MemberEnum->setInstantiationOfMemberEnum( |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8418 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8419 | } else { |
| 8420 | llvm_unreachable("unknown member specialization kind"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8421 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8422 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8423 | // Save the caller the trouble of having to figure out which declaration |
| 8424 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8425 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8426 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8427 | return false; |
| 8428 | } |
| 8429 | |
Richard Smith | 77e9e84 | 2017-05-09 23:02:10 +0000 | [diff] [blame] | 8430 | /// Complete the explicit specialization of a member of a class template by |
| 8431 | /// updating the instantiated member to be marked as an explicit specialization. |
| 8432 | /// |
| 8433 | /// \param OrigD The member declaration instantiated from the template. |
| 8434 | /// \param Loc The location of the explicit specialization of the member. |
| 8435 | template<typename DeclT> |
| 8436 | static void completeMemberSpecializationImpl(Sema &S, DeclT *OrigD, |
| 8437 | SourceLocation Loc) { |
| 8438 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 8439 | return; |
| 8440 | |
| 8441 | // FIXME: Inform AST mutation listeners of this AST mutation. |
| 8442 | // FIXME: If there are multiple in-class declarations of the member (from |
| 8443 | // multiple modules, or a declaration and later definition of a member type), |
| 8444 | // should we update all of them? |
| 8445 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
| 8446 | OrigD->setLocation(Loc); |
| 8447 | } |
| 8448 | |
| 8449 | void Sema::CompleteMemberSpecialization(NamedDecl *Member, |
| 8450 | LookupResult &Previous) { |
| 8451 | NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl()); |
| 8452 | if (Instantiation == Member) |
| 8453 | return; |
| 8454 | |
| 8455 | if (auto *Function = dyn_cast<CXXMethodDecl>(Instantiation)) |
| 8456 | completeMemberSpecializationImpl(*this, Function, Member->getLocation()); |
| 8457 | else if (auto *Var = dyn_cast<VarDecl>(Instantiation)) |
| 8458 | completeMemberSpecializationImpl(*this, Var, Member->getLocation()); |
| 8459 | else if (auto *Record = dyn_cast<CXXRecordDecl>(Instantiation)) |
| 8460 | completeMemberSpecializationImpl(*this, Record, Member->getLocation()); |
| 8461 | else if (auto *Enum = dyn_cast<EnumDecl>(Instantiation)) |
| 8462 | completeMemberSpecializationImpl(*this, Enum, Member->getLocation()); |
| 8463 | else |
| 8464 | llvm_unreachable("unknown member specialization kind"); |
| 8465 | } |
| 8466 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8467 | /// Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8468 | /// |
| 8469 | /// \returns true if a serious error occurs, false otherwise. |
| 8470 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8471 | SourceLocation InstLoc, |
| 8472 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8473 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 8474 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8475 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8476 | if (CurContext->isRecord()) { |
| 8477 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 8478 | << D; |
| 8479 | return true; |
| 8480 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8481 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8482 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8483 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8484 | // template. If the name declared in the explicit instantiation is an |
| 8485 | // unqualified name, the explicit instantiation shall appear in the |
| 8486 | // namespace where its template is declared or, if that namespace is inline |
| 8487 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8488 | // |
| 8489 | // 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] | 8490 | if (WasQualifiedName) { |
| 8491 | if (CurContext->Encloses(OrigContext)) |
| 8492 | return false; |
| 8493 | } else { |
| 8494 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 8495 | return false; |
| 8496 | } |
| 8497 | |
| 8498 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 8499 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8500 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8501 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8502 | diag::err_explicit_instantiation_out_of_scope : |
| 8503 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8504 | << D << NS; |
| 8505 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8506 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8507 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8508 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 8509 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 8510 | << D << NS; |
| 8511 | } else |
| 8512 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8513 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8514 | diag::err_explicit_instantiation_must_be_global : |
| 8515 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 8516 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8517 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8518 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8519 | } |
| 8520 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8521 | /// Determine whether the given scope specifier has a template-id in it. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8522 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 8523 | if (!SS.isSet()) |
| 8524 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8525 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8526 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8527 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8528 | // or a static data member of a class template specialization, the name of |
| 8529 | // the class template specialization in the qualified-id for the member |
| 8530 | // name shall be a simple-template-id. |
| 8531 | // |
| 8532 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8533 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 8534 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8535 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8536 | if (isa<TemplateSpecializationType>(T)) |
| 8537 | return true; |
| 8538 | |
| 8539 | return false; |
| 8540 | } |
| 8541 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8542 | /// Make a dllexport or dllimport attr on a class template specialization take |
| 8543 | /// effect. |
| 8544 | static void dllExportImportClassTemplateSpecialization( |
| 8545 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 8546 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 8547 | assert(A && "dllExportImportClassTemplateSpecialization called " |
| 8548 | "on Def without dllexport or dllimport"); |
| 8549 | |
| 8550 | // We reject explicit instantiations in class scope, so there should |
| 8551 | // never be any delayed exported classes to worry about. |
| 8552 | assert(S.DelayedDllExportClasses.empty() && |
| 8553 | "delayed exports present at explicit instantiation"); |
| 8554 | S.checkClassLevelDLLAttribute(Def); |
| 8555 | |
| 8556 | // Propagate attribute to base class templates. |
| 8557 | for (auto &B : Def->bases()) { |
| 8558 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 8559 | B.getType()->getAsCXXRecordDecl())) |
| 8560 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 8561 | } |
| 8562 | |
| 8563 | S.referenceDLLExportedClassMethods(); |
| 8564 | } |
| 8565 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8566 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8567 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8568 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 8569 | SourceLocation ExternLoc, |
| 8570 | SourceLocation TemplateLoc, |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 8571 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8572 | SourceLocation KWLoc, |
| 8573 | const CXXScopeSpec &SS, |
| 8574 | TemplateTy TemplateD, |
| 8575 | SourceLocation TemplateNameLoc, |
| 8576 | SourceLocation LAngleLoc, |
| 8577 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8578 | SourceLocation RAngleLoc, |
| 8579 | AttributeList *Attr) { |
| 8580 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 8581 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8582 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8583 | // Check that the specialization uses the same tag kind as the |
| 8584 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8585 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 8586 | assert(Kind != TTK_Enum && |
| 8587 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8588 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8589 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 8590 | |
| 8591 | if (!ClassTemplate) { |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 8592 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 8593 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8594 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8595 | return true; |
| 8596 | } |
| 8597 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 8598 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 8599 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 8600 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8601 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8602 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8603 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8604 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8605 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8606 | diag::note_previous_use); |
| 8607 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 8608 | } |
| 8609 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8610 | // C++0x [temp.explicit]p2: |
| 8611 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8612 | // definition and an explicit instantiation declaration. An explicit |
| 8613 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8614 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 8615 | ? TSK_ExplicitInstantiationDefinition |
| 8616 | : TSK_ExplicitInstantiationDeclaration; |
| 8617 | |
| 8618 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 8619 | // Check for dllexport class template instantiation declarations. |
| 8620 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 8621 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 8622 | Diag(ExternLoc, |
| 8623 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8624 | Diag(A->getLoc(), diag::note_attribute); |
| 8625 | break; |
| 8626 | } |
| 8627 | } |
| 8628 | |
| 8629 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 8630 | Diag(ExternLoc, |
| 8631 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8632 | Diag(A->getLocation(), diag::note_attribute); |
| 8633 | } |
| 8634 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8635 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8636 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 8637 | // instantiation declarations for most purposes. |
| 8638 | bool DLLImportExplicitInstantiationDef = false; |
| 8639 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 8640 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 8641 | // Check for dllimport class template instantiation definitions. |
| 8642 | bool DLLImport = |
| 8643 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 8644 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 8645 | if (A->getKind() == AttributeList::AT_DLLImport) |
| 8646 | DLLImport = true; |
| 8647 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 8648 | // dllexport trumps dllimport here. |
| 8649 | DLLImport = false; |
| 8650 | break; |
| 8651 | } |
| 8652 | } |
| 8653 | if (DLLImport) { |
| 8654 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 8655 | DLLImportExplicitInstantiationDef = true; |
| 8656 | } |
| 8657 | } |
| 8658 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8659 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8660 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 8661 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8662 | |
| 8663 | // Check that the template argument list is well-formed for this |
| 8664 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8665 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8666 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 8667 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8668 | return true; |
| 8669 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8670 | // Find the class template specialization declaration that |
| 8671 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8672 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8673 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 8674 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8675 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8676 | TemplateSpecializationKind PrevDecl_TSK |
| 8677 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 8678 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8679 | // C++0x [temp.explicit]p2: |
| 8680 | // [...] An explicit instantiation shall appear in an enclosing |
| 8681 | // namespace of its template. [...] |
| 8682 | // |
| 8683 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8684 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 8685 | SS.isSet())) |
| 8686 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8687 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8688 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8689 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8690 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8691 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8692 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8693 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8694 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8695 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8696 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8697 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8698 | // Even though HasNoEffect == true means that this explicit instantiation |
| 8699 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 8700 | |
| 8701 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 8702 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8703 | // Since the only prior class template specialization with these |
| 8704 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8705 | // declaration node as our own, updating the source location |
| 8706 | // for the template name to reflect our new declaration. |
| 8707 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8708 | Specialization = PrevDecl; |
| 8709 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8710 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8711 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8712 | |
| 8713 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 8714 | DLLImportExplicitInstantiationDef) { |
| 8715 | // The new specialization might add a dllimport attribute. |
| 8716 | HasNoEffect = false; |
| 8717 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8718 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8719 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8720 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8721 | // Create a new class template specialization declaration node for |
| 8722 | // this explicit specialization. |
| 8723 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 8724 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8725 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 8726 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8727 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 8728 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8729 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 8730 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8731 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8732 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8733 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8734 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8735 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8736 | } |
| 8737 | |
| 8738 | // Build the fully-sugared type for this explicit instantiation as |
| 8739 | // the user wrote in the explicit instantiation itself. This means |
| 8740 | // that we'll pretty-print the type retrieved from the |
| 8741 | // specialization's declaration the way that the user actually wrote |
| 8742 | // the explicit instantiation, rather than formatting the name based |
| 8743 | // on the "canonical" representation used to store the template |
| 8744 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 8745 | TypeSourceInfo *WrittenTy |
| 8746 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 8747 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8748 | Context.getTypeDeclType(Specialization)); |
| 8749 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8750 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8751 | // Set source locations for keywords. |
| 8752 | Specialization->setExternLoc(ExternLoc); |
| 8753 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 8754 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8755 | |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8756 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 8757 | if (Attr) |
| 8758 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 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 |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8856 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 8857 | SourceLocation ExternLoc, |
| 8858 | SourceLocation TemplateLoc, |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 8859 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8860 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 8861 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8862 | IdentifierInfo *Name, |
| 8863 | SourceLocation NameLoc, |
| 8864 | AttributeList *Attr) { |
| 8865 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 8866 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8867 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8868 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8869 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 8870 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8871 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 8872 | SourceLocation(), false, TypeResult(), |
Akira Hatanaka | 12ddcee | 2017-06-26 18:46:12 +0000 | [diff] [blame] | 8873 | /*IsTypeSpecifier*/false, |
| 8874 | /*IsTemplateParamOrArg*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8875 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 8876 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8877 | if (!TagD) |
| 8878 | return true; |
| 8879 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8880 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8881 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8882 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 8883 | if (Tag->isInvalidDecl()) |
| 8884 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8885 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8886 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 8887 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 8888 | if (!Pattern) { |
| 8889 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 8890 | << Context.getTypeDeclType(Record); |
| 8891 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 8892 | return true; |
| 8893 | } |
| 8894 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8895 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8896 | // If the explicit instantiation is for a class or member class, the |
| 8897 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8898 | // simple-template-id. |
| 8899 | // |
| 8900 | // C++98 has the same restriction, just worded differently. |
| 8901 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8902 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8903 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8904 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8905 | // C++0x [temp.explicit]p2: |
| 8906 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8907 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8908 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 8909 | TemplateSpecializationKind TSK |
| 8910 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8911 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8912 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8913 | // C++0x [temp.explicit]p2: |
| 8914 | // [...] An explicit instantiation shall appear in an enclosing |
| 8915 | // namespace of its template. [...] |
| 8916 | // |
| 8917 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8918 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8919 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8920 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8921 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8922 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8923 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8924 | PrevDecl = Record; |
| 8925 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8926 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8927 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8928 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8929 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8930 | PrevDecl, |
| 8931 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8932 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8933 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8934 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8935 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8936 | return TagD; |
| 8937 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8938 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8939 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8940 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8941 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8942 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8943 | // 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] | 8944 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8945 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8946 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8947 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 8948 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 8949 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8950 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 8951 | << Pattern; |
| 8952 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8953 | } else { |
| 8954 | if (InstantiateClass(NameLoc, Record, Def, |
| 8955 | getTemplateInstantiationArgs(Record), |
| 8956 | TSK)) |
| 8957 | return true; |
| 8958 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8959 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8960 | if (!RecordDef) |
| 8961 | return true; |
| 8962 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8963 | } |
| 8964 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8965 | // Instantiate all of the members of the class. |
| 8966 | InstantiateClassMembers(NameLoc, RecordDef, |
| 8967 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8968 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8969 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8970 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 8971 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8972 | // FIXME: We don't have any representation for explicit instantiations of |
| 8973 | // member classes. Such a representation is not needed for compilation, but it |
| 8974 | // should be available for clients that want to see all of the declarations in |
| 8975 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8976 | return TagD; |
| 8977 | } |
| 8978 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8979 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 8980 | SourceLocation ExternLoc, |
| 8981 | SourceLocation TemplateLoc, |
| 8982 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8983 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8984 | // TODO: check if/when DNInfo should replace Name. |
| 8985 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 8986 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8987 | if (!Name) { |
| 8988 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8989 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8990 | diag::err_explicit_instantiation_requires_name) |
| 8991 | << D.getDeclSpec().getSourceRange() |
| 8992 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8993 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8994 | return true; |
| 8995 | } |
| 8996 | |
| 8997 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 8998 | // we find one that is. |
| 8999 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 9000 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 9001 | S = S->getParent(); |
| 9002 | |
| 9003 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9004 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 9005 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9006 | if (R.isNull()) |
| 9007 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9008 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9009 | // C++ [dcl.stc]p1: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9010 | // A storage-class-specifier shall not be specified in [...] an explicit |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9011 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9012 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9013 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 9014 | << Name; |
| 9015 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9016 | } else if (D.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9017 | != DeclSpec::SCS_unspecified) { |
| 9018 | // Complain about then remove the storage class specifier. |
| 9019 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 9020 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9021 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 9022 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9023 | } |
| 9024 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 9025 | // C++0x [temp.explicit]p1: |
| 9026 | // [...] An explicit instantiation of a function template shall not use the |
| 9027 | // inline or constexpr specifiers. |
| 9028 | // Presumably, this also applies to member functions of class templates as |
| 9029 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9030 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9031 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9032 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 9033 | diag::err_explicit_instantiation_inline : |
| 9034 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9035 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9036 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 9037 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 9038 | // not already specified. |
| 9039 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 9040 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9041 | |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 9042 | // A deduction guide is not on the list of entities that can be explicitly |
| 9043 | // instantiated. |
| 9044 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
| 9045 | Diag(D.getDeclSpec().getLocStart(), diag::err_deduction_guide_specialized) |
| 9046 | << /*explicit instantiation*/ 0; |
| 9047 | return true; |
| 9048 | } |
| 9049 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9050 | // C++0x [temp.explicit]p2: |
| 9051 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9052 | // definition and an explicit instantiation declaration. An explicit |
| 9053 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9054 | TemplateSpecializationKind TSK |
| 9055 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 9056 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9057 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9058 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9059 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9060 | |
| 9061 | if (!R->isFunctionType()) { |
| 9062 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9063 | // A [...] static data member of a class template can be explicitly |
| 9064 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9065 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9066 | // C++1y [temp.explicit]p1: |
| 9067 | // A [...] variable [...] template specialization can be explicitly |
| 9068 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9069 | if (Previous.isAmbiguous()) |
| 9070 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9071 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 9072 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9073 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9074 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9075 | if (!PrevTemplate) { |
| 9076 | if (!Prev || !Prev->isStaticDataMember()) { |
| 9077 | // We expect to see a data data member here. |
| 9078 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 9079 | << Name; |
| 9080 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9081 | P != PEnd; ++P) |
| 9082 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 9083 | return true; |
| 9084 | } |
| 9085 | |
| 9086 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 9087 | // FIXME: Check for explicit specialization? |
| 9088 | Diag(D.getIdentifierLoc(), |
| 9089 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 9090 | << Prev; |
| 9091 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 9092 | // FIXME: Can we provide a note showing where this was declared? |
| 9093 | return true; |
| 9094 | } |
| 9095 | } else { |
| 9096 | // Explicitly instantiate a variable template. |
| 9097 | |
| 9098 | // C++1y [dcl.spec.auto]p6: |
| 9099 | // ... A program that uses auto or decltype(auto) in a context not |
| 9100 | // explicitly allowed in this section is ill-formed. |
| 9101 | // |
| 9102 | // This includes auto-typed variable template instantiations. |
| 9103 | if (R->isUndeducedType()) { |
| 9104 | Diag(T->getTypeLoc().getLocStart(), |
| 9105 | diag::err_auto_not_allowed_var_inst); |
| 9106 | return true; |
| 9107 | } |
| 9108 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9109 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9110 | // C++1y [temp.explicit]p3: |
| 9111 | // If the explicit instantiation is for a variable, the unqualified-id |
| 9112 | // in the declaration shall be a template-id. |
| 9113 | Diag(D.getIdentifierLoc(), |
| 9114 | diag::err_explicit_instantiation_without_template_id) |
| 9115 | << PrevTemplate; |
| 9116 | Diag(PrevTemplate->getLocation(), |
| 9117 | diag::note_explicit_instantiation_here); |
| 9118 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9119 | } |
| 9120 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9121 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9122 | TemplateArgumentListInfo TemplateArgs = |
| 9123 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 9124 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9125 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 9126 | D.getIdentifierLoc(), TemplateArgs); |
| 9127 | if (Res.isInvalid()) |
| 9128 | return true; |
| 9129 | |
| 9130 | // Ignore access control bits, we don't need them for redeclaration |
| 9131 | // checking. |
| 9132 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9133 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9134 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9135 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9136 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9137 | // or a static data member of a class template specialization, the name of |
| 9138 | // the class template specialization in the qualified-id for the member |
| 9139 | // name shall be a simple-template-id. |
| 9140 | // |
| 9141 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9142 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 9143 | // This does not apply to variable template specializations, where the |
| 9144 | // template-id is in the unqualified-id instead. |
| 9145 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9146 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9147 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9148 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9149 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9150 | // Check the scope of this explicit instantiation. |
| 9151 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9152 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9153 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 9154 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 9155 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9156 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9157 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9158 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 9159 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9160 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9161 | if (!HasNoEffect) { |
| 9162 | // Instantiate static data member or variable template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9163 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 9164 | if (PrevTemplate) { |
| 9165 | // Merge attributes. |
| 9166 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 9167 | ProcessDeclAttributeList(S, Prev, Attr); |
| 9168 | } |
| 9169 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9170 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 9171 | } |
| 9172 | |
| 9173 | // Check the new variable specialization against the parsed input. |
| 9174 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 9175 | Diag(T->getTypeLoc().getLocStart(), |
| 9176 | diag::err_invalid_var_template_spec_type) |
| 9177 | << 0 << PrevTemplate << R << Prev->getType(); |
| 9178 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 9179 | << 2 << PrevTemplate->getDeclName(); |
| 9180 | return true; |
| 9181 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9182 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9183 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9184 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9185 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9186 | |
| 9187 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 9188 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9189 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9190 | TemplateArgumentListInfo TemplateArgs; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9191 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 9192 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9193 | HasExplicitTemplateArgs = true; |
| 9194 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9195 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9196 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9197 | // A [...] function [...] can be explicitly instantiated from its template. |
| 9198 | // A member function [...] of a class template can be explicitly |
| 9199 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9200 | // template. |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9201 | UnresolvedSet<8> TemplateMatches; |
| 9202 | FunctionDecl *NonTemplateMatch = nullptr; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 9203 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9204 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9205 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9206 | P != PEnd; ++P) { |
| 9207 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9208 | if (!HasExplicitTemplateArgs) { |
| 9209 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 9210 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 9211 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 9212 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9213 | if (Method->getPrimaryTemplate()) { |
| 9214 | TemplateMatches.addDecl(Method, P.getAccess()); |
| 9215 | } else { |
| 9216 | // FIXME: Can this assert ever happen? Needs a test. |
| 9217 | assert(!NonTemplateMatch && "Multiple NonTemplateMatches"); |
| 9218 | NonTemplateMatch = Method; |
| 9219 | } |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 9220 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9221 | } |
| 9222 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9223 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9224 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 9225 | if (!FunTmpl) |
| 9226 | continue; |
| 9227 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9228 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9229 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9230 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9231 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9232 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 9233 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9234 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9235 | // Keep track of almost-matches. |
| 9236 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9237 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9238 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9239 | (void)TDK; |
| 9240 | continue; |
| 9241 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9242 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 9243 | // Target attributes are part of the cuda function signature, so |
| 9244 | // the cuda target of the instantiated function must match that of its |
| 9245 | // template. Given that C++ template deduction does not take |
| 9246 | // target attributes into account, we reject candidates here that |
| 9247 | // have a different target. |
| 9248 | if (LangOpts.CUDA && |
| 9249 | IdentifyCUDATarget(Specialization, |
| 9250 | /* IgnoreImplicitHDAttributes = */ true) != |
| 9251 | IdentifyCUDATarget(Attr)) { |
| 9252 | FailedCandidates.addCandidate().set( |
| 9253 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 9254 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 9255 | continue; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 9256 | } |
| 9257 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9258 | TemplateMatches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9259 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9260 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9261 | FunctionDecl *Specialization = NonTemplateMatch; |
| 9262 | if (!Specialization) { |
| 9263 | // Find the most specialized function template specialization. |
| 9264 | UnresolvedSetIterator Result = getMostSpecialized( |
| 9265 | TemplateMatches.begin(), TemplateMatches.end(), FailedCandidates, |
| 9266 | D.getIdentifierLoc(), |
| 9267 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 9268 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 9269 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9270 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9271 | if (Result == TemplateMatches.end()) |
| 9272 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9273 | |
John McCall | 27c11dd | 2017-06-07 23:00:05 +0000 | [diff] [blame] | 9274 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 9275 | Specialization = cast<FunctionDecl>(*Result); |
| 9276 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9277 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 9278 | // C++11 [except.spec]p4 |
| 9279 | // In an explicit instantiation an exception-specification may be specified, |
| 9280 | // but is not required. |
| 9281 | // If an exception-specification is specified in an explicit instantiation |
| 9282 | // directive, it shall be compatible with the exception-specifications of |
| 9283 | // other declarations of that function. |
| 9284 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 9285 | if (FPT->hasExceptionSpec()) { |
| 9286 | unsigned DiagID = |
| 9287 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 9288 | if (getLangOpts().MicrosoftExt) |
| 9289 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 9290 | bool Result = CheckEquivalentExceptionSpec( |
| 9291 | PDiag(DiagID) << Specialization->getType(), |
| 9292 | PDiag(diag::note_explicit_instantiation_here), |
| 9293 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 9294 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 9295 | // In Microsoft mode, mismatching exception specifications just cause a |
| 9296 | // warning. |
| 9297 | if (!getLangOpts().MicrosoftExt && Result) |
| 9298 | return true; |
| 9299 | } |
| 9300 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9301 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9302 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9303 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 9304 | << Specialization |
| 9305 | << (Specialization->getTemplateSpecializationKind() == |
| 9306 | TSK_ExplicitSpecialization); |
| 9307 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 9308 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9309 | } |
| 9310 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 9311 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 9312 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 9313 | PrevDecl = Specialization; |
| 9314 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9315 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9316 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 9317 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9318 | PrevDecl, |
| 9319 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9320 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9321 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9322 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9323 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9324 | // FIXME: We may still want to build some representation of this |
| 9325 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 9326 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9327 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9328 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 9329 | |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 9330 | if (Attr) |
| 9331 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9332 | |
Hans Wennborg | b8304a6 | 2017-11-29 23:44:11 +0000 | [diff] [blame] | 9333 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 9334 | // instantiation declarations. |
| 9335 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 9336 | Specialization->hasAttr<DLLImportAttr>() && |
| 9337 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 9338 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 9339 | |
| 9340 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 9341 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 9342 | if (Specialization->isDefined()) { |
| 9343 | // Let the ASTConsumer know that this function has been explicitly |
| 9344 | // instantiated now, and its linkage might have changed. |
| 9345 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 9346 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 9347 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9348 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9349 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9350 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9351 | // or a static data member of a class template specialization, the name of |
| 9352 | // the class template specialization in the qualified-id for the member |
| 9353 | // name shall be a simple-template-id. |
| 9354 | // |
| 9355 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 9356 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 9357 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9358 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9359 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9360 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 9361 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9362 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9363 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9364 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9365 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9366 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9367 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9368 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9369 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9370 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9371 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9372 | } |
| 9373 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9374 | TypeResult |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 9375 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9376 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 9377 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 9378 | // This has to hold, because SS is expected to be defined. |
| 9379 | assert(Name && "Expected a name in a dependent tag"); |
| 9380 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9381 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9382 | if (!NNS) |
| 9383 | return true; |
| 9384 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9385 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 9386 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9387 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 9388 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9389 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9390 | return true; |
| 9391 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9392 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9393 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9394 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9395 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9396 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9397 | // Create type-source location information for this type. |
| 9398 | TypeLocBuilder TLB; |
| 9399 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9400 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9401 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 9402 | TL.setNameLoc(NameLoc); |
| 9403 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9404 | } |
| 9405 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9406 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9407 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 9408 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 9409 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9410 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9411 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9412 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9413 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9414 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9415 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9416 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9417 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9418 | << FixItHint::CreateRemoval(TypenameLoc); |
| 9419 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9420 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9421 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 9422 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 9423 | if (T.isNull()) |
| 9424 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9425 | |
| 9426 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9427 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9428 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9429 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9430 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9431 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9432 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9433 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9434 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9435 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9436 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9437 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9438 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9439 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9440 | } |
| 9441 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9442 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9443 | Sema::ActOnTypenameType(Scope *S, |
| 9444 | SourceLocation TypenameLoc, |
| 9445 | const CXXScopeSpec &SS, |
| 9446 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9447 | TemplateTy TemplateIn, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9448 | IdentifierInfo *TemplateII, |
| 9449 | SourceLocation TemplateIILoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9450 | SourceLocation LAngleLoc, |
| 9451 | ASTTemplateArgsPtr TemplateArgsIn, |
| 9452 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9453 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9454 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9455 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9456 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9457 | diag::ext_typename_outside_of_template) |
| 9458 | << FixItHint::CreateRemoval(TypenameLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9459 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9460 | // Strangely, non-type results are not ignored by this lookup, so the |
| 9461 | // program is ill-formed if it finds an injected-class-name. |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 9462 | if (TypenameLoc.isValid()) { |
| 9463 | auto *LookupRD = |
| 9464 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
| 9465 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 9466 | Diag(TemplateIILoc, |
| 9467 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9468 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 9469 | << (TemplateKWLoc.isValid() ? 1 : 0 /*'template'/'typename' keyword*/); |
| 9470 | } |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9471 | } |
| 9472 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9473 | // Translate the parser's template argument list in our AST format. |
| 9474 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 9475 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9476 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9477 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9478 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 9479 | // Construct a dependent template specialization type. |
| 9480 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9481 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9482 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 9483 | DTN->getQualifier(), |
| 9484 | DTN->getIdentifier(), |
| 9485 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9486 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9487 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9488 | TypeLocBuilder Builder; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9489 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9490 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9491 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 9492 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 9493 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9494 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9495 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9496 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9497 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9498 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9499 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 9500 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9501 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9502 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9503 | if (T.isNull()) |
| 9504 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9505 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9506 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9507 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9508 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9509 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9510 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9511 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9512 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9513 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9514 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9515 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9516 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9517 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 9518 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9519 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9520 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9521 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9522 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 9523 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 9524 | } |
| 9525 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9526 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9527 | /// Determine whether this failed name lookup should be treated as being |
| 9528 | /// disabled by a usage of std::enable_if. |
| 9529 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9530 | SourceRange &CondRange, Expr *&Cond) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9531 | // We must be looking for a ::type... |
| 9532 | if (!II.isStr("type")) |
| 9533 | return false; |
| 9534 | |
| 9535 | // ... within an explicitly-written template specialization... |
| 9536 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 9537 | return false; |
| 9538 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9539 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 9540 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 9541 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9542 | return false; |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 9543 | const TemplateSpecializationType *EnableIfTST = EnableIfTSTLoc.getTypePtr(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9544 | |
| 9545 | // ... which names a complete class template declaration... |
| 9546 | const TemplateDecl *EnableIfDecl = |
| 9547 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 9548 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 9549 | return false; |
| 9550 | |
| 9551 | // ... called "enable_if". |
| 9552 | const IdentifierInfo *EnableIfII = |
| 9553 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 9554 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 9555 | return false; |
| 9556 | |
| 9557 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9558 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9559 | |
| 9560 | // Dig out the condition. |
| 9561 | Cond = nullptr; |
| 9562 | if (EnableIfTSTLoc.getArgLoc(0).getArgument().getKind() |
| 9563 | != TemplateArgument::Expression) |
| 9564 | return true; |
| 9565 | |
| 9566 | Cond = EnableIfTSTLoc.getArgLoc(0).getSourceExpression(); |
| 9567 | |
| 9568 | // Ignore Boolean literals; they add no value. |
| 9569 | if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts())) |
| 9570 | Cond = nullptr; |
| 9571 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9572 | return true; |
| 9573 | } |
| 9574 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9575 | /// Build the type that describes a C++ typename specifier, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9576 | /// e.g., "typename T::type". |
| 9577 | QualType |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9578 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9579 | SourceLocation KeywordLoc, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9580 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9581 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9582 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9583 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9584 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9585 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9586 | DeclContext *Ctx = computeDeclContext(SS); |
| 9587 | if (!Ctx) { |
| 9588 | // If the nested-name-specifier is dependent and couldn't be |
| 9589 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9590 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9591 | return Context.getDependentNameType(Keyword, |
| 9592 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9593 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 9594 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9595 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9596 | // If the nested-name-specifier refers to the current instantiation, |
| 9597 | // the "typename" keyword itself is superfluous. In C++03, the |
| 9598 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 9599 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 9600 | // 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] | 9601 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9602 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 9603 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9604 | |
| 9605 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9606 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 9607 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9608 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9609 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9610 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9611 | case LookupResult::NotFound: { |
| 9612 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 9613 | // a more specific diagnostic. |
| 9614 | SourceRange CondRange; |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9615 | Expr *Cond = nullptr; |
| 9616 | if (isEnableIf(QualifierLoc, II, CondRange, Cond)) { |
| 9617 | // If we have a condition, narrow it down to the specific failed |
| 9618 | // condition. |
| 9619 | if (Cond) { |
| 9620 | Expr *FailedCond; |
| 9621 | std::string FailedDescription; |
| 9622 | std::tie(FailedCond, FailedDescription) = |
Douglas Gregor | 672281a | 2017-09-14 23:38:42 +0000 | [diff] [blame] | 9623 | findFailedBooleanCondition(Cond, /*AllowTopLevelCond=*/true); |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9624 | |
| 9625 | Diag(FailedCond->getExprLoc(), |
| 9626 | diag::err_typename_nested_not_found_requirement) |
| 9627 | << FailedDescription |
| 9628 | << FailedCond->getSourceRange(); |
| 9629 | return QualType(); |
| 9630 | } |
| 9631 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9632 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9633 | << Ctx << CondRange; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9634 | return QualType(); |
| 9635 | } |
| 9636 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 9637 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9638 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9639 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9640 | |
| 9641 | case LookupResult::FoundUnresolvedValue: { |
| 9642 | // We found a using declaration that is a value. Most likely, the using |
| 9643 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9644 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9645 | IILoc); |
| 9646 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 9647 | << Name << Ctx << FullRange; |
| 9648 | if (UnresolvedUsingValueDecl *Using |
| 9649 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 9650 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9651 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 9652 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 9653 | } |
| 9654 | } |
| 9655 | // Fall through to create a dependent typename type, from which we can recover |
| 9656 | // better. |
Galina Kistanova | 3779cb3 | 2017-06-07 06:25:05 +0000 | [diff] [blame] | 9657 | LLVM_FALLTHROUGH; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9658 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 9659 | case LookupResult::NotFoundInCurrentInstantiation: |
| 9660 | // Okay, it's a member of an unknown instantiation. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9661 | return Context.getDependentNameType(Keyword, |
| 9662 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9663 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9664 | |
| 9665 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9666 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9667 | // C++ [class.qual]p2: |
| 9668 | // In a lookup in which function names are not ignored and the |
| 9669 | // nested-name-specifier nominates a class C, if the name specified |
| 9670 | // after the nested-name-specifier, when looked up in C, is the |
| 9671 | // injected-class-name of C [...] then the name is instead considered |
| 9672 | // to name the constructor of class C. |
| 9673 | // |
| 9674 | // Unlike in an elaborated-type-specifier, function names are not ignored |
| 9675 | // in typename-specifier lookup. However, they are ignored in all the |
| 9676 | // contexts where we form a typename type with no keyword (that is, in |
| 9677 | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). |
| 9678 | // |
| 9679 | // FIXME: That's not strictly true: mem-initializer-id lookup does not |
| 9680 | // ignore functions, but that appears to be an oversight. |
| 9681 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
| 9682 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
| 9683 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
| 9684 | FoundRD->isInjectedClassName() && |
| 9685 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
| 9686 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9687 | << &II << 1 << 0 /*'typename' keyword used*/; |
| 9688 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9689 | // We found a type. Build an ElaboratedType, since the |
| 9690 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 9691 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9692 | return Context.getElaboratedType(Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9693 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9694 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9695 | } |
| 9696 | |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9697 | // C++ [dcl.type.simple]p2: |
| 9698 | // A type-specifier of the form |
| 9699 | // typename[opt] nested-name-specifier[opt] template-name |
| 9700 | // is a placeholder for a deduced class type [...]. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 9701 | if (getLangOpts().CPlusPlus17) { |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9702 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
| 9703 | return Context.getElaboratedType( |
| 9704 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
| 9705 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
| 9706 | QualType(), false)); |
| 9707 | } |
| 9708 | } |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 9709 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9710 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 9711 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9712 | break; |
| 9713 | |
| 9714 | case LookupResult::FoundOverloaded: |
| 9715 | DiagID = diag::err_typename_nested_not_type; |
| 9716 | Referenced = *Result.begin(); |
| 9717 | break; |
| 9718 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 9719 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9720 | return QualType(); |
| 9721 | } |
| 9722 | |
| 9723 | // If we get here, it's because name lookup did not find a |
| 9724 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9725 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9726 | IILoc); |
| 9727 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9728 | if (Referenced) |
| 9729 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 9730 | << Name; |
| 9731 | return QualType(); |
| 9732 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9733 | |
| 9734 | namespace { |
| 9735 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 9736 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9737 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9738 | SourceLocation Loc; |
| 9739 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9740 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9741 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 9742 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9743 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9744 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9745 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9746 | DeclarationName Entity) |
| 9747 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9748 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9749 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9750 | /// Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9751 | /// transformed. |
| 9752 | /// |
| 9753 | /// For the purposes of type reconstruction, a type has already been |
| 9754 | /// transformed if it is NULL or if it is not dependent. |
| 9755 | bool AlreadyTransformed(QualType T) { |
| 9756 | return T.isNull() || !T->isDependentType(); |
| 9757 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9758 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9759 | /// Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9760 | /// rebuilt. |
| 9761 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9762 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9763 | /// Returns the name of the entity whose type is being rebuilt. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9764 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9765 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9766 | /// Sets the "base" location and entity when that |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 9767 | /// information is known based on another transformation. |
| 9768 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 9769 | this->Loc = Loc; |
| 9770 | this->Entity = Entity; |
| 9771 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9772 | |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 9773 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 9774 | // Lambdas never need to be transformed. |
| 9775 | return E; |
| 9776 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9777 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 9778 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9779 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9780 | /// Rebuilds a type within the context of the current instantiation. |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9781 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9782 | /// 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] | 9783 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9784 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9785 | /// partial specialization thereof). This routine will rebuild that type now |
| 9786 | /// that we have entered the declarator's scope, which may produce different |
| 9787 | /// canonical types, e.g., |
| 9788 | /// |
| 9789 | /// \code |
| 9790 | /// template<typename T> |
| 9791 | /// struct X { |
| 9792 | /// typedef T* pointer; |
| 9793 | /// pointer data(); |
| 9794 | /// }; |
| 9795 | /// |
| 9796 | /// template<typename T> |
| 9797 | /// typename X<T>::pointer X<T>::data() { ... } |
| 9798 | /// \endcode |
| 9799 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 9800 | /// 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] | 9801 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 9802 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9803 | /// 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] | 9804 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 9805 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9806 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 9807 | SourceLocation Loc, |
| 9808 | DeclarationName Name) { |
| 9809 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9810 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9811 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9812 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 9813 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 9814 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9815 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9816 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9817 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 9818 | DeclarationName()); |
| 9819 | return Rebuilder.TransformExpr(E); |
| 9820 | } |
| 9821 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9822 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9823 | if (SS.isInvalid()) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9824 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9825 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9826 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9827 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 9828 | DeclarationName()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9829 | NestedNameSpecifierLoc Rebuilt |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9830 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9831 | if (!Rebuilt) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9832 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9833 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9834 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9835 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9836 | } |
| 9837 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9838 | /// 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] | 9839 | /// instantiation. |
| 9840 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 9841 | TemplateParameterList *Params) { |
| 9842 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 9843 | Decl *Param = Params->getParam(I); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9844 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9845 | // There is nothing to rebuild in a type parameter. |
| 9846 | if (isa<TemplateTypeParmDecl>(Param)) |
| 9847 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9848 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9849 | // Rebuild the template parameter list of a template template parameter. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9850 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9851 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 9852 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 9853 | TTP->getTemplateParameters())) |
| 9854 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9855 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9856 | continue; |
| 9857 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9858 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9859 | // Rebuild the type of a non-type template parameter. |
| 9860 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9861 | TypeSourceInfo *NewTSI |
| 9862 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 9863 | NTTP->getLocation(), |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9864 | NTTP->getDeclName()); |
| 9865 | if (!NewTSI) |
| 9866 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9867 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9868 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 9869 | NTTP->setTypeSourceInfo(NewTSI); |
| 9870 | NTTP->setType(NewTSI->getType()); |
| 9871 | } |
| 9872 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9873 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9874 | return false; |
| 9875 | } |
| 9876 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9877 | /// Produces a formatted string that describes the binding of |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9878 | /// template parameters to template arguments. |
| 9879 | std::string |
| 9880 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9881 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9882 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9883 | } |
| 9884 | |
| 9885 | std::string |
| 9886 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9887 | const TemplateArgument *Args, |
| 9888 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9889 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9890 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9891 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9892 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9893 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9894 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9895 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9896 | if (I >= NumArgs) |
| 9897 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9898 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9899 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9900 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9901 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9902 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9903 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9904 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9905 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9906 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9907 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9908 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9909 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9910 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 9911 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9912 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9913 | |
| 9914 | Out << ']'; |
| 9915 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9916 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9917 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9918 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 9919 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9920 | if (!FD) |
| 9921 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9922 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9923 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9924 | |
| 9925 | // Take tokens to avoid allocations |
| 9926 | LPT->Toks.swap(Toks); |
| 9927 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9928 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9929 | |
| 9930 | FD->setLateTemplateParsed(true); |
| 9931 | } |
| 9932 | |
| 9933 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 9934 | if (!FD) |
| 9935 | return; |
| 9936 | FD->setLateTemplateParsed(false); |
| 9937 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9938 | |
| 9939 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 9940 | DeclContext *DC = CurContext; |
| 9941 | |
| 9942 | while (DC) { |
| 9943 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 9944 | const FunctionDecl *FD = RD->isLocalClass(); |
| 9945 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 9946 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 9947 | return false; |
| 9948 | |
| 9949 | DC = DC->getParent(); |
| 9950 | } |
| 9951 | return false; |
| 9952 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9953 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 9954 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9955 | /// Walk the path from which a declaration was instantiated, and check |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9956 | /// that every explicit specialization along that path is visible. This enforces |
| 9957 | /// C++ [temp.expl.spec]/6: |
| 9958 | /// |
| 9959 | /// If a template, a member template or a member of a class template is |
| 9960 | /// explicitly specialized then that specialization shall be declared before |
| 9961 | /// the first use of that specialization that would cause an implicit |
| 9962 | /// instantiation to take place, in every translation unit in which such a |
| 9963 | /// use occurs; no diagnostic is required. |
| 9964 | /// |
| 9965 | /// and also C++ [temp.class.spec]/1: |
| 9966 | /// |
| 9967 | /// A partial specialization shall be declared before the first use of a |
| 9968 | /// class template specialization that would make use of the partial |
| 9969 | /// specialization as the result of an implicit or explicit instantiation |
| 9970 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 9971 | /// required. |
| 9972 | class ExplicitSpecializationVisibilityChecker { |
| 9973 | Sema &S; |
| 9974 | SourceLocation Loc; |
| 9975 | llvm::SmallVector<Module *, 8> Modules; |
| 9976 | |
| 9977 | public: |
| 9978 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 9979 | : S(S), Loc(Loc) {} |
| 9980 | |
| 9981 | void check(NamedDecl *ND) { |
| 9982 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 9983 | return checkImpl(FD); |
| 9984 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 9985 | return checkImpl(RD); |
| 9986 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 9987 | return checkImpl(VD); |
| 9988 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 9989 | return checkImpl(ED); |
| 9990 | } |
| 9991 | |
| 9992 | private: |
| 9993 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 9994 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 9995 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 9996 | const bool Recover = true; |
| 9997 | |
| 9998 | // If we got a custom set of modules (because only a subset of the |
| 9999 | // declarations are interesting), use them, otherwise let |
| 10000 | // diagnoseMissingImport intelligently pick some. |
| 10001 | if (Modules.empty()) |
| 10002 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 10003 | else |
| 10004 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 10005 | } |
| 10006 | |
| 10007 | // Check a specific declaration. There are three problematic cases: |
| 10008 | // |
| 10009 | // 1) The declaration is an explicit specialization of a template |
| 10010 | // specialization. |
| 10011 | // 2) The declaration is an explicit specialization of a member of an |
| 10012 | // templated class. |
| 10013 | // 3) The declaration is an instantiation of a template, and that template |
| 10014 | // is an explicit specialization of a member of a templated class. |
| 10015 | // |
| 10016 | // We don't need to go any deeper than that, as the instantiation of the |
| 10017 | // surrounding class / etc is not triggered by whatever triggered this |
| 10018 | // instantiation, and thus should be checked elsewhere. |
| 10019 | template<typename SpecDecl> |
| 10020 | void checkImpl(SpecDecl *Spec) { |
| 10021 | bool IsHiddenExplicitSpecialization = false; |
| 10022 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 10023 | IsHiddenExplicitSpecialization = |
| 10024 | Spec->getMemberSpecializationInfo() |
| 10025 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 10026 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10027 | } else { |
| 10028 | checkInstantiated(Spec); |
| 10029 | } |
| 10030 | |
| 10031 | if (IsHiddenExplicitSpecialization) |
| 10032 | diagnose(Spec->getMostRecentDecl(), false); |
| 10033 | } |
| 10034 | |
| 10035 | void checkInstantiated(FunctionDecl *FD) { |
| 10036 | if (auto *TD = FD->getPrimaryTemplate()) |
| 10037 | checkTemplate(TD); |
| 10038 | } |
| 10039 | |
| 10040 | void checkInstantiated(CXXRecordDecl *RD) { |
| 10041 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 10042 | if (!SD) |
| 10043 | return; |
| 10044 | |
| 10045 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10046 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 10047 | checkTemplate(TD); |
| 10048 | else if (auto *TD = |
| 10049 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 10050 | if (!S.hasVisibleDeclaration(TD)) |
| 10051 | diagnose(TD, true); |
| 10052 | checkTemplate(TD); |
| 10053 | } |
| 10054 | } |
| 10055 | |
| 10056 | void checkInstantiated(VarDecl *RD) { |
| 10057 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 10058 | if (!SD) |
| 10059 | return; |
| 10060 | |
| 10061 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10062 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 10063 | checkTemplate(TD); |
| 10064 | else if (auto *TD = |
| 10065 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 10066 | if (!S.hasVisibleDeclaration(TD)) |
| 10067 | diagnose(TD, true); |
| 10068 | checkTemplate(TD); |
| 10069 | } |
| 10070 | } |
| 10071 | |
| 10072 | void checkInstantiated(EnumDecl *FD) {} |
| 10073 | |
| 10074 | template<typename TemplDecl> |
| 10075 | void checkTemplate(TemplDecl *TD) { |
| 10076 | if (TD->isMemberSpecialization()) { |
| 10077 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 10078 | diagnose(TD->getMostRecentDecl(), false); |
| 10079 | } |
| 10080 | } |
| 10081 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 10082 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10083 | |
| 10084 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 10085 | if (!getLangOpts().Modules) |
| 10086 | return; |
| 10087 | |
| 10088 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 10089 | } |
| 10090 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10091 | /// Check whether a template partial specialization that we've discovered |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 10092 | /// is hidden, and produce suitable diagnostics if so. |
| 10093 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 10094 | NamedDecl *Spec) { |
| 10095 | llvm::SmallVector<Module *, 8> Modules; |
| 10096 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 10097 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 10098 | MissingImportKind::PartialSpecialization, |
| 10099 | /*Recover*/true); |
| 10100 | } |