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 { |
| 49 | /// \brief [temp.constr.decl]p2: A template's associated constraints are |
| 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 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 68 | /// \brief Determine whether the declaration found is acceptable as the name |
| 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 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 108 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 111 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 112 | bool AllowFunctionTemplates) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 113 | // The set of class templates we've already seen. |
| 114 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 115 | LookupResult::Filter filter = R.makeFilter(); |
| 116 | while (filter.hasNext()) { |
| 117 | NamedDecl *Orig = filter.next(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 118 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 119 | AllowFunctionTemplates); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 120 | if (!Repl) |
| 121 | filter.erase(); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 122 | else if (Repl != Orig) { |
| 123 | |
| 124 | // C++ [temp.local]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 125 | // 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] | 126 | // 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] | 127 | // one base class). If all of the injected-class-names that are found |
| 128 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 129 | // is used as a template-name, the reference refers to the class |
| 130 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 131 | // ambiguous. |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 132 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 133 | if (!ClassTemplates.insert(ClassTmpl).second) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 134 | filter.erase(); |
| 135 | continue; |
| 136 | } |
John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 137 | |
| 138 | // FIXME: we promote access to public here as a workaround to |
| 139 | // the fact that LookupResult doesn't let us remember that we |
| 140 | // found this template through a particular injected class name, |
| 141 | // which means we end up doing nasty things to the invariants. |
| 142 | // Pretending that access is public is *much* safer. |
| 143 | filter.replace(Repl, AS_public); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 144 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 145 | } |
| 146 | filter.done(); |
| 147 | } |
| 148 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 149 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 150 | bool AllowFunctionTemplates) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 151 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 152 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 153 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 154 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 155 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 158 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 159 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 160 | bool hasTemplateKeyword, |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 161 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 162 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 163 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 164 | TemplateTy &TemplateResult, |
| 165 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 166 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 167 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 168 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 169 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 170 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 171 | switch (Name.getKind()) { |
| 172 | case UnqualifiedId::IK_Identifier: |
| 173 | TName = DeclarationName(Name.Identifier); |
| 174 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 176 | case UnqualifiedId::IK_OperatorFunctionId: |
| 177 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 178 | Name.OperatorFunctionId.Operator); |
| 179 | break; |
| 180 | |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 181 | case UnqualifiedId::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 182 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 183 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 184 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 185 | default: |
| 186 | return TNK_Non_template; |
| 187 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 189 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 191 | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 192 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 193 | MemberOfUnknownSpecialization); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 194 | if (R.empty()) return TNK_Non_template; |
| 195 | if (R.isAmbiguous()) { |
| 196 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 197 | R.suppressDiagnostics(); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 198 | |
| 199 | // FIXME: we might have ambiguous templates, in which case we |
| 200 | // should at least parse them properly! |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 201 | return TNK_Non_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 202 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 203 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 204 | TemplateName Template; |
| 205 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 207 | unsigned ResultCount = R.end() - R.begin(); |
| 208 | if (ResultCount > 1) { |
| 209 | // We assume that we'll preserve the qualifier from a function |
| 210 | // template name in other ways. |
| 211 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 212 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 213 | |
| 214 | // We'll do this lookup again later. |
| 215 | R.suppressDiagnostics(); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 216 | } else { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 217 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 218 | |
| 219 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 220 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 221 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 222 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 223 | } else { |
| 224 | Template = TemplateName(TD); |
| 225 | } |
| 226 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 227 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 228 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 229 | |
| 230 | // We'll do this lookup again later. |
| 231 | R.suppressDiagnostics(); |
| 232 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 233 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 234 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
| 235 | isa<BuiltinTemplateDecl>(TD)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 236 | TemplateKind = |
| 237 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 238 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 239 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 241 | TemplateResult = TemplateTy::make(Template); |
| 242 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Richard Smith | 278890f | 2017-02-10 20:39:58 +0000 | [diff] [blame] | 245 | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, |
| 246 | SourceLocation NameLoc, |
| 247 | ParsedTemplateTy *Template) { |
| 248 | CXXScopeSpec SS; |
| 249 | bool MemberOfUnknownSpecialization = false; |
| 250 | |
| 251 | // We could use redeclaration lookup here, but we don't need to: the |
| 252 | // syntactic form of a deduction guide is enough to identify it even |
| 253 | // if we can't look up the template name at all. |
| 254 | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); |
| 255 | LookupTemplateName(R, S, SS, /*ObjectType*/QualType(), |
| 256 | /*EnteringContext*/false, MemberOfUnknownSpecialization); |
| 257 | |
| 258 | if (R.empty()) return false; |
| 259 | if (R.isAmbiguous()) { |
| 260 | // FIXME: Diagnose an ambiguity if we find at least one template. |
| 261 | R.suppressDiagnostics(); |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | // We only treat template-names that name type templates as valid deduction |
| 266 | // guide names. |
| 267 | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); |
| 268 | if (!TD || !getAsTypeTemplateDecl(TD)) |
| 269 | return false; |
| 270 | |
| 271 | if (Template) |
| 272 | *Template = TemplateTy::make(TemplateName(TD)); |
| 273 | return true; |
| 274 | } |
| 275 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 276 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 277 | SourceLocation IILoc, |
| 278 | Scope *S, |
| 279 | const CXXScopeSpec *SS, |
| 280 | TemplateTy &SuggestedTemplate, |
| 281 | TemplateNameKind &SuggestedKind) { |
| 282 | // We can't recover unless there's a dependent scope specifier preceding the |
| 283 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 284 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 285 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 286 | computeDeclContext(*SS)) |
| 287 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 289 | // The code is missing a 'template' keyword prior to the dependent template |
| 290 | // name. |
| 291 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 292 | Diag(IILoc, diag::err_template_kw_missing) |
| 293 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 294 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 295 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 296 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 297 | SuggestedKind = TNK_Dependent_template_name; |
| 298 | return true; |
| 299 | } |
| 300 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 301 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 302 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 303 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 304 | bool EnteringContext, |
| 305 | bool &MemberOfUnknownSpecialization) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 306 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 307 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 308 | DeclContext *LookupCtx = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 309 | bool isDependent = false; |
| 310 | if (!ObjectType.isNull()) { |
| 311 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 312 | // x->B::f, and we are looking into the type of the object. |
| 313 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 314 | LookupCtx = computeDeclContext(ObjectType); |
| 315 | isDependent = ObjectType->isDependentType(); |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 316 | assert((isDependent || !ObjectType->isIncompleteType() || |
| 317 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 318 | "Caller should have completed object type"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 319 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 320 | // Template names cannot appear inside an Objective-C class or object type. |
| 321 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 322 | Found.clear(); |
| 323 | return; |
| 324 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 325 | } else if (SS.isSet()) { |
| 326 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 327 | // so long into the context associated with the prior nested-name-specifier. |
| 328 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 329 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 330 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 331 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 332 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
| 336 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 337 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 338 | if (LookupCtx) { |
| 339 | // Perform "qualified" name lookup into the declaration context we |
| 340 | // computed, which is either the type of the base of a member access |
| 341 | // expression or the declaration context associated with a prior |
| 342 | // nested-name-specifier. |
| 343 | LookupQualifiedName(Found, LookupCtx); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 344 | if (!ObjectType.isNull() && Found.empty()) { |
| 345 | // C++ [basic.lookup.classref]p1: |
| 346 | // In a class member access expression (5.2.5), if the . or -> token is |
| 347 | // immediately followed by an identifier followed by a <, the |
| 348 | // identifier must be looked up to determine whether the < is the |
| 349 | // beginning of a template argument list (14.2) or a less-than operator. |
| 350 | // The identifier is first looked up in the class of the object |
| 351 | // expression. If the identifier is not found, it is then looked up in |
| 352 | // the context of the entire postfix-expression and shall name a class |
| 353 | // or function template. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 354 | if (S) LookupName(Found, S); |
| 355 | ObjectTypeSearchedInScope = true; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 356 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 357 | } |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 358 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 359 | // We cannot look into a dependent object type or nested nme |
| 360 | // specifier. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 361 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 362 | return; |
| 363 | } else { |
| 364 | // Perform unqualified name lookup in the current scope. |
| 365 | LookupName(Found, S); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 366 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 367 | if (!ObjectType.isNull()) |
| 368 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 371 | if (Found.empty() && !isDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 372 | // If we did not find any names, attempt to correct any typos. |
| 373 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 374 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 375 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 376 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 377 | FilterCCC->WantTypeSpecifiers = false; |
| 378 | FilterCCC->WantExpressionKeywords = false; |
| 379 | FilterCCC->WantRemainingKeywords = false; |
| 380 | FilterCCC->WantCXXNamedCasts = true; |
| 381 | if (TypoCorrection Corrected = CorrectTypo( |
| 382 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 383 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 384 | Found.setLookupName(Corrected.getCorrection()); |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 385 | if (auto *ND = Corrected.getFoundDecl()) |
| 386 | Found.addDecl(ND); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 387 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 388 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 389 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 390 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 391 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 392 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 393 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 394 | << Name << LookupCtx << DroppedSpecifier |
| 395 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 396 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 397 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 398 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 399 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 400 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 401 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 405 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 406 | if (Found.empty()) { |
| 407 | if (isDependent) |
| 408 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 409 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 410 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 411 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 412 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 413 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 414 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 415 | // [...] If the lookup in the class of the object expression finds a |
| 416 | // template, the name is also looked up in the context of the entire |
| 417 | // postfix-expression and [...] |
| 418 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 419 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 420 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 421 | LookupOrdinaryName); |
| 422 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 423 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 424 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 425 | if (FoundOuter.empty()) { |
| 426 | // - if the name is not found, the name found in the class of the |
| 427 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 428 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 429 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 430 | // - if the name is found in the context of the entire |
| 431 | // postfix-expression and does not name a class template, the name |
| 432 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 433 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 434 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 435 | // - if the name found is a class template, it must refer to the same |
| 436 | // entity as the one found in the class of the object expression, |
| 437 | // otherwise the program is ill-formed. |
| 438 | if (!Found.isSingleResult() || |
| 439 | Found.getFoundDecl()->getCanonicalDecl() |
| 440 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 441 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 442 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 443 | << Found.getLookupName() |
| 444 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 445 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 446 | diag::note_ambig_member_ref_object_type) |
| 447 | << ObjectType; |
| 448 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 449 | diag::note_ambig_member_ref_scope); |
| 450 | |
| 451 | // Recover by taking the template that we found in the object |
| 452 | // expression's type. |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 458 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 459 | /// was just parsed. This is only possible with an explicit scope |
| 460 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 461 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 462 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 463 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 464 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 465 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 466 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 467 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 468 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 469 | // C++11 [expr.prim.general]p12: |
| 470 | // An id-expression that denotes a non-static data member or non-static |
| 471 | // member function of a class can only be used: |
| 472 | // (...) |
| 473 | // - if that id-expression denotes a non-static data member and it |
| 474 | // appears in an unevaluated operand. |
| 475 | // |
| 476 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 477 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 478 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 479 | // always a MemberExpr. |
| 480 | bool MightBeCxx11UnevalField = |
| 481 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 482 | |
Akira Hatanaka | d644e02 | 2016-12-16 03:19:41 +0000 | [diff] [blame] | 483 | // Check if the nested name specifier is an enum type. |
| 484 | bool IsEnum = false; |
| 485 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
| 486 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
| 487 | |
| 488 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 489 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 490 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 491 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 492 | // Since the 'this' expression is synthesized, we don't need to |
| 493 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 494 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 495 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 496 | return CXXDependentScopeMemberExpr::Create( |
| 497 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 498 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 499 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 502 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 503 | } |
| 504 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 505 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 506 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 507 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 508 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 509 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 510 | return DependentScopeDeclRefExpr::Create( |
| 511 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 512 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 515 | |
| 516 | /// Determine whether we would be unable to instantiate this template (because |
| 517 | /// it either has no definition, or is in the process of being instantiated). |
| 518 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 519 | NamedDecl *Instantiation, |
| 520 | bool InstantiatedFromMember, |
| 521 | const NamedDecl *Pattern, |
| 522 | const NamedDecl *PatternDef, |
| 523 | TemplateSpecializationKind TSK, |
| 524 | bool Complain /*= true*/) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 525 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 526 | isa<VarDecl>(Instantiation)); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 527 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 528 | bool IsEntityBeingDefined = false; |
| 529 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 530 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 531 | |
| 532 | if (PatternDef && !IsEntityBeingDefined) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 533 | NamedDecl *SuggestedDef = nullptr; |
| 534 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 535 | /*OnlyNeedComplete*/false)) { |
| 536 | // If we're allowed to diagnose this and recover, do so. |
| 537 | bool Recover = Complain && !isSFINAEContext(); |
| 538 | if (Complain) |
| 539 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 540 | Sema::MissingImportKind::Definition, Recover); |
| 541 | return !Recover; |
| 542 | } |
| 543 | return false; |
| 544 | } |
| 545 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 546 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 547 | return true; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 548 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 549 | llvm::Optional<unsigned> Note; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 550 | QualType InstantiationTy; |
| 551 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 552 | InstantiationTy = Context.getTypeDeclType(TD); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 553 | if (PatternDef) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 554 | Diag(PointOfInstantiation, |
| 555 | diag::err_template_instantiate_within_definition) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 556 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 557 | << InstantiationTy; |
| 558 | // Not much point in noting the template declaration here, since |
| 559 | // we're lexically inside it. |
| 560 | Instantiation->setInvalidDecl(); |
| 561 | } else if (InstantiatedFromMember) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 562 | if (isa<FunctionDecl>(Instantiation)) { |
| 563 | Diag(PointOfInstantiation, |
| 564 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 565 | << /*member function*/ 1 << Instantiation->getDeclName() |
| 566 | << Instantiation->getDeclContext(); |
| 567 | Note = diag::note_explicit_instantiation_here; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 568 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 569 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 570 | Diag(PointOfInstantiation, |
| 571 | diag::err_implicit_instantiate_member_undefined) |
| 572 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 573 | Note = diag::note_member_declared_at; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 574 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 575 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 576 | if (isa<FunctionDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 577 | Diag(PointOfInstantiation, |
| 578 | diag::err_explicit_instantiation_undefined_func_template) |
| 579 | << Pattern; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 580 | Note = diag::note_explicit_instantiation_here; |
| 581 | } else if (isa<TagDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 582 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 583 | << (TSK != TSK_ImplicitInstantiation) |
| 584 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 585 | Note = diag::note_template_decl_here; |
| 586 | } else { |
| 587 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 588 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 589 | Diag(PointOfInstantiation, |
| 590 | diag::err_explicit_instantiation_undefined_var_template) |
| 591 | << Instantiation; |
| 592 | Instantiation->setInvalidDecl(); |
| 593 | } else |
| 594 | Diag(PointOfInstantiation, |
| 595 | diag::err_explicit_instantiation_undefined_member) |
| 596 | << /*static data member*/ 2 << Instantiation->getDeclName() |
| 597 | << Instantiation->getDeclContext(); |
| 598 | Note = diag::note_explicit_instantiation_here; |
| 599 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 600 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 601 | if (Note) // Diagnostics were emitted. |
| 602 | Diag(Pattern->getLocation(), Note.getValue()); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 603 | |
| 604 | // In general, Instantiation isn't marked invalid to get more than one |
| 605 | // error for multiple undefined instantiations. But the code that does |
| 606 | // explicit declaration -> explicit definition conversion can't handle |
| 607 | // invalid declarations, so mark as invalid in that case. |
| 608 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 609 | Instantiation->setInvalidDecl(); |
| 610 | return true; |
| 611 | } |
| 612 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 613 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 614 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 615 | /// declaration at location Loc. Returns true to indicate that this is |
| 616 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 617 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 618 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 619 | |
| 620 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 621 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 622 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 623 | |
| 624 | // C++ [temp.local]p4: |
| 625 | // A template-parameter shall not be redeclared within its |
| 626 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 628 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 629 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 632 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 633 | /// the parameter D to reference the templated declaration and return a pointer |
| 634 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 635 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 636 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 637 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 638 | return Temp; |
| 639 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 640 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 643 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 644 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 645 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 646 | "Only template template arguments can be pack expansions here"); |
| 647 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 648 | "Template template argument pack expansion without packs"); |
| 649 | ParsedTemplateArgument Result(*this); |
| 650 | Result.EllipsisLoc = EllipsisLoc; |
| 651 | return Result; |
| 652 | } |
| 653 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 654 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 655 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 657 | switch (Arg.getKind()) { |
| 658 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 659 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 660 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 661 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 662 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 663 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 664 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 666 | case ParsedTemplateArgument::NonType: { |
| 667 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 668 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 669 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 671 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 672 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 673 | TemplateArgument TArg; |
| 674 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 675 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 676 | else |
| 677 | TArg = Template; |
| 678 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 679 | Arg.getScopeSpec().getWithLocInContext( |
| 680 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 681 | Arg.getLocation(), |
| 682 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 685 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 686 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 687 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 688 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 689 | /// \brief Translates template arguments as provided by the parser |
| 690 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 691 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 692 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 693 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 694 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 695 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 696 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 697 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 698 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 699 | SourceLocation Loc, |
| 700 | IdentifierInfo *Name) { |
| 701 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 702 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 703 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 704 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 705 | } |
| 706 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 707 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 708 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 709 | /// the keyword "typename" was used to declare the type parameter |
| 710 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 711 | /// "class" or "typename" keyword. ParamName is the name of the |
| 712 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 713 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 714 | /// If the type parameter has a default argument, it will be added |
| 715 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 716 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 717 | SourceLocation EllipsisLoc, |
| 718 | SourceLocation KeyLoc, |
| 719 | IdentifierInfo *ParamName, |
| 720 | SourceLocation ParamNameLoc, |
| 721 | unsigned Depth, unsigned Position, |
| 722 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 723 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | assert(S->isTemplateParamScope() && |
| 725 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 727 | SourceLocation Loc = ParamNameLoc; |
| 728 | if (!ParamName) |
| 729 | Loc = KeyLoc; |
| 730 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 731 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 732 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 733 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 734 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 735 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 736 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 737 | |
| 738 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 739 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 740 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 741 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 742 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 743 | IdResolver.AddDecl(Param); |
| 744 | } |
| 745 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 746 | // C++0x [temp.param]p9: |
| 747 | // A default template-argument may be specified for any kind of |
| 748 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 749 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 750 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 751 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 754 | // Handle the default argument, if provided. |
| 755 | if (DefaultArg) { |
| 756 | TypeSourceInfo *DefaultTInfo; |
| 757 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 758 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 759 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 760 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 761 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 762 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 763 | UPPC_DefaultArgument)) |
| 764 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 765 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 766 | // Check the template argument itself. |
| 767 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 768 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 769 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 770 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 771 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 772 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 773 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 774 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 775 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 778 | /// \brief Check that the type of a non-type template parameter is |
| 779 | /// well-formed. |
| 780 | /// |
| 781 | /// \returns the (possibly-promoted) parameter type if valid; |
| 782 | /// otherwise, produces a diagnostic and returns a NULL type. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 783 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
| 784 | SourceLocation Loc) { |
| 785 | if (TSI->getType()->isUndeducedType()) { |
| 786 | // C++1z [temp.dep.expr]p3: |
| 787 | // An id-expression is type-dependent if it contains |
| 788 | // - an identifier associated by name lookup with a non-type |
| 789 | // template-parameter declared with a type that contains a |
| 790 | // placeholder type (7.1.7.4), |
| 791 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
| 792 | } |
| 793 | |
| 794 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
| 795 | } |
| 796 | |
| 797 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
| 798 | SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 799 | // We don't allow variably-modified types as the type of non-type template |
| 800 | // parameters. |
| 801 | if (T->isVariablyModifiedType()) { |
| 802 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 803 | << T; |
| 804 | return QualType(); |
| 805 | } |
| 806 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 807 | // C++ [temp.param]p4: |
| 808 | // |
| 809 | // A non-type template-parameter shall have one of the following |
| 810 | // (optionally cv-qualified) types: |
| 811 | // |
| 812 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 813 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 814 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 815 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 817 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 818 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 819 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 820 | // -- std::nullptr_t. |
| 821 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 822 | // If T is a dependent type, we can't do the check now, so we |
| 823 | // assume that it is well-formed. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 824 | T->isDependentType() || |
| 825 | // Allow use of auto in template parameter declarations. |
| 826 | T->isUndeducedType()) { |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 827 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 828 | // are ignored when determining its type. |
| 829 | return T.getUnqualifiedType(); |
| 830 | } |
| 831 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 832 | // C++ [temp.param]p8: |
| 833 | // |
| 834 | // A non-type template-parameter of type "array of T" or |
| 835 | // "function returning T" is adjusted to be of type "pointer to |
| 836 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 837 | else if (T->isArrayType() || T->isFunctionType()) |
| 838 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 840 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 841 | << T; |
| 842 | |
| 843 | return QualType(); |
| 844 | } |
| 845 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 846 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 847 | unsigned Depth, |
| 848 | unsigned Position, |
| 849 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 850 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 851 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 852 | |
| 853 | if (TInfo->getType()->isUndeducedType()) { |
| 854 | Diag(D.getIdentifierLoc(), |
| 855 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 856 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
| 857 | } |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 858 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 859 | assert(S->isTemplateParamScope() && |
| 860 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 861 | bool Invalid = false; |
| 862 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 863 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 864 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 865 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 866 | Invalid = true; |
| 867 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 868 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 869 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 870 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 871 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 872 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 873 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 874 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 875 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 876 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 877 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 879 | if (Invalid) |
| 880 | Param->setInvalidDecl(); |
| 881 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 882 | if (ParamName) { |
| 883 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 884 | ParamName); |
| 885 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 886 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 887 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 888 | IdResolver.AddDecl(Param); |
| 889 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 890 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 891 | // C++0x [temp.param]p9: |
| 892 | // A default template-argument may be specified for any kind of |
| 893 | // template-parameter that is not a template parameter pack. |
| 894 | if (Default && IsParameterPack) { |
| 895 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 896 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 899 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 900 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 901 | // Check for unexpanded parameter packs. |
| 902 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 903 | return Param; |
| 904 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 905 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 906 | ExprResult DefaultRes = |
| 907 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 908 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 909 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 910 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 911 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 912 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 913 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 914 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 915 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 917 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 918 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 919 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 920 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 921 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 922 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 923 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 924 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 925 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 926 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 927 | IdentifierInfo *Name, |
| 928 | SourceLocation NameLoc, |
| 929 | unsigned Depth, |
| 930 | unsigned Position, |
| 931 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 932 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 933 | assert(S->isTemplateParamScope() && |
| 934 | "Template template parameter not in template parameter scope!"); |
| 935 | |
| 936 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 937 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 938 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 939 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 940 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 941 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 942 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 943 | Param->setAccess(AS_public); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 944 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 945 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 946 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 947 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 948 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 949 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 950 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 951 | IdResolver.AddDecl(Param); |
| 952 | } |
| 953 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 954 | if (Params->size() == 0) { |
| 955 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 956 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 957 | Param->setInvalidDecl(); |
| 958 | } |
| 959 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 960 | // C++0x [temp.param]p9: |
| 961 | // A default template-argument may be specified for any kind of |
| 962 | // template-parameter that is not a template parameter pack. |
| 963 | if (IsParameterPack && !Default.isInvalid()) { |
| 964 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 965 | Default = ParsedTemplateArgument(); |
| 966 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 967 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 968 | if (!Default.isInvalid()) { |
| 969 | // Check only that we have a template template argument. We don't want to |
| 970 | // try to check well-formedness now, because our template template parameter |
| 971 | // might have dependent types in its template parameters, which we wouldn't |
| 972 | // be able to match now. |
| 973 | // |
| 974 | // If none of the template template parameter's template arguments mention |
| 975 | // other template parameters, we could actually perform more checking here. |
| 976 | // However, it isn't worth doing. |
| 977 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 978 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 979 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 980 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 981 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 982 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 983 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 984 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 985 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 986 | DefaultArg.getArgument().getAsTemplate(), |
| 987 | UPPC_DefaultArgument)) |
| 988 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 989 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 990 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 991 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 992 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 993 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 996 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 997 | /// constrained by RequiresClause, that contains the template parameters in |
| 998 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 999 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1000 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 1001 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1003 | SourceLocation LAngleLoc, |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 1004 | ArrayRef<Decl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1005 | SourceLocation RAngleLoc, |
| 1006 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1007 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1008 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1009 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1010 | return TemplateParameterList::Create( |
| 1011 | Context, TemplateLoc, LAngleLoc, |
| 1012 | llvm::makeArrayRef((NamedDecl *const *)Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 1013 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1014 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 1015 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1016 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 1017 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1018 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1021 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1022 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1023 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1024 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 1025 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1026 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 1027 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1028 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1029 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1030 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1031 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1033 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1034 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1035 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1036 | |
| 1037 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1038 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1039 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1040 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1041 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 1042 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1043 | |
| 1044 | // There is no such thing as an unnamed class template. |
| 1045 | if (!Name) { |
| 1046 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1047 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1050 | // Find any previous declaration with this name. For a friend with no |
| 1051 | // scope explicitly specified, we only look for tag declarations (per |
| 1052 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1053 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1054 | LookupResult Previous(*this, Name, NameLoc, |
| 1055 | (SS.isEmpty() && TUK == TUK_Friend) |
| 1056 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1057 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1058 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 1059 | SemanticContext = computeDeclContext(SS, true); |
| 1060 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1061 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 1062 | // in the AST, and historically we have just ignored such friend |
| 1063 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1064 | Diag(NameLoc, TUK == TUK_Friend |
| 1065 | ? diag::warn_template_qualified_friend_ignored |
| 1066 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1067 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1068 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1069 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1071 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 1072 | return true; |
| 1073 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1074 | // If we're adding a template to a dependent context, we may need to |
| 1075 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 1076 | // now that we know what the current instantiation is. |
| 1077 | if (SemanticContext->isDependentContext()) { |
| 1078 | ContextRAII SavedContext(*this, SemanticContext); |
| 1079 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 1080 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1081 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 1082 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1083 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1084 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1085 | } else { |
| 1086 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 1087 | |
| 1088 | // C++14 [class.mem]p14: |
| 1089 | // If T is the name of a class, then each of the following shall have a |
| 1090 | // name different from T: |
| 1091 | // -- every member template of class T |
| 1092 | if (TUK != TUK_Friend && |
| 1093 | DiagnoseClassNameShadow(SemanticContext, |
| 1094 | DeclarationNameInfo(Name, NameLoc))) |
| 1095 | return true; |
| 1096 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1097 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1098 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1100 | if (Previous.isAmbiguous()) |
| 1101 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1102 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1103 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1104 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1105 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1106 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1107 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1108 | // Maybe we will complain about the shadowed template parameter. |
| 1109 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1110 | // Just pretend that we didn't see the previous declaration. |
| 1111 | PrevDecl = nullptr; |
| 1112 | } |
| 1113 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1114 | // If there is a previous declaration with the same name, check |
| 1115 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1117 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1118 | |
| 1119 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1120 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1121 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1122 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1123 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1124 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1125 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1126 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1127 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1128 | PrevClassTemplate |
| 1129 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1130 | ->getSpecializedTemplate(); |
| 1131 | } |
| 1132 | } |
| 1133 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1134 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1135 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1136 | // [...] When looking for a prior declaration of a class or a function |
| 1137 | // 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] | 1138 | // function is neither a qualified name nor a template-id, scopes outside |
| 1139 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1140 | if (!SS.isSet()) { |
| 1141 | DeclContext *OutermostContext = CurContext; |
| 1142 | while (!OutermostContext->isFileContext()) |
| 1143 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1144 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 1145 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1146 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1147 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1148 | SemanticContext = PrevDecl->getDeclContext(); |
| 1149 | } else { |
| 1150 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1151 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1152 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1153 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1154 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1155 | |
| 1156 | // Check that the chosen semantic context doesn't already contain a |
| 1157 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1158 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1159 | DeclContext *LookupContext = SemanticContext; |
| 1160 | while (LookupContext->isTransparentContext()) |
| 1161 | LookupContext = LookupContext->getLookupParent(); |
| 1162 | LookupQualifiedName(Previous, LookupContext); |
| 1163 | |
| 1164 | if (Previous.isAmbiguous()) |
| 1165 | return true; |
| 1166 | |
| 1167 | if (Previous.begin() != Previous.end()) |
| 1168 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1169 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1170 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1171 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1172 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1173 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1174 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1175 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1176 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1177 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1178 | if (SS.isEmpty() && |
| 1179 | !(PrevClassTemplate && |
| 1180 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1181 | SemanticContext->getRedeclContext()))) { |
| 1182 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1183 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1184 | diag::note_using_decl_target); |
| 1185 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1186 | // Recover by ignoring the old declaration. |
| 1187 | PrevDecl = PrevClassTemplate = nullptr; |
| 1188 | } |
| 1189 | } |
| 1190 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1191 | // TODO Memory management; associated constraints are not always stored. |
| 1192 | Expr *const CurAC = formAssociatedConstraints(TemplateParams, nullptr); |
| 1193 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1194 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1195 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1196 | // for a friend in a dependent context: the template parameter list itself |
| 1197 | // could be dependent. |
| 1198 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1199 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1200 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1201 | /*Complain=*/true, |
| 1202 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1203 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1204 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1205 | // Check for matching associated constraints on redeclarations. |
| 1206 | const Expr *const PrevAC = PrevClassTemplate->getAssociatedConstraints(); |
| 1207 | const bool RedeclACMismatch = [&] { |
| 1208 | if (!(CurAC || PrevAC)) |
| 1209 | return false; // Nothing to check; no mismatch. |
| 1210 | if (CurAC && PrevAC) { |
| 1211 | llvm::FoldingSetNodeID CurACInfo, PrevACInfo; |
| 1212 | CurAC->Profile(CurACInfo, Context, /*Canonical=*/true); |
| 1213 | PrevAC->Profile(PrevACInfo, Context, /*Canonical=*/true); |
| 1214 | if (CurACInfo == PrevACInfo) |
| 1215 | return false; // All good; no mismatch. |
| 1216 | } |
| 1217 | return true; |
| 1218 | }(); |
| 1219 | |
| 1220 | if (RedeclACMismatch) { |
| 1221 | Diag(CurAC ? CurAC->getLocStart() : NameLoc, |
| 1222 | diag::err_template_different_associated_constraints); |
| 1223 | Diag(PrevAC ? PrevAC->getLocStart() : PrevClassTemplate->getLocation(), |
| 1224 | diag::note_template_prev_declaration) << /*declaration*/0; |
| 1225 | return true; |
| 1226 | } |
| 1227 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1228 | // C++ [temp.class]p4: |
| 1229 | // In a redeclaration, partial specialization, explicit |
| 1230 | // specialization or explicit instantiation of a class template, |
| 1231 | // the class-key shall agree in kind with the original class |
| 1232 | // template declaration (7.1.5.3). |
| 1233 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1234 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1235 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1237 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1238 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1239 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1240 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1243 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1244 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1245 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1246 | // If we have a prior definition that is not visible, treat this as |
| 1247 | // simply making that previous definition visible. |
| 1248 | NamedDecl *Hidden = nullptr; |
| 1249 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1250 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1251 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1252 | assert(Tmpl && "original definition of a class template is not a " |
| 1253 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1254 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1255 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1256 | return Def; |
| 1257 | } |
| 1258 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1259 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1260 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1261 | // FIXME: Would it make sense to try to "forget" the previous |
| 1262 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1263 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1264 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1265 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1266 | } else if (PrevDecl) { |
| 1267 | // C++ [temp]p5: |
| 1268 | // A class template shall not have the same name as any other |
| 1269 | // template, class, function, object, enumeration, enumerator, |
| 1270 | // namespace, or type in the same scope (3.3), except as specified |
| 1271 | // in (14.5.4). |
| 1272 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1273 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1274 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1277 | // Check the template parameter list of this declaration, possibly |
| 1278 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1279 | // template declaration. Skip this check for a friend in a dependent |
| 1280 | // context, because the template parameter list might be dependent. |
| 1281 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1282 | CheckTemplateParameterList( |
| 1283 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1284 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1285 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1286 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1287 | SemanticContext->isDependentContext()) |
| 1288 | ? TPC_ClassTemplateMember |
| 1289 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1290 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1291 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1292 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1293 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1294 | // 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] | 1295 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1296 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1297 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1298 | : diag::err_member_decl_does_not_match) |
| 1299 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1300 | Invalid = true; |
| 1301 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1304 | // If this is a templated friend in a dependent context we should not put it |
| 1305 | // on the redecl chain. In some cases, the templated friend can be the most |
| 1306 | // recent declaration tricking the template instantiator to make substitutions |
| 1307 | // there. |
| 1308 | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
| 1309 | bool ShouldAddRedecl |
| 1310 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
| 1311 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1313 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1314 | PrevClassTemplate && ShouldAddRedecl ? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1315 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1316 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1317 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1318 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1319 | NewClass->setTemplateParameterListsInfo( |
| 1320 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1321 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1322 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1323 | // Add alignment attributes if necessary; these attributes are checked when |
| 1324 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1325 | if (TUK == TUK_Definition) { |
| 1326 | AddAlignmentAttributesForRecord(NewClass); |
| 1327 | AddMsStructLayoutForRecord(NewClass); |
| 1328 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1329 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1330 | // Attach the associated constraints when the declaration will not be part of |
| 1331 | // a decl chain. |
| 1332 | Expr *const ACtoAttach = |
| 1333 | PrevClassTemplate && ShouldAddRedecl ? nullptr : CurAC; |
| 1334 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1335 | ClassTemplateDecl *NewTemplate |
| 1336 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1337 | DeclarationName(Name), TemplateParams, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame] | 1338 | NewClass, ACtoAttach); |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1339 | |
| 1340 | if (ShouldAddRedecl) |
| 1341 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
| 1342 | |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1343 | NewClass->setDescribedClassTemplate(NewTemplate); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1344 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1345 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1346 | NewTemplate->setModulePrivate(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1347 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1348 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1349 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1350 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1351 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1352 | (void)T; |
| 1353 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1354 | // 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] | 1355 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1356 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1357 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1358 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1359 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1360 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1361 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1362 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1364 | // Set the lexical context of these templates |
| 1365 | NewClass->setLexicalDeclContext(CurContext); |
| 1366 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1367 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1368 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1369 | NewClass->startDefinition(); |
| 1370 | |
| 1371 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1372 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1373 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1374 | if (PrevClassTemplate) |
| 1375 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1376 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1377 | AddPushedVisibilityAttribute(NewClass); |
| 1378 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1379 | if (TUK != TUK_Friend) { |
| 1380 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1381 | Scope *Outer = S; |
| 1382 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1383 | Outer = Outer->getParent(); |
| 1384 | PushOnScopeChains(NewTemplate, Outer); |
| 1385 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1386 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1387 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1388 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1389 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1390 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1391 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1392 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1393 | // Friend templates are visible in fairly strange ways. |
| 1394 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1395 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1396 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1397 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1398 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1399 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1400 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1401 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1402 | FriendDecl *Friend = FriendDecl::Create( |
| 1403 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1404 | Friend->setAccess(AS_public); |
| 1405 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1406 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1407 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1408 | if (Invalid) { |
| 1409 | NewTemplate->setInvalidDecl(); |
| 1410 | NewClass->setInvalidDecl(); |
| 1411 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1412 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1413 | ActOnDocumentableDecl(NewTemplate); |
| 1414 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1415 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1418 | namespace { |
| 1419 | /// Transform to convert portions of a constructor declaration into the |
| 1420 | /// corresponding deduction guide, per C++1z [over.match.class.deduct]p1. |
| 1421 | struct ConvertConstructorToDeductionGuideTransform { |
| 1422 | ConvertConstructorToDeductionGuideTransform(Sema &S, |
| 1423 | ClassTemplateDecl *Template) |
| 1424 | : SemaRef(S), Template(Template) {} |
| 1425 | |
| 1426 | Sema &SemaRef; |
| 1427 | ClassTemplateDecl *Template; |
| 1428 | |
| 1429 | DeclContext *DC = Template->getDeclContext(); |
| 1430 | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
| 1431 | DeclarationName DeductionGuideName = |
| 1432 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
| 1433 | |
| 1434 | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
| 1435 | |
| 1436 | // Index adjustment to apply to convert depth-1 template parameters into |
| 1437 | // depth-0 template parameters. |
| 1438 | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
| 1439 | |
| 1440 | /// Transform a constructor declaration into a deduction guide. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1441 | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
| 1442 | CXXConstructorDecl *CD) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1443 | SmallVector<TemplateArgument, 16> SubstArgs; |
| 1444 | |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1445 | LocalInstantiationScope Scope(SemaRef); |
| 1446 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1447 | // C++ [over.match.class.deduct]p1: |
| 1448 | // -- For each constructor of the class template designated by the |
| 1449 | // template-name, a function template with the following properties: |
| 1450 | |
| 1451 | // -- The template parameters are the template parameters of the class |
| 1452 | // template followed by the template parameters (including default |
| 1453 | // template arguments) of the constructor, if any. |
| 1454 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 1455 | if (FTD) { |
| 1456 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
| 1457 | SmallVector<NamedDecl *, 16> AllParams; |
| 1458 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
| 1459 | AllParams.insert(AllParams.begin(), |
| 1460 | TemplateParams->begin(), TemplateParams->end()); |
| 1461 | SubstArgs.reserve(InnerParams->size()); |
| 1462 | |
| 1463 | // Later template parameters could refer to earlier ones, so build up |
| 1464 | // a list of substituted template arguments as we go. |
| 1465 | for (NamedDecl *Param : *InnerParams) { |
| 1466 | MultiLevelTemplateArgumentList Args; |
| 1467 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1468 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1469 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
| 1470 | if (!NewParam) |
| 1471 | return nullptr; |
| 1472 | AllParams.push_back(NewParam); |
| 1473 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
| 1474 | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
| 1475 | } |
| 1476 | TemplateParams = TemplateParameterList::Create( |
| 1477 | SemaRef.Context, InnerParams->getTemplateLoc(), |
| 1478 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
| 1479 | /*FIXME: RequiresClause*/ nullptr); |
| 1480 | } |
| 1481 | |
| 1482 | // If we built a new template-parameter-list, track that we need to |
| 1483 | // substitute references to the old parameters into references to the |
| 1484 | // new ones. |
| 1485 | MultiLevelTemplateArgumentList Args; |
| 1486 | if (FTD) { |
| 1487 | Args.addOuterTemplateArguments(SubstArgs); |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1488 | Args.addOuterRetainedLevel(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1491 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1492 | .getAsAdjusted<FunctionProtoTypeLoc>(); |
| 1493 | assert(FPTL && "no prototype for constructor declaration"); |
| 1494 | |
| 1495 | // Transform the type of the function, adjusting the return type and |
| 1496 | // replacing references to the old parameters with references to the |
| 1497 | // new ones. |
| 1498 | TypeLocBuilder TLB; |
| 1499 | SmallVector<ParmVarDecl*, 8> Params; |
| 1500 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args); |
| 1501 | if (NewType.isNull()) |
| 1502 | return nullptr; |
| 1503 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
| 1504 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1505 | return buildDeductionGuide(TemplateParams, CD->isExplicit(), NewTInfo, |
| 1506 | CD->getLocStart(), CD->getLocation(), |
| 1507 | CD->getLocEnd()); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /// Build a deduction guide with the specified parameter types. |
| 1511 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
| 1512 | SourceLocation Loc = Template->getLocation(); |
| 1513 | |
| 1514 | // Build the requested type. |
| 1515 | FunctionProtoType::ExtProtoInfo EPI; |
| 1516 | EPI.HasTrailingReturn = true; |
| 1517 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
| 1518 | DeductionGuideName, EPI); |
| 1519 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
| 1520 | |
| 1521 | FunctionProtoTypeLoc FPTL = |
| 1522 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
| 1523 | |
| 1524 | // Build the parameters, needed during deduction / substitution. |
| 1525 | SmallVector<ParmVarDecl*, 4> Params; |
| 1526 | for (auto T : ParamTypes) { |
| 1527 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
| 1528 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
| 1529 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
| 1530 | NewParam->setScopeInfo(0, Params.size()); |
| 1531 | FPTL.setParam(Params.size(), NewParam); |
| 1532 | Params.push_back(NewParam); |
| 1533 | } |
| 1534 | |
| 1535 | return buildDeductionGuide(Template->getTemplateParameters(), false, TSI, |
| 1536 | Loc, Loc, Loc); |
| 1537 | } |
| 1538 | |
| 1539 | private: |
| 1540 | /// Transform a constructor template parameter into a deduction guide template |
| 1541 | /// parameter, rebuilding any internal references to earlier parameters and |
| 1542 | /// renumbering as we go. |
| 1543 | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
| 1544 | MultiLevelTemplateArgumentList &Args) { |
| 1545 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
| 1546 | // TemplateTypeParmDecl's index cannot be changed after creation, so |
| 1547 | // substitute it directly. |
| 1548 | auto *NewTTP = TemplateTypeParmDecl::Create( |
| 1549 | SemaRef.Context, DC, TTP->getLocStart(), TTP->getLocation(), |
| 1550 | /*Depth*/0, Depth1IndexAdjustment + TTP->getIndex(), |
| 1551 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
| 1552 | TTP->isParameterPack()); |
| 1553 | if (TTP->hasDefaultArgument()) { |
| 1554 | TypeSourceInfo *InstantiatedDefaultArg = |
| 1555 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
| 1556 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
| 1557 | if (InstantiatedDefaultArg) |
| 1558 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
| 1559 | } |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 1560 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
| 1561 | NewTTP); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1562 | return NewTTP; |
| 1563 | } |
| 1564 | |
| 1565 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
| 1566 | return transformTemplateParameterImpl(TTP, Args); |
| 1567 | |
| 1568 | return transformTemplateParameterImpl( |
| 1569 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
| 1570 | } |
| 1571 | template<typename TemplateParmDecl> |
| 1572 | TemplateParmDecl * |
| 1573 | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
| 1574 | MultiLevelTemplateArgumentList &Args) { |
| 1575 | // Ask the template instantiator to do the heavy lifting for us, then adjust |
| 1576 | // the index of the parameter once it's done. |
| 1577 | auto *NewParam = |
| 1578 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
| 1579 | assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
| 1580 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
| 1581 | return NewParam; |
| 1582 | } |
| 1583 | |
| 1584 | QualType transformFunctionProtoType(TypeLocBuilder &TLB, |
| 1585 | FunctionProtoTypeLoc TL, |
| 1586 | SmallVectorImpl<ParmVarDecl*> &Params, |
| 1587 | MultiLevelTemplateArgumentList &Args) { |
| 1588 | SmallVector<QualType, 4> ParamTypes; |
| 1589 | const FunctionProtoType *T = TL.getTypePtr(); |
| 1590 | |
| 1591 | // -- The types of the function parameters are those of the constructor. |
| 1592 | for (auto *OldParam : TL.getParams()) { |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1593 | ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1594 | if (!NewParam) |
| 1595 | return QualType(); |
| 1596 | ParamTypes.push_back(NewParam->getType()); |
| 1597 | Params.push_back(NewParam); |
| 1598 | } |
| 1599 | |
| 1600 | // -- The return type is the class template specialization designated by |
| 1601 | // the template-name and template arguments corresponding to the |
| 1602 | // template parameters obtained from the class template. |
| 1603 | // |
| 1604 | // We use the injected-class-name type of the primary template instead. |
| 1605 | // This has the convenient property that it is different from any type that |
| 1606 | // the user can write in a deduction-guide (because they cannot enter the |
| 1607 | // context of the template), so implicit deduction guides can never collide |
| 1608 | // with explicit ones. |
| 1609 | QualType ReturnType = DeducedType; |
| 1610 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
| 1611 | |
| 1612 | // Resolving a wording defect, we also inherit the variadicness of the |
| 1613 | // constructor. |
| 1614 | FunctionProtoType::ExtProtoInfo EPI; |
| 1615 | EPI.Variadic = T->isVariadic(); |
| 1616 | EPI.HasTrailingReturn = true; |
| 1617 | |
| 1618 | QualType Result = SemaRef.BuildFunctionType( |
| 1619 | ReturnType, ParamTypes, TL.getLocStart(), DeductionGuideName, EPI); |
| 1620 | if (Result.isNull()) |
| 1621 | return QualType(); |
| 1622 | |
| 1623 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
| 1624 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
| 1625 | NewTL.setLParenLoc(TL.getLParenLoc()); |
| 1626 | NewTL.setRParenLoc(TL.getRParenLoc()); |
| 1627 | NewTL.setExceptionSpecRange(SourceRange()); |
| 1628 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
| 1629 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I) |
| 1630 | NewTL.setParam(I, Params[I]); |
| 1631 | |
| 1632 | return Result; |
| 1633 | } |
| 1634 | |
| 1635 | ParmVarDecl * |
| 1636 | transformFunctionTypeParam(ParmVarDecl *OldParam, |
| 1637 | MultiLevelTemplateArgumentList &Args) { |
| 1638 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1639 | TypeSourceInfo *NewDI = |
| 1640 | Args.getNumLevels() |
| 1641 | ? SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
| 1642 | OldParam->getDeclName()) |
| 1643 | : OldDI; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1644 | if (!NewDI) |
| 1645 | return nullptr; |
| 1646 | |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1647 | // Canonicalize the type. This (for instance) replaces references to |
| 1648 | // typedef members of the current instantiations with the definitions of |
| 1649 | // those typedefs, avoiding triggering instantiation of the deduced type |
| 1650 | // during deduction. |
| 1651 | // FIXME: It would be preferable to retain type sugar and source |
| 1652 | // information here (and handle this in substitution instead). |
| 1653 | NewDI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 1654 | SemaRef.Context.getCanonicalType(NewDI->getType()), |
| 1655 | OldParam->getLocation()); |
| 1656 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1657 | // Resolving a wording defect, we also inherit default arguments from the |
| 1658 | // constructor. |
| 1659 | ExprResult NewDefArg; |
| 1660 | if (OldParam->hasDefaultArg()) { |
Richard Smith | c27b3d7 | 2017-02-14 01:49:59 +0000 | [diff] [blame] | 1661 | NewDefArg = Args.getNumLevels() |
| 1662 | ? SemaRef.SubstExpr(OldParam->getDefaultArg(), Args) |
| 1663 | : OldParam->getDefaultArg(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1664 | if (NewDefArg.isInvalid()) |
| 1665 | return nullptr; |
| 1666 | } |
| 1667 | |
| 1668 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
| 1669 | OldParam->getInnerLocStart(), |
| 1670 | OldParam->getLocation(), |
| 1671 | OldParam->getIdentifier(), |
| 1672 | NewDI->getType(), |
| 1673 | NewDI, |
| 1674 | OldParam->getStorageClass(), |
| 1675 | NewDefArg.get()); |
| 1676 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
| 1677 | OldParam->getFunctionScopeIndex()); |
| 1678 | return NewParam; |
| 1679 | } |
| 1680 | |
| 1681 | NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams, |
| 1682 | bool Explicit, TypeSourceInfo *TInfo, |
| 1683 | SourceLocation LocStart, SourceLocation Loc, |
| 1684 | SourceLocation LocEnd) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1685 | DeclarationNameInfo Name(DeductionGuideName, Loc); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1686 | ArrayRef<ParmVarDecl *> Params = |
| 1687 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
| 1688 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1689 | // Build the implicit deduction guide template. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1690 | auto *Guide = |
| 1691 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, Explicit, |
| 1692 | Name, TInfo->getType(), TInfo, LocEnd); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1693 | Guide->setImplicit(); |
Richard Smith | efa919a | 2017-02-16 21:29:21 +0000 | [diff] [blame] | 1694 | Guide->setParams(Params); |
| 1695 | |
| 1696 | for (auto *Param : Params) |
| 1697 | Param->setDeclContext(Guide); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1698 | |
| 1699 | auto *GuideTemplate = FunctionTemplateDecl::Create( |
| 1700 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
| 1701 | GuideTemplate->setImplicit(); |
| 1702 | Guide->setDescribedFunctionTemplate(GuideTemplate); |
| 1703 | |
| 1704 | if (isa<CXXRecordDecl>(DC)) { |
| 1705 | Guide->setAccess(AS_public); |
| 1706 | GuideTemplate->setAccess(AS_public); |
| 1707 | } |
| 1708 | |
| 1709 | DC->addDecl(GuideTemplate); |
| 1710 | return GuideTemplate; |
| 1711 | } |
| 1712 | }; |
| 1713 | } |
| 1714 | |
| 1715 | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
| 1716 | SourceLocation Loc) { |
| 1717 | DeclContext *DC = Template->getDeclContext(); |
| 1718 | if (DC->isDependentContext()) |
| 1719 | return; |
| 1720 | |
| 1721 | ConvertConstructorToDeductionGuideTransform Transform( |
| 1722 | *this, cast<ClassTemplateDecl>(Template)); |
| 1723 | if (!isCompleteType(Loc, Transform.DeducedType)) |
| 1724 | return; |
| 1725 | |
| 1726 | // Check whether we've already declared deduction guides for this template. |
| 1727 | // FIXME: Consider storing a flag on the template to indicate this. |
| 1728 | auto Existing = DC->lookup(Transform.DeductionGuideName); |
| 1729 | for (auto *D : Existing) |
| 1730 | if (D->isImplicit()) |
| 1731 | return; |
| 1732 | |
| 1733 | // In case we were expanding a pack when we attempted to declare deduction |
| 1734 | // guides, turn off pack expansion for everything we're about to do. |
| 1735 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
| 1736 | // Create a template instantiation record to track the "instantiation" of |
| 1737 | // constructors into deduction guides. |
| 1738 | // FIXME: Add a kind for this to give more meaningful diagnostics. But can |
| 1739 | // this substitution process actually fail? |
| 1740 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
| 1741 | |
| 1742 | // Convert declared constructors into deduction guide templates. |
| 1743 | // FIXME: Skip constructors for which deduction must necessarily fail (those |
| 1744 | // for which some class template parameter without a default argument never |
| 1745 | // appears in a deduced context). |
| 1746 | bool AddedAny = false; |
| 1747 | bool AddedCopyOrMove = false; |
| 1748 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
| 1749 | D = D->getUnderlyingDecl(); |
| 1750 | if (D->isInvalidDecl() || D->isImplicit()) |
| 1751 | continue; |
| 1752 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 1753 | |
| 1754 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1755 | auto *CD = |
| 1756 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1757 | // Class-scope explicit specializations (MS extension) do not result in |
| 1758 | // deduction guides. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1759 | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization())) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1760 | continue; |
| 1761 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1762 | Transform.transformConstructor(FTD, CD); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1763 | AddedAny = true; |
| 1764 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1765 | AddedCopyOrMove |= CD->isCopyOrMoveConstructor(); |
| 1766 | } |
| 1767 | |
| 1768 | // Synthesize an X() -> X<...> guide if there were no declared constructors. |
| 1769 | // FIXME: The standard doesn't say (how) to do this. |
| 1770 | if (!AddedAny) |
| 1771 | Transform.buildSimpleDeductionGuide(None); |
| 1772 | |
| 1773 | // Synthesize an X(X<...>) -> X<...> guide if there was no declared constructor |
| 1774 | // resembling a copy or move constructor. |
| 1775 | // FIXME: The standard doesn't say (how) to do this. |
| 1776 | if (!AddedCopyOrMove) |
| 1777 | Transform.buildSimpleDeductionGuide(Transform.DeducedType); |
| 1778 | } |
| 1779 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1780 | /// \brief Diagnose the presence of a default template argument on a |
| 1781 | /// template parameter, which is ill-formed in certain contexts. |
| 1782 | /// |
| 1783 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1784 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1785 | Sema::TemplateParamListContext TPC, |
| 1786 | SourceLocation ParamLoc, |
| 1787 | SourceRange DefArgRange) { |
| 1788 | switch (TPC) { |
| 1789 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1790 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1791 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1792 | return false; |
| 1793 | |
| 1794 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1795 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1796 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1797 | // A default template-argument shall not be specified in a |
| 1798 | // function template declaration or a function template |
| 1799 | // definition [...] |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1800 | // If a friend function template declaration specifies a default |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1801 | // template-argument, that declaration shall be a definition and shall be |
| 1802 | // the only declaration of the function template in the translation unit. |
| 1803 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1804 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1805 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1806 | : diag::ext_template_parameter_default_in_function_template) |
| 1807 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1808 | return false; |
| 1809 | |
| 1810 | case Sema::TPC_ClassTemplateMember: |
| 1811 | // C++0x [temp.param]p9: |
| 1812 | // A default template-argument shall not be specified in the |
| 1813 | // template-parameter-lists of the definition of a member of a |
| 1814 | // class template that appears outside of the member's class. |
| 1815 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1816 | << DefArgRange; |
| 1817 | return true; |
| 1818 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1819 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1820 | case Sema::TPC_FriendFunctionTemplate: |
| 1821 | // C++ [temp.param]p9: |
| 1822 | // A default template-argument shall not be specified in a |
| 1823 | // friend template declaration. |
| 1824 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1825 | << DefArgRange; |
| 1826 | return true; |
| 1827 | |
| 1828 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1829 | // for friend function templates if there is only a single |
| 1830 | // declaration (and it is a definition). Strange! |
| 1831 | } |
| 1832 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1833 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1836 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1837 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1838 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1839 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1840 | // A template template parameter which is a parameter pack is also a pack |
| 1841 | // expansion. |
| 1842 | if (TTP->isParameterPack()) |
| 1843 | return false; |
| 1844 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1845 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1846 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1847 | NamedDecl *P = Params->getParam(I); |
| 1848 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1849 | if (!NTTP->isParameterPack() && |
| 1850 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1851 | NTTP->getTypeSourceInfo(), |
| 1852 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1853 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1854 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1855 | continue; |
| 1856 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1857 | |
| 1858 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1859 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1860 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1861 | return true; |
| 1862 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1863 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1864 | return false; |
| 1865 | } |
| 1866 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1867 | /// \brief Checks the validity of a template parameter list, possibly |
| 1868 | /// considering the template parameter list from a previous |
| 1869 | /// declaration. |
| 1870 | /// |
| 1871 | /// If an "old" template parameter list is provided, it must be |
| 1872 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1873 | /// template parameter list. |
| 1874 | /// |
| 1875 | /// \param NewParams Template parameter list for a new template |
| 1876 | /// declaration. This template parameter list will be updated with any |
| 1877 | /// default arguments that are carried through from the previous |
| 1878 | /// template parameter list. |
| 1879 | /// |
| 1880 | /// \param OldParams If provided, template parameter list from a |
| 1881 | /// previous declaration of the same template. Default template |
| 1882 | /// arguments will be merged from the old template parameter list to |
| 1883 | /// the new template parameter list. |
| 1884 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1885 | /// \param TPC Describes the context in which we are checking the given |
| 1886 | /// template parameter list. |
| 1887 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1888 | /// \returns true if an error occurred, false otherwise. |
| 1889 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1890 | TemplateParameterList *OldParams, |
| 1891 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1892 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1894 | // C++ [temp.param]p10: |
| 1895 | // The set of default template-arguments available for use with a |
| 1896 | // template declaration or definition is obtained by merging the |
| 1897 | // default arguments from the definition (if in scope) and all |
| 1898 | // declarations in scope in the same way default function |
| 1899 | // arguments are (8.3.6). |
| 1900 | bool SawDefaultArgument = false; |
| 1901 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1902 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1903 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1904 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1905 | if (OldParams) |
| 1906 | OldParam = OldParams->begin(); |
| 1907 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1908 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1909 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1910 | NewParamEnd = NewParams->end(); |
| 1911 | NewParam != NewParamEnd; ++NewParam) { |
| 1912 | // Variables used to diagnose redundant default arguments |
| 1913 | bool RedundantDefaultArg = false; |
| 1914 | SourceLocation OldDefaultLoc; |
| 1915 | SourceLocation NewDefaultLoc; |
| 1916 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1917 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1918 | bool MissingDefaultArg = false; |
| 1919 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1920 | // Variable used to diagnose non-final parameter packs |
| 1921 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1922 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1923 | if (TemplateTypeParmDecl *NewTypeParm |
| 1924 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1925 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1926 | if (NewTypeParm->hasDefaultArgument() && |
| 1927 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1928 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1929 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1930 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1931 | NewTypeParm->removeDefaultArgument(); |
| 1932 | |
| 1933 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1935 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1936 | if (NewTypeParm->isParameterPack()) { |
| 1937 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1938 | "Parameter packs can't have a default argument!"); |
| 1939 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1940 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1941 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1942 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1943 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1944 | SawDefaultArgument = true; |
| 1945 | RedundantDefaultArg = true; |
| 1946 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1947 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1948 | // Merge the default argument from the old declaration to the |
| 1949 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1950 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1951 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1952 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1953 | SawDefaultArgument = true; |
| 1954 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1955 | } else if (SawDefaultArgument) |
| 1956 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1957 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1958 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1959 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1960 | if (!NewNonTypeParm->isParameterPack() && |
| 1961 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1962 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1963 | UPPC_NonTypeTemplateParameterType)) { |
| 1964 | Invalid = true; |
| 1965 | continue; |
| 1966 | } |
| 1967 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1968 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1969 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1970 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1971 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1972 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1973 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1976 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1977 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1978 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1979 | if (NewNonTypeParm->isParameterPack()) { |
| 1980 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1981 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1982 | if (!NewNonTypeParm->isPackExpansion()) |
| 1983 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1984 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1985 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1986 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1987 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1988 | SawDefaultArgument = true; |
| 1989 | RedundantDefaultArg = true; |
| 1990 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1991 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1992 | // Merge the default argument from the old declaration to the |
| 1993 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1994 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1995 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1996 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1997 | SawDefaultArgument = true; |
| 1998 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1999 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2001 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2002 | TemplateTemplateParmDecl *NewTemplateParm |
| 2003 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2004 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2005 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 2006 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 2007 | Invalid = true; |
| 2008 | continue; |
| 2009 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2010 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2011 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2012 | if (NewTemplateParm->hasDefaultArgument() && |
| 2013 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2014 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2015 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 2016 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2017 | |
| 2018 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2019 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2020 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 2021 | if (NewTemplateParm->isParameterPack()) { |
| 2022 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 2023 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2024 | if (!NewTemplateParm->isPackExpansion()) |
| 2025 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 2026 | } else if (OldTemplateParm && |
| 2027 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 2028 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2029 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 2030 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2031 | SawDefaultArgument = true; |
| 2032 | RedundantDefaultArg = true; |
| 2033 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2034 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 2035 | // Merge the default argument from the old declaration to the |
| 2036 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2037 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2038 | PreviousDefaultArgLoc |
| 2039 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2040 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 2041 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2042 | PreviousDefaultArgLoc |
| 2043 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2044 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2045 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2048 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2049 | // If a template parameter of a primary class template or alias template |
| 2050 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2051 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2052 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 2053 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 2054 | Diag((*NewParam)->getLocation(), |
| 2055 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 2056 | Invalid = true; |
| 2057 | } |
| 2058 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2059 | if (RedundantDefaultArg) { |
| 2060 | // C++ [temp.param]p12: |
| 2061 | // A template-parameter shall not be given default arguments |
| 2062 | // by two different declarations in the same scope. |
| 2063 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 2064 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 2065 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 2066 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2067 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2068 | // If a template-parameter of a class template has a default |
| 2069 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 2070 | // have a default template-argument supplied or be a template parameter |
| 2071 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2073 | diag::err_template_param_default_arg_missing); |
| 2074 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 2075 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2076 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | // If we have an old template parameter list that we're merging |
| 2080 | // in, move on to the next parameter. |
| 2081 | if (OldParams) |
| 2082 | ++OldParam; |
| 2083 | } |
| 2084 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2085 | // We were missing some default arguments at the end of the list, so remove |
| 2086 | // all of the default arguments. |
| 2087 | if (RemoveDefaultArguments) { |
| 2088 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2089 | NewParamEnd = NewParams->end(); |
| 2090 | NewParam != NewParamEnd; ++NewParam) { |
| 2091 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 2092 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2093 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 2094 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 2095 | NTTP->removeDefaultArgument(); |
| 2096 | else |
| 2097 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 2098 | } |
| 2099 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2100 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 2101 | return Invalid; |
| 2102 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2103 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2104 | namespace { |
| 2105 | |
| 2106 | /// A class which looks for a use of a certain level of template |
| 2107 | /// parameter. |
| 2108 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 2109 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 2110 | |
| 2111 | unsigned Depth; |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 2112 | bool FindLessThanDepth; |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2113 | |
| 2114 | // Whether we're looking for a use of a template parameter that makes the |
| 2115 | // overall construct type-dependent / a dependent type. This is strictly |
| 2116 | // best-effort for now; we may fail to match at all for a dependent type |
| 2117 | // in some cases if this is set. |
| 2118 | bool IgnoreNonTypeDependent; |
| 2119 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2120 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2121 | SourceLocation MatchLoc; |
| 2122 | |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 2123 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent, |
| 2124 | bool FindLessThanDepth = false) |
| 2125 | : Depth(Depth), FindLessThanDepth(FindLessThanDepth), |
| 2126 | IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2127 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2128 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 2129 | : DependencyChecker(Params->getDepth(), IgnoreNonTypeDependent) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2130 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2131 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 2132 | if (FindLessThanDepth ^ (ParmDepth >= Depth)) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2133 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2134 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2135 | return true; |
| 2136 | } |
| 2137 | return false; |
| 2138 | } |
| 2139 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2140 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 2141 | // Prune out non-type-dependent expressions if requested. This can |
| 2142 | // sometimes result in us failing to find a template parameter reference |
| 2143 | // (if a value-dependent expression creates a dependent type), but this |
| 2144 | // mode is best-effort only. |
| 2145 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 2146 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 2147 | return true; |
| 2148 | return super::TraverseStmt(S, Q); |
| 2149 | } |
| 2150 | |
| 2151 | bool TraverseTypeLoc(TypeLoc TL) { |
| 2152 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 2153 | !TL.getType()->isDependentType()) |
| 2154 | return true; |
| 2155 | return super::TraverseTypeLoc(TL); |
| 2156 | } |
| 2157 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2158 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2159 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 2160 | } |
| 2161 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2162 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2163 | // For a best-effort search, keep looking until we find a location. |
| 2164 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | bool TraverseTemplateName(TemplateName N) { |
| 2168 | if (TemplateTemplateParmDecl *PD = |
| 2169 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2170 | if (Matches(PD->getDepth())) |
| 2171 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2172 | return super::TraverseTemplateName(N); |
| 2173 | } |
| 2174 | |
| 2175 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 2176 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2177 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 2178 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2179 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2180 | return super::VisitDeclRefExpr(E); |
| 2181 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2182 | |
| 2183 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 2184 | return TraverseType(T->getReplacementType()); |
| 2185 | } |
| 2186 | |
| 2187 | bool |
| 2188 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 2189 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 2190 | } |
| 2191 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 2192 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 2193 | return TraverseType(T->getInjectedSpecializationType()); |
| 2194 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2195 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2196 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2197 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2198 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2199 | /// list. |
| 2200 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2201 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2202 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2203 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 2204 | return Checker.Match; |
| 2205 | } |
| 2206 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2207 | // Find the source range corresponding to the named type in the given |
| 2208 | // nested-name-specifier, if any. |
| 2209 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 2210 | QualType T, |
| 2211 | const CXXScopeSpec &SS) { |
| 2212 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 2213 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 2214 | if (const Type *CurType = NNS->getAsType()) { |
| 2215 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 2216 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 2217 | } else |
| 2218 | break; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2219 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2220 | NNSLoc = NNSLoc.getPrefix(); |
| 2221 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2222 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2223 | return SourceRange(); |
| 2224 | } |
| 2225 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2226 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2227 | /// specifier, returning the template parameter list that applies to the |
| 2228 | /// name. |
| 2229 | /// |
| 2230 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 2231 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2233 | /// \param DeclLoc The location of the declaration itself. |
| 2234 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2235 | /// \param SS the scope specifier that will be matched to the given template |
| 2236 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 2237 | /// being declared. |
| 2238 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2239 | /// \param TemplateId The template-id following the scope specifier, if there |
| 2240 | /// is one. Used to check for a missing 'template<>'. |
| 2241 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2242 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 2243 | /// innermost template parameter lists. |
| 2244 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2245 | /// \param IsFriend Whether to apply the slightly different rules for |
| 2246 | /// matching template parameters to scope specifiers in friend |
| 2247 | /// declarations. |
| 2248 | /// |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2249 | /// \param IsMemberSpecialization will be set true if the scope specifier |
| 2250 | /// denotes a fully-specialized type, and therefore this is a declaration of |
| 2251 | /// a member specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2252 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2254 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2255 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2257 | /// 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] | 2258 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2259 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 2260 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2261 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2262 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2263 | bool &IsMemberSpecialization, bool &Invalid) { |
| 2264 | IsMemberSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2265 | Invalid = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2266 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2267 | // The sequence of nested types to which we will match up the template |
| 2268 | // parameter lists. We first build this list by starting with the type named |
| 2269 | // 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] | 2270 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2271 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2272 | if (SS.getScopeRep()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2273 | if (CXXRecordDecl *Record |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 2274 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 2275 | T = Context.getTypeDeclType(Record); |
| 2276 | else |
| 2277 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 2278 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2279 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2280 | // If we found an explicit specialization that prevents us from needing |
| 2281 | // 'template<>' headers, this will be set to the location of that |
| 2282 | // explicit specialization. |
| 2283 | SourceLocation ExplicitSpecLoc; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2284 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2285 | while (!T.isNull()) { |
| 2286 | NestedTypes.push_back(T); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2287 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2288 | // Retrieve the parent of a record type. |
| 2289 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2290 | // If this type is an explicit specialization, we're done. |
| 2291 | if (ClassTemplateSpecializationDecl *Spec |
| 2292 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2293 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2294 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 2295 | ExplicitSpecLoc = Spec->getLocation(); |
| 2296 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 2297 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2298 | } else if (Record->getTemplateSpecializationKind() |
| 2299 | == TSK_ExplicitSpecialization) { |
| 2300 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 2301 | break; |
| 2302 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2303 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2304 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 2305 | T = Context.getTypeDeclType(Parent); |
| 2306 | else |
| 2307 | T = QualType(); |
| 2308 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2311 | if (const TemplateSpecializationType *TST |
| 2312 | = T->getAs<TemplateSpecializationType>()) { |
| 2313 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 2314 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 2315 | T = Context.getTypeDeclType(Parent); |
| 2316 | else |
| 2317 | T = QualType(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2318 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2319 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2320 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2321 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2322 | // Look one step prior in a dependent template specialization type. |
| 2323 | if (const DependentTemplateSpecializationType *DependentTST |
| 2324 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2325 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 2326 | T = QualType(NNS->getAsType(), 0); |
| 2327 | else |
| 2328 | T = QualType(); |
| 2329 | continue; |
| 2330 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2331 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2332 | // Look one step prior in a dependent name type. |
| 2333 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 2334 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 2335 | T = QualType(NNS->getAsType(), 0); |
| 2336 | else |
| 2337 | T = QualType(); |
| 2338 | continue; |
| 2339 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2341 | // Retrieve the parent of an enumeration type. |
| 2342 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 2343 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 2344 | // check here. |
| 2345 | EnumDecl *Enum = EnumT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2347 | // Get to the parent type. |
| 2348 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 2349 | T = Context.getTypeDeclType(Parent); |
| 2350 | else |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2351 | T = QualType(); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2352 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2353 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2354 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2355 | T = QualType(); |
| 2356 | } |
| 2357 | // Reverse the nested types list, since we want to traverse from the outermost |
| 2358 | // to the innermost while checking template-parameter-lists. |
| 2359 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 2360 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2361 | // C++0x [temp.expl.spec]p17: |
| 2362 | // A member or a member template may be nested within many |
| 2363 | // enclosing class templates. In an explicit specialization for |
| 2364 | // such a member, the member declaration shall be preceded by a |
| 2365 | // template<> for each enclosing class template that is |
| 2366 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2367 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2368 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2369 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2370 | if (SawNonEmptyTemplateParameterList) { |
| 2371 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 2372 | << !Recovery << Range; |
| 2373 | Invalid = true; |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2374 | IsMemberSpecialization = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2375 | return true; |
| 2376 | } |
| 2377 | |
| 2378 | return false; |
| 2379 | }; |
| 2380 | |
| 2381 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 2382 | // Check that we can have an explicit specialization here. |
| 2383 | if (CheckExplicitSpecialization(Range, true)) |
| 2384 | return true; |
| 2385 | |
| 2386 | // We don't have a template header, but we should. |
| 2387 | SourceLocation ExpectedTemplateLoc; |
| 2388 | if (!ParamLists.empty()) |
| 2389 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 2390 | else |
| 2391 | ExpectedTemplateLoc = DeclStartLoc; |
| 2392 | |
| 2393 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 2394 | << Range |
| 2395 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 2396 | return false; |
| 2397 | }; |
| 2398 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2399 | unsigned ParamIdx = 0; |
| 2400 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 2401 | ++TypeIdx) { |
| 2402 | T = NestedTypes[TypeIdx]; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2403 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2404 | // Whether we expect a 'template<>' header. |
| 2405 | bool NeedEmptyTemplateHeader = false; |
| 2406 | |
| 2407 | // Whether we expect a template header with parameters. |
| 2408 | bool NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2409 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2410 | // For a dependent type, the set of template parameters that we |
| 2411 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2412 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2413 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2414 | // C++0x [temp.expl.spec]p15: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2415 | // A member or a member template may be nested within many enclosing |
| 2416 | // class templates. In an explicit specialization for such a member, the |
| 2417 | // member declaration shall be preceded by a template<> for each |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2418 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2419 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2420 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 2421 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 2422 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 2423 | NeedNonemptyTemplateHeader = true; |
| 2424 | } else if (Record->isDependentType()) { |
| 2425 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2426 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2427 | ->getTemplateParameters(); |
| 2428 | NeedNonemptyTemplateHeader = true; |
| 2429 | } |
| 2430 | } else if (ClassTemplateSpecializationDecl *Spec |
| 2431 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 2432 | // C++0x [temp.expl.spec]p4: |
| 2433 | // Members of an explicitly specialized class template are defined |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2434 | // in the same manner as members of normal classes, and not using |
| 2435 | // the template<> syntax. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2436 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 2437 | NeedEmptyTemplateHeader = true; |
| 2438 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 2439 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2440 | } else if (Record->getTemplateSpecializationKind()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2441 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2442 | != TSK_ExplicitSpecialization && |
| 2443 | TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2444 | IsMemberSpecialization = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2445 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2446 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2447 | } |
| 2448 | } else if (const TemplateSpecializationType *TST |
| 2449 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 2450 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2451 | ExpectedTemplateParams = Template->getTemplateParameters(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2452 | NeedNonemptyTemplateHeader = true; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2453 | } |
| 2454 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2455 | // FIXME: We actually could/should check the template arguments here |
| 2456 | // against the corresponding template parameter list. |
| 2457 | NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2460 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2461 | // In an explicit specialization declaration for a member of a class |
| 2462 | // template or a member template that ap- pears in namespace scope, the |
| 2463 | // member template and some of its enclosing class templates may remain |
| 2464 | // unspecialized, except that the declaration shall not explicitly |
| 2465 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2466 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2467 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2468 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2469 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2470 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2471 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2472 | } else |
| 2473 | SawNonEmptyTemplateParameterList = true; |
| 2474 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2475 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2476 | if (NeedEmptyTemplateHeader) { |
| 2477 | // 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] | 2478 | // here, then it's a member specialization. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2479 | if (TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2480 | IsMemberSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2481 | |
| 2482 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2483 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2484 | // The header has template parameters when it shouldn't. Complain. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2485 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2486 | diag::err_template_param_list_matches_nontemplate) |
| 2487 | << T |
| 2488 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2489 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2490 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2491 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2492 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2493 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2494 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2495 | // Consume this template header. |
| 2496 | ++ParamIdx; |
| 2497 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2498 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2499 | |
| 2500 | if (!IsFriend) |
| 2501 | if (DiagnoseMissingExplicitSpecialization( |
| 2502 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2503 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2504 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2505 | continue; |
| 2506 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2508 | if (NeedNonemptyTemplateHeader) { |
| 2509 | // In friend declarations we can have template-ids which don't |
| 2510 | // depend on the corresponding template parameter lists. But |
| 2511 | // assume that empty parameter lists are supposed to match this |
| 2512 | // template-id. |
| 2513 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2514 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2515 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2516 | ExpectedTemplateParams = nullptr; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2517 | else |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2518 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2520 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2521 | if (ParamIdx < ParamLists.size()) { |
| 2522 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2523 | if (ExpectedTemplateParams && |
| 2524 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2525 | ExpectedTemplateParams, |
| 2526 | true, TPL_TemplateMatch)) |
| 2527 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2528 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2529 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2530 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2531 | TPC_ClassTemplateMember)) |
| 2532 | Invalid = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2533 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2534 | ++ParamIdx; |
| 2535 | continue; |
| 2536 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2537 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2538 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2539 | << T |
| 2540 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2541 | Invalid = true; |
| 2542 | continue; |
| 2543 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2544 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2545 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2546 | // If there were at least as many template-ids as there were template |
| 2547 | // parameter lists, then there are no template parameter lists remaining for |
| 2548 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2549 | if (ParamIdx >= ParamLists.size()) { |
| 2550 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2551 | // We don't have a template header for the declaration itself, but we |
| 2552 | // should. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2553 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2554 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2555 | |
| 2556 | // Fabricate an empty template parameter list for the invented header. |
| 2557 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2558 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2559 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2562 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2563 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2565 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2566 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2567 | bool HasAnyExplicitSpecHeader = false; |
| 2568 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2569 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2570 | if (ParamLists[I]->size() == 0) |
| 2571 | HasAnyExplicitSpecHeader = true; |
| 2572 | else |
| 2573 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2574 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2575 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2576 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2577 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2578 | : diag::err_template_spec_extra_headers) |
| 2579 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2580 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2581 | |
| 2582 | // If there was a specialization somewhere, such that 'template<>' is |
| 2583 | // not required, and there were any 'template<>' headers, note where the |
| 2584 | // specialization occurred. |
| 2585 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2586 | Diag(ExplicitSpecLoc, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2587 | diag::note_explicit_template_spec_does_not_need_header) |
| 2588 | << NestedTypes.back(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2589 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2590 | // We have a template parameter list with no corresponding scope, which |
| 2591 | // means that the resulting template declaration can't be instantiated |
| 2592 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2593 | if (!AllExplicitSpecHeaders) |
| 2594 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2595 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2597 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2598 | // In an explicit specialization declaration for a member of a class |
| 2599 | // template or a member template that ap- pears in namespace scope, the |
| 2600 | // member template and some of its enclosing class templates may remain |
| 2601 | // unspecialized, except that the declaration shall not explicitly |
| 2602 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2603 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2604 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2605 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2606 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2607 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2608 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2609 | // Return the last template parameter list, which corresponds to the |
| 2610 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2611 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2614 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2615 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2616 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2617 | << (isa<FunctionTemplateDecl>(Template) |
| 2618 | ? 0 |
| 2619 | : isa<ClassTemplateDecl>(Template) |
| 2620 | ? 1 |
| 2621 | : isa<VarTemplateDecl>(Template) |
| 2622 | ? 2 |
| 2623 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2624 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2625 | return; |
| 2626 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2628 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2629 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2630 | IEnd = OST->end(); |
| 2631 | I != IEnd; ++I) |
| 2632 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2633 | << 0 << (*I)->getDeclName(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2634 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2635 | return; |
| 2636 | } |
| 2637 | } |
| 2638 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2639 | static QualType |
| 2640 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2641 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2642 | SourceLocation TemplateLoc, |
| 2643 | TemplateArgumentListInfo &TemplateArgs) { |
| 2644 | ASTContext &Context = SemaRef.getASTContext(); |
| 2645 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2646 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2647 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2648 | // S<T, 0, ..., N-1>. |
| 2649 | |
| 2650 | // C++14 [inteseq.intseq]p1: |
| 2651 | // T shall be an integer type. |
| 2652 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2653 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2654 | diag::err_integer_sequence_integral_element_type); |
| 2655 | return QualType(); |
| 2656 | } |
| 2657 | |
| 2658 | // C++14 [inteseq.make]p1: |
| 2659 | // If N is negative the program is ill-formed. |
| 2660 | TemplateArgument NumArgsArg = Converted[2]; |
| 2661 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2662 | if (NumArgs < 0) { |
| 2663 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2664 | diag::err_integer_sequence_negative_length); |
| 2665 | return QualType(); |
| 2666 | } |
| 2667 | |
| 2668 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2669 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2670 | // The type argument gets reused as the first template argument in the |
| 2671 | // synthetic template argument list. |
| 2672 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2673 | // Expand N into 0 ... N-1. |
| 2674 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2675 | I < NumArgs; ++I) { |
| 2676 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2677 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2678 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2679 | } |
| 2680 | // The first template argument will be reused as the template decl that |
| 2681 | // our synthetic template arguments will be applied to. |
| 2682 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2683 | TemplateLoc, SyntheticTemplateArgs); |
| 2684 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2685 | |
| 2686 | case BTK__type_pack_element: |
| 2687 | // Specializations of |
| 2688 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2689 | // are treated like T_Index. |
| 2690 | assert(Converted.size() == 2 && |
| 2691 | "__type_pack_element should be given an index and a parameter pack"); |
| 2692 | |
| 2693 | // If the Index is out of bounds, the program is ill-formed. |
| 2694 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2695 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2696 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2697 | "type std::size_t, and hence be non-negative"); |
| 2698 | if (Index >= Ts.pack_size()) { |
| 2699 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2700 | diag::err_type_pack_element_out_of_bounds); |
| 2701 | return QualType(); |
| 2702 | } |
| 2703 | |
| 2704 | // We simply return the type at index `Index`. |
| 2705 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2706 | return Nth->getAsType(); |
| 2707 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2708 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2709 | } |
| 2710 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2711 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2712 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2713 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2714 | DependentTemplateName *DTN |
| 2715 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2716 | if (DTN && DTN->isIdentifier()) |
| 2717 | // When building a template-id where the template-name is dependent, |
| 2718 | // assume the template is a type template. Either our assumption is |
| 2719 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2720 | // dependent name is substituted. |
| 2721 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2722 | DTN->getQualifier(), |
| 2723 | DTN->getIdentifier(), |
| 2724 | TemplateArgs); |
| 2725 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2726 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2727 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2728 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2729 | // We might have a substituted template template parameter pack. If so, |
| 2730 | // build a template specialization type for it. |
| 2731 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2732 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2733 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2734 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2735 | << Name; |
| 2736 | NoteAllFoundTemplates(Name); |
| 2737 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2738 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2739 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2740 | // Check that the template argument list is well-formed for this |
| 2741 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2742 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2743 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2744 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2745 | return QualType(); |
| 2746 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2747 | QualType CanonType; |
| 2748 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2749 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2750 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2751 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2752 | // Find the canonical type for this type alias template specialization. |
| 2753 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2754 | if (Pattern->isInvalidDecl()) |
| 2755 | return QualType(); |
| 2756 | |
| 2757 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2758 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2759 | |
| 2760 | // Only substitute for the innermost template argument list. |
| 2761 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2762 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2763 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2764 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2765 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2766 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2767 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2768 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2769 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2770 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2771 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2772 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2773 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2774 | AliasTemplate->getDeclName()); |
| 2775 | if (CanonType.isNull()) |
| 2776 | return QualType(); |
| 2777 | } else if (Name.isDependent() || |
| 2778 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2779 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2780 | // This class template specialization is a dependent |
| 2781 | // type. Therefore, its canonical type is another class template |
| 2782 | // specialization type that contains all of the converted |
| 2783 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2784 | // A<T, T> have identical types when A is declared as: |
| 2785 | // |
| 2786 | // template<typename T, typename U = T> struct A; |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 2787 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2788 | |
| 2789 | // This might work out to be a current instantiation, in which |
| 2790 | // case the canonical type needs to be the InjectedClassNameType. |
| 2791 | // |
| 2792 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2793 | // changes to CurContext don't change the set of current |
| 2794 | // instantiations. |
| 2795 | if (isa<ClassTemplateDecl>(Template)) { |
| 2796 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2797 | // If we get out to a namespace, we're done. |
| 2798 | if (Ctx->isFileContext()) break; |
| 2799 | |
| 2800 | // If this isn't a record, keep looking. |
| 2801 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2802 | if (!Record) continue; |
| 2803 | |
| 2804 | // Look for one of the two cases with InjectedClassNameTypes |
| 2805 | // and check whether it's the same template. |
| 2806 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2807 | !Record->getDescribedClassTemplate()) |
| 2808 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2809 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2810 | // Fetch the injected class name type and check whether its |
| 2811 | // injected type is equal to the type we just built. |
| 2812 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2813 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2814 | ->getInjectedSpecializationType(); |
| 2815 | |
| 2816 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2817 | continue; |
| 2818 | |
| 2819 | // If so, the canonical type of this TST is the injected |
| 2820 | // class name type of the record we just found. |
| 2821 | assert(ICNT.isCanonical()); |
| 2822 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2823 | break; |
| 2824 | } |
| 2825 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2826 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2827 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2828 | // Find the class template specialization declaration that |
| 2829 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2830 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2831 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2832 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2833 | if (!Decl) { |
| 2834 | // This is the first time we have referenced this class template |
| 2835 | // specialization. Create the canonical declaration and add it to |
| 2836 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2838 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2839 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2840 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2841 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2842 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2843 | Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2844 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2845 | if (ClassTemplate->isOutOfLine()) |
| 2846 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame^] | 2849 | if (Decl->getSpecializationKind() == TSK_Undeclared) { |
| 2850 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 2851 | TemplateArgLists.addOuterTemplateArguments(Converted); |
| 2852 | InstantiateAttrsForDecl(TemplateArgLists, ClassTemplate->getTemplatedDecl(), |
| 2853 | Decl); |
| 2854 | } |
| 2855 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2856 | // Diagnose uses of this specialization. |
| 2857 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2858 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2859 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2860 | assert(isa<RecordType>(CanonType) && |
| 2861 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2862 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2863 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2864 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2865 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2867 | // Build the fully-sugared type for this class template |
| 2868 | // specialization, which refers back to the class template |
| 2869 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2870 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2873 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2874 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2875 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
| 2876 | SourceLocation TemplateIILoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2878 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2879 | SourceLocation RAngleLoc, |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 2880 | bool IsCtorOrDtorName, bool IsClassName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2881 | if (SS.isInvalid()) |
| 2882 | return true; |
| 2883 | |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 2884 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { |
| 2885 | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); |
| 2886 | |
| 2887 | // C++ [temp.res]p3: |
| 2888 | // A qualified-id that refers to a type and in which the |
| 2889 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 2890 | // shall be prefixed by the keyword typename to indicate that the |
| 2891 | // qualified-id denotes a type, forming an |
| 2892 | // elaborated-type-specifier (7.1.5.3). |
| 2893 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { |
Richard Smith | 3411fbf | 2017-02-01 21:41:18 +0000 | [diff] [blame] | 2894 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 2895 | << SS.getScopeRep() << TemplateII->getName(); |
| 2896 | // Recover as if 'typename' were specified. |
| 2897 | // FIXME: This is not quite correct recovery as we don't transform SS |
| 2898 | // into the corresponding dependent form (and we don't diagnose missing |
| 2899 | // 'template' keywords within SS as a result). |
| 2900 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
| 2901 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
| 2902 | TemplateArgsIn, RAngleLoc); |
| 2903 | } |
| 2904 | |
| 2905 | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
| 2906 | // it's not actually allowed to be used as a type in most cases. Because |
| 2907 | // we annotate it before we know whether it's valid, we have to check for |
| 2908 | // this case here. |
| 2909 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2910 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 2911 | Diag(TemplateIILoc, |
| 2912 | TemplateKWLoc.isInvalid() |
| 2913 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
| 2914 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 2915 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 2916 | << 1 /*if any keyword was present, it was 'template'*/; |
| 2917 | } |
| 2918 | } |
| 2919 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2920 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2921 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2922 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2923 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2924 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2925 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2926 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2927 | QualType T |
| 2928 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2929 | DTN->getQualifier(), |
| 2930 | DTN->getIdentifier(), |
| 2931 | TemplateArgs); |
| 2932 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2933 | TypeLocBuilder TLB; |
| 2934 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2935 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2936 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2937 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2938 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2939 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2940 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2941 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2942 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2943 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2944 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2945 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2946 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2947 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2948 | if (Result.isNull()) |
| 2949 | return true; |
| 2950 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2951 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2952 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2953 | TemplateSpecializationTypeLoc SpecTL |
| 2954 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2955 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2956 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2957 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2958 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2959 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2960 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2961 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2962 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2963 | // constructor or destructor name (in such a case, the scope specifier |
| 2964 | // will be attached to the enclosing Decl or Expr node). |
| 2965 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2966 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2967 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2968 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2969 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2970 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2971 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2972 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2973 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2974 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2975 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2976 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2977 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2978 | SourceLocation TagLoc, |
| 2979 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2980 | SourceLocation TemplateKWLoc, |
| 2981 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2982 | SourceLocation TemplateLoc, |
| 2983 | SourceLocation LAngleLoc, |
| 2984 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2985 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2986 | TemplateName Template = TemplateD.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2987 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2988 | // Translate the parser's template argument list in our AST format. |
| 2989 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2990 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2992 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2993 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2994 | ElaboratedTypeKeyword Keyword |
| 2995 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2996 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2997 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2998 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2999 | DTN->getQualifier(), |
| 3000 | DTN->getIdentifier(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3001 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3002 | |
| 3003 | // Build type-source information. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3004 | TypeLocBuilder TLB; |
| 3005 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3006 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 3007 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 3008 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3009 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3010 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3011 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3012 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3013 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3014 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3015 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3016 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3017 | |
| 3018 | if (TypeAliasTemplateDecl *TAT = |
| 3019 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 3020 | // C++0x [dcl.type.elab]p2: |
| 3021 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 3022 | // resolves to an alias template specialization, the |
| 3023 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 3024 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 3025 | << TAT << NTK_TypeAliasTemplate << TagKind; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3026 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 3027 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3028 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3029 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 3030 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 3031 | return TypeResult(true); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3032 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3033 | // Check the tag kind |
| 3034 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3035 | RecordDecl *D = RT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3036 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3037 | IdentifierInfo *Id = D->getIdentifier(); |
| 3038 | assert(Id && "templated class must have an identifier"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3039 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 3040 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 3041 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 3042 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3043 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3044 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3045 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3046 | } |
| 3047 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3048 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3049 | // Provide source-location information for the template specialization. |
| 3050 | TypeLocBuilder TLB; |
| 3051 | TemplateSpecializationTypeLoc SpecTL |
| 3052 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3053 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3054 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 3055 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3056 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3057 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3058 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3059 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3060 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3061 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3062 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 3063 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3064 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3065 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3066 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3069 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 3070 | NamedDecl *PrevDecl, |
| 3071 | SourceLocation Loc, |
| 3072 | bool IsPartialSpecialization); |
| 3073 | |
| 3074 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3075 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3076 | static bool isTemplateArgumentTemplateParameter( |
| 3077 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 3078 | switch (Arg.getKind()) { |
| 3079 | case TemplateArgument::Null: |
| 3080 | case TemplateArgument::NullPtr: |
| 3081 | case TemplateArgument::Integral: |
| 3082 | case TemplateArgument::Declaration: |
| 3083 | case TemplateArgument::Pack: |
| 3084 | case TemplateArgument::TemplateExpansion: |
| 3085 | return false; |
| 3086 | |
| 3087 | case TemplateArgument::Type: { |
| 3088 | QualType Type = Arg.getAsType(); |
| 3089 | const TemplateTypeParmType *TPT = |
| 3090 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 3091 | return TPT && !Type.hasQualifiers() && |
| 3092 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 3093 | } |
| 3094 | |
| 3095 | case TemplateArgument::Expression: { |
| 3096 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 3097 | if (!DRE || !DRE->getDecl()) |
| 3098 | return false; |
| 3099 | const NonTypeTemplateParmDecl *NTTP = |
| 3100 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 3101 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 3102 | } |
| 3103 | |
| 3104 | case TemplateArgument::Template: |
| 3105 | const TemplateTemplateParmDecl *TTP = |
| 3106 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 3107 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 3108 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 3109 | } |
| 3110 | llvm_unreachable("unexpected kind of template argument"); |
| 3111 | } |
| 3112 | |
| 3113 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 3114 | ArrayRef<TemplateArgument> Args) { |
| 3115 | if (Params->size() != Args.size()) |
| 3116 | return false; |
| 3117 | |
| 3118 | unsigned Depth = Params->getDepth(); |
| 3119 | |
| 3120 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3121 | TemplateArgument Arg = Args[I]; |
| 3122 | |
| 3123 | // If the parameter is a pack expansion, the argument must be a pack |
| 3124 | // whose only element is a pack expansion. |
| 3125 | if (Params->getParam(I)->isParameterPack()) { |
| 3126 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 3127 | !Arg.pack_begin()->isPackExpansion()) |
| 3128 | return false; |
| 3129 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 3130 | } |
| 3131 | |
| 3132 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 3133 | return false; |
| 3134 | } |
| 3135 | |
| 3136 | return true; |
| 3137 | } |
| 3138 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3139 | /// Convert the parser's template argument list representation into our form. |
| 3140 | static TemplateArgumentListInfo |
| 3141 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 3142 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 3143 | TemplateId.RAngleLoc); |
| 3144 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 3145 | TemplateId.NumArgs); |
| 3146 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 3147 | return TemplateArgs; |
| 3148 | } |
| 3149 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3150 | template<typename PartialSpecDecl> |
| 3151 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 3152 | if (Partial->getDeclContext()->isDependentContext()) |
| 3153 | return; |
| 3154 | |
| 3155 | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
| 3156 | // for non-substitution-failure issues? |
| 3157 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 3158 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 3159 | return; |
| 3160 | |
| 3161 | auto *Template = Partial->getSpecializedTemplate(); |
| 3162 | S.Diag(Partial->getLocation(), |
Richard Smith | fa4a09d | 2016-12-27 20:03:09 +0000 | [diff] [blame] | 3163 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 3164 | << isa<VarTemplateDecl>(Template); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 3165 | |
| 3166 | if (Info.hasSFINAEDiagnostic()) { |
| 3167 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 3168 | PartialDiagnostic::NullDiagnostic()}; |
| 3169 | Info.takeSFINAEDiagnostic(Diag); |
| 3170 | SmallString<128> SFINAEArgString; |
| 3171 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 3172 | S.Diag(Diag.first, |
| 3173 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 3174 | << SFINAEArgString; |
| 3175 | } |
| 3176 | |
| 3177 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 3178 | } |
| 3179 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3180 | static void |
| 3181 | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
| 3182 | const llvm::SmallBitVector &DeducibleParams) { |
| 3183 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3184 | if (!DeducibleParams[I]) { |
| 3185 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3186 | if (Param->getDeclName()) |
| 3187 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3188 | << Param->getDeclName(); |
| 3189 | else |
| 3190 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3191 | << "(anonymous)"; |
| 3192 | } |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3197 | template<typename PartialSpecDecl> |
| 3198 | static void checkTemplatePartialSpecialization(Sema &S, |
| 3199 | PartialSpecDecl *Partial) { |
| 3200 | // C++1z [temp.class.spec]p8: (DR1495) |
| 3201 | // - The specialization shall be more specialized than the primary |
| 3202 | // template (14.5.5.2). |
| 3203 | checkMoreSpecializedThanPrimary(S, Partial); |
| 3204 | |
| 3205 | // C++ [temp.class.spec]p8: (DR1315) |
| 3206 | // - Each template-parameter shall appear at least once in the |
| 3207 | // template-id outside a non-deduced context. |
| 3208 | // C++1z [temp.class.spec.match]p3 (P0127R2) |
| 3209 | // If the template arguments of a partial specialization cannot be |
| 3210 | // deduced because of the structure of its template-parameter-list |
| 3211 | // and the template-id, the program is ill-formed. |
| 3212 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 3213 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3214 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 3215 | TemplateParams->getDepth(), DeducibleParams); |
| 3216 | |
| 3217 | if (!DeducibleParams.all()) { |
| 3218 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3219 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 3220 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 3221 | << (NumNonDeducible > 1) |
| 3222 | << SourceRange(Partial->getLocation(), |
| 3223 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3224 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | void Sema::CheckTemplatePartialSpecialization( |
| 3229 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 3230 | checkTemplatePartialSpecialization(*this, Partial); |
| 3231 | } |
| 3232 | |
| 3233 | void Sema::CheckTemplatePartialSpecialization( |
| 3234 | VarTemplatePartialSpecializationDecl *Partial) { |
| 3235 | checkTemplatePartialSpecialization(*this, Partial); |
| 3236 | } |
| 3237 | |
Richard Smith | 4e05eaa | 2017-02-16 00:36:47 +0000 | [diff] [blame] | 3238 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
| 3239 | // C++1z [temp.param]p11: |
| 3240 | // A template parameter of a deduction guide template that does not have a |
| 3241 | // default-argument shall be deducible from the parameter-type-list of the |
| 3242 | // deduction guide template. |
| 3243 | auto *TemplateParams = TD->getTemplateParameters(); |
| 3244 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3245 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
| 3246 | for (unsigned I = 0; I != TemplateParams->size(); ++I) { |
| 3247 | // A parameter pack is deducible (to an empty pack). |
| 3248 | auto *Param = TemplateParams->getParam(I); |
| 3249 | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)) |
| 3250 | DeducibleParams[I] = true; |
| 3251 | } |
| 3252 | |
| 3253 | if (!DeducibleParams.all()) { |
| 3254 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3255 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
| 3256 | << (NumNonDeducible > 1); |
| 3257 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
| 3258 | } |
| 3259 | } |
| 3260 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3261 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3262 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 3263 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3264 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3265 | // D must be variable template id. |
| 3266 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 3267 | "Variable template specialization is declared with a template it."); |
| 3268 | |
| 3269 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3270 | TemplateArgumentListInfo TemplateArgs = |
| 3271 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3272 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 3273 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 3274 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 3275 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3276 | TemplateName Name = TemplateId->Template.get(); |
| 3277 | |
| 3278 | // The template-id must name a variable template. |
| 3279 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3280 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 3281 | if (!VarTemplate) { |
| 3282 | NamedDecl *FnTemplate; |
| 3283 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 3284 | FnTemplate = *OTS->begin(); |
| 3285 | else |
| 3286 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 3287 | if (FnTemplate) |
| 3288 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 3289 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3290 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 3291 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 3292 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3293 | |
| 3294 | // Check for unexpanded parameter packs in any of the template arguments. |
| 3295 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 3296 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 3297 | UPPC_PartialSpecialization)) |
| 3298 | return true; |
| 3299 | |
| 3300 | // Check that the template argument list is well-formed for this |
| 3301 | // template. |
| 3302 | SmallVector<TemplateArgument, 4> Converted; |
| 3303 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 3304 | false, Converted)) |
| 3305 | return true; |
| 3306 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3307 | // Find the variable template (partial) specialization declaration that |
| 3308 | // corresponds to these arguments. |
| 3309 | if (IsPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3310 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 3311 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3312 | return true; |
| 3313 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3314 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
| 3315 | // also do them during instantiation. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3316 | bool InstantiationDependent; |
| 3317 | if (!Name.isDependent() && |
| 3318 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3319 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3320 | InstantiationDependent)) { |
| 3321 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3322 | << VarTemplate->getDeclName(); |
| 3323 | IsPartialSpecialization = false; |
| 3324 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 3325 | |
| 3326 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 3327 | Converted)) { |
| 3328 | // C++ [temp.class.spec]p9b3: |
| 3329 | // |
| 3330 | // -- The argument list of the specialization shall not be identical |
| 3331 | // to the implicit argument list of the primary template. |
| 3332 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 3333 | << /*variable template*/ 1 |
| 3334 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 3335 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 3336 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 3337 | // of the primary template. |
| 3338 | return true; |
| 3339 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3342 | void *InsertPos = nullptr; |
| 3343 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3344 | |
| 3345 | if (IsPartialSpecialization) |
| 3346 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3347 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3348 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3349 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3350 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3351 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3352 | |
| 3353 | // Check whether we can declare a variable template specialization in |
| 3354 | // the current scope. |
| 3355 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 3356 | TemplateNameLoc, |
| 3357 | IsPartialSpecialization)) |
| 3358 | return true; |
| 3359 | |
| 3360 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 3361 | // Since the only prior variable template specialization with these |
| 3362 | // arguments was referenced but not declared, reuse that |
| 3363 | // declaration node as our own, updating its source location and |
| 3364 | // the list of outer template parameters to reflect our new declaration. |
| 3365 | Specialization = PrevDecl; |
| 3366 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3367 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3368 | } else if (IsPartialSpecialization) { |
| 3369 | // Create a new class template partial specialization declaration node. |
| 3370 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 3371 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3372 | VarTemplatePartialSpecializationDecl *Partial = |
| 3373 | VarTemplatePartialSpecializationDecl::Create( |
| 3374 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 3375 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3376 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3377 | |
| 3378 | if (!PrevPartial) |
| 3379 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 3380 | Specialization = Partial; |
| 3381 | |
| 3382 | // If we are providing an explicit specialization of a member variable |
| 3383 | // template specialization, make a note of that. |
| 3384 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3385 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3386 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3387 | CheckTemplatePartialSpecialization(Partial); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3388 | } else { |
| 3389 | // Create a new class template specialization declaration node for |
| 3390 | // this explicit specialization or friend declaration. |
| 3391 | Specialization = VarTemplateSpecializationDecl::Create( |
| 3392 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3393 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3394 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 3395 | |
| 3396 | if (!PrevDecl) |
| 3397 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 3398 | } |
| 3399 | |
| 3400 | // C++ [temp.expl.spec]p6: |
| 3401 | // If a template, a member template or the member of a class template is |
| 3402 | // explicitly specialized then that specialization shall be declared |
| 3403 | // before the first use of that specialization that would cause an implicit |
| 3404 | // instantiation to take place, in every translation unit in which such a |
| 3405 | // use occurs; no diagnostic is required. |
| 3406 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3407 | bool Okay = false; |
| 3408 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 3409 | // Is there any previous explicit specialization declaration? |
| 3410 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3411 | Okay = true; |
| 3412 | break; |
| 3413 | } |
| 3414 | } |
| 3415 | |
| 3416 | if (!Okay) { |
| 3417 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3418 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3419 | << Name << Range; |
| 3420 | |
| 3421 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3422 | diag::note_instantiation_required_here) |
| 3423 | << (PrevDecl->getTemplateSpecializationKind() != |
| 3424 | TSK_ImplicitInstantiation); |
| 3425 | return true; |
| 3426 | } |
| 3427 | } |
| 3428 | |
| 3429 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 3430 | Specialization->setLexicalDeclContext(CurContext); |
| 3431 | |
| 3432 | // Add the specialization into its lexical context, so that it can |
| 3433 | // be seen when iterating through the list of declarations in that |
| 3434 | // context. However, specializations are not found by name lookup. |
| 3435 | CurContext->addDecl(Specialization); |
| 3436 | |
| 3437 | // Note that this is an explicit specialization. |
| 3438 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 3439 | |
| 3440 | if (PrevDecl) { |
| 3441 | // Check that this isn't a redefinition of this specialization, |
| 3442 | // merging with previous declarations. |
| 3443 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 3444 | ForRedeclaration); |
| 3445 | PrevSpec.addDecl(PrevDecl); |
| 3446 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3447 | } else if (Specialization->isStaticDataMember() && |
| 3448 | Specialization->isOutOfLine()) { |
| 3449 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | // Link instantiations of static data members back to the template from |
| 3453 | // which they were instantiated. |
| 3454 | if (Specialization->isStaticDataMember()) |
| 3455 | Specialization->setInstantiationOfStaticDataMember( |
| 3456 | VarTemplate->getTemplatedDecl(), |
| 3457 | Specialization->getSpecializationKind()); |
| 3458 | |
| 3459 | return Specialization; |
| 3460 | } |
| 3461 | |
| 3462 | namespace { |
| 3463 | /// \brief A partial specialization whose template arguments have matched |
| 3464 | /// a given template-id. |
| 3465 | struct PartialSpecMatchResult { |
| 3466 | VarTemplatePartialSpecializationDecl *Partial; |
| 3467 | TemplateArgumentList *Args; |
| 3468 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 3469 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3470 | |
| 3471 | DeclResult |
| 3472 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3473 | SourceLocation TemplateNameLoc, |
| 3474 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3475 | assert(Template && "A variable template id without template?"); |
| 3476 | |
| 3477 | // Check that the template argument list is well-formed for this template. |
| 3478 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3479 | if (CheckTemplateArgumentList( |
| 3480 | Template, TemplateNameLoc, |
| 3481 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3482 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3483 | return true; |
| 3484 | |
| 3485 | // Find the variable template specialization declaration that |
| 3486 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3487 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3488 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3489 | Converted, InsertPos)) { |
| 3490 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3491 | // If we already have a variable template specialization, return it. |
| 3492 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3493 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3494 | |
| 3495 | // This is the first time we have referenced this variable template |
| 3496 | // specialization. Create the canonical declaration and add it to |
| 3497 | // the set of specializations, based on the closest partial specialization |
| 3498 | // that it represents. That is, |
| 3499 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 3500 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3501 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3502 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 3503 | bool AmbiguousPartialSpec = false; |
| 3504 | typedef PartialSpecMatchResult MatchResult; |
| 3505 | SmallVector<MatchResult, 4> Matched; |
| 3506 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3507 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 3508 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3509 | |
| 3510 | // 1. Attempt to find the closest partial specialization that this |
| 3511 | // specializes, if any. |
| 3512 | // If any of the template arguments is dependent, then this is probably |
| 3513 | // a placeholder for an incomplete declarative context; which must be |
| 3514 | // complete by instantiation time. Thus, do not search through the partial |
| 3515 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3516 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 3517 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 3518 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3519 | bool InstantiationDependent = false; |
| 3520 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3521 | TemplateArgs, InstantiationDependent)) { |
| 3522 | |
| 3523 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3524 | Template->getPartialSpecializations(PartialSpecs); |
| 3525 | |
| 3526 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 3527 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 3528 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 3529 | |
| 3530 | if (TemplateDeductionResult Result = |
| 3531 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 3532 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3533 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3534 | FailedCandidates.addCandidate().set( |
| 3535 | DeclAccessPair::make(Template, AS_public), Partial, |
| 3536 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3537 | (void)Result; |
| 3538 | } else { |
| 3539 | Matched.push_back(PartialSpecMatchResult()); |
| 3540 | Matched.back().Partial = Partial; |
| 3541 | Matched.back().Args = Info.take(); |
| 3542 | } |
| 3543 | } |
| 3544 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3545 | if (Matched.size() >= 1) { |
| 3546 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 3547 | if (Matched.size() == 1) { |
| 3548 | // -- If exactly one matching specialization is found, the |
| 3549 | // instantiation is generated from that specialization. |
| 3550 | // We don't need to do anything for this. |
| 3551 | } else { |
| 3552 | // -- If more than one matching specialization is found, the |
| 3553 | // partial order rules (14.5.4.2) are used to determine |
| 3554 | // whether one of the specializations is more specialized |
| 3555 | // than the others. If none of the specializations is more |
| 3556 | // specialized than all of the other matching |
| 3557 | // specializations, then the use of the variable template is |
| 3558 | // ambiguous and the program is ill-formed. |
| 3559 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 3560 | PEnd = Matched.end(); |
| 3561 | P != PEnd; ++P) { |
| 3562 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 3563 | PointOfInstantiation) == |
| 3564 | P->Partial) |
| 3565 | Best = P; |
| 3566 | } |
| 3567 | |
| 3568 | // Determine if the best partial specialization is more specialized than |
| 3569 | // the others. |
| 3570 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 3571 | PEnd = Matched.end(); |
| 3572 | P != PEnd; ++P) { |
| 3573 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 3574 | P->Partial, Best->Partial, |
| 3575 | PointOfInstantiation) != Best->Partial) { |
| 3576 | AmbiguousPartialSpec = true; |
| 3577 | break; |
| 3578 | } |
| 3579 | } |
| 3580 | } |
| 3581 | |
| 3582 | // Instantiate using the best variable template partial specialization. |
| 3583 | InstantiationPattern = Best->Partial; |
| 3584 | InstantiationArgs = Best->Args; |
| 3585 | } else { |
| 3586 | // -- If no match is found, the instantiation is generated |
| 3587 | // from the primary template. |
| 3588 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 3589 | } |
| 3590 | } |
| 3591 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3592 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3593 | // Note that we do not instantiate a definition until we see an odr-use |
| 3594 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3595 | // FIXME: LateAttrs et al.? |
| 3596 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 3597 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 3598 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 3599 | if (!Decl) |
| 3600 | return true; |
| 3601 | |
| 3602 | if (AmbiguousPartialSpec) { |
| 3603 | // Partial ordering did not produce a clear winner. Complain. |
| 3604 | Decl->setInvalidDecl(); |
| 3605 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 3606 | << Decl; |
| 3607 | |
| 3608 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 3609 | for (MatchResult P : Matched) |
| 3610 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 3611 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 3612 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3613 | return true; |
| 3614 | } |
| 3615 | |
| 3616 | if (VarTemplatePartialSpecializationDecl *D = |
| 3617 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 3618 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 3619 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3620 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 3621 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3622 | assert(Decl && "No variable template specialization?"); |
| 3623 | return Decl; |
| 3624 | } |
| 3625 | |
| 3626 | ExprResult |
| 3627 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 3628 | const DeclarationNameInfo &NameInfo, |
| 3629 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3630 | const TemplateArgumentListInfo *TemplateArgs) { |
| 3631 | |
| 3632 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 3633 | *TemplateArgs); |
| 3634 | if (Decl.isInvalid()) |
| 3635 | return ExprError(); |
| 3636 | |
| 3637 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 3638 | if (!Var->getTemplateSpecializationKind()) |
| 3639 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 3640 | NameInfo.getLoc()); |
| 3641 | |
| 3642 | // Build an ordinary singleton decl ref. |
| 3643 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3644 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3647 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3648 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3649 | LookupResult &R, |
| 3650 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3651 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3652 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 3653 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3654 | // 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] | 3655 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3656 | // foo<int> could identify a single function unambiguously |
| 3657 | // This approach does NOT work, since f<int>(1); |
| 3658 | // gets resolved prior to resorting to overload resolution |
| 3659 | // i.e., template<class T> void f(double); |
| 3660 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3661 | |
| 3662 | // These should be filtered out by our callers. |
| 3663 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 3664 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 3665 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3666 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 3667 | bool InstantiationDependent; |
| 3668 | if (R.getAsSingle<VarTemplateDecl>() && |
| 3669 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3670 | *TemplateArgs, InstantiationDependent)) { |
| 3671 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 3672 | R.getAsSingle<VarTemplateDecl>(), |
| 3673 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3676 | // We don't want lookup warnings at this point. |
| 3677 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3678 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3679 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3680 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3681 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3682 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3683 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3684 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3685 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3686 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3687 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3690 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3691 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3692 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3693 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3694 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3695 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3696 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3697 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3698 | DeclContext *DC; |
| 3699 | if (!(DC = computeDeclContext(SS, false)) || |
| 3700 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3701 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 3702 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3704 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3705 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3706 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3707 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3709 | if (R.isAmbiguous()) |
| 3710 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3711 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3712 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3713 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 3714 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3715 | return ExprError(); |
| 3716 | } |
| 3717 | |
| 3718 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3719 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3720 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 3721 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3722 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 3723 | return ExprError(); |
| 3724 | } |
| 3725 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3726 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3729 | /// \brief Form a dependent template name. |
| 3730 | /// |
| 3731 | /// This action forms a dependent template name given the template |
| 3732 | /// name and its (presumably dependent) scope specifier. For |
| 3733 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 3734 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 3735 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3736 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3737 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3738 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3739 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3740 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3741 | bool EnteringContext, |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 3742 | TemplateTy &Result, |
| 3743 | bool AllowInjectedClassName) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3744 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 3745 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3746 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3747 | diag::warn_cxx98_compat_template_outside_of_template : |
| 3748 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3749 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 3750 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3751 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3752 | if (SS.isSet()) |
| 3753 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 3754 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3755 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3756 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3757 | // C++0x [temp.names]p5: |
| 3758 | // If a name prefixed by the keyword template is not the name of |
| 3759 | // a template, the program is ill-formed. [Note: the keyword |
| 3760 | // template may not be applied to non-template members of class |
| 3761 | // templates. -end note ] [ Note: as is the case with the |
| 3762 | // typename prefix, the template prefix is allowed in cases |
| 3763 | // where it is not strictly necessary; i.e., when the |
| 3764 | // nested-name-specifier or the expression on the left of the -> |
| 3765 | // or . is not dependent on a template-parameter, or the use |
| 3766 | // does not appear in the scope of a template. -end note] |
| 3767 | // |
| 3768 | // Note: C++03 was more strict here, because it banned the use of |
| 3769 | // the "template" keyword prior to a template-name that was not a |
| 3770 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3771 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3772 | // 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] | 3773 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3774 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3775 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3776 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3777 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3778 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3779 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3780 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3781 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3782 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3783 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3784 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3785 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3786 | << Name.getSourceRange() |
| 3787 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3788 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3789 | } else { |
| 3790 | // We found something; return it. |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 3791 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
| 3792 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && |
| 3793 | Name.getKind() == UnqualifiedId::IK_Identifier && Name.Identifier && |
| 3794 | LookupRD->getIdentifier() == Name.Identifier) { |
| 3795 | // C++14 [class.qual]p2: |
| 3796 | // In a lookup in which function names are not ignored and the |
| 3797 | // nested-name-specifier nominates a class C, if the name specified |
| 3798 | // [...] is the injected-class-name of C, [...] the name is instead |
| 3799 | // considered to name the constructor |
| 3800 | // |
| 3801 | // We don't get here if naming the constructor would be valid, so we |
| 3802 | // just reject immediately and recover by treating the |
| 3803 | // injected-class-name as naming the template. |
| 3804 | Diag(Name.getLocStart(), |
| 3805 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 3806 | << Name.Identifier << 0 /*injected-class-name used as template name*/ |
| 3807 | << 1 /*'template' keyword was used*/; |
| 3808 | } |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3809 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3810 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3813 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3814 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3815 | switch (Name.getKind()) { |
| 3816 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3817 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3818 | Name.Identifier)); |
| 3819 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3820 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3821 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3822 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3823 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3824 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3825 | |
| 3826 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3827 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3828 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3829 | default: |
| 3830 | break; |
| 3831 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3832 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3833 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3834 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3835 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3836 | << Name.getSourceRange() |
| 3837 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3838 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3841 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3842 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3843 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3844 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3845 | QualType ArgType; |
| 3846 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3847 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3848 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3849 | switch(Arg.getKind()) { |
| 3850 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3851 | // C++ [temp.arg.type]p1: |
| 3852 | // A template-argument for a template-parameter which is a |
| 3853 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3854 | ArgType = Arg.getAsType(); |
| 3855 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3856 | break; |
| 3857 | case TemplateArgument::Template: { |
| 3858 | // We have a template type parameter but the template argument |
| 3859 | // is a template without any arguments. |
| 3860 | SourceRange SR = AL.getSourceRange(); |
| 3861 | TemplateName Name = Arg.getAsTemplate(); |
| 3862 | Diag(SR.getBegin(), diag::err_template_missing_args) |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 3863 | << (int)getTemplateNameKindForDiagnostics(Name) << Name << SR; |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3864 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3865 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3866 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3867 | return true; |
| 3868 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3869 | case TemplateArgument::Expression: { |
| 3870 | // We have a template type parameter but the template argument is an |
| 3871 | // expression; see if maybe it is missing the "typename" keyword. |
| 3872 | CXXScopeSpec SS; |
| 3873 | DeclarationNameInfo NameInfo; |
| 3874 | |
| 3875 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3876 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3877 | NameInfo = ArgExpr->getNameInfo(); |
| 3878 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3879 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3880 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3881 | NameInfo = ArgExpr->getNameInfo(); |
| 3882 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3883 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3884 | if (ArgExpr->isImplicitAccess()) { |
| 3885 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3886 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3887 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3890 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3891 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3892 | LookupParsedName(Result, CurScope, &SS); |
| 3893 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3894 | if (Result.getAsSingle<TypeDecl>() || |
| 3895 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3896 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3897 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3898 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3899 | Diag(Loc, getLangOpts().MSVCCompat |
| 3900 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3901 | : diag::err_template_arg_must_be_type_suggest) |
| 3902 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3903 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3904 | |
| 3905 | // Recover by synthesizing a type using the location information that we |
| 3906 | // already have. |
| 3907 | ArgType = |
| 3908 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3909 | TypeLocBuilder TLB; |
| 3910 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3911 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3912 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3913 | TL.setNameLoc(NameInfo.getLoc()); |
| 3914 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3915 | |
| 3916 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3917 | // properly. |
| 3918 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3919 | TemplateArgumentLocInfo(TSI)); |
| 3920 | |
| 3921 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | // fallthrough |
| 3925 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3926 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3927 | // We have a template type parameter but the template argument |
| 3928 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3929 | SourceRange SR = AL.getSourceRange(); |
| 3930 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3931 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3933 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3935 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3936 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3937 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3938 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3939 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3940 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3941 | ArgType = Context.getCanonicalType(ArgType); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3942 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3943 | // Objective-C ARC: |
| 3944 | // If an explicitly-specified template argument type is a lifetime type |
| 3945 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3946 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3947 | ArgType->isObjCLifetimeType() && |
| 3948 | !ArgType.getObjCLifetime()) { |
| 3949 | Qualifiers Qs; |
| 3950 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3951 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3952 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3953 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3954 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3955 | return false; |
| 3956 | } |
| 3957 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3958 | /// \brief Substitute template arguments into the default template argument for |
| 3959 | /// the given template type parameter. |
| 3960 | /// |
| 3961 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3962 | /// the substitution. |
| 3963 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3964 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3965 | /// for. |
| 3966 | /// |
| 3967 | /// \param TemplateLoc the location of the template name that started the |
| 3968 | /// template-id we are checking. |
| 3969 | /// |
| 3970 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3971 | /// terminates the template-id. |
| 3972 | /// |
| 3973 | /// \param Param the template template parameter whose default we are |
| 3974 | /// substituting into. |
| 3975 | /// |
| 3976 | /// \param Converted the list of template arguments provided for template |
| 3977 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3978 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3979 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3980 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3981 | TemplateDecl *Template, |
| 3982 | SourceLocation TemplateLoc, |
| 3983 | SourceLocation RAngleLoc, |
| 3984 | TemplateTypeParmDecl *Param, |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 3985 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3986 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3987 | |
| 3988 | // If the argument type is dependent, instantiate it now based |
| 3989 | // on the previously-computed template arguments. |
| 3990 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3991 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3992 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3993 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3994 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3995 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3996 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3997 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3998 | |
| 3999 | // Only substitute for the innermost template argument list. |
| 4000 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4001 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4002 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4003 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4004 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4005 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4006 | ArgType = |
| 4007 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 4008 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | return ArgType; |
| 4012 | } |
| 4013 | |
| 4014 | /// \brief Substitute template arguments into the default template argument for |
| 4015 | /// the given non-type template parameter. |
| 4016 | /// |
| 4017 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4018 | /// the substitution. |
| 4019 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4020 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4021 | /// for. |
| 4022 | /// |
| 4023 | /// \param TemplateLoc the location of the template name that started the |
| 4024 | /// template-id we are checking. |
| 4025 | /// |
| 4026 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4027 | /// terminates the template-id. |
| 4028 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4029 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4030 | /// substituting into. |
| 4031 | /// |
| 4032 | /// \param Converted the list of template arguments provided for template |
| 4033 | /// parameters that precede \p Param in the template parameter list. |
| 4034 | /// |
| 4035 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4036 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4037 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4038 | TemplateDecl *Template, |
| 4039 | SourceLocation TemplateLoc, |
| 4040 | SourceLocation RAngleLoc, |
| 4041 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4042 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4043 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4044 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4045 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4046 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4047 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4048 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4049 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4050 | |
| 4051 | // Only substitute for the innermost template argument list. |
| 4052 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4053 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4054 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4055 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4056 | |
Faisal Vali | 48401eb | 2015-11-19 19:20:17 +0000 | [diff] [blame] | 4057 | EnterExpressionEvaluationContext ConstantEvaluated(SemaRef, |
| 4058 | Sema::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4059 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4062 | /// \brief Substitute template arguments into the default template argument for |
| 4063 | /// the given template template parameter. |
| 4064 | /// |
| 4065 | /// \param SemaRef the semantic analysis object for which we are performing |
| 4066 | /// the substitution. |
| 4067 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4068 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4069 | /// for. |
| 4070 | /// |
| 4071 | /// \param TemplateLoc the location of the template name that started the |
| 4072 | /// template-id we are checking. |
| 4073 | /// |
| 4074 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 4075 | /// terminates the template-id. |
| 4076 | /// |
| 4077 | /// \param Param the template template parameter whose default we are |
| 4078 | /// substituting into. |
| 4079 | /// |
| 4080 | /// \param Converted the list of template arguments provided for template |
| 4081 | /// parameters that precede \p Param in the template parameter list. |
| 4082 | /// |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4083 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4084 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4085 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4086 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 4087 | static TemplateName |
| 4088 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4089 | TemplateDecl *Template, |
| 4090 | SourceLocation TemplateLoc, |
| 4091 | SourceLocation RAngleLoc, |
| 4092 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4093 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4094 | NestedNameSpecifierLoc &QualifierLoc) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4095 | Sema::InstantiatingTemplate Inst( |
| 4096 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 4097 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4098 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4099 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4100 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4101 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4102 | |
| 4103 | // Only substitute for the innermost template argument list. |
| 4104 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4105 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4106 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4107 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4108 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 4109 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4110 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4111 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4112 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4113 | QualifierLoc = |
| 4114 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4115 | if (!QualifierLoc) |
| 4116 | return TemplateName(); |
| 4117 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 4118 | |
| 4119 | return SemaRef.SubstTemplateName( |
| 4120 | QualifierLoc, |
| 4121 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 4122 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 4123 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4126 | /// \brief If the given template parameter has a default template |
| 4127 | /// argument, substitute into that default template argument and |
| 4128 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4129 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4130 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 4131 | SourceLocation TemplateLoc, |
| 4132 | SourceLocation RAngleLoc, |
| 4133 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4134 | SmallVectorImpl<TemplateArgument> |
| 4135 | &Converted, |
| 4136 | bool &HasDefaultArg) { |
| 4137 | HasDefaultArg = false; |
| 4138 | |
| 4139 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4140 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4141 | return TemplateArgumentLoc(); |
| 4142 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4143 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4144 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4145 | TemplateLoc, |
| 4146 | RAngleLoc, |
| 4147 | TypeParm, |
| 4148 | Converted); |
| 4149 | if (DI) |
| 4150 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 4151 | |
| 4152 | return TemplateArgumentLoc(); |
| 4153 | } |
| 4154 | |
| 4155 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 4156 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4157 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4158 | return TemplateArgumentLoc(); |
| 4159 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4160 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4161 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4162 | TemplateLoc, |
| 4163 | RAngleLoc, |
| 4164 | NonTypeParm, |
| 4165 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4166 | if (Arg.isInvalid()) |
| 4167 | return TemplateArgumentLoc(); |
| 4168 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4169 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4170 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 4171 | } |
| 4172 | |
| 4173 | TemplateTemplateParmDecl *TempTempParm |
| 4174 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4175 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4176 | return TemplateArgumentLoc(); |
| 4177 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 4178 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4179 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4180 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4182 | RAngleLoc, |
| 4183 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4184 | Converted, |
| 4185 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4186 | if (TName.isNull()) |
| 4187 | return TemplateArgumentLoc(); |
| 4188 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4189 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4190 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 4191 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 4192 | } |
| 4193 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4194 | /// Convert a template-argument that we parsed as a type into a template, if |
| 4195 | /// possible. C++ permits injected-class-names to perform dual service as |
| 4196 | /// template template arguments and as template type arguments. |
| 4197 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { |
| 4198 | // Extract and step over any surrounding nested-name-specifier. |
| 4199 | NestedNameSpecifierLoc QualLoc; |
| 4200 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { |
| 4201 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
| 4202 | return TemplateArgumentLoc(); |
| 4203 | |
| 4204 | QualLoc = ETLoc.getQualifierLoc(); |
| 4205 | TLoc = ETLoc.getNamedTypeLoc(); |
| 4206 | } |
| 4207 | |
| 4208 | // If this type was written as an injected-class-name, it can be used as a |
| 4209 | // template template argument. |
| 4210 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) |
| 4211 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), |
| 4212 | QualLoc, InjLoc.getNameLoc()); |
| 4213 | |
| 4214 | // If this type was written as an injected-class-name, it may have been |
| 4215 | // converted to a RecordType during instantiation. If the RecordType is |
| 4216 | // *not* wrapped in a TemplateSpecializationType and denotes a class |
| 4217 | // template specialization, it must have come from an injected-class-name. |
| 4218 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) |
| 4219 | if (auto *CTSD = |
| 4220 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
| 4221 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), |
| 4222 | QualLoc, RecLoc.getNameLoc()); |
| 4223 | |
| 4224 | return TemplateArgumentLoc(); |
| 4225 | } |
| 4226 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4227 | /// \brief Check that the given template argument corresponds to the given |
| 4228 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4229 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4231 | /// checked. |
| 4232 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4233 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4234 | /// |
| 4235 | /// \param Template The template in which the template argument resides. |
| 4236 | /// |
| 4237 | /// \param TemplateLoc The location of the template name for the template |
| 4238 | /// whose argument list we're matching. |
| 4239 | /// |
| 4240 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 4241 | /// the template argument list. |
| 4242 | /// |
| 4243 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 4244 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 4245 | /// |
| 4246 | /// \param Converted The checked, converted argument will be added to the |
| 4247 | /// end of this small vector. |
| 4248 | /// |
| 4249 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 4250 | /// explicitly written, deduced, etc. |
| 4251 | /// |
| 4252 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4253 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 4254 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4255 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4256 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4257 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4258 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4259 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4260 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4261 | // Check template type parameters. |
| 4262 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4263 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4264 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4265 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4266 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4267 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4268 | // with the template arguments we've seen thus far. But if the |
| 4269 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4270 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4271 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 4272 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 4274 | if (NTTPType->isDependentType() && |
| 4275 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4276 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4277 | // Do substitution on the type of the non-type template parameter. |
| 4278 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4279 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4280 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4281 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4282 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4283 | |
| 4284 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4285 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4286 | NTTPType = SubstType(NTTPType, |
| 4287 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 4288 | NTTP->getLocation(), |
| 4289 | NTTP->getDeclName()); |
| 4290 | // If that worked, check the non-type template parameter type |
| 4291 | // for validity. |
| 4292 | if (!NTTPType.isNull()) |
| 4293 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 4294 | NTTP->getLocation()); |
| 4295 | if (NTTPType.isNull()) |
| 4296 | return true; |
| 4297 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4298 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4299 | switch (Arg.getArgument().getKind()) { |
| 4300 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4301 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4303 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4304 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4305 | ExprResult Res = |
| 4306 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 4307 | Result, CTAK); |
| 4308 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4309 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4310 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4311 | // If the resulting expression is new, then use it in place of the |
| 4312 | // old expression in the template argument. |
| 4313 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 4314 | TemplateArgument TA(Res.get()); |
| 4315 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 4316 | } |
| 4317 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4318 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4319 | break; |
| 4320 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4321 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4322 | case TemplateArgument::Declaration: |
| 4323 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4324 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4325 | // We've already checked this template argument, so just copy |
| 4326 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4327 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4328 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4329 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4330 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4331 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4332 | // We were given a template template argument. It may not be ill-formed; |
| 4333 | // see below. |
| 4334 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4335 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 4336 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4337 | // We have a template argument such as \c T::template X, which we |
| 4338 | // parsed as a template template argument. However, since we now |
| 4339 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4340 | // template name into an expression. |
| 4341 | |
| 4342 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 4343 | Arg.getTemplateNameLoc()); |
| 4344 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 4345 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4346 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4347 | // FIXME: the template-template arg was a DependentTemplateName, |
| 4348 | // so it was provided with a template keyword. However, its source |
| 4349 | // location is not stored in the template argument structure. |
| 4350 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4351 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 4352 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 4353 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4354 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4355 | // If we parsed the template argument as a pack expansion, create a |
| 4356 | // pack expansion expression. |
| 4357 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4358 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4359 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4360 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4361 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4362 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4363 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4364 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4365 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4366 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4368 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4369 | break; |
| 4370 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4371 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4372 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4373 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4374 | // therefore cannot be a non-type template argument. |
| 4375 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 4376 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4377 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4378 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4379 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4381 | case TemplateArgument::Type: { |
| 4382 | // We have a non-type template parameter but the template |
| 4383 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4384 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4385 | // C++ [temp.arg]p2: |
| 4386 | // In a template-argument, an ambiguity between a type-id and |
| 4387 | // an expression is resolved to a type-id, regardless of the |
| 4388 | // form of the corresponding template-parameter. |
| 4389 | // |
| 4390 | // We warn specifically about this case, since it can be rather |
| 4391 | // confusing for users. |
| 4392 | QualType T = Arg.getArgument().getAsType(); |
| 4393 | SourceRange SR = Arg.getSourceRange(); |
| 4394 | if (T->isFunctionType()) |
| 4395 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 4396 | else |
| 4397 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 4398 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4399 | return true; |
| 4400 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4401 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4402 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4403 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4404 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4405 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4406 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4410 | // Check template template parameters. |
| 4411 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4412 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4413 | // Substitute into the template parameter list of the template |
| 4414 | // template parameter, since previously-supplied template arguments |
| 4415 | // may appear within the template template parameter. |
| 4416 | { |
| 4417 | // Set up a template instantiation context. |
| 4418 | LocalInstantiationScope Scope(*this); |
| 4419 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4420 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4421 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4422 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4423 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 4425 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4426 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4427 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4428 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 4429 | if (!TempParm) |
| 4430 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4431 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4432 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4433 | // C++1z [temp.local]p1: (DR1004) |
| 4434 | // When [the injected-class-name] is used [...] as a template-argument for |
| 4435 | // a template template-parameter [...] it refers to the class template |
| 4436 | // itself. |
| 4437 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { |
| 4438 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
| 4439 | Arg.getTypeSourceInfo()->getTypeLoc()); |
| 4440 | if (!ConvertedArg.getArgument().isNull()) |
| 4441 | Arg = ConvertedArg; |
| 4442 | } |
| 4443 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4444 | switch (Arg.getArgument().getKind()) { |
| 4445 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4446 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4447 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4448 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4449 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4450 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4451 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4453 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4454 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4455 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4456 | case TemplateArgument::Expression: |
| 4457 | case TemplateArgument::Type: |
| 4458 | // We have a template template parameter but the template |
| 4459 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4460 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4461 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4462 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4463 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4464 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4465 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4466 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4467 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4468 | case TemplateArgument::NullPtr: |
| 4469 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4471 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4472 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4473 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4475 | return false; |
| 4476 | } |
| 4477 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4478 | /// \brief Diagnose an arity mismatch in the |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4479 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 4480 | SourceLocation TemplateLoc, |
| 4481 | TemplateArgumentListInfo &TemplateArgs) { |
| 4482 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 4483 | unsigned NumParams = Params->size(); |
| 4484 | unsigned NumArgs = TemplateArgs.size(); |
| 4485 | |
| 4486 | SourceRange Range; |
| 4487 | if (NumArgs > NumParams) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4488 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4489 | TemplateArgs.getRAngleLoc()); |
| 4490 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 4491 | << (NumArgs > NumParams) |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 4492 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4493 | << Template << Range; |
| 4494 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 4495 | << Params->getSourceRange(); |
| 4496 | return true; |
| 4497 | } |
| 4498 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4499 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 4500 | /// determine the number of parameters produced by that expansion. For instance: |
| 4501 | /// |
| 4502 | /// \code |
| 4503 | /// template<typename ...Ts> struct A { |
| 4504 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 4505 | /// }; |
| 4506 | /// \endcode |
| 4507 | /// |
| 4508 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 4509 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4510 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4511 | if (NonTypeTemplateParmDecl *NTTP |
| 4512 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4513 | if (NTTP->isExpandedParameterPack()) |
| 4514 | return NTTP->getNumExpansionTypes(); |
| 4515 | } |
| 4516 | |
| 4517 | if (TemplateTemplateParmDecl *TTP |
| 4518 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 4519 | if (TTP->isExpandedParameterPack()) |
| 4520 | return TTP->getNumExpansionTemplateParameters(); |
| 4521 | } |
| 4522 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 4523 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4526 | /// Diagnose a missing template argument. |
| 4527 | template<typename TemplateParmDecl> |
| 4528 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 4529 | TemplateDecl *TD, |
| 4530 | const TemplateParmDecl *D, |
| 4531 | TemplateArgumentListInfo &Args) { |
| 4532 | // Dig out the most recent declaration of the template parameter; there may be |
| 4533 | // declarations of the template that are more recent than TD. |
| 4534 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 4535 | ->getTemplateParameters() |
| 4536 | ->getParam(D->getIndex())); |
| 4537 | |
| 4538 | // If there's a default argument that's not visible, diagnose that we're |
| 4539 | // missing a module import. |
| 4540 | llvm::SmallVector<Module*, 8> Modules; |
| 4541 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 4542 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 4543 | D->getDefaultArgumentLoc(), Modules, |
| 4544 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4545 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4546 | return true; |
| 4547 | } |
| 4548 | |
| 4549 | // FIXME: If there's a more recent default argument that *is* visible, |
| 4550 | // diagnose that it was declared too late. |
| 4551 | |
| 4552 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 4553 | } |
| 4554 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4555 | /// \brief Check that the given template argument list is well-formed |
| 4556 | /// for specializing the given template. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4557 | bool Sema::CheckTemplateArgumentList( |
| 4558 | TemplateDecl *Template, SourceLocation TemplateLoc, |
| 4559 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
| 4560 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4561 | bool UpdateArgsWithConversions) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4562 | // Make a copy of the template arguments for processing. Only make the |
| 4563 | // changes at the end when successful in matching the arguments to the |
| 4564 | // template. |
| 4565 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 4566 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4567 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4568 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4569 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4570 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4571 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4572 | // [...] The type and form of each template-argument specified in |
| 4573 | // a template-id shall match the type and form specified for the |
| 4574 | // corresponding parameter declared by the template in its |
| 4575 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4576 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4577 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4578 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4579 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4580 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4581 | ParamEnd = Params->end(); |
| 4582 | Param != ParamEnd; /* increment in loop */) { |
| 4583 | // If we have an expanded parameter pack, make sure we don't have too |
| 4584 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4585 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4586 | if (*Expansions == ArgumentPack.size()) { |
| 4587 | // We're done with this parameter pack. Pack up its arguments and add |
| 4588 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4589 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4590 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4591 | ArgumentPack.clear(); |
| 4592 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4593 | // This argument is assigned to the next parameter. |
| 4594 | ++Param; |
| 4595 | continue; |
| 4596 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 4597 | // Not enough arguments for this parameter pack. |
| 4598 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 4599 | << false |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 4600 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4601 | << Template; |
| 4602 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 4603 | << Params->getSourceRange(); |
| 4604 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4605 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4606 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4607 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4608 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4609 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4610 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4611 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4612 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4613 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4614 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4615 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4616 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4617 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 4618 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4619 | // 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] | 4620 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4621 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4622 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4623 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4624 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4625 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 4626 | return true; |
| 4627 | } |
| 4628 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4629 | // We're now done with this argument. |
| 4630 | ++ArgIdx; |
| 4631 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4632 | if ((*Param)->isTemplateParameterPack()) { |
| 4633 | // The template parameter was a template parameter pack, so take the |
| 4634 | // deduced argument and place it on the argument pack. Note that we |
| 4635 | // stay on the same template parameter so that we can deduce more |
| 4636 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 4637 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4638 | } else { |
| 4639 | // Move to the next template parameter. |
| 4640 | ++Param; |
| 4641 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4642 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4643 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 4644 | // the remaining arguments, because we don't know what parameters they'll |
| 4645 | // match up with. |
| 4646 | if (PackExpansionIntoNonPack) { |
| 4647 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4648 | // If we were part way through filling in an expanded parameter pack, |
| 4649 | // fall back to just producing individual arguments. |
| 4650 | Converted.insert(Converted.end(), |
| 4651 | ArgumentPack.begin(), ArgumentPack.end()); |
| 4652 | ArgumentPack.clear(); |
| 4653 | } |
| 4654 | |
| 4655 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4656 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4657 | ++ArgIdx; |
| 4658 | } |
| 4659 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4660 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4661 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4662 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4663 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4664 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4665 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 4666 | // If we're checking a partial template argument list, we're done. |
| 4667 | if (PartialTemplateArgs) { |
| 4668 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4669 | Converted.push_back( |
| 4670 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 4671 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4672 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4675 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4676 | // 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] | 4677 | if ((*Param)->isTemplateParameterPack()) { |
| 4678 | assert(!getExpandedPackSize(*Param) && |
| 4679 | "Should have dealt with this already"); |
| 4680 | |
| 4681 | // A non-expanded parameter pack before the end of the parameter list |
| 4682 | // only occurs for an ill-formed template parameter list, unless we've |
| 4683 | // got a partial argument list for a function template, so just bail out. |
| 4684 | if (Param + 1 != ParamEnd) |
| 4685 | return true; |
| 4686 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4687 | Converted.push_back( |
| 4688 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4689 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4690 | |
| 4691 | ++Param; |
| 4692 | continue; |
| 4693 | } |
| 4694 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4695 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4696 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4697 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4698 | // Retrieve the default template argument from the template |
| 4699 | // parameter. For each kind of template parameter, we substitute the |
| 4700 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4701 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4702 | // the default argument. |
| 4703 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4704 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4705 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 4706 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4707 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4708 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4709 | Template, |
| 4710 | TemplateLoc, |
| 4711 | RAngleLoc, |
| 4712 | TTP, |
| 4713 | Converted); |
| 4714 | if (!ArgType) |
| 4715 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4716 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4717 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 4718 | ArgType); |
| 4719 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4720 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4721 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4722 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 4723 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4724 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4725 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4726 | TemplateLoc, |
| 4727 | RAngleLoc, |
| 4728 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4729 | Converted); |
| 4730 | if (E.isInvalid()) |
| 4731 | return true; |
| 4732 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4733 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4734 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 4735 | } else { |
| 4736 | TemplateTemplateParmDecl *TempParm |
| 4737 | = cast<TemplateTemplateParmDecl>(*Param); |
| 4738 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4739 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4740 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 4741 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4742 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4743 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4744 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4745 | TemplateLoc, |
| 4746 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4747 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4748 | Converted, |
| 4749 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4750 | if (Name.isNull()) |
| 4751 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4752 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4753 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 4754 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4755 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4757 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4758 | // the default template argument. We're not actually instantiating a |
| 4759 | // template here, we just create this object to put a note into the |
| 4760 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4761 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 4762 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4763 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4764 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4765 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4766 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4767 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4768 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4769 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4770 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4771 | // Core issue 150 (assumed resolution): if this is a template template |
| 4772 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4773 | // template definition. |
| 4774 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4775 | NewArgs.addArgument(Arg); |
| 4776 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4777 | // Move to the next template parameter and argument. |
| 4778 | ++Param; |
| 4779 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4780 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4781 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4782 | // If we're performing a partial argument substitution, allow any trailing |
| 4783 | // pack expansions; they might be empty. This can happen even if |
| 4784 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 4785 | // still dependent). |
| 4786 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 4787 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4788 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 4789 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4792 | // If we have any leftover arguments, then there were too many arguments. |
| 4793 | // Complain and fail. |
| 4794 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4795 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 4796 | |
| 4797 | // No problems found with the new argument list, propagate changes back |
| 4798 | // to caller. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4799 | if (UpdateArgsWithConversions) |
| 4800 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4801 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4802 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4805 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | class UnnamedLocalNoLinkageFinder |
| 4807 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4808 | { |
| 4809 | Sema &S; |
| 4810 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4811 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4812 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4813 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4814 | public: |
| 4815 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 4816 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4817 | bool Visit(QualType T) { |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 4818 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4819 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4820 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4821 | #define TYPE(Class, Parent) \ |
| 4822 | bool Visit##Class##Type(const Class##Type *); |
| 4823 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4824 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4825 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4826 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4827 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4828 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4829 | bool VisitTagDecl(const TagDecl *Tag); |
| 4830 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4831 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4832 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4833 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4834 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4835 | return false; |
| 4836 | } |
| 4837 | |
| 4838 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4839 | return Visit(T->getElementType()); |
| 4840 | } |
| 4841 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4842 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4843 | return Visit(T->getPointeeType()); |
| 4844 | } |
| 4845 | |
| 4846 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4847 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4848 | return Visit(T->getPointeeType()); |
| 4849 | } |
| 4850 | |
| 4851 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4852 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4853 | return Visit(T->getPointeeType()); |
| 4854 | } |
| 4855 | |
| 4856 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4857 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4858 | return Visit(T->getPointeeType()); |
| 4859 | } |
| 4860 | |
| 4861 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4862 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4863 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4864 | } |
| 4865 | |
| 4866 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4867 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4868 | return Visit(T->getElementType()); |
| 4869 | } |
| 4870 | |
| 4871 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4872 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4873 | return Visit(T->getElementType()); |
| 4874 | } |
| 4875 | |
| 4876 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4877 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4878 | return Visit(T->getElementType()); |
| 4879 | } |
| 4880 | |
| 4881 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4882 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4883 | return Visit(T->getElementType()); |
| 4884 | } |
| 4885 | |
| 4886 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4887 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4888 | return Visit(T->getElementType()); |
| 4889 | } |
| 4890 | |
| 4891 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4892 | return Visit(T->getElementType()); |
| 4893 | } |
| 4894 | |
| 4895 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4896 | return Visit(T->getElementType()); |
| 4897 | } |
| 4898 | |
| 4899 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4900 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4901 | for (const auto &A : T->param_types()) { |
| 4902 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4903 | return true; |
| 4904 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4905 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4906 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4910 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4911 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4915 | const UnresolvedUsingType*) { |
| 4916 | return false; |
| 4917 | } |
| 4918 | |
| 4919 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4920 | return false; |
| 4921 | } |
| 4922 | |
| 4923 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4924 | return Visit(T->getUnderlyingType()); |
| 4925 | } |
| 4926 | |
| 4927 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4928 | return false; |
| 4929 | } |
| 4930 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4931 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4932 | const UnaryTransformType*) { |
| 4933 | return false; |
| 4934 | } |
| 4935 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4936 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4937 | return Visit(T->getDeducedType()); |
| 4938 | } |
| 4939 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 4940 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
| 4941 | const DeducedTemplateSpecializationType *T) { |
| 4942 | return Visit(T->getDeducedType()); |
| 4943 | } |
| 4944 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4945 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4946 | return VisitTagDecl(T->getDecl()); |
| 4947 | } |
| 4948 | |
| 4949 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4950 | return VisitTagDecl(T->getDecl()); |
| 4951 | } |
| 4952 | |
| 4953 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4954 | const TemplateTypeParmType*) { |
| 4955 | return false; |
| 4956 | } |
| 4957 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4958 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4959 | const SubstTemplateTypeParmPackType *) { |
| 4960 | return false; |
| 4961 | } |
| 4962 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4963 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4964 | const TemplateSpecializationType*) { |
| 4965 | return false; |
| 4966 | } |
| 4967 | |
| 4968 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4969 | const InjectedClassNameType* T) { |
| 4970 | return VisitTagDecl(T->getDecl()); |
| 4971 | } |
| 4972 | |
| 4973 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4974 | const DependentNameType* T) { |
| 4975 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4976 | } |
| 4977 | |
| 4978 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4979 | const DependentTemplateSpecializationType* T) { |
| 4980 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4981 | } |
| 4982 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4983 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4984 | const PackExpansionType* T) { |
| 4985 | return Visit(T->getPattern()); |
| 4986 | } |
| 4987 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4988 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4989 | return false; |
| 4990 | } |
| 4991 | |
| 4992 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4993 | const ObjCInterfaceType *) { |
| 4994 | return false; |
| 4995 | } |
| 4996 | |
| 4997 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4998 | const ObjCObjectPointerType *) { |
| 4999 | return false; |
| 5000 | } |
| 5001 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5002 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 5003 | return Visit(T->getValueType()); |
| 5004 | } |
| 5005 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5006 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 5007 | return false; |
| 5008 | } |
| 5009 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5010 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 5011 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5012 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5013 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5014 | diag::warn_cxx98_compat_template_arg_local_type : |
| 5015 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5016 | << S.Context.getTypeDeclType(Tag) << SR; |
| 5017 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 5020 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5021 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5022 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5023 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 5024 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5025 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 5026 | return true; |
| 5027 | } |
| 5028 | |
| 5029 | return false; |
| 5030 | } |
| 5031 | |
| 5032 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 5033 | NestedNameSpecifier *NNS) { |
| 5034 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 5035 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5036 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5037 | switch (NNS->getKind()) { |
| 5038 | case NestedNameSpecifier::Identifier: |
| 5039 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5040 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5041 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5042 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5043 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5044 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5045 | case NestedNameSpecifier::TypeSpec: |
| 5046 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 5047 | return Visit(QualType(NNS->getAsType(), 0)); |
| 5048 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5049 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5052 | /// \brief Check a template argument against its corresponding |
| 5053 | /// template type parameter. |
| 5054 | /// |
| 5055 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 5056 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5057 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5058 | TypeSourceInfo *ArgInfo) { |
| 5059 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5060 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 5061 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 5062 | |
| 5063 | if (Arg->isVariablyModifiedType()) { |
| 5064 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5065 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5066 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5069 | // C++03 [temp.arg.type]p2: |
| 5070 | // A local type, a type with no linkage, an unnamed type or a type |
| 5071 | // compounded from any of these types shall not be used as a |
| 5072 | // template-argument for a template type-parameter. |
| 5073 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5074 | // 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] | 5075 | // a warning. |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 5076 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 5077 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 5078 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 5079 | } |
| 5080 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5081 | return false; |
| 5082 | } |
| 5083 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5084 | enum NullPointerValueKind { |
| 5085 | NPV_NotNullPointer, |
| 5086 | NPV_NullPointer, |
| 5087 | NPV_Error |
| 5088 | }; |
| 5089 | |
| 5090 | /// \brief Determine whether the given template argument is a null pointer |
| 5091 | /// value of the appropriate type. |
| 5092 | static NullPointerValueKind |
| 5093 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 5094 | QualType ParamType, Expr *Arg) { |
| 5095 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 5096 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5097 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5098 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 5099 | llvm_unreachable( |
| 5100 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 5101 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 5102 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5103 | return NPV_NotNullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5104 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5105 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5106 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 5107 | if (ArgRV.isInvalid()) |
| 5108 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5109 | Arg = ArgRV.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5110 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5111 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5112 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5113 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5114 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5115 | EvalResult.HasSideEffects) { |
| 5116 | SourceLocation DiagLoc = Arg->getExprLoc(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5117 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5118 | // If our only note is the usual "invalid subexpression" note, just point |
| 5119 | // the caret at its location rather than producing an essentially |
| 5120 | // redundant note. |
| 5121 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 5122 | diag::note_invalid_subexpr_in_const_expr) { |
| 5123 | DiagLoc = Notes[0].first; |
| 5124 | Notes.clear(); |
| 5125 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5126 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5127 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 5128 | << Arg->getType() << Arg->getSourceRange(); |
| 5129 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 5130 | S.Diag(Notes[I].first, Notes[I].second); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5131 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 5132 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5133 | return NPV_Error; |
| 5134 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5135 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5136 | // C++11 [temp.arg.nontype]p1: |
| 5137 | // - an address constant expression of type std::nullptr_t |
| 5138 | if (Arg->getType()->isNullPtrType()) |
| 5139 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5140 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5141 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 5142 | // - a constant expression that evaluates to a null member pointer value |
| 5143 | // (4.11); or |
| 5144 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 5145 | (EvalResult.Val.isMemberPointer() && |
| 5146 | !EvalResult.Val.getMemberPointerDecl())) { |
| 5147 | // If our expression has an appropriate type, we've succeeded. |
| 5148 | bool ObjCLifetimeConversion; |
| 5149 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 5150 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 5151 | ObjCLifetimeConversion)) |
| 5152 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5153 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5154 | // The types didn't match, but we know we got a null pointer; complain, |
| 5155 | // then recover as if the types were correct. |
| 5156 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 5157 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 5158 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5159 | return NPV_NullPointer; |
| 5160 | } |
| 5161 | |
| 5162 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 5163 | // constant, suggest a cast to the appropriate type. |
| 5164 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 5165 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 5166 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5167 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 5168 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 5169 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5170 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5171 | return NPV_NullPointer; |
| 5172 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5173 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5174 | // FIXME: If we ever want to support general, address-constant expressions |
| 5175 | // as non-type template arguments, we should return the ExprResult here to |
| 5176 | // be interpreted by the caller. |
| 5177 | return NPV_NotNullPointer; |
| 5178 | } |
| 5179 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5180 | /// \brief Checks whether the given template argument is compatible with its |
| 5181 | /// template parameter. |
| 5182 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 5183 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 5184 | Expr *Arg, QualType ArgType) { |
| 5185 | bool ObjCLifetimeConversion; |
| 5186 | if (ParamType->isPointerType() && |
| 5187 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 5188 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 5189 | ObjCLifetimeConversion)) { |
| 5190 | // For pointer-to-object types, qualification conversions are |
| 5191 | // permitted. |
| 5192 | } else { |
| 5193 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 5194 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 5195 | // C++ [temp.arg.nontype]p5b3: |
| 5196 | // For a non-type template-parameter of type reference to |
| 5197 | // object, no conversions apply. The type referred to by the |
| 5198 | // reference may be more cv-qualified than the (otherwise |
| 5199 | // identical) type of the template- argument. The |
| 5200 | // template-parameter is bound directly to the |
| 5201 | // template-argument, which shall be an lvalue. |
| 5202 | |
| 5203 | // FIXME: Other qualifiers? |
| 5204 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 5205 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 5206 | |
| 5207 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 5208 | S.Diag(Arg->getLocStart(), |
| 5209 | diag::err_template_arg_ref_bind_ignores_quals) |
| 5210 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 5211 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5212 | return true; |
| 5213 | } |
| 5214 | } |
| 5215 | } |
| 5216 | |
| 5217 | // At this point, the template argument refers to an object or |
| 5218 | // function with external linkage. We now need to check whether the |
| 5219 | // argument and parameter types are compatible. |
| 5220 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 5221 | ParamType.getNonReferenceType())) { |
| 5222 | // We can't perform this conversion or binding. |
| 5223 | if (ParamType->isReferenceType()) |
| 5224 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 5225 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 5226 | else |
| 5227 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 5228 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 5229 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5230 | return true; |
| 5231 | } |
| 5232 | } |
| 5233 | |
| 5234 | return false; |
| 5235 | } |
| 5236 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5237 | /// \brief Checks whether the given template argument is the address |
| 5238 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5239 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5240 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 5241 | NonTypeTemplateParmDecl *Param, |
| 5242 | QualType ParamType, |
| 5243 | Expr *ArgIn, |
| 5244 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5245 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5246 | Expr *Arg = ArgIn; |
| 5247 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5248 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5249 | bool AddressTaken = false; |
| 5250 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5251 | if (S.getLangOpts().MicrosoftExt) { |
| 5252 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 5253 | // dereference and address-of operators. |
| 5254 | Arg = Arg->IgnoreParenCasts(); |
| 5255 | |
| 5256 | bool ExtWarnMSTemplateArg = false; |
| 5257 | UnaryOperatorKind FirstOpKind; |
| 5258 | SourceLocation FirstOpLoc; |
| 5259 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5260 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 5261 | if (UnOpKind == UO_Deref) |
| 5262 | ExtWarnMSTemplateArg = true; |
| 5263 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 5264 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 5265 | if (!AddrOpLoc.isValid()) { |
| 5266 | FirstOpKind = UnOpKind; |
| 5267 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 5268 | } |
| 5269 | } else |
| 5270 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5271 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5272 | if (FirstOpLoc.isValid()) { |
| 5273 | if (ExtWarnMSTemplateArg) |
| 5274 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 5275 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5276 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5277 | if (FirstOpKind == UO_AddrOf) |
| 5278 | AddressTaken = true; |
| 5279 | else if (Arg->getType()->isPointerType()) { |
| 5280 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 5281 | // constant expression. |
| 5282 | assert(FirstOpKind == UO_Deref); |
| 5283 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5284 | << Arg->getSourceRange(); |
| 5285 | } |
| 5286 | } |
| 5287 | } else { |
| 5288 | // See through any implicit casts we added to fix the type. |
| 5289 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5290 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5291 | // C++ [temp.arg.nontype]p1: |
| 5292 | // |
| 5293 | // A template-argument for a non-type, non-template |
| 5294 | // template-parameter shall be one of: [...] |
| 5295 | // |
| 5296 | // -- the address of an object or function with external |
| 5297 | // linkage, including function templates and function |
| 5298 | // template-ids but excluding non-static class members, |
| 5299 | // expressed as & id-expression where the & is optional if |
| 5300 | // the name refers to a function or array, or if the |
| 5301 | // corresponding template-parameter is a reference; or |
| 5302 | |
| 5303 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 5304 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 5305 | bool ExtraParens = false; |
| 5306 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 5307 | if (!Invalid && !ExtraParens) { |
| 5308 | S.Diag(Arg->getLocStart(), |
| 5309 | S.getLangOpts().CPlusPlus11 |
| 5310 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 5311 | : diag::ext_template_arg_extra_parens) |
| 5312 | << Arg->getSourceRange(); |
| 5313 | ExtraParens = true; |
| 5314 | } |
| 5315 | |
| 5316 | Arg = Parens->getSubExpr(); |
| 5317 | } |
| 5318 | |
| 5319 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5320 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5321 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5322 | |
| 5323 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5324 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 5325 | Arg = UnOp->getSubExpr(); |
| 5326 | AddressTaken = true; |
| 5327 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 5328 | } |
| 5329 | } |
| 5330 | |
| 5331 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5332 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5333 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5334 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5335 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5336 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 5337 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 5338 | |
| 5339 | // If our parameter has pointer type, check for a null template value. |
| 5340 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 5341 | NullPointerValueKind NPV; |
| 5342 | // dllimport'd entities aren't constant but are available inside of template |
| 5343 | // arguments. |
| 5344 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 5345 | NPV = NPV_NotNullPointer; |
| 5346 | else |
| 5347 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 5348 | switch (NPV) { |
| 5349 | case NPV_NullPointer: |
| 5350 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5351 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 5352 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 5353 | return false; |
| 5354 | |
| 5355 | case NPV_Error: |
| 5356 | return true; |
| 5357 | |
| 5358 | case NPV_NotNullPointer: |
| 5359 | break; |
| 5360 | } |
| 5361 | } |
| 5362 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5363 | // Stop checking the precise nature of the argument if it is value dependent, |
| 5364 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5365 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5366 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5367 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5368 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5369 | |
| 5370 | if (isa<CXXUuidofExpr>(Arg)) { |
| 5371 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 5372 | ArgIn, Arg, ArgType)) |
| 5373 | return true; |
| 5374 | |
| 5375 | Converted = TemplateArgument(ArgIn); |
| 5376 | return false; |
| 5377 | } |
| 5378 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5379 | if (!DRE) { |
| 5380 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5381 | << Arg->getSourceRange(); |
| 5382 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5383 | return true; |
| 5384 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 5385 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5386 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 5387 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5388 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 5389 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5390 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5391 | return true; |
| 5392 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5393 | |
| 5394 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5395 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5396 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5397 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5398 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5399 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5400 | return true; |
| 5401 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5402 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5403 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5404 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 5405 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5406 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5407 | // A non-type template argument must refer to an object or function. |
| 5408 | if (!Func && !Var) { |
| 5409 | // We found something, but we don't know specifically what it is. |
| 5410 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 5411 | << Arg->getSourceRange(); |
| 5412 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 5413 | return true; |
| 5414 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5415 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5416 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5417 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5418 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5419 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 5420 | diag::ext_template_arg_object_internal) |
| 5421 | << !Func << Entity << Arg->getSourceRange(); |
| 5422 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5423 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 5424 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5425 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 5426 | << !Func << Entity << Arg->getSourceRange(); |
| 5427 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5428 | << !Func; |
| 5429 | return true; |
| 5430 | } |
| 5431 | |
| 5432 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5433 | // If the template parameter has pointer type, the function decays. |
| 5434 | if (ParamType->isPointerType() && !AddressTaken) |
| 5435 | ArgType = S.Context.getPointerType(Func->getType()); |
| 5436 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 5437 | // If we originally had an address-of operator, but the |
| 5438 | // parameter has reference type, complain and (if things look |
| 5439 | // like they will work) drop the address-of operator. |
| 5440 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 5441 | ParamType.getNonReferenceType())) { |
| 5442 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5443 | << ParamType; |
| 5444 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5445 | return true; |
| 5446 | } |
| 5447 | |
| 5448 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5449 | << ParamType |
| 5450 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5451 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5452 | |
| 5453 | ArgType = Func->getType(); |
| 5454 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5455 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5456 | // A value of reference type is not an object. |
| 5457 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5458 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5459 | diag::err_template_arg_reference_var) |
| 5460 | << Var->getType() << Arg->getSourceRange(); |
| 5461 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5462 | return true; |
| 5463 | } |
| 5464 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5465 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 5466 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5467 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 5468 | << Arg->getSourceRange(); |
| 5469 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 5470 | return true; |
| 5471 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5472 | |
| 5473 | // If the template parameter has pointer type, we must have taken |
| 5474 | // the address of this object. |
| 5475 | if (ParamType->isReferenceType()) { |
| 5476 | if (AddressTaken) { |
| 5477 | // If we originally had an address-of operator, but the |
| 5478 | // parameter has reference type, complain and (if things look |
| 5479 | // like they will work) drop the address-of operator. |
| 5480 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 5481 | ParamType.getNonReferenceType())) { |
| 5482 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5483 | << ParamType; |
| 5484 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5485 | return true; |
| 5486 | } |
| 5487 | |
| 5488 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5489 | << ParamType |
| 5490 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5491 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5492 | |
| 5493 | ArgType = Var->getType(); |
| 5494 | } |
| 5495 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 5496 | if (Var->getType()->isArrayType()) { |
| 5497 | // Array-to-pointer decay. |
| 5498 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 5499 | } else { |
| 5500 | // If the template parameter has pointer type but the address of |
| 5501 | // this object was not taken, complain and (possibly) recover by |
| 5502 | // taking the address of the entity. |
| 5503 | ArgType = S.Context.getPointerType(Var->getType()); |
| 5504 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 5505 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 5506 | << ParamType; |
| 5507 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5508 | return true; |
| 5509 | } |
| 5510 | |
| 5511 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 5512 | << ParamType |
| 5513 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 5514 | |
| 5515 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5516 | } |
| 5517 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5518 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5519 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5520 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 5521 | Arg, ArgType)) |
| 5522 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5523 | |
| 5524 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5525 | Converted = |
| 5526 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 5527 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5528 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | /// \brief Checks whether the given template argument is a pointer to |
| 5532 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5533 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 5534 | NonTypeTemplateParmDecl *Param, |
| 5535 | QualType ParamType, |
| 5536 | Expr *&ResultArg, |
| 5537 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5538 | bool Invalid = false; |
| 5539 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5540 | // Check for a null pointer value. |
| 5541 | Expr *Arg = ResultArg; |
| 5542 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 5543 | case NPV_Error: |
| 5544 | return true; |
| 5545 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5546 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5547 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 5548 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5549 | return false; |
| 5550 | case NPV_NotNullPointer: |
| 5551 | break; |
| 5552 | } |
| 5553 | |
| 5554 | bool ObjCLifetimeConversion; |
| 5555 | if (S.IsQualificationConversion(Arg->getType(), |
| 5556 | ParamType.getNonReferenceType(), |
| 5557 | false, ObjCLifetimeConversion)) { |
| 5558 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5559 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5560 | ResultArg = Arg; |
| 5561 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 5562 | ParamType.getNonReferenceType())) { |
| 5563 | // We can't perform this conversion. |
| 5564 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 5565 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 5566 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5567 | return true; |
| 5568 | } |
| 5569 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5570 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5571 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5572 | Arg = Cast->getSubExpr(); |
| 5573 | |
| 5574 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5575 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5576 | // A template-argument for a non-type, non-template |
| 5577 | // template-parameter shall be one of: [...] |
| 5578 | // |
| 5579 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5580 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5581 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 5582 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 5583 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 5584 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5585 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5586 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5587 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5588 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5589 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 5590 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5591 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 5592 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
| 5595 | Arg = Parens->getSubExpr(); |
| 5596 | } |
| 5597 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5598 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5599 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5600 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5601 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5602 | // A pointer-to-member constant written &Class::member. |
| 5603 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5604 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5605 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 5606 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5607 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5608 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5609 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5610 | // A constant of pointer-to-member type. |
| 5611 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 5612 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 5613 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 5614 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5615 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5616 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5617 | } else { |
| 5618 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5619 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5620 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5621 | return Invalid; |
| 5622 | } |
| 5623 | } |
| 5624 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5625 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5626 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5627 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5628 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5629 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5630 | return S.Diag(Arg->getLocStart(), |
| 5631 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5632 | << Arg->getSourceRange(); |
| 5633 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 5634 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 5635 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 5636 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5637 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 5638 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5639 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 5640 | "Only non-static member pointers can make it here"); |
| 5641 | |
| 5642 | // Okay: this is the address of a non-static member, and therefore |
| 5643 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5644 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5645 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5646 | } else { |
| 5647 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5648 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5649 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5650 | return Invalid; |
| 5651 | } |
| 5652 | |
| 5653 | // 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] | 5654 | S.Diag(Arg->getLocStart(), |
| 5655 | diag::err_template_arg_not_pointer_to_member_form) |
| 5656 | << Arg->getSourceRange(); |
| 5657 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5658 | return true; |
| 5659 | } |
| 5660 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5661 | /// \brief Check a template argument against its corresponding |
| 5662 | /// non-type template parameter. |
| 5663 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 5664 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5665 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5666 | /// returns the converted template argument. \p ParamType is the |
| 5667 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5668 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5669 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5670 | TemplateArgument &Converted, |
| 5671 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5672 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5673 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5674 | // If the parameter type somehow involves auto, deduce the type now. |
| 5675 | if (getLangOpts().CPlusPlus1z && ParamType->isUndeducedType()) { |
Richard Smith | 4ae5ec8 | 2017-02-22 20:01:55 +0000 | [diff] [blame] | 5676 | // During template argument deduction, we allow 'decltype(auto)' to |
| 5677 | // match an arbitrary dependent argument. |
| 5678 | // FIXME: The language rules don't say what happens in this case. |
| 5679 | // FIXME: We get an opaque dependent type out of decltype(auto) if the |
| 5680 | // expression is merely instantiation-dependent; is this enough? |
| 5681 | if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) { |
| 5682 | auto *AT = dyn_cast<AutoType>(ParamType); |
| 5683 | if (AT && AT->isDecltypeAuto()) { |
| 5684 | Converted = TemplateArgument(Arg); |
| 5685 | return Arg; |
| 5686 | } |
| 5687 | } |
| 5688 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5689 | // When checking a deduced template argument, deduce from its type even if |
| 5690 | // the type is dependent, in order to check the types of non-type template |
| 5691 | // arguments line up properly in partial ordering. |
| 5692 | Optional<unsigned> Depth; |
| 5693 | if (CTAK != CTAK_Specified) |
| 5694 | Depth = Param->getDepth() + 1; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5695 | if (DeduceAutoType( |
| 5696 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5697 | Arg, ParamType, Depth) == DAR_Failed) { |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5698 | Diag(Arg->getExprLoc(), |
| 5699 | diag::err_non_type_template_parm_type_deduction_failure) |
| 5700 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 5701 | << Arg->getSourceRange(); |
| 5702 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5703 | return ExprError(); |
| 5704 | } |
| 5705 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 5706 | // an error. The error message normally references the parameter |
| 5707 | // declaration, but here we'll pass the argument location because that's |
| 5708 | // where the parameter type is deduced. |
| 5709 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 5710 | if (ParamType.isNull()) { |
| 5711 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5712 | return ExprError(); |
| 5713 | } |
| 5714 | } |
| 5715 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5716 | // We should have already dropped all cv-qualifiers by now. |
| 5717 | assert(!ParamType.hasQualifiers() && |
| 5718 | "non-type template parameter type cannot be qualified"); |
| 5719 | |
| 5720 | if (CTAK == CTAK_Deduced && |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 5721 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5722 | Arg->getType())) { |
Richard Smith | 957fbf1 | 2017-01-17 02:14:37 +0000 | [diff] [blame] | 5723 | // FIXME: If either type is dependent, we skip the check. This isn't |
| 5724 | // correct, since during deduction we're supposed to have replaced each |
| 5725 | // template parameter with some unique (non-dependent) placeholder. |
| 5726 | // FIXME: If the argument type contains 'auto', we carry on and fail the |
| 5727 | // type check in order to force specific types to be more specialized than |
| 5728 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to |
| 5729 | // work. |
| 5730 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
| 5731 | !Arg->getType()->getContainedAutoType()) { |
| 5732 | Converted = TemplateArgument(Arg); |
| 5733 | return Arg; |
| 5734 | } |
| 5735 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, |
| 5736 | // we should actually be checking the type of the template argument in P, |
| 5737 | // not the type of the template argument deduced from A, against the |
| 5738 | // template parameter type. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5739 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5740 | << Arg->getType() |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5741 | << ParamType.getUnqualifiedType(); |
| 5742 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5743 | return ExprError(); |
| 5744 | } |
| 5745 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5746 | // If either the parameter has a dependent type or the argument is |
| 5747 | // type-dependent, there's nothing we can check now. |
| 5748 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 5749 | // FIXME: Produce a cloned, canonical expression? |
| 5750 | Converted = TemplateArgument(Arg); |
| 5751 | return Arg; |
| 5752 | } |
| 5753 | |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 5754 | // The initialization of the parameter from the argument is |
| 5755 | // a constant-evaluated context. |
| 5756 | EnterExpressionEvaluationContext ConstantEvaluated(*this, |
| 5757 | Sema::ConstantEvaluated); |
| 5758 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5759 | if (getLangOpts().CPlusPlus1z) { |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5760 | // C++1z [temp.arg.nontype]p1: |
| 5761 | // A template-argument for a non-type template parameter shall be |
| 5762 | // a converted constant expression of the type of the template-parameter. |
| 5763 | APValue Value; |
| 5764 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 5765 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 5766 | if (ArgResult.isInvalid()) |
| 5767 | return ExprError(); |
| 5768 | |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5769 | // For a value-dependent argument, CheckConvertedConstantExpression is |
| 5770 | // permitted (and expected) to be unable to determine a value. |
| 5771 | if (ArgResult.get()->isValueDependent()) { |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 5772 | Converted = TemplateArgument(ArgResult.get()); |
| 5773 | return ArgResult; |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5774 | } |
| 5775 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5776 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 5777 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5778 | // Convert the APValue to a TemplateArgument. |
| 5779 | switch (Value.getKind()) { |
| 5780 | case APValue::Uninitialized: |
| 5781 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5782 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5783 | break; |
| 5784 | case APValue::Int: |
| 5785 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5786 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5787 | break; |
| 5788 | case APValue::MemberPointer: { |
| 5789 | assert(ParamType->isMemberPointerType()); |
| 5790 | |
| 5791 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 5792 | if (!Value.getMemberPointerPath().empty()) { |
| 5793 | Diag(Arg->getLocStart(), |
| 5794 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 5795 | << Value.getMemberPointerDecl() << ParamType |
| 5796 | << Arg->getSourceRange(); |
| 5797 | return ExprError(); |
| 5798 | } |
| 5799 | |
| 5800 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5801 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5802 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5803 | break; |
| 5804 | } |
| 5805 | case APValue::LValue: { |
| 5806 | // For a non-type template-parameter of pointer or reference type, |
| 5807 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5808 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 5809 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5810 | // -- a temporary object |
| 5811 | // -- a string literal |
| 5812 | // -- the result of a typeid expression, or |
| 5813 | // -- a predefind __func__ variable |
| 5814 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 5815 | if (isa<CXXUuidofExpr>(E)) { |
| 5816 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 5817 | break; |
| 5818 | } |
| 5819 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5820 | << Arg->getSourceRange(); |
| 5821 | return ExprError(); |
| 5822 | } |
| 5823 | auto *VD = const_cast<ValueDecl *>( |
| 5824 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 5825 | // -- a subobject |
| 5826 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 5827 | VD && VD->getType()->isArrayType() && |
| 5828 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 5829 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 5830 | // Per defect report (no number yet): |
| 5831 | // ... other than a pointer to the first element of a complete array |
| 5832 | // object. |
| 5833 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 5834 | Value.isLValueOnePastTheEnd()) { |
| 5835 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 5836 | << Value.getAsString(Context, ParamType); |
| 5837 | return ExprError(); |
| 5838 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5839 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5840 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5841 | assert((!VD || !ParamType->isNullPtrType()) && |
| 5842 | "non-null value of type nullptr_t?"); |
| 5843 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5844 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5845 | break; |
| 5846 | } |
| 5847 | case APValue::AddrLabelDiff: |
| 5848 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 5849 | case APValue::Float: |
| 5850 | case APValue::ComplexInt: |
| 5851 | case APValue::ComplexFloat: |
| 5852 | case APValue::Vector: |
| 5853 | case APValue::Array: |
| 5854 | case APValue::Struct: |
| 5855 | case APValue::Union: |
| 5856 | llvm_unreachable("invalid kind for template argument"); |
| 5857 | } |
| 5858 | |
| 5859 | return ArgResult.get(); |
| 5860 | } |
| 5861 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5862 | // C++ [temp.arg.nontype]p5: |
| 5863 | // The following conversions are performed on each expression used |
| 5864 | // as a non-type template-argument. If a non-type |
| 5865 | // template-argument cannot be converted to the type of the |
| 5866 | // corresponding template-parameter then the program is |
| 5867 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5868 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5869 | // C++11: |
| 5870 | // -- for a non-type template-parameter of integral or |
| 5871 | // enumeration type, conversions permitted in a converted |
| 5872 | // constant expression are applied. |
| 5873 | // |
| 5874 | // C++98: |
| 5875 | // -- for a non-type template-parameter of integral or |
| 5876 | // enumeration type, integral promotions (4.5) and integral |
| 5877 | // conversions (4.7) are applied. |
| 5878 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5879 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5880 | // C++ [temp.arg.nontype]p1: |
| 5881 | // A template-argument for a non-type, non-template template-parameter |
| 5882 | // shall be one of: |
| 5883 | // |
| 5884 | // -- for a non-type template-parameter of integral or enumeration |
| 5885 | // type, a converted constant expression of the type of the |
| 5886 | // template-parameter; or |
| 5887 | llvm::APSInt Value; |
| 5888 | ExprResult ArgResult = |
| 5889 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5890 | CCEK_TemplateArg); |
| 5891 | if (ArgResult.isInvalid()) |
| 5892 | return ExprError(); |
| 5893 | |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 5894 | // We can't check arbitrary value-dependent arguments. |
| 5895 | if (ArgResult.get()->isValueDependent()) { |
| 5896 | Converted = TemplateArgument(ArgResult.get()); |
| 5897 | return ArgResult; |
| 5898 | } |
| 5899 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5900 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5901 | // always a no-op, except when the parameter type is bool. In |
| 5902 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5903 | QualType IntegerType = ParamType; |
| 5904 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5905 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5906 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5907 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5908 | Converted = TemplateArgument(Context, Value, |
| 5909 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5910 | return ArgResult; |
| 5911 | } |
| 5912 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5913 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5914 | if (ArgResult.isInvalid()) |
| 5915 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5916 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5917 | |
| 5918 | QualType ArgType = Arg->getType(); |
| 5919 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5920 | // C++ [temp.arg.nontype]p1: |
| 5921 | // A template-argument for a non-type, non-template |
| 5922 | // template-parameter shall be one of: |
| 5923 | // |
| 5924 | // -- an integral constant-expression of integral or enumeration |
| 5925 | // type; or |
| 5926 | // -- the name of a non-type template-parameter; or |
| 5927 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5928 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5929 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5930 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5931 | diag::err_template_arg_not_integral_or_enumeral) |
| 5932 | << ArgType << Arg->getSourceRange(); |
| 5933 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5934 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5935 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5936 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5937 | QualType T; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5938 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5939 | public: |
| 5940 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5941 | |
| 5942 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5943 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5944 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5945 | } |
| 5946 | } Diagnoser(ArgType); |
| 5947 | |
| 5948 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5949 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5950 | if (!Arg) |
| 5951 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5952 | } |
| 5953 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5954 | // From here on out, all we care about is the unqualified form |
| 5955 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5956 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5957 | |
| 5958 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5959 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5960 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5961 | } else if (ParamType->isBooleanType()) { |
| 5962 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5963 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5964 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5965 | !ParamType->isEnumeralType()) { |
| 5966 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5967 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5968 | } else { |
| 5969 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5970 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5971 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5972 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5973 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5974 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5975 | } |
| 5976 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5977 | // Add the value of this argument to the list of converted |
| 5978 | // arguments. We use the bitwidth and signedness of the template |
| 5979 | // parameter. |
| 5980 | if (Arg->isValueDependent()) { |
| 5981 | // The argument is value-dependent. Create a new |
| 5982 | // TemplateArgument with the converted expression. |
| 5983 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5984 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5985 | } |
| 5986 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5987 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5988 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5989 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5990 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5991 | if (ParamType->isBooleanType()) { |
| 5992 | // Value must be zero or one. |
| 5993 | Value = Value != 0; |
| 5994 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5995 | if (Value.getBitWidth() != AllowedBits) |
| 5996 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5997 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5998 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5999 | llvm::APSInt OldValue = Value; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6000 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6001 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6002 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6003 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 6004 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6005 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6006 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6007 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6008 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6009 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 6010 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6011 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6012 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6013 | << Arg->getSourceRange(); |
| 6014 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6015 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6016 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6017 | // Complain if we overflowed the template parameter's type. |
| 6018 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6019 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6020 | RequiredBits = OldValue.getActiveBits(); |
| 6021 | else if (OldValue.isUnsigned()) |
| 6022 | RequiredBits = OldValue.getActiveBits() + 1; |
| 6023 | else |
| 6024 | RequiredBits = OldValue.getMinSignedBits(); |
| 6025 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6026 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 6027 | diag::warn_template_arg_too_large) |
| 6028 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6029 | << Arg->getSourceRange(); |
| 6030 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6031 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 6032 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 6033 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 6034 | Converted = TemplateArgument(Context, Value, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6035 | ParamType->isEnumeralType() |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 6036 | ? Context.getCanonicalType(ParamType) |
| 6037 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6038 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 6039 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6040 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 6041 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6042 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 6043 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6044 | // Handle pointer-to-function, reference-to-function, and |
| 6045 | // pointer-to-member-function all in (roughly) the same way. |
| 6046 | if (// -- For a non-type template-parameter of type pointer to |
| 6047 | // function, only the function-to-pointer conversion (4.3) is |
| 6048 | // applied. If the template-argument represents a set of |
| 6049 | // overloaded functions (or a pointer to such), the matching |
| 6050 | // function is selected from the set (13.4). |
| 6051 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6052 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6053 | // -- For a non-type template-parameter of type reference to |
| 6054 | // function, no conversions apply. If the template-argument |
| 6055 | // represents a set of overloaded functions, the matching |
| 6056 | // function is selected from the set (13.4). |
| 6057 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6058 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6059 | // -- For a non-type template-parameter of type pointer to |
| 6060 | // member function, no conversions apply. If the |
| 6061 | // template-argument represents a set of overloaded member |
| 6062 | // functions, the matching member function is selected from |
| 6063 | // the set (13.4). |
| 6064 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6065 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6066 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6067 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6068 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6069 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6070 | true, |
| 6071 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6072 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6073 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6074 | |
| 6075 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6076 | ArgType = Arg->getType(); |
| 6077 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6078 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6079 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6080 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6081 | if (!ParamType->isMemberPointerType()) { |
| 6082 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6083 | ParamType, |
| 6084 | Arg, Converted)) |
| 6085 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6086 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6087 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6088 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6089 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6090 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6091 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6092 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 6093 | } |
| 6094 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 6095 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6096 | // -- for a non-type template-parameter of type pointer to |
| 6097 | // object, qualification conversions (4.4) and the |
| 6098 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6099 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6100 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6101 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6102 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6103 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6104 | ParamType, |
| 6105 | Arg, Converted)) |
| 6106 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6107 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6108 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6109 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6110 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6111 | // -- For a non-type template-parameter of type reference to |
| 6112 | // object, no conversions apply. The type referred to by the |
| 6113 | // reference may be more cv-qualified than the (otherwise |
| 6114 | // identical) type of the template-argument. The |
| 6115 | // template-parameter is bound directly to the |
| 6116 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 6117 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6118 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 6119 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6120 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6121 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 6122 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6123 | true, |
| 6124 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6125 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6126 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6127 | |
| 6128 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6129 | ArgType = Arg->getType(); |
| 6130 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6131 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6132 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6133 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6134 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6135 | ParamType, |
| 6136 | Arg, Converted)) |
| 6137 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6138 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6139 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6140 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6141 | // Deal with parameters of type std::nullptr_t. |
| 6142 | if (ParamType->isNullPtrType()) { |
| 6143 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6144 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6145 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6146 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6147 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6148 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 6149 | case NPV_NotNullPointer: |
| 6150 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 6151 | << Arg->getType() << ParamType; |
| 6152 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6153 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6154 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6155 | case NPV_Error: |
| 6156 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6157 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6158 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 6159 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 6160 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 6161 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6162 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6163 | } |
| 6164 | } |
| 6165 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 6166 | // -- For a non-type template-parameter of type pointer to data |
| 6167 | // member, qualification conversions (4.4) are applied. |
| 6168 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 6169 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 6170 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6171 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6172 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6173 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6176 | static void DiagnoseTemplateParameterListArityMismatch( |
| 6177 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 6178 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 6179 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6180 | /// \brief Check a template argument against its corresponding |
| 6181 | /// template template parameter. |
| 6182 | /// |
| 6183 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 6184 | /// It returns true if an error occurred, and false otherwise. |
| 6185 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 6186 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 6187 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6188 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6189 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 6190 | if (!Template) { |
| 6191 | // Any dependent template name is fine. |
| 6192 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 6193 | return false; |
| 6194 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6195 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6196 | if (Template->isInvalidDecl()) |
| 6197 | return true; |
| 6198 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6199 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6200 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6201 | // the name of a class template or an alias template, expressed as an |
| 6202 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6203 | // primary class templates are considered when matching the |
| 6204 | // template template argument with the corresponding parameter; |
| 6205 | // partial specializations are not considered even if their |
| 6206 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6207 | // |
| 6208 | // Note that we also allow template template parameters here, which |
| 6209 | // will happen when we are dealing with, e.g., class template |
| 6210 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6211 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6212 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6213 | !isa<TypeAliasTemplateDecl>(Template) && |
| 6214 | !isa<BuiltinTemplateDecl>(Template)) { |
| 6215 | assert(isa<FunctionTemplateDecl>(Template) && |
| 6216 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 6217 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 6218 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 6219 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6220 | } |
| 6221 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 6222 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 6223 | if (Param->isExpandedParameterPack()) |
| 6224 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 6225 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6226 | // C++1z [temp.arg.template]p3: (DR 150) |
| 6227 | // A template-argument matches a template template-parameter P when P |
| 6228 | // is at least as specialized as the template-argument A. |
| 6229 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 6230 | // Quick check for the common case: |
| 6231 | // If P contains a parameter pack, then A [...] matches P if each of A's |
| 6232 | // template parameters matches the corresponding template parameter in |
| 6233 | // the template-parameter-list of P. |
| 6234 | if (TemplateParameterListsAreEqual( |
| 6235 | Template->getTemplateParameters(), Params, false, |
| 6236 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 6237 | return false; |
| 6238 | |
| 6239 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 6240 | Arg.getLocation())) |
| 6241 | return false; |
| 6242 | // FIXME: Produce better diagnostics for deduction failures. |
| 6243 | } |
| 6244 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6245 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 6246 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6247 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6248 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6249 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 6250 | } |
| 6251 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6252 | /// \brief Given a non-type template argument that refers to a |
| 6253 | /// declaration and the type of its corresponding non-type template |
| 6254 | /// parameter, produce an expression that properly refers to that |
| 6255 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6256 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6257 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 6258 | QualType ParamType, |
| 6259 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 6260 | // C++ [temp.param]p8: |
| 6261 | // |
| 6262 | // A non-type template-parameter of type "array of T" or |
| 6263 | // "function returning T" is adjusted to be of type "pointer to |
| 6264 | // T" or "pointer to function returning T", respectively. |
| 6265 | if (ParamType->isArrayType()) |
| 6266 | ParamType = Context.getArrayDecayedType(ParamType); |
| 6267 | else if (ParamType->isFunctionType()) |
| 6268 | ParamType = Context.getPointerType(ParamType); |
| 6269 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6270 | // For a NULL non-type template argument, return nullptr casted to the |
| 6271 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6272 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 6273 | return ImpCastExprToType( |
| 6274 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 6275 | ParamType, |
| 6276 | ParamType->getAs<MemberPointerType>() |
| 6277 | ? CK_NullToMemberPointer |
| 6278 | : CK_NullToPointer); |
| 6279 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6280 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 6281 | "Only declaration template arguments permitted here"); |
| 6282 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6283 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 6284 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6285 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 6286 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 6287 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6288 | // If the value is a class member, we might have a pointer-to-member. |
| 6289 | // Determine whether the non-type template template parameter is of |
| 6290 | // pointer-to-member type. If so, we need to build an appropriate |
| 6291 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 6292 | // would refer to the member itself. |
| 6293 | if (ParamType->isMemberPointerType()) { |
| 6294 | QualType ClassType |
| 6295 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 6296 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6297 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6298 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6299 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 6300 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6301 | |
| 6302 | // The actual value-ness of this is unimportant, but for |
| 6303 | // internal consistency's sake, references to instance methods |
| 6304 | // are r-values. |
| 6305 | ExprValueKind VK = VK_LValue; |
| 6306 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 6307 | VK = VK_RValue; |
| 6308 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6309 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6310 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 6311 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6312 | Loc, |
| 6313 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6314 | if (RefExpr.isInvalid()) |
| 6315 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6316 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6317 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6318 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6319 | // We might need to perform a trailing qualification conversion, since |
| 6320 | // the element type on the parameter could be more qualified than the |
| 6321 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6322 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6323 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6324 | ParamType.getUnqualifiedType(), false, |
| 6325 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6326 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6327 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6328 | assert(!RefExpr.isInvalid() && |
| 6329 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 6330 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6331 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6332 | } |
| 6333 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6334 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6335 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6336 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6337 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6338 | // When the non-type template parameter is a pointer, take the |
| 6339 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6340 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6341 | if (RefExpr.isInvalid()) |
| 6342 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6343 | |
Richard Smith | fc6fca1 | 2017-01-28 00:38:35 +0000 | [diff] [blame] | 6344 | if (!Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
| 6345 | (T->isFunctionType() || T->isArrayType())) { |
| 6346 | // Decay functions and arrays unless we're forming a pointer to array. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6347 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6348 | if (RefExpr.isInvalid()) |
| 6349 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6350 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6351 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6352 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6353 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6354 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6355 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6356 | } |
| 6357 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6358 | ExprValueKind VK = VK_RValue; |
| 6359 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6360 | // If the non-type template parameter has reference type, qualify the |
| 6361 | // resulting declaration reference with the extra qualifiers on the |
| 6362 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6363 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 6364 | VK = VK_LValue; |
| 6365 | T = Context.getQualifiedType(T, |
| 6366 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 6367 | } else if (isa<FunctionDecl>(VD)) { |
| 6368 | // References to functions are always lvalues. |
| 6369 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6370 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6371 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6372 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6373 | } |
| 6374 | |
| 6375 | /// \brief Construct a new expression that refers to the given |
| 6376 | /// integral template argument with the given source-location |
| 6377 | /// information. |
| 6378 | /// |
| 6379 | /// This routine takes care of the mapping from an integral template |
| 6380 | /// argument (which may have any integral type) to the appropriate |
| 6381 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6382 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6383 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 6384 | SourceLocation Loc) { |
| 6385 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 6386 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6387 | QualType OrigT = Arg.getIntegralType(); |
| 6388 | |
| 6389 | // If this is an enum type that we're instantiating, we need to use an integer |
| 6390 | // type the same size as the enumerator. We don't want to build an |
| 6391 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 6392 | // any integral type with C++11 enum classes, make sure we create the right |
| 6393 | // type of literal for it. |
| 6394 | QualType T = OrigT; |
| 6395 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 6396 | T = ET->getDecl()->getIntegerType(); |
| 6397 | |
| 6398 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6399 | if (T->isAnyCharacterType()) { |
Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 6400 | // This does not need to handle u8 character literals because those are |
| 6401 | // of type char, and so can also be covered by an ASCII character literal. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6402 | CharacterLiteral::CharacterKind Kind; |
| 6403 | if (T->isWideCharType()) |
| 6404 | Kind = CharacterLiteral::Wide; |
| 6405 | else if (T->isChar16Type()) |
| 6406 | Kind = CharacterLiteral::UTF16; |
| 6407 | else if (T->isChar32Type()) |
| 6408 | Kind = CharacterLiteral::UTF32; |
| 6409 | else |
| 6410 | Kind = CharacterLiteral::Ascii; |
| 6411 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6412 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 6413 | Kind, T, Loc); |
| 6414 | } else if (T->isBooleanType()) { |
| 6415 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 6416 | T, Loc); |
| 6417 | } else if (T->isNullPtrType()) { |
| 6418 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 6419 | } else { |
| 6420 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6423 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6424 | // FIXME: This is a hack. We need a better way to handle substituted |
| 6425 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6426 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 6427 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 6428 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 6429 | Loc, Loc); |
| 6430 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6431 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6432 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 6435 | static bool isDependentOnOuter(NonTypeTemplateParmDecl *NTTP) { |
| 6436 | if (NTTP->getDepth() == 0 || !NTTP->getType()->isDependentType()) |
| 6437 | return false; |
| 6438 | DependencyChecker Checker(NTTP->getDepth(), /*IgnoreNonTypeDependent*/ false, |
| 6439 | /*FindLessThanDepth*/ true); |
| 6440 | Checker.TraverseType(NTTP->getType()); |
| 6441 | return Checker.Match; |
| 6442 | } |
| 6443 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6444 | /// \brief Match two template parameters within template parameter lists. |
| 6445 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 6446 | bool Complain, |
| 6447 | Sema::TemplateParameterListEqualKind Kind, |
| 6448 | SourceLocation TemplateArgLoc) { |
| 6449 | // Check the actual kind (type, non-type, template). |
| 6450 | if (Old->getKind() != New->getKind()) { |
| 6451 | if (Complain) { |
| 6452 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 6453 | if (TemplateArgLoc.isValid()) { |
| 6454 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6455 | NextDiag = diag::note_template_param_different_kind; |
| 6456 | } |
| 6457 | S.Diag(New->getLocation(), NextDiag) |
| 6458 | << (Kind != Sema::TPL_TemplateMatch); |
| 6459 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 6460 | << (Kind != Sema::TPL_TemplateMatch); |
| 6461 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6462 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6463 | return false; |
| 6464 | } |
| 6465 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6466 | // Check that both are parameter packs or neither are parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6467 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6468 | // template template parameter, the template template parameter can have |
| 6469 | // a parameter pack where the template template argument does not. |
| 6470 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 6471 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 6472 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6473 | if (Complain) { |
| 6474 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 6475 | if (TemplateArgLoc.isValid()) { |
| 6476 | S.Diag(TemplateArgLoc, |
| 6477 | diag::err_template_arg_template_params_mismatch); |
| 6478 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 6479 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6480 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6481 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 6482 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 6483 | : 2; |
| 6484 | S.Diag(New->getLocation(), NextDiag) |
| 6485 | << ParamKind << New->isParameterPack(); |
| 6486 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 6487 | << ParamKind << Old->isParameterPack(); |
| 6488 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6489 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6490 | return false; |
| 6491 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6492 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6493 | // For non-type template parameters, check the type of the parameter. |
| 6494 | if (NonTypeTemplateParmDecl *OldNTTP |
| 6495 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 6496 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6497 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6498 | // If we are matching a template template argument to a template |
| 6499 | // template parameter and one of the non-type template parameter types |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 6500 | // is dependent on an outer template's parameter, then we must wait until |
| 6501 | // template instantiation time to actually compare the arguments. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6502 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 6503 | (isDependentOnOuter(OldNTTP) || isDependentOnOuter(NewNTTP))) |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6504 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6505 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6506 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 6507 | if (Complain) { |
| 6508 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 6509 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6510 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6511 | diag::err_template_arg_template_params_mismatch); |
| 6512 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 6513 | } |
| 6514 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 6515 | << NewNTTP->getType() |
| 6516 | << (Kind != Sema::TPL_TemplateMatch); |
| 6517 | S.Diag(OldNTTP->getLocation(), |
| 6518 | diag::note_template_nontype_parm_prev_declaration) |
| 6519 | << OldNTTP->getType(); |
| 6520 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6521 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6522 | return false; |
| 6523 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6524 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6525 | return true; |
| 6526 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6527 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6528 | // For template template parameters, check the template parameter types. |
| 6529 | // The template parameter lists of template template |
| 6530 | // parameters must agree. |
| 6531 | if (TemplateTemplateParmDecl *OldTTP |
| 6532 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6533 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6534 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 6535 | OldTTP->getTemplateParameters(), |
| 6536 | Complain, |
| 6537 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6538 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6539 | : Kind), |
| 6540 | TemplateArgLoc); |
| 6541 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6542 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6543 | return true; |
| 6544 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6545 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6546 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 6547 | /// lists. |
| 6548 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6549 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6550 | TemplateParameterList *New, |
| 6551 | TemplateParameterList *Old, |
| 6552 | Sema::TemplateParameterListEqualKind Kind, |
| 6553 | SourceLocation TemplateArgLoc) { |
| 6554 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 6555 | if (TemplateArgLoc.isValid()) { |
| 6556 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6557 | NextDiag = diag::note_template_param_list_different_arity; |
| 6558 | } |
| 6559 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 6560 | << (New->size() > Old->size()) |
| 6561 | << (Kind != Sema::TPL_TemplateMatch) |
| 6562 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 6563 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 6564 | << (Kind != Sema::TPL_TemplateMatch) |
| 6565 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 6566 | } |
| 6567 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6568 | /// \brief Determine whether the given template parameter lists are |
| 6569 | /// equivalent. |
| 6570 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6571 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6572 | /// source code as part of a new template declaration. |
| 6573 | /// |
| 6574 | /// \param Old The old template parameter list, typically found via |
| 6575 | /// name lookup of the template declared with this template parameter |
| 6576 | /// list. |
| 6577 | /// |
| 6578 | /// \param Complain If true, this routine will produce a diagnostic if |
| 6579 | /// the template parameter lists are not equivalent. |
| 6580 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6581 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6582 | /// |
| 6583 | /// \param TemplateArgLoc If this source location is valid, then we |
| 6584 | /// are actually checking the template parameter list of a template |
| 6585 | /// argument (New) against the template parameter list of its |
| 6586 | /// corresponding template template parameter (Old). We produce |
| 6587 | /// slightly different diagnostics in this scenario. |
| 6588 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6589 | /// \returns True if the template parameter lists are equal, false |
| 6590 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6591 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6592 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 6593 | TemplateParameterList *Old, |
| 6594 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6595 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6596 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6597 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 6598 | if (Complain) |
| 6599 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6600 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6601 | |
| 6602 | return false; |
| 6603 | } |
| 6604 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6605 | // C++0x [temp.arg.template]p3: |
| 6606 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6607 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6608 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6609 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6610 | // template-parameter-list of P. [...] |
| 6611 | TemplateParameterList::iterator NewParm = New->begin(); |
| 6612 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6613 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6614 | OldParmEnd = Old->end(); |
| 6615 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 6616 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 6617 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6618 | if (NewParm == NewParmEnd) { |
| 6619 | if (Complain) |
| 6620 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6621 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6622 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6623 | return false; |
| 6624 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6625 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6626 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 6627 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6628 | return false; |
| 6629 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6630 | ++NewParm; |
| 6631 | continue; |
| 6632 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6633 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6634 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6635 | // [...] When P's template- parameter-list contains a template parameter |
| 6636 | // pack (14.5.3), the template parameter pack will match zero or more |
| 6637 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6638 | // template-parameter-list of A with the same type and form as the |
| 6639 | // template parameter pack in P (ignoring whether those template |
| 6640 | // parameters are template parameter packs). |
| 6641 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 6642 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 6643 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6644 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6645 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6646 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6647 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6648 | // Make sure we exhausted all of the arguments. |
| 6649 | if (NewParm != NewParmEnd) { |
| 6650 | if (Complain) |
| 6651 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6652 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6653 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6654 | return false; |
| 6655 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6656 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6657 | return true; |
| 6658 | } |
| 6659 | |
| 6660 | /// \brief Check whether a template can be declared within this scope. |
| 6661 | /// |
| 6662 | /// If the template declaration is valid in this scope, returns |
| 6663 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6664 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6665 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 6666 | if (!S) |
| 6667 | return false; |
| 6668 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6669 | // Find the nearest enclosing declaration scope. |
| 6670 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6671 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6672 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6673 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 6674 | // C++ [temp]p4: |
| 6675 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 6676 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 6677 | if (Ctx && Ctx->isExternCContext()) { |
| 6678 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 6679 | << TemplateParams->getSourceRange(); |
| 6680 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 6681 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 6682 | return true; |
| 6683 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 6684 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6685 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 6686 | // C++ [temp]p2: |
| 6687 | // A template-declaration can appear only as a namespace scope or |
| 6688 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 6689 | if (Ctx) { |
| 6690 | if (Ctx->isFileContext()) |
| 6691 | return false; |
| 6692 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 6693 | // C++ [temp.mem]p2: |
| 6694 | // A local class shall not have member templates. |
| 6695 | if (RD->isLocalClass()) |
| 6696 | return Diag(TemplateParams->getTemplateLoc(), |
| 6697 | diag::err_template_inside_local_class) |
| 6698 | << TemplateParams->getSourceRange(); |
| 6699 | else |
| 6700 | return false; |
| 6701 | } |
| 6702 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6703 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6704 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6705 | diag::err_template_outside_namespace_or_class_scope) |
| 6706 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6707 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6708 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6709 | /// \brief Determine what kind of template specialization the given declaration |
| 6710 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6711 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6712 | if (!D) |
| 6713 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6714 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6715 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 6716 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6717 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 6718 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6719 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 6720 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6721 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6722 | return TSK_Undeclared; |
| 6723 | } |
| 6724 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6725 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6726 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6727 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6728 | /// This routine determines whether a template specialization can be declared |
| 6729 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6730 | /// |
| 6731 | /// \param S the semantic analysis object for which this check is being |
| 6732 | /// performed. |
| 6733 | /// |
| 6734 | /// \param Specialized the entity being specialized or instantiated, which |
| 6735 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6736 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6737 | /// member class). |
| 6738 | /// |
| 6739 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 6740 | /// |
| 6741 | /// \param Loc the location of the explicit specialization or instantiation of |
| 6742 | /// this entity. |
| 6743 | /// |
| 6744 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 6745 | /// a class template. |
| 6746 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6747 | /// \returns true if there was an error that we cannot recover from, false |
| 6748 | /// otherwise. |
| 6749 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 6750 | NamedDecl *Specialized, |
| 6751 | NamedDecl *PrevDecl, |
| 6752 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6753 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6754 | // Keep these "kind" numbers in sync with the %select statements in the |
| 6755 | // various diagnostics emitted by this routine. |
| 6756 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6757 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6758 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6759 | else if (isa<VarTemplateDecl>(Specialized)) |
| 6760 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6761 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6762 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6763 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6764 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6765 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6766 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6767 | else if (isa<RecordDecl>(Specialized)) |
| 6768 | EntityKind = 7; |
| 6769 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 6770 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6771 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6772 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6773 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6774 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6775 | return true; |
| 6776 | } |
| 6777 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6778 | // C++ [temp.expl.spec]p2: |
| 6779 | // An explicit specialization shall be declared in the namespace |
| 6780 | // of which the template is a member, or, for member templates, in |
| 6781 | // the namespace of which the enclosing class or enclosing class |
| 6782 | // template is a member. An explicit specialization of a member |
| 6783 | // function, member class or static data member of a class |
| 6784 | // template shall be declared in the namespace of which the class |
| 6785 | // template is a member. Such a declaration may also be a |
| 6786 | // definition. If the declaration is not a definition, the |
| 6787 | // specialization may be defined later in the name- space in which |
| 6788 | // the explicit specialization was declared, or in a namespace |
| 6789 | // that encloses the one in which the explicit specialization was |
| 6790 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6791 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6792 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6793 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6794 | return true; |
| 6795 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6796 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6797 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6798 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6799 | // Do not warn for class scope explicit specialization during |
| 6800 | // instantiation, warning was already emitted during pattern |
| 6801 | // semantic analysis. |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 6802 | if (!S.inTemplateInstantiation()) |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6803 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 6804 | << Specialized; |
| 6805 | } else { |
| 6806 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6807 | << Specialized; |
| 6808 | return true; |
| 6809 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6810 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6811 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 6812 | if (S.CurContext->isRecord() && |
| 6813 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 6814 | // Make sure that we're specializing in the right record context. |
| 6815 | // Otherwise, things can go horribly wrong. |
| 6816 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6817 | << Specialized; |
| 6818 | return true; |
| 6819 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6820 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6821 | // C++ [temp.class.spec]p6: |
| 6822 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6823 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 6824 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6825 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6826 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6827 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6828 | |
| 6829 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 6830 | // namespace. |
| 6831 | // Note that HandleDeclarator() performs this check for explicit |
| 6832 | // specializations of function templates, static data members, and member |
| 6833 | // functions, so we skip the check here for those kinds of entities. |
| 6834 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 6835 | // Should we refactor that check, so that it occurs later? |
| 6836 | if (!DC->Encloses(SpecializedContext) && |
| 6837 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 6838 | isa<FunctionDecl>(Specialized) || |
| 6839 | isa<VarTemplateDecl>(Specialized) || |
| 6840 | isa<VarDecl>(Specialized))) { |
| 6841 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 6842 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 6843 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 6844 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6845 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 6846 | if (S.getLangOpts().MicrosoftExt) |
| 6847 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 6848 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 6849 | << cast<NamedDecl>(SpecializedContext); |
| 6850 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6851 | llvm_unreachable("unexpected namespace context for specialization"); |
| 6852 | |
| 6853 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 6854 | } else if ((!PrevDecl || |
| 6855 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 6856 | getTemplateSpecializationKind(PrevDecl) == |
| 6857 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6858 | // C++ [temp.exp.spec]p2: |
| 6859 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6860 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6861 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6862 | // An explicit specialization of a member function, member class or |
| 6863 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6864 | // namespace of which the class template is a member. |
| 6865 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6866 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6867 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6868 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6869 | // C++11 [temp.explicit]p3: |
| 6870 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 6871 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6872 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6873 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6874 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6875 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6876 | "DC encloses TU but isn't in enclosing namespace set"); |
| 6877 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 6878 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6879 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6880 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6881 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6882 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6883 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6884 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 6885 | else |
| 6886 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 6887 | S.Diag(Loc, Diag) |
| 6888 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 6889 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6890 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6891 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6892 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6893 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6894 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6895 | return false; |
| 6896 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6897 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6898 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 6899 | if (!E->isTypeDependent()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6900 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6901 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6902 | Checker.TraverseStmt(E); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6903 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6904 | return E->getSourceRange(); |
| 6905 | return Checker.MatchLoc; |
| 6906 | } |
| 6907 | |
| 6908 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6909 | if (!TL.getType()->isDependentType()) |
| 6910 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6911 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6912 | Checker.TraverseTypeLoc(TL); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6913 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6914 | return TL.getSourceRange(); |
| 6915 | return Checker.MatchLoc; |
| 6916 | } |
| 6917 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6918 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6919 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6920 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6921 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6922 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6923 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6924 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6925 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6926 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6927 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6928 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6929 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6930 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6931 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6932 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6933 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6934 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6935 | |
| 6936 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6937 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6938 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6939 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6940 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6941 | |
| 6942 | // Strip off any implicit casts we added as part of type checking. |
| 6943 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6944 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6945 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6946 | // C++ [temp.class.spec]p8: |
| 6947 | // A non-type argument is non-specialized if it is the name of a |
| 6948 | // non-type parameter. All other non-type arguments are |
| 6949 | // specialized. |
| 6950 | // |
| 6951 | // Below, we check the two conditions that only apply to |
| 6952 | // specialized non-type arguments, so skip any non-specialized |
| 6953 | // arguments. |
| 6954 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6955 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6956 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6957 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6958 | // C++ [temp.class.spec]p9: |
| 6959 | // Within the argument list of a class template partial |
| 6960 | // specialization, the following restrictions apply: |
| 6961 | // -- A partially specialized non-type argument expression |
| 6962 | // shall not involve a template parameter of the partial |
| 6963 | // specialization except when the argument expression is a |
| 6964 | // simple identifier. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6965 | // -- The type of a template parameter corresponding to a |
| 6966 | // specialized non-type argument shall not be dependent on a |
| 6967 | // parameter of the specialization. |
| 6968 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
| 6969 | // We implement a compromise between the original rules and DR1315: |
| 6970 | // -- A specialized non-type template argument shall not be |
| 6971 | // type-dependent and the corresponding template parameter |
| 6972 | // shall have a non-dependent type. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6973 | SourceRange ParamUseRange = |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6974 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6975 | if (ParamUseRange.isValid()) { |
| 6976 | if (IsDefaultArgument) { |
| 6977 | S.Diag(TemplateNameLoc, |
| 6978 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6979 | S.Diag(ParamUseRange.getBegin(), |
| 6980 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6981 | << ParamUseRange; |
| 6982 | } else { |
| 6983 | S.Diag(ParamUseRange.getBegin(), |
| 6984 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6985 | << ParamUseRange; |
| 6986 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6987 | return true; |
| 6988 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6989 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6990 | ParamUseRange = findTemplateParameter( |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6991 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6992 | if (ParamUseRange.isValid()) { |
| 6993 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6994 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6995 | << Param->getType(); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6996 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6997 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 6998 | << ParamUseRange; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6999 | return true; |
| 7000 | } |
| 7001 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7002 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7003 | return false; |
| 7004 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7005 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7006 | /// \brief Check the non-type template arguments of a class template |
| 7007 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 7008 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7009 | /// \param TemplateNameLoc the location of the template name. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7010 | /// \param PrimaryTemplate the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7011 | /// template. |
| 7012 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 7013 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7014 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7015 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 7016 | /// \returns \c true if there was an error, \c false otherwise. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7017 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 7018 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 7019 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 7020 | // We have to be conservative when checking a template in a dependent |
| 7021 | // context. |
| 7022 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 7023 | return false; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7024 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7025 | TemplateParameterList *TemplateParams = |
| 7026 | PrimaryTemplate->getTemplateParameters(); |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7027 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7028 | NonTypeTemplateParmDecl *Param |
| 7029 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 7030 | if (!Param) |
| 7031 | continue; |
| 7032 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7033 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 7034 | Param, &TemplateArgs[I], |
| 7035 | 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 7036 | return true; |
| 7037 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7038 | |
| 7039 | return false; |
| 7040 | } |
| 7041 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7042 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7043 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 7044 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7045 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 7046 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7047 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7048 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7049 | MultiTemplateParamsArg |
| 7050 | TemplateParameterLists, |
| 7051 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7052 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 7053 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7054 | CXXScopeSpec &SS = TemplateId.SS; |
| 7055 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7056 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 7057 | // store the location of the outermost template keyword in the declaration. |
| 7058 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7059 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 7060 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 7061 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 7062 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7063 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7064 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7065 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7066 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7067 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 7068 | |
| 7069 | if (!ClassTemplate) { |
| 7070 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7071 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 7072 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 7073 | return true; |
| 7074 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7075 | |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7076 | bool isMemberSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7077 | bool isPartialSpecialization = false; |
| 7078 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7079 | // Check the validity of the template headers that introduce this |
| 7080 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7081 | // FIXME: We probably shouldn't complain about these headers for |
| 7082 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7083 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 7084 | TemplateParameterList *TemplateParams = |
| 7085 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7086 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 7087 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7088 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 7089 | if (Invalid) |
| 7090 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7091 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7092 | if (TemplateParams && TemplateParams->size() > 0) { |
| 7093 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7094 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 7095 | if (TUK == TUK_Friend) { |
| 7096 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 7097 | << SourceRange(LAngleLoc, RAngleLoc); |
| 7098 | return true; |
| 7099 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7100 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7101 | // C++ [temp.class.spec]p10: |
| 7102 | // The template parameter list of a specialization shall not |
| 7103 | // contain default template argument values. |
| 7104 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7105 | Decl *Param = TemplateParams->getParam(I); |
| 7106 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 7107 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7108 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7109 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 7110 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7111 | } |
| 7112 | } else if (NonTypeTemplateParmDecl *NTTP |
| 7113 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 7114 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7115 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7116 | diag::err_default_arg_in_partial_spec) |
| 7117 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7118 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7119 | } |
| 7120 | } else { |
| 7121 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7122 | if (TTP->hasDefaultArgument()) { |
| 7123 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 7124 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 7125 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 7126 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7127 | } |
| 7128 | } |
| 7129 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7130 | } else if (TemplateParams) { |
| 7131 | if (TUK == TUK_Friend) |
| 7132 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7133 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7134 | SourceRange(TemplateParams->getTemplateLoc(), |
| 7135 | TemplateParams->getRAngleLoc())) |
| 7136 | << SourceRange(LAngleLoc, RAngleLoc); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7137 | } else { |
| 7138 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7139 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7140 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7141 | // Check that the specialization uses the same tag kind as the |
| 7142 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7143 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7144 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7145 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7146 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7147 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7148 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7149 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7150 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 7151 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7152 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7153 | diag::note_previous_use); |
| 7154 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7155 | } |
| 7156 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7157 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7158 | TemplateArgumentListInfo TemplateArgs = |
| 7159 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 7160 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7161 | // Check for unexpanded parameter packs in any of the template arguments. |
| 7162 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7163 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 7164 | UPPC_PartialSpecialization)) |
| 7165 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7166 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7167 | // Check that the template argument list is well-formed for this |
| 7168 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7169 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7170 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7171 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7172 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7173 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7174 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7175 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 7176 | if (isPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7177 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 7178 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 7179 | return true; |
| 7180 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7181 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
| 7182 | // also do it during instantiation. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 7183 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7184 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7185 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7186 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7187 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 7188 | << ClassTemplate->getDeclName(); |
| 7189 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7190 | } |
| 7191 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7192 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7193 | void *InsertPos = nullptr; |
| 7194 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7195 | |
| 7196 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7197 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7198 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7199 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7200 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7201 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7202 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7203 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7204 | // Check whether we can declare a class template specialization in |
| 7205 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7206 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7207 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 7208 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7209 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7210 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7211 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7212 | // The canonical type |
| 7213 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 7214 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7215 | // Build the canonical type that describes the converted template |
| 7216 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 7217 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7218 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7219 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7220 | |
| 7221 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7222 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 7223 | // C++ [temp.class.spec]p9b3: |
| 7224 | // |
| 7225 | // -- The argument list of the specialization shall not be identical |
| 7226 | // to the implicit argument list of the primary template. |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 7227 | // |
| 7228 | // This rule has since been removed, because it's redundant given DR1495, |
| 7229 | // but we keep it because it produces better diagnostics and recovery. |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7230 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 7231 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 7232 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7233 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 7234 | ClassTemplate->getIdentifier(), |
| 7235 | TemplateNameLoc, |
| 7236 | Attr, |
| 7237 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7238 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 7239 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7240 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7241 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 7242 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7243 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7244 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7245 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 7246 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7247 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7248 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7249 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7250 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 7251 | TemplateParams, |
| 7252 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7253 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7254 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7255 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 7256 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7257 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7258 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7259 | Partial->setTemplateParameterListsInfo( |
| 7260 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7261 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7262 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7263 | if (!PrevPartial) |
| 7264 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 7265 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 7266 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7267 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 7268 | // template specialization, make a note of that. |
| 7269 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 7270 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7271 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 7272 | CheckTemplatePartialSpecialization(Partial); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7273 | } else { |
| 7274 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7275 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7276 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7277 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7278 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7279 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7280 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7281 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7282 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7283 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7284 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 7285 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 7286 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 7287 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7288 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7289 | if (!PrevDecl) |
| 7290 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 7291 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7292 | if (CurContext->isDependentContext()) { |
| 7293 | // -fms-extensions permits specialization of nested classes without |
| 7294 | // fully specializing the outer class(es). |
| 7295 | assert(getLangOpts().MicrosoftExt && |
| 7296 | "Only possible with -fms-extensions!"); |
| 7297 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7298 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 7299 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 7300 | } else { |
| 7301 | CanonType = Context.getTypeDeclType(Specialization); |
| 7302 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7303 | } |
| 7304 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7305 | // C++ [temp.expl.spec]p6: |
| 7306 | // 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] | 7307 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7308 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7309 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7310 | // use occurs; no diagnostic is required. |
| 7311 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7312 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7313 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7314 | // Is there any previous explicit specialization declaration? |
| 7315 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7316 | Okay = true; |
| 7317 | break; |
| 7318 | } |
| 7319 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7320 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7321 | if (!Okay) { |
| 7322 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 7323 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 7324 | << Context.getTypeDeclType(Specialization) << Range; |
| 7325 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7326 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7327 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7328 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7329 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7330 | return true; |
| 7331 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7332 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7333 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7334 | // If this is not a friend, note that this is an explicit specialization. |
| 7335 | if (TUK != TUK_Friend) |
| 7336 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7337 | |
| 7338 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7339 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 7340 | RecordDecl *Def = Specialization->getDefinition(); |
| 7341 | NamedDecl *Hidden = nullptr; |
| 7342 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 7343 | SkipBody->ShouldSkip = true; |
| 7344 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 7345 | // From here on out, treat this as just a redeclaration. |
| 7346 | TUK = TUK_Declaration; |
| 7347 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7348 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 7349 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7350 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 7351 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 7352 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7353 | } |
| 7354 | } |
| 7355 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 7356 | if (Attr) |
| 7357 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7358 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 7359 | // Add alignment attributes if necessary; these attributes are checked when |
| 7360 | // the ASTContext lays out the structure. |
| 7361 | if (TUK == TUK_Definition) { |
| 7362 | AddAlignmentAttributesForRecord(Specialization); |
| 7363 | AddMsStructLayoutForRecord(Specialization); |
| 7364 | } |
| 7365 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 7366 | if (ModulePrivateLoc.isValid()) |
| 7367 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 7368 | << (isPartialSpecialization? 1 : 0) |
| 7369 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7370 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 7371 | // Build the fully-sugared type for this class template |
| 7372 | // specialization as the user wrote in the specialization |
| 7373 | // itself. This means that we'll pretty-print the type retrieved |
| 7374 | // from the specialization's declaration the way that the user |
| 7375 | // actually wrote the specialization, rather than formatting the |
| 7376 | // name based on the "canonical" representation used to store the |
| 7377 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7378 | TypeSourceInfo *WrittenTy |
| 7379 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7380 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7381 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7382 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7383 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7384 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7385 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 7386 | // C++ [temp.expl.spec]p9: |
| 7387 | // A template explicit specialization is in the scope of the |
| 7388 | // namespace in which the template was defined. |
| 7389 | // |
| 7390 | // We actually implement this paragraph where we set the semantic |
| 7391 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 7392 | // but we also maintain the lexical context where the actual |
| 7393 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7394 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7395 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7396 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 7397 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7398 | Specialization->startDefinition(); |
| 7399 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7400 | if (TUK == TUK_Friend) { |
| 7401 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 7402 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 7403 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 7404 | /*FIXME:*/KWLoc); |
| 7405 | Friend->setAccess(AS_public); |
| 7406 | CurContext->addDecl(Friend); |
| 7407 | } else { |
| 7408 | // Add the specialization into its lexical context, so that it can |
| 7409 | // be seen when iterating through the list of declarations in that |
| 7410 | // context. However, specializations are not found by name lookup. |
| 7411 | CurContext->addDecl(Specialization); |
| 7412 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7413 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7414 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7415 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7416 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7417 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7418 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7419 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 7420 | ActOnDocumentableDecl(NewDecl); |
| 7421 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 7422 | } |
| 7423 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7424 | /// \brief Strips various properties off an implicit instantiation |
| 7425 | /// that has just been explicitly specialized. |
| 7426 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7427 | D->dropAttr<DLLImportAttr>(); |
| 7428 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7429 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 7430 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7431 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7432 | } |
| 7433 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7434 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 7435 | // declaration or definition. |
| 7436 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7437 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7438 | // Explicit instantiations following a specialization have no effect and |
| 7439 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 7440 | // until a valid name loc is found. |
| 7441 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7442 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 7443 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7444 | PrevDiagLoc = Prev->getLocation(); |
| 7445 | } |
| 7446 | assert(PrevDiagLoc.isValid() && |
| 7447 | "Explicit instantiation without point of instantiation?"); |
| 7448 | return PrevDiagLoc; |
| 7449 | } |
| 7450 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7451 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7452 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7453 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7454 | /// new specialization/instantiation will have any effect. |
| 7455 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7456 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7457 | /// instantiation. |
| 7458 | /// |
| 7459 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 7460 | /// |
| 7461 | /// \param PrevDecl the previous declaration of the entity. |
| 7462 | /// |
| 7463 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 7464 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7465 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7466 | /// declaration was instantiated (either implicitly or explicitly). |
| 7467 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7468 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7469 | /// specialization or instantiation has no effect and should be ignored. |
| 7470 | /// |
| 7471 | /// \returns true if there was an error that should prevent the introduction of |
| 7472 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7473 | bool |
| 7474 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 7475 | TemplateSpecializationKind NewTSK, |
| 7476 | NamedDecl *PrevDecl, |
| 7477 | TemplateSpecializationKind PrevTSK, |
| 7478 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7479 | bool &HasNoEffect) { |
| 7480 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7481 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7482 | switch (NewTSK) { |
| 7483 | case TSK_Undeclared: |
| 7484 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 7485 | assert( |
| 7486 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 7487 | "previous declaration must be implicit!"); |
| 7488 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7489 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7490 | case TSK_ExplicitSpecialization: |
| 7491 | switch (PrevTSK) { |
| 7492 | case TSK_Undeclared: |
| 7493 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7494 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7495 | // explicitly specialized or has merely been mentioned without any |
| 7496 | // instantiation. |
| 7497 | return false; |
| 7498 | |
| 7499 | case TSK_ImplicitInstantiation: |
| 7500 | if (PrevPointOfInstantiation.isInvalid()) { |
| 7501 | // The declaration itself has not actually been instantiated, so it is |
| 7502 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7503 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7504 | return false; |
| 7505 | } |
| 7506 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7507 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7508 | case TSK_ExplicitInstantiationDeclaration: |
| 7509 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7510 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 7511 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7512 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7513 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7514 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7515 | // 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] | 7516 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7517 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7518 | // implicit instantiation to take place, in every translation unit in |
| 7519 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7520 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7521 | // Is there any previous explicit specialization declaration? |
| 7522 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 7523 | return false; |
| 7524 | } |
| 7525 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7526 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7527 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7528 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7529 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7530 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7531 | return true; |
| 7532 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7533 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7534 | case TSK_ExplicitInstantiationDeclaration: |
| 7535 | switch (PrevTSK) { |
| 7536 | case TSK_ExplicitInstantiationDeclaration: |
| 7537 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7538 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7539 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7540 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7541 | case TSK_Undeclared: |
| 7542 | case TSK_ImplicitInstantiation: |
| 7543 | // We're explicitly instantiating something that may have already been |
| 7544 | // implicitly instantiated; that's fine. |
| 7545 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7546 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7547 | case TSK_ExplicitSpecialization: |
| 7548 | // C++0x [temp.explicit]p4: |
| 7549 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7550 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7551 | // specialization for that template, the explicit instantiation has no |
| 7552 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7553 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7554 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7555 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7556 | case TSK_ExplicitInstantiationDefinition: |
| 7557 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7558 | // If an entity is the subject of both an explicit instantiation |
| 7559 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7560 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7561 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7562 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7563 | |
| 7564 | // Explicit instantiations following a specialization have no effect and |
| 7565 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 7566 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7567 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 7568 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7569 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7570 | return false; |
| 7571 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7572 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7573 | case TSK_ExplicitInstantiationDefinition: |
| 7574 | switch (PrevTSK) { |
| 7575 | case TSK_Undeclared: |
| 7576 | case TSK_ImplicitInstantiation: |
| 7577 | // We're explicitly instantiating something that may have already been |
| 7578 | // implicitly instantiated; that's fine. |
| 7579 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7580 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7581 | case TSK_ExplicitSpecialization: |
| 7582 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 7583 | // For a given set of template parameters, if an explicit |
| 7584 | // instantiation of a template appears after a declaration of |
| 7585 | // an explicit specialization for that template, the explicit |
| 7586 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 7587 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7588 | << PrevDecl; |
| 7589 | Diag(PrevDecl->getLocation(), |
| 7590 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7591 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7592 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7593 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7594 | case TSK_ExplicitInstantiationDeclaration: |
| 7595 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7596 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7597 | |
| 7598 | // C++0x [temp.explicit]p4: |
| 7599 | // For a given set of template parameters, if an explicit instantiation |
| 7600 | // of a template appears after a declaration of an explicit |
| 7601 | // specialization for that template, the explicit instantiation has no |
| 7602 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7603 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7604 | // Is there any previous explicit specialization declaration? |
| 7605 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7606 | HasNoEffect = true; |
| 7607 | break; |
| 7608 | } |
| 7609 | } |
| 7610 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7611 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7612 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7613 | case TSK_ExplicitInstantiationDefinition: |
| 7614 | // C++0x [temp.spec]p5: |
| 7615 | // For a given template and a given set of template-arguments, |
| 7616 | // - an explicit instantiation definition shall appear at most once |
| 7617 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7618 | |
| 7619 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 7620 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 7621 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7622 | : diag::err_explicit_instantiation_duplicate) |
| 7623 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7624 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7625 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7626 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7627 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7628 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7629 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7630 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7631 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7632 | } |
| 7633 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7634 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7635 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7636 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7637 | /// The only possible way to get a dependent function template specialization |
| 7638 | /// is with a friend declaration, like so: |
| 7639 | /// |
| 7640 | /// \code |
| 7641 | /// template \<class T> void foo(T); |
| 7642 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7643 | /// friend void foo<>(T); |
| 7644 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7645 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7646 | /// |
| 7647 | /// There really isn't any useful analysis we can do here, so we |
| 7648 | /// just store the information. |
| 7649 | bool |
| 7650 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 7651 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 7652 | LookupResult &Previous) { |
| 7653 | // Remove anything from Previous that isn't a function template in |
| 7654 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7655 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7656 | LookupResult::Filter F = Previous.makeFilter(); |
| 7657 | while (F.hasNext()) { |
| 7658 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 7659 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7660 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 7661 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7662 | F.erase(); |
| 7663 | } |
| 7664 | F.done(); |
| 7665 | |
| 7666 | // Should this be diagnosed here? |
| 7667 | if (Previous.empty()) return true; |
| 7668 | |
| 7669 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 7670 | ExplicitTemplateArgs); |
| 7671 | return false; |
| 7672 | } |
| 7673 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7674 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7675 | /// specialization. |
| 7676 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7677 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7678 | /// explicit function template specialization. On successful completion, |
| 7679 | /// the function declaration \p FD will become a function template |
| 7680 | /// specialization. |
| 7681 | /// |
| 7682 | /// \param FD the function declaration, which will be updated to become a |
| 7683 | /// function template specialization. |
| 7684 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7685 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 7686 | /// if any. Note that this may be valid info even when 0 arguments are |
| 7687 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 7688 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7689 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7690 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7691 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7692 | bool Sema::CheckFunctionTemplateSpecialization( |
| 7693 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 7694 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7695 | // The set of function template specializations that could match this |
| 7696 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7697 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 7698 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 7699 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7700 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7701 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 7702 | ConvertedTemplateArgs; |
| 7703 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7704 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7705 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7706 | I != E; ++I) { |
| 7707 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 7708 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7709 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7710 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7711 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 7712 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7713 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7714 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7715 | // When matching a constexpr member function template specialization |
| 7716 | // against the primary template, we don't yet know whether the |
| 7717 | // specialization has an implicit 'const' (because we don't know whether |
| 7718 | // it will be a static member function until we know which template it |
| 7719 | // specializes), so adjust it now assuming it specializes this template. |
| 7720 | QualType FT = FD->getType(); |
| 7721 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7722 | CXXMethodDecl *OldMD = |
| 7723 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7724 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7725 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7726 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7727 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7728 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7729 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7730 | } |
| 7731 | } |
| 7732 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7733 | TemplateArgumentListInfo Args; |
| 7734 | if (ExplicitTemplateArgs) |
| 7735 | Args = *ExplicitTemplateArgs; |
| 7736 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7737 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7738 | // A trailing template-argument can be left unspecified in the |
| 7739 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7740 | // provided it can be deduced from the function argument type. |
| 7741 | // Perform template argument deduction to determine whether we may be |
| 7742 | // specializing this template. |
| 7743 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7744 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7745 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 7746 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 7747 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7748 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 7749 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7750 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7751 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7752 | FailedCandidates.addCandidate().set( |
| 7753 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 7754 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7755 | (void)TDK; |
| 7756 | continue; |
| 7757 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7758 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7759 | // Target attributes are part of the cuda function signature, so |
| 7760 | // the deduced template's cuda target must match that of the |
| 7761 | // specialization. Given that C++ template deduction does not |
| 7762 | // take target attributes into account, we reject candidates |
| 7763 | // here that have a different target. |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 7764 | if (LangOpts.CUDA && |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7765 | IdentifyCUDATarget(Specialization, |
| 7766 | /* IgnoreImplicitHDAttributes = */ true) != |
| 7767 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 7768 | FailedCandidates.addCandidate().set( |
| 7769 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 7770 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 7771 | continue; |
| 7772 | } |
| 7773 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7774 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7775 | if (ExplicitTemplateArgs) |
| 7776 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7777 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7778 | } |
| 7779 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7780 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 7781 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7782 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7783 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7784 | FD->getLocation(), |
| 7785 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 7786 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7787 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7788 | PDiag(diag::note_function_template_spec_matched)); |
| 7789 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7790 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7791 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7792 | |
| 7793 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 7794 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7795 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7796 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare [...] |
| 7797 | // an explicit specialization (14.8.3) [...] of a concept definition. |
| 7798 | if (Specialization->getPrimaryTemplate()->isConcept()) { |
| 7799 | Diag(FD->getLocation(), diag::err_concept_specialized) |
| 7800 | << 0 /*function*/ << 1 /*explicitly specialized*/; |
| 7801 | Diag(Specialization->getLocation(), diag::note_previous_declaration); |
| 7802 | return true; |
| 7803 | } |
| 7804 | |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7805 | FunctionTemplateSpecializationInfo *SpecInfo |
| 7806 | = Specialization->getTemplateSpecializationInfo(); |
| 7807 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7808 | |
| 7809 | // Note: do not overwrite location info if previous template |
| 7810 | // specialization kind was explicit. |
| 7811 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7812 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7813 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7814 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 7815 | // function can differ from the template declaration with respect to |
| 7816 | // the constexpr specifier. |
| 7817 | Specialization->setConstexpr(FD->isConstexpr()); |
| 7818 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7819 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7820 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7821 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7822 | |
| 7823 | // If this is a friend declaration, then we're not really declaring |
| 7824 | // an explicit specialization. |
| 7825 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7826 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7827 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7828 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7829 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7830 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7831 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7832 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7833 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7834 | |
| 7835 | // C++ [temp.expl.spec]p6: |
| 7836 | // 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] | 7837 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7838 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7839 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7840 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7841 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7842 | if (!isFriend && |
| 7843 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7844 | TSK_ExplicitSpecialization, |
| 7845 | Specialization, |
| 7846 | SpecInfo->getTemplateSpecializationKind(), |
| 7847 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7848 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7849 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7850 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7851 | // Mark the prior declaration as an explicit specialization, so that later |
| 7852 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7853 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 7854 | // Since explicit specializations do not inherit '=delete' from their |
| 7855 | // primary function template - check if the 'specialization' that was |
| 7856 | // implicitly generated (during template argument deduction for partial |
| 7857 | // ordering) from the most specialized of all the function templates that |
| 7858 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 7859 | // first check that it was implicitly generated during template argument |
| 7860 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 7861 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 7862 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 7863 | !Specialization->getCanonicalDecl()->isReferenced()) { |
| 7864 | assert( |
| 7865 | Specialization->getCanonicalDecl() == Specialization && |
| 7866 | "This must be the only existing declaration of this specialization"); |
| 7867 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7868 | } |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7869 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7870 | MarkUnusedFileScopedDecl(Specialization); |
| 7871 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7872 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7873 | // Turn the given function declaration into a function template |
| 7874 | // specialization, with the template arguments from the previous |
| 7875 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7876 | // Take copies of (semantic and syntactic) template argument lists. |
| 7877 | const TemplateArgumentList* TemplArgs = new (Context) |
| 7878 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7879 | FD->setFunctionTemplateSpecialization( |
| 7880 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 7881 | SpecInfo->getTemplateSpecializationKind(), |
| 7882 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7883 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7884 | // A function template specialization inherits the target attributes |
| 7885 | // of its template. (We require the attributes explicitly in the |
| 7886 | // code to match, but a template may have implicit attributes by |
| 7887 | // virtue e.g. of being constexpr, and it passes these implicit |
| 7888 | // attributes on to its specializations.) |
| 7889 | if (LangOpts.CUDA) |
| 7890 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 7891 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7892 | // The "previous declaration" for this function template specialization is |
| 7893 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7894 | Previous.clear(); |
| 7895 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7896 | return false; |
| 7897 | } |
| 7898 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7899 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7900 | /// specialization. |
| 7901 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7902 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7903 | /// explicit member function specialization. On successful completion, |
| 7904 | /// the function declaration \p FD will become a member function |
| 7905 | /// specialization. |
| 7906 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7907 | /// \param Member the member declaration, which will be updated to become a |
| 7908 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7909 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7910 | /// \param Previous the set of declarations, one of which may be specialized |
| 7911 | /// by this function specialization; the set will be modified to contain the |
| 7912 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7913 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7914 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7915 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7916 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7917 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7918 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7919 | NamedDecl *Instantiation = nullptr; |
| 7920 | NamedDecl *InstantiatedFrom = nullptr; |
| 7921 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7922 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7923 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7924 | // Nowhere to look anyway. |
| 7925 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7926 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7927 | I != E; ++I) { |
| 7928 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7929 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7930 | QualType Adjusted = Function->getType(); |
| 7931 | if (!hasExplicitCallingConv(Adjusted)) |
| 7932 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7933 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7934 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7935 | Instantiation = Method; |
| 7936 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7937 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7938 | break; |
| 7939 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7940 | } |
| 7941 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7942 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7943 | VarDecl *PrevVar; |
| 7944 | if (Previous.isSingleResult() && |
| 7945 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7946 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7947 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7948 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7949 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7950 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7951 | } |
| 7952 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7953 | CXXRecordDecl *PrevRecord; |
| 7954 | if (Previous.isSingleResult() && |
| 7955 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7956 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7957 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7958 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7959 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7960 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7961 | } else if (isa<EnumDecl>(Member)) { |
| 7962 | EnumDecl *PrevEnum; |
| 7963 | if (Previous.isSingleResult() && |
| 7964 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7965 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7966 | Instantiation = PrevEnum; |
| 7967 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7968 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7969 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7970 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7971 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7972 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7973 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7974 | // specializations are always out-of-line, the caller will complain about |
| 7975 | // this mismatch later. |
| 7976 | return false; |
| 7977 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7978 | |
| 7979 | // If this is a friend, just bail out here before we start turning |
| 7980 | // things into explicit specializations. |
| 7981 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7982 | // Preserve instantiation information. |
| 7983 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7984 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7985 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7986 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7987 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7988 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7989 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7990 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7991 | } |
| 7992 | |
| 7993 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7994 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7995 | return false; |
| 7996 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7997 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7998 | // Make sure that this is a specialization of a member. |
| 7999 | if (!InstantiatedFrom) { |
| 8000 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 8001 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8002 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 8003 | return true; |
| 8004 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8005 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8006 | // C++ [temp.expl.spec]p6: |
| 8007 | // 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] | 8008 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8009 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8010 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8011 | // use occurs; no diagnostic is required. |
| 8012 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8013 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8014 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 8015 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 8016 | TSK_ExplicitSpecialization, |
| 8017 | Instantiation, |
| 8018 | MSInfo->getTemplateSpecializationKind(), |
| 8019 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8020 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8021 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8022 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8023 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8024 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8025 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8026 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 8027 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8028 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 8029 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8030 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8031 | // the original declaration to note that it is an explicit specialization |
| 8032 | // (if it was previously an implicit instantiation). This latter step |
| 8033 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8034 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8035 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 8036 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 8037 | TSK_ImplicitInstantiation) { |
| 8038 | InstantiationFunction->setTemplateSpecializationKind( |
| 8039 | TSK_ExplicitSpecialization); |
| 8040 | InstantiationFunction->setLocation(Member->getLocation()); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8041 | // Explicit specializations of member functions of class templates do not |
| 8042 | // inherit '=delete' from the member function they are specializing. |
| 8043 | if (InstantiationFunction->isDeleted()) { |
| 8044 | assert(InstantiationFunction->getCanonicalDecl() == |
| 8045 | InstantiationFunction); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 8046 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 8047 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8048 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8049 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8050 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 8051 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 8052 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8053 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8054 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8055 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 8056 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 8057 | TSK_ImplicitInstantiation) { |
| 8058 | InstantiationVar->setTemplateSpecializationKind( |
| 8059 | TSK_ExplicitSpecialization); |
| 8060 | InstantiationVar->setLocation(Member->getLocation()); |
| 8061 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8062 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8063 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 8064 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 8065 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8066 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8067 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 8068 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 8069 | TSK_ImplicitInstantiation) { |
| 8070 | InstantiationClass->setTemplateSpecializationKind( |
| 8071 | TSK_ExplicitSpecialization); |
| 8072 | InstantiationClass->setLocation(Member->getLocation()); |
| 8073 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8074 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8075 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 8076 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 8077 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8078 | } else { |
| 8079 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 8080 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 8081 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 8082 | TSK_ImplicitInstantiation) { |
| 8083 | InstantiationEnum->setTemplateSpecializationKind( |
| 8084 | TSK_ExplicitSpecialization); |
| 8085 | InstantiationEnum->setLocation(Member->getLocation()); |
| 8086 | } |
| 8087 | |
| 8088 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 8089 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 8090 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8091 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8092 | // Save the caller the trouble of having to figure out which declaration |
| 8093 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8094 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 8095 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 8096 | return false; |
| 8097 | } |
| 8098 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8099 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8100 | /// |
| 8101 | /// \returns true if a serious error occurs, false otherwise. |
| 8102 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8103 | SourceLocation InstLoc, |
| 8104 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8105 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 8106 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8107 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8108 | if (CurContext->isRecord()) { |
| 8109 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 8110 | << D; |
| 8111 | return true; |
| 8112 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8113 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8114 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8115 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8116 | // template. If the name declared in the explicit instantiation is an |
| 8117 | // unqualified name, the explicit instantiation shall appear in the |
| 8118 | // namespace where its template is declared or, if that namespace is inline |
| 8119 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8120 | // |
| 8121 | // 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] | 8122 | if (WasQualifiedName) { |
| 8123 | if (CurContext->Encloses(OrigContext)) |
| 8124 | return false; |
| 8125 | } else { |
| 8126 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 8127 | return false; |
| 8128 | } |
| 8129 | |
| 8130 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 8131 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8132 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8133 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8134 | diag::err_explicit_instantiation_out_of_scope : |
| 8135 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8136 | << D << NS; |
| 8137 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8138 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8139 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8140 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 8141 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 8142 | << D << NS; |
| 8143 | } else |
| 8144 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8145 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8146 | diag::err_explicit_instantiation_must_be_global : |
| 8147 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 8148 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8149 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8150 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8151 | } |
| 8152 | |
| 8153 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 8154 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 8155 | if (!SS.isSet()) |
| 8156 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8157 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 8158 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8159 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8160 | // or a static data member of a class template specialization, the name of |
| 8161 | // the class template specialization in the qualified-id for the member |
| 8162 | // name shall be a simple-template-id. |
| 8163 | // |
| 8164 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8165 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 8166 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8167 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8168 | if (isa<TemplateSpecializationType>(T)) |
| 8169 | return true; |
| 8170 | |
| 8171 | return false; |
| 8172 | } |
| 8173 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8174 | /// Make a dllexport or dllimport attr on a class template specialization take |
| 8175 | /// effect. |
| 8176 | static void dllExportImportClassTemplateSpecialization( |
| 8177 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 8178 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 8179 | assert(A && "dllExportImportClassTemplateSpecialization called " |
| 8180 | "on Def without dllexport or dllimport"); |
| 8181 | |
| 8182 | // We reject explicit instantiations in class scope, so there should |
| 8183 | // never be any delayed exported classes to worry about. |
| 8184 | assert(S.DelayedDllExportClasses.empty() && |
| 8185 | "delayed exports present at explicit instantiation"); |
| 8186 | S.checkClassLevelDLLAttribute(Def); |
| 8187 | |
| 8188 | // Propagate attribute to base class templates. |
| 8189 | for (auto &B : Def->bases()) { |
| 8190 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 8191 | B.getType()->getAsCXXRecordDecl())) |
| 8192 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 8193 | } |
| 8194 | |
| 8195 | S.referenceDLLExportedClassMethods(); |
| 8196 | } |
| 8197 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8198 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8199 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8200 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 8201 | SourceLocation ExternLoc, |
| 8202 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8203 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8204 | SourceLocation KWLoc, |
| 8205 | const CXXScopeSpec &SS, |
| 8206 | TemplateTy TemplateD, |
| 8207 | SourceLocation TemplateNameLoc, |
| 8208 | SourceLocation LAngleLoc, |
| 8209 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8210 | SourceLocation RAngleLoc, |
| 8211 | AttributeList *Attr) { |
| 8212 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 8213 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8214 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8215 | // Check that the specialization uses the same tag kind as the |
| 8216 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8217 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 8218 | assert(Kind != TTK_Enum && |
| 8219 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8220 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8221 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 8222 | |
| 8223 | if (!ClassTemplate) { |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 8224 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 8225 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 8226 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 8227 | return true; |
| 8228 | } |
| 8229 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 8230 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 8231 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 8232 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8233 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8234 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8235 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8236 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8237 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8238 | diag::note_previous_use); |
| 8239 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 8240 | } |
| 8241 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8242 | // C++0x [temp.explicit]p2: |
| 8243 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8244 | // definition and an explicit instantiation declaration. An explicit |
| 8245 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 8246 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 8247 | ? TSK_ExplicitInstantiationDefinition |
| 8248 | : TSK_ExplicitInstantiationDeclaration; |
| 8249 | |
| 8250 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 8251 | // Check for dllexport class template instantiation declarations. |
| 8252 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 8253 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 8254 | Diag(ExternLoc, |
| 8255 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8256 | Diag(A->getLoc(), diag::note_attribute); |
| 8257 | break; |
| 8258 | } |
| 8259 | } |
| 8260 | |
| 8261 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 8262 | Diag(ExternLoc, |
| 8263 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8264 | Diag(A->getLocation(), diag::note_attribute); |
| 8265 | } |
| 8266 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8267 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8268 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 8269 | // instantiation declarations for most purposes. |
| 8270 | bool DLLImportExplicitInstantiationDef = false; |
| 8271 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 8272 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 8273 | // Check for dllimport class template instantiation definitions. |
| 8274 | bool DLLImport = |
| 8275 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 8276 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 8277 | if (A->getKind() == AttributeList::AT_DLLImport) |
| 8278 | DLLImport = true; |
| 8279 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 8280 | // dllexport trumps dllimport here. |
| 8281 | DLLImport = false; |
| 8282 | break; |
| 8283 | } |
| 8284 | } |
| 8285 | if (DLLImport) { |
| 8286 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 8287 | DLLImportExplicitInstantiationDef = true; |
| 8288 | } |
| 8289 | } |
| 8290 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8291 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8292 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 8293 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8294 | |
| 8295 | // Check that the template argument list is well-formed for this |
| 8296 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8297 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8298 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 8299 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8300 | return true; |
| 8301 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8302 | // Find the class template specialization declaration that |
| 8303 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8304 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8305 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 8306 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8307 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8308 | TemplateSpecializationKind PrevDecl_TSK |
| 8309 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 8310 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 8311 | // C++0x [temp.explicit]p2: |
| 8312 | // [...] An explicit instantiation shall appear in an enclosing |
| 8313 | // namespace of its template. [...] |
| 8314 | // |
| 8315 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 8316 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 8317 | SS.isSet())) |
| 8318 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8319 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8320 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8321 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8322 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8323 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8324 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8325 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8326 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8327 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8328 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8329 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8330 | // Even though HasNoEffect == true means that this explicit instantiation |
| 8331 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 8332 | |
| 8333 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 8334 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8335 | // Since the only prior class template specialization with these |
| 8336 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8337 | // declaration node as our own, updating the source location |
| 8338 | // for the template name to reflect our new declaration. |
| 8339 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8340 | Specialization = PrevDecl; |
| 8341 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8342 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8343 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8344 | |
| 8345 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 8346 | DLLImportExplicitInstantiationDef) { |
| 8347 | // The new specialization might add a dllimport attribute. |
| 8348 | HasNoEffect = false; |
| 8349 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8350 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8351 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 8352 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8353 | // Create a new class template specialization declaration node for |
| 8354 | // this explicit specialization. |
| 8355 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 8356 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8357 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 8358 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8359 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 8360 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8361 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 8362 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8363 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8364 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8365 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 8366 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8367 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8368 | } |
| 8369 | |
| 8370 | // Build the fully-sugared type for this explicit instantiation as |
| 8371 | // the user wrote in the explicit instantiation itself. This means |
| 8372 | // that we'll pretty-print the type retrieved from the |
| 8373 | // specialization's declaration the way that the user actually wrote |
| 8374 | // the explicit instantiation, rather than formatting the name based |
| 8375 | // on the "canonical" representation used to store the template |
| 8376 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 8377 | TypeSourceInfo *WrittenTy |
| 8378 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 8379 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8380 | Context.getTypeDeclType(Specialization)); |
| 8381 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8382 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8383 | // Set source locations for keywords. |
| 8384 | Specialization->setExternLoc(ExternLoc); |
| 8385 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 8386 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8387 | |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8388 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 8389 | if (Attr) |
| 8390 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 8391 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8392 | // Add the explicit instantiation into its lexical context. However, |
| 8393 | // since explicit instantiations are never found by name lookup, we |
| 8394 | // just put it into the declaration context directly. |
| 8395 | Specialization->setLexicalDeclContext(CurContext); |
| 8396 | CurContext->addDecl(Specialization); |
| 8397 | |
| 8398 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 8399 | if (HasNoEffect) { |
| 8400 | // Set the template specialization kind. |
| 8401 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8402 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 8403 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8404 | |
| 8405 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8406 | // A definition of a class template or class member template |
| 8407 | // shall be in scope at the point of the explicit instantiation of |
| 8408 | // the class template or class member template. |
| 8409 | // |
| 8410 | // This check comes when we actually try to perform the |
| 8411 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8412 | ClassTemplateSpecializationDecl *Def |
| 8413 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8414 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8415 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8416 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8417 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8418 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8419 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 8420 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8421 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8422 | // Instantiate the members of this class template specialization. |
| 8423 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8424 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8425 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8426 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8427 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 8428 | // TSK_ExplicitInstantiationDefinition |
| 8429 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 8430 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 8431 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8432 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 8433 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8434 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 8435 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
Shoaib Meenai | ab3f96c | 2016-11-09 23:52:20 +0000 | [diff] [blame] | 8436 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8437 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 8438 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 8439 | // attribute to a template with a previous instantiation declaration. |
| 8440 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8441 | auto *A = cast<InheritableAttr>( |
| 8442 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 8443 | A->setInherited(true); |
| 8444 | Def->addAttr(A); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8445 | dllExportImportClassTemplateSpecialization(*this, Def); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8446 | } |
| 8447 | } |
| 8448 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8449 | // Fix a TSK_ImplicitInstantiation followed by a |
| 8450 | // TSK_ExplicitInstantiationDefinition |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8451 | bool NewlyDLLExported = |
| 8452 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
| 8453 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8454 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8455 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 8456 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 8457 | // attribute to a template with a previous implicit instantiation. |
| 8458 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to |
| 8459 | // avoid potentially strange codegen behavior. For example, if we extend |
| 8460 | // this conditional to dllimport, and we have a source file calling a |
| 8461 | // method on an implicitly instantiated template class instance and then |
| 8462 | // declaring a dllimport explicit instantiation definition for the same |
| 8463 | // template class, the codegen for the method call will not respect the |
| 8464 | // dllimport, while it will with cl. The Def will already have the DLL |
| 8465 | // attribute, since the Def and Specialization will be the same in the |
| 8466 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the |
| 8467 | // attribute to the Specialization; we just need to make it take effect. |
| 8468 | assert(Def == Specialization && |
| 8469 | "Def and Specialization should match for implicit instantiation"); |
| 8470 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 8471 | } |
| 8472 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8473 | // Set the template specialization kind. Make sure it is set before |
| 8474 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 8475 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8476 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8477 | } else { |
| 8478 | |
| 8479 | // Set the template specialization kind. |
| 8480 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8481 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8482 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8483 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8484 | } |
| 8485 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8486 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8487 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8488 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 8489 | SourceLocation ExternLoc, |
| 8490 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8491 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8492 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 8493 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8494 | IdentifierInfo *Name, |
| 8495 | SourceLocation NameLoc, |
| 8496 | AttributeList *Attr) { |
| 8497 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 8498 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8499 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8500 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8501 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 8502 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8503 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 8504 | SourceLocation(), false, TypeResult(), |
| 8505 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8506 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 8507 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8508 | if (!TagD) |
| 8509 | return true; |
| 8510 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8511 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8512 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8513 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 8514 | if (Tag->isInvalidDecl()) |
| 8515 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8516 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8517 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 8518 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 8519 | if (!Pattern) { |
| 8520 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 8521 | << Context.getTypeDeclType(Record); |
| 8522 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 8523 | return true; |
| 8524 | } |
| 8525 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8526 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8527 | // If the explicit instantiation is for a class or member class, the |
| 8528 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8529 | // simple-template-id. |
| 8530 | // |
| 8531 | // C++98 has the same restriction, just worded differently. |
| 8532 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8533 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8534 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8535 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8536 | // C++0x [temp.explicit]p2: |
| 8537 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8538 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8539 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 8540 | TemplateSpecializationKind TSK |
| 8541 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8542 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8543 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8544 | // C++0x [temp.explicit]p2: |
| 8545 | // [...] An explicit instantiation shall appear in an enclosing |
| 8546 | // namespace of its template. [...] |
| 8547 | // |
| 8548 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8549 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8550 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8551 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8552 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8553 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8554 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8555 | PrevDecl = Record; |
| 8556 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8557 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8558 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8559 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8560 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8561 | PrevDecl, |
| 8562 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8563 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8564 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8565 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8566 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8567 | return TagD; |
| 8568 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8569 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8570 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8571 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8572 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8573 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8574 | // 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] | 8575 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8576 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8577 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8578 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 8579 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 8580 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8581 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 8582 | << Pattern; |
| 8583 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8584 | } else { |
| 8585 | if (InstantiateClass(NameLoc, Record, Def, |
| 8586 | getTemplateInstantiationArgs(Record), |
| 8587 | TSK)) |
| 8588 | return true; |
| 8589 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8590 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8591 | if (!RecordDef) |
| 8592 | return true; |
| 8593 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8594 | } |
| 8595 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8596 | // Instantiate all of the members of the class. |
| 8597 | InstantiateClassMembers(NameLoc, RecordDef, |
| 8598 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8599 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8600 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8601 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 8602 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8603 | // FIXME: We don't have any representation for explicit instantiations of |
| 8604 | // member classes. Such a representation is not needed for compilation, but it |
| 8605 | // should be available for clients that want to see all of the declarations in |
| 8606 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8607 | return TagD; |
| 8608 | } |
| 8609 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8610 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 8611 | SourceLocation ExternLoc, |
| 8612 | SourceLocation TemplateLoc, |
| 8613 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8614 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8615 | // TODO: check if/when DNInfo should replace Name. |
| 8616 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 8617 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8618 | if (!Name) { |
| 8619 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8620 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8621 | diag::err_explicit_instantiation_requires_name) |
| 8622 | << D.getDeclSpec().getSourceRange() |
| 8623 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8624 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8625 | return true; |
| 8626 | } |
| 8627 | |
| 8628 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 8629 | // we find one that is. |
| 8630 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 8631 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 8632 | S = S->getParent(); |
| 8633 | |
| 8634 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8635 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 8636 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8637 | if (R.isNull()) |
| 8638 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8639 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8640 | // C++ [dcl.stc]p1: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8641 | // A storage-class-specifier shall not be specified in [...] an explicit |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8642 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8643 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8644 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 8645 | << Name; |
| 8646 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8647 | } else if (D.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8648 | != DeclSpec::SCS_unspecified) { |
| 8649 | // Complain about then remove the storage class specifier. |
| 8650 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 8651 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8652 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8653 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8654 | } |
| 8655 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 8656 | // C++0x [temp.explicit]p1: |
| 8657 | // [...] An explicit instantiation of a function template shall not use the |
| 8658 | // inline or constexpr specifiers. |
| 8659 | // Presumably, this also applies to member functions of class templates as |
| 8660 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 8661 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8662 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8663 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 8664 | diag::err_explicit_instantiation_inline : |
| 8665 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 8666 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8667 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 8668 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 8669 | // not already specified. |
| 8670 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 8671 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8672 | |
Nathan Wilson | de49845 | 2016-02-08 05:34:00 +0000 | [diff] [blame] | 8673 | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
| 8674 | // applied only to the definition of a function template or variable template, |
| 8675 | // declared in namespace scope. |
| 8676 | if (D.getDeclSpec().isConceptSpecified()) { |
| 8677 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
| 8678 | diag::err_concept_specified_specialization) << 0; |
| 8679 | return true; |
| 8680 | } |
| 8681 | |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 8682 | // A deduction guide is not on the list of entities that can be explicitly |
| 8683 | // instantiated. |
| 8684 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
| 8685 | Diag(D.getDeclSpec().getLocStart(), diag::err_deduction_guide_specialized) |
| 8686 | << /*explicit instantiation*/ 0; |
| 8687 | return true; |
| 8688 | } |
| 8689 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8690 | // C++0x [temp.explicit]p2: |
| 8691 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8692 | // definition and an explicit instantiation declaration. An explicit |
| 8693 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8694 | TemplateSpecializationKind TSK |
| 8695 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8696 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8697 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8698 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8699 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8700 | |
| 8701 | if (!R->isFunctionType()) { |
| 8702 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8703 | // A [...] static data member of a class template can be explicitly |
| 8704 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8705 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8706 | // C++1y [temp.explicit]p1: |
| 8707 | // A [...] variable [...] template specialization can be explicitly |
| 8708 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8709 | if (Previous.isAmbiguous()) |
| 8710 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8711 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 8712 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8713 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8714 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8715 | if (!PrevTemplate) { |
| 8716 | if (!Prev || !Prev->isStaticDataMember()) { |
| 8717 | // We expect to see a data data member here. |
| 8718 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 8719 | << Name; |
| 8720 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8721 | P != PEnd; ++P) |
| 8722 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 8723 | return true; |
| 8724 | } |
| 8725 | |
| 8726 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 8727 | // FIXME: Check for explicit specialization? |
| 8728 | Diag(D.getIdentifierLoc(), |
| 8729 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 8730 | << Prev; |
| 8731 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 8732 | // FIXME: Can we provide a note showing where this was declared? |
| 8733 | return true; |
| 8734 | } |
| 8735 | } else { |
| 8736 | // Explicitly instantiate a variable template. |
| 8737 | |
| 8738 | // C++1y [dcl.spec.auto]p6: |
| 8739 | // ... A program that uses auto or decltype(auto) in a context not |
| 8740 | // explicitly allowed in this section is ill-formed. |
| 8741 | // |
| 8742 | // This includes auto-typed variable template instantiations. |
| 8743 | if (R->isUndeducedType()) { |
| 8744 | Diag(T->getTypeLoc().getLocStart(), |
| 8745 | diag::err_auto_not_allowed_var_inst); |
| 8746 | return true; |
| 8747 | } |
| 8748 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8749 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 8750 | // C++1y [temp.explicit]p3: |
| 8751 | // If the explicit instantiation is for a variable, the unqualified-id |
| 8752 | // in the declaration shall be a template-id. |
| 8753 | Diag(D.getIdentifierLoc(), |
| 8754 | diag::err_explicit_instantiation_without_template_id) |
| 8755 | << PrevTemplate; |
| 8756 | Diag(PrevTemplate->getLocation(), |
| 8757 | diag::note_explicit_instantiation_here); |
| 8758 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8759 | } |
| 8760 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8761 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8762 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8763 | if (PrevTemplate->isConcept()) { |
| 8764 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8765 | << 1 /*variable*/ << 0 /*explicitly instantiated*/; |
| 8766 | Diag(PrevTemplate->getLocation(), diag::note_previous_declaration); |
| 8767 | return true; |
| 8768 | } |
| 8769 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8770 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8771 | TemplateArgumentListInfo TemplateArgs = |
| 8772 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8773 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8774 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 8775 | D.getIdentifierLoc(), TemplateArgs); |
| 8776 | if (Res.isInvalid()) |
| 8777 | return true; |
| 8778 | |
| 8779 | // Ignore access control bits, we don't need them for redeclaration |
| 8780 | // checking. |
| 8781 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8782 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8783 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8784 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8785 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8786 | // or a static data member of a class template specialization, the name of |
| 8787 | // the class template specialization in the qualified-id for the member |
| 8788 | // name shall be a simple-template-id. |
| 8789 | // |
| 8790 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8791 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 8792 | // This does not apply to variable template specializations, where the |
| 8793 | // template-id is in the unqualified-id instead. |
| 8794 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8795 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8796 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8797 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8798 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8799 | // Check the scope of this explicit instantiation. |
| 8800 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8801 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8802 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8803 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 8804 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8805 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8806 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8807 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8808 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8809 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8810 | if (!HasNoEffect) { |
| 8811 | // Instantiate static data member or variable template. |
| 8812 | |
| 8813 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 8814 | if (PrevTemplate) { |
| 8815 | // Merge attributes. |
| 8816 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 8817 | ProcessDeclAttributeList(S, Prev, Attr); |
| 8818 | } |
| 8819 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8820 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 8821 | } |
| 8822 | |
| 8823 | // Check the new variable specialization against the parsed input. |
| 8824 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 8825 | Diag(T->getTypeLoc().getLocStart(), |
| 8826 | diag::err_invalid_var_template_spec_type) |
| 8827 | << 0 << PrevTemplate << R << Prev->getType(); |
| 8828 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 8829 | << 2 << PrevTemplate->getDeclName(); |
| 8830 | return true; |
| 8831 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8832 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8833 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8834 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8835 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8836 | |
| 8837 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 8838 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8839 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8840 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8841 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8842 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8843 | HasExplicitTemplateArgs = true; |
| 8844 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8845 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8846 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8847 | // A [...] function [...] can be explicitly instantiated from its template. |
| 8848 | // A member function [...] of a class template can be explicitly |
| 8849 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8850 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8851 | UnresolvedSet<8> Matches; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8852 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8853 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8854 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8855 | P != PEnd; ++P) { |
| 8856 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8857 | if (!HasExplicitTemplateArgs) { |
| 8858 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 8859 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 8860 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 8861 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8862 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8863 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8864 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8865 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 8866 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8867 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8868 | } |
| 8869 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8870 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8871 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 8872 | if (!FunTmpl) |
| 8873 | continue; |
| 8874 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8875 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8876 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8877 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8878 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8879 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 8880 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8881 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8882 | // Keep track of almost-matches. |
| 8883 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8884 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8885 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8886 | (void)TDK; |
| 8887 | continue; |
| 8888 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8889 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8890 | // Target attributes are part of the cuda function signature, so |
| 8891 | // the cuda target of the instantiated function must match that of its |
| 8892 | // template. Given that C++ template deduction does not take |
| 8893 | // target attributes into account, we reject candidates here that |
| 8894 | // have a different target. |
| 8895 | if (LangOpts.CUDA && |
| 8896 | IdentifyCUDATarget(Specialization, |
| 8897 | /* IgnoreImplicitHDAttributes = */ true) != |
| 8898 | IdentifyCUDATarget(Attr)) { |
| 8899 | FailedCandidates.addCandidate().set( |
| 8900 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 8901 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8902 | continue; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8903 | } |
| 8904 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8905 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8906 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8907 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8908 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8909 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 8910 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8911 | D.getIdentifierLoc(), |
| 8912 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 8913 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 8914 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8915 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8916 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8917 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8918 | |
| 8919 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 8920 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8921 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 8922 | // C++11 [except.spec]p4 |
| 8923 | // In an explicit instantiation an exception-specification may be specified, |
| 8924 | // but is not required. |
| 8925 | // If an exception-specification is specified in an explicit instantiation |
| 8926 | // directive, it shall be compatible with the exception-specifications of |
| 8927 | // other declarations of that function. |
| 8928 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 8929 | if (FPT->hasExceptionSpec()) { |
| 8930 | unsigned DiagID = |
| 8931 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 8932 | if (getLangOpts().MicrosoftExt) |
| 8933 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 8934 | bool Result = CheckEquivalentExceptionSpec( |
| 8935 | PDiag(DiagID) << Specialization->getType(), |
| 8936 | PDiag(diag::note_explicit_instantiation_here), |
| 8937 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 8938 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 8939 | // In Microsoft mode, mismatching exception specifications just cause a |
| 8940 | // warning. |
| 8941 | if (!getLangOpts().MicrosoftExt && Result) |
| 8942 | return true; |
| 8943 | } |
| 8944 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8945 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8946 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8947 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 8948 | << Specialization |
| 8949 | << (Specialization->getTemplateSpecializationKind() == |
| 8950 | TSK_ExplicitSpecialization); |
| 8951 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 8952 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8953 | } |
| 8954 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8955 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8956 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 8957 | PrevDecl = Specialization; |
| 8958 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8959 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8960 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8961 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8962 | PrevDecl, |
| 8963 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8964 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8965 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8966 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8967 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8968 | // FIXME: We may still want to build some representation of this |
| 8969 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8970 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8971 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8972 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 8973 | |
| 8974 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 8975 | if (Attr) |
| 8976 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8977 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8978 | if (Specialization->isDefined()) { |
| 8979 | // Let the ASTConsumer know that this function has been explicitly |
| 8980 | // instantiated now, and its linkage might have changed. |
| 8981 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 8982 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 8983 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8984 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8985 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8986 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8987 | // or a static data member of a class template specialization, the name of |
| 8988 | // the class template specialization in the qualified-id for the member |
| 8989 | // name shall be a simple-template-id. |
| 8990 | // |
| 8991 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8992 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8993 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8994 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8995 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8996 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8997 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8998 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8999 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 9000 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 9001 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 9002 | if (FunTmpl && FunTmpl->isConcept() && |
| 9003 | !D.getDeclSpec().isConceptSpecified()) { |
| 9004 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 9005 | << 0 /*function*/ << 0 /*explicitly instantiated*/; |
| 9006 | Diag(FunTmpl->getLocation(), diag::note_previous_declaration); |
| 9007 | return true; |
| 9008 | } |
| 9009 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9010 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9011 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9012 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9013 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 9014 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9015 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9016 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9017 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 9018 | } |
| 9019 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9020 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9021 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 9022 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 9023 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 9024 | // This has to hold, because SS is expected to be defined. |
| 9025 | assert(Name && "Expected a name in a dependent tag"); |
| 9026 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9027 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9028 | if (!NNS) |
| 9029 | return true; |
| 9030 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9031 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 9032 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9033 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 9034 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9035 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 9036 | return true; |
| 9037 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9038 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9039 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9040 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9041 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9042 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9043 | // Create type-source location information for this type. |
| 9044 | TypeLocBuilder TLB; |
| 9045 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9046 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 9047 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 9048 | TL.setNameLoc(NameLoc); |
| 9049 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 9050 | } |
| 9051 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9052 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9053 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 9054 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 9055 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9056 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9057 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9058 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9059 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9060 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9061 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9062 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9063 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9064 | << FixItHint::CreateRemoval(TypenameLoc); |
| 9065 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9066 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9067 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 9068 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 9069 | if (T.isNull()) |
| 9070 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9071 | |
| 9072 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9073 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9074 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9075 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9076 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9077 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9078 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9079 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9080 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9081 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9082 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9083 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9084 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9085 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9086 | } |
| 9087 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9088 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9089 | Sema::ActOnTypenameType(Scope *S, |
| 9090 | SourceLocation TypenameLoc, |
| 9091 | const CXXScopeSpec &SS, |
| 9092 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9093 | TemplateTy TemplateIn, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9094 | IdentifierInfo *TemplateII, |
| 9095 | SourceLocation TemplateIILoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9096 | SourceLocation LAngleLoc, |
| 9097 | ASTTemplateArgsPtr TemplateArgsIn, |
| 9098 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9099 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9100 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9101 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 9102 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9103 | diag::ext_typename_outside_of_template) |
| 9104 | << FixItHint::CreateRemoval(TypenameLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9105 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9106 | // Strangely, non-type results are not ignored by this lookup, so the |
| 9107 | // program is ill-formed if it finds an injected-class-name. |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 9108 | if (TypenameLoc.isValid()) { |
| 9109 | auto *LookupRD = |
| 9110 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
| 9111 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 9112 | Diag(TemplateIILoc, |
| 9113 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9114 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 9115 | << (TemplateKWLoc.isValid() ? 1 : 0 /*'template'/'typename' keyword*/); |
| 9116 | } |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9117 | } |
| 9118 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9119 | // Translate the parser's template argument list in our AST format. |
| 9120 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 9121 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9122 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9123 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9124 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 9125 | // Construct a dependent template specialization type. |
| 9126 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 9127 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9128 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 9129 | DTN->getQualifier(), |
| 9130 | DTN->getIdentifier(), |
| 9131 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9132 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9133 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 9134 | TypeLocBuilder Builder; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9135 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9136 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9137 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 9138 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 9139 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9140 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9141 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9142 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9143 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9144 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9145 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 9146 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9147 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9148 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9149 | if (T.isNull()) |
| 9150 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9151 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9152 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9153 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9154 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9155 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 9156 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9157 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9158 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9159 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9160 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9161 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9162 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9163 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 9164 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9165 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9166 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9167 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 9168 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 9169 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 9170 | } |
| 9171 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 9172 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9173 | /// Determine whether this failed name lookup should be treated as being |
| 9174 | /// disabled by a usage of std::enable_if. |
| 9175 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 9176 | SourceRange &CondRange) { |
| 9177 | // We must be looking for a ::type... |
| 9178 | if (!II.isStr("type")) |
| 9179 | return false; |
| 9180 | |
| 9181 | // ... within an explicitly-written template specialization... |
| 9182 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 9183 | return false; |
| 9184 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9185 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 9186 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 9187 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9188 | return false; |
| 9189 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9190 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9191 | |
| 9192 | // ... which names a complete class template declaration... |
| 9193 | const TemplateDecl *EnableIfDecl = |
| 9194 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 9195 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 9196 | return false; |
| 9197 | |
| 9198 | // ... called "enable_if". |
| 9199 | const IdentifierInfo *EnableIfII = |
| 9200 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 9201 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 9202 | return false; |
| 9203 | |
| 9204 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9205 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9206 | return true; |
| 9207 | } |
| 9208 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9209 | /// \brief Build the type that describes a C++ typename specifier, |
| 9210 | /// e.g., "typename T::type". |
| 9211 | QualType |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9212 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9213 | SourceLocation KeywordLoc, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9214 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9215 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9216 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9217 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9218 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9219 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9220 | DeclContext *Ctx = computeDeclContext(SS); |
| 9221 | if (!Ctx) { |
| 9222 | // If the nested-name-specifier is dependent and couldn't be |
| 9223 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9224 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9225 | return Context.getDependentNameType(Keyword, |
| 9226 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9227 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 9228 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9229 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9230 | // If the nested-name-specifier refers to the current instantiation, |
| 9231 | // the "typename" keyword itself is superfluous. In C++03, the |
| 9232 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 9233 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 9234 | // 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] | 9235 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 9236 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 9237 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9238 | |
| 9239 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9240 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 9241 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9242 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9243 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9244 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9245 | case LookupResult::NotFound: { |
| 9246 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 9247 | // a more specific diagnostic. |
| 9248 | SourceRange CondRange; |
| 9249 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 9250 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 9251 | << Ctx << CondRange; |
| 9252 | return QualType(); |
| 9253 | } |
| 9254 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 9255 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9256 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9257 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9258 | |
| 9259 | case LookupResult::FoundUnresolvedValue: { |
| 9260 | // We found a using declaration that is a value. Most likely, the using |
| 9261 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9262 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9263 | IILoc); |
| 9264 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 9265 | << Name << Ctx << FullRange; |
| 9266 | if (UnresolvedUsingValueDecl *Using |
| 9267 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 9268 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 9269 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 9270 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 9271 | } |
| 9272 | } |
| 9273 | // Fall through to create a dependent typename type, from which we can recover |
| 9274 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9275 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 9276 | case LookupResult::NotFoundInCurrentInstantiation: |
| 9277 | // Okay, it's a member of an unknown instantiation. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9278 | return Context.getDependentNameType(Keyword, |
| 9279 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9280 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9281 | |
| 9282 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9283 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9284 | // C++ [class.qual]p2: |
| 9285 | // In a lookup in which function names are not ignored and the |
| 9286 | // nested-name-specifier nominates a class C, if the name specified |
| 9287 | // after the nested-name-specifier, when looked up in C, is the |
| 9288 | // injected-class-name of C [...] then the name is instead considered |
| 9289 | // to name the constructor of class C. |
| 9290 | // |
| 9291 | // Unlike in an elaborated-type-specifier, function names are not ignored |
| 9292 | // in typename-specifier lookup. However, they are ignored in all the |
| 9293 | // contexts where we form a typename type with no keyword (that is, in |
| 9294 | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). |
| 9295 | // |
| 9296 | // FIXME: That's not strictly true: mem-initializer-id lookup does not |
| 9297 | // ignore functions, but that appears to be an oversight. |
| 9298 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
| 9299 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
| 9300 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
| 9301 | FoundRD->isInjectedClassName() && |
| 9302 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
| 9303 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9304 | << &II << 1 << 0 /*'typename' keyword used*/; |
| 9305 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9306 | // We found a type. Build an ElaboratedType, since the |
| 9307 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 9308 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 9309 | return Context.getElaboratedType(Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9310 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9311 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9312 | } |
| 9313 | |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 9314 | // C++ [dcl.type.simple]p2: |
| 9315 | // A type-specifier of the form |
| 9316 | // typename[opt] nested-name-specifier[opt] template-name |
| 9317 | // is a placeholder for a deduced class type [...]. |
| 9318 | if (getLangOpts().CPlusPlus1z) { |
| 9319 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
| 9320 | return Context.getElaboratedType( |
| 9321 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
| 9322 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
| 9323 | QualType(), false)); |
| 9324 | } |
| 9325 | } |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 9326 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9327 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 9328 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9329 | break; |
| 9330 | |
| 9331 | case LookupResult::FoundOverloaded: |
| 9332 | DiagID = diag::err_typename_nested_not_type; |
| 9333 | Referenced = *Result.begin(); |
| 9334 | break; |
| 9335 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 9336 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9337 | return QualType(); |
| 9338 | } |
| 9339 | |
| 9340 | // If we get here, it's because name lookup did not find a |
| 9341 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9342 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 9343 | IILoc); |
| 9344 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 9345 | if (Referenced) |
| 9346 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 9347 | << Name; |
| 9348 | return QualType(); |
| 9349 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9350 | |
| 9351 | namespace { |
| 9352 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 9353 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9354 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9355 | SourceLocation Loc; |
| 9356 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9357 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9358 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 9359 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9360 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9361 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9362 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9363 | DeclarationName Entity) |
| 9364 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9365 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9366 | |
| 9367 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9368 | /// transformed. |
| 9369 | /// |
| 9370 | /// For the purposes of type reconstruction, a type has already been |
| 9371 | /// transformed if it is NULL or if it is not dependent. |
| 9372 | bool AlreadyTransformed(QualType T) { |
| 9373 | return T.isNull() || !T->isDependentType(); |
| 9374 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9375 | |
| 9376 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9377 | /// rebuilt. |
| 9378 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9379 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9380 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 9381 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9382 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 9383 | /// \brief Sets the "base" location and entity when that |
| 9384 | /// information is known based on another transformation. |
| 9385 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 9386 | this->Loc = Loc; |
| 9387 | this->Entity = Entity; |
| 9388 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9389 | |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 9390 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 9391 | // Lambdas never need to be transformed. |
| 9392 | return E; |
| 9393 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9394 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 9395 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9396 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9397 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 9398 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9399 | /// 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] | 9400 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9401 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9402 | /// partial specialization thereof). This routine will rebuild that type now |
| 9403 | /// that we have entered the declarator's scope, which may produce different |
| 9404 | /// canonical types, e.g., |
| 9405 | /// |
| 9406 | /// \code |
| 9407 | /// template<typename T> |
| 9408 | /// struct X { |
| 9409 | /// typedef T* pointer; |
| 9410 | /// pointer data(); |
| 9411 | /// }; |
| 9412 | /// |
| 9413 | /// template<typename T> |
| 9414 | /// typename X<T>::pointer X<T>::data() { ... } |
| 9415 | /// \endcode |
| 9416 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 9417 | /// 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] | 9418 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 9419 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 9420 | /// 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] | 9421 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 9422 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9423 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 9424 | SourceLocation Loc, |
| 9425 | DeclarationName Name) { |
| 9426 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9427 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9428 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 9429 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 9430 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 9431 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9432 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9433 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9434 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 9435 | DeclarationName()); |
| 9436 | return Rebuilder.TransformExpr(E); |
| 9437 | } |
| 9438 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9439 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9440 | if (SS.isInvalid()) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9441 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9442 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9443 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9444 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 9445 | DeclarationName()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9446 | NestedNameSpecifierLoc Rebuilt |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9447 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9448 | if (!Rebuilt) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9449 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9450 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9451 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9452 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9453 | } |
| 9454 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9455 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 9456 | /// instantiation. |
| 9457 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 9458 | TemplateParameterList *Params) { |
| 9459 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 9460 | Decl *Param = Params->getParam(I); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9461 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9462 | // There is nothing to rebuild in a type parameter. |
| 9463 | if (isa<TemplateTypeParmDecl>(Param)) |
| 9464 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9465 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9466 | // Rebuild the template parameter list of a template template parameter. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9467 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9468 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 9469 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 9470 | TTP->getTemplateParameters())) |
| 9471 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9472 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9473 | continue; |
| 9474 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9475 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9476 | // Rebuild the type of a non-type template parameter. |
| 9477 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9478 | TypeSourceInfo *NewTSI |
| 9479 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 9480 | NTTP->getLocation(), |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9481 | NTTP->getDeclName()); |
| 9482 | if (!NewTSI) |
| 9483 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9484 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9485 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 9486 | NTTP->setTypeSourceInfo(NewTSI); |
| 9487 | NTTP->setType(NewTSI->getType()); |
| 9488 | } |
| 9489 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9490 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9491 | return false; |
| 9492 | } |
| 9493 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9494 | /// \brief Produces a formatted string that describes the binding of |
| 9495 | /// template parameters to template arguments. |
| 9496 | std::string |
| 9497 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9498 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9499 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9500 | } |
| 9501 | |
| 9502 | std::string |
| 9503 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9504 | const TemplateArgument *Args, |
| 9505 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9506 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9507 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9508 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9509 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9510 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9511 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9512 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9513 | if (I >= NumArgs) |
| 9514 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9515 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9516 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9517 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9518 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9519 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9520 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9521 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9522 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9523 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9524 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9525 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9526 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9527 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 9528 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9529 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9530 | |
| 9531 | Out << ']'; |
| 9532 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9533 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9534 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9535 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 9536 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9537 | if (!FD) |
| 9538 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9539 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9540 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9541 | |
| 9542 | // Take tokens to avoid allocations |
| 9543 | LPT->Toks.swap(Toks); |
| 9544 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9545 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9546 | |
| 9547 | FD->setLateTemplateParsed(true); |
| 9548 | } |
| 9549 | |
| 9550 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 9551 | if (!FD) |
| 9552 | return; |
| 9553 | FD->setLateTemplateParsed(false); |
| 9554 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9555 | |
| 9556 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 9557 | DeclContext *DC = CurContext; |
| 9558 | |
| 9559 | while (DC) { |
| 9560 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 9561 | const FunctionDecl *FD = RD->isLocalClass(); |
| 9562 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 9563 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 9564 | return false; |
| 9565 | |
| 9566 | DC = DC->getParent(); |
| 9567 | } |
| 9568 | return false; |
| 9569 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9570 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 9571 | namespace { |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9572 | /// \brief Walk the path from which a declaration was instantiated, and check |
| 9573 | /// that every explicit specialization along that path is visible. This enforces |
| 9574 | /// C++ [temp.expl.spec]/6: |
| 9575 | /// |
| 9576 | /// If a template, a member template or a member of a class template is |
| 9577 | /// explicitly specialized then that specialization shall be declared before |
| 9578 | /// the first use of that specialization that would cause an implicit |
| 9579 | /// instantiation to take place, in every translation unit in which such a |
| 9580 | /// use occurs; no diagnostic is required. |
| 9581 | /// |
| 9582 | /// and also C++ [temp.class.spec]/1: |
| 9583 | /// |
| 9584 | /// A partial specialization shall be declared before the first use of a |
| 9585 | /// class template specialization that would make use of the partial |
| 9586 | /// specialization as the result of an implicit or explicit instantiation |
| 9587 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 9588 | /// required. |
| 9589 | class ExplicitSpecializationVisibilityChecker { |
| 9590 | Sema &S; |
| 9591 | SourceLocation Loc; |
| 9592 | llvm::SmallVector<Module *, 8> Modules; |
| 9593 | |
| 9594 | public: |
| 9595 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 9596 | : S(S), Loc(Loc) {} |
| 9597 | |
| 9598 | void check(NamedDecl *ND) { |
| 9599 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 9600 | return checkImpl(FD); |
| 9601 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 9602 | return checkImpl(RD); |
| 9603 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 9604 | return checkImpl(VD); |
| 9605 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 9606 | return checkImpl(ED); |
| 9607 | } |
| 9608 | |
| 9609 | private: |
| 9610 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 9611 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 9612 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 9613 | const bool Recover = true; |
| 9614 | |
| 9615 | // If we got a custom set of modules (because only a subset of the |
| 9616 | // declarations are interesting), use them, otherwise let |
| 9617 | // diagnoseMissingImport intelligently pick some. |
| 9618 | if (Modules.empty()) |
| 9619 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 9620 | else |
| 9621 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 9622 | } |
| 9623 | |
| 9624 | // Check a specific declaration. There are three problematic cases: |
| 9625 | // |
| 9626 | // 1) The declaration is an explicit specialization of a template |
| 9627 | // specialization. |
| 9628 | // 2) The declaration is an explicit specialization of a member of an |
| 9629 | // templated class. |
| 9630 | // 3) The declaration is an instantiation of a template, and that template |
| 9631 | // is an explicit specialization of a member of a templated class. |
| 9632 | // |
| 9633 | // We don't need to go any deeper than that, as the instantiation of the |
| 9634 | // surrounding class / etc is not triggered by whatever triggered this |
| 9635 | // instantiation, and thus should be checked elsewhere. |
| 9636 | template<typename SpecDecl> |
| 9637 | void checkImpl(SpecDecl *Spec) { |
| 9638 | bool IsHiddenExplicitSpecialization = false; |
| 9639 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 9640 | IsHiddenExplicitSpecialization = |
| 9641 | Spec->getMemberSpecializationInfo() |
| 9642 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
| 9643 | : !S.hasVisibleDeclaration(Spec); |
| 9644 | } else { |
| 9645 | checkInstantiated(Spec); |
| 9646 | } |
| 9647 | |
| 9648 | if (IsHiddenExplicitSpecialization) |
| 9649 | diagnose(Spec->getMostRecentDecl(), false); |
| 9650 | } |
| 9651 | |
| 9652 | void checkInstantiated(FunctionDecl *FD) { |
| 9653 | if (auto *TD = FD->getPrimaryTemplate()) |
| 9654 | checkTemplate(TD); |
| 9655 | } |
| 9656 | |
| 9657 | void checkInstantiated(CXXRecordDecl *RD) { |
| 9658 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 9659 | if (!SD) |
| 9660 | return; |
| 9661 | |
| 9662 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 9663 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 9664 | checkTemplate(TD); |
| 9665 | else if (auto *TD = |
| 9666 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 9667 | if (!S.hasVisibleDeclaration(TD)) |
| 9668 | diagnose(TD, true); |
| 9669 | checkTemplate(TD); |
| 9670 | } |
| 9671 | } |
| 9672 | |
| 9673 | void checkInstantiated(VarDecl *RD) { |
| 9674 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 9675 | if (!SD) |
| 9676 | return; |
| 9677 | |
| 9678 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 9679 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 9680 | checkTemplate(TD); |
| 9681 | else if (auto *TD = |
| 9682 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 9683 | if (!S.hasVisibleDeclaration(TD)) |
| 9684 | diagnose(TD, true); |
| 9685 | checkTemplate(TD); |
| 9686 | } |
| 9687 | } |
| 9688 | |
| 9689 | void checkInstantiated(EnumDecl *FD) {} |
| 9690 | |
| 9691 | template<typename TemplDecl> |
| 9692 | void checkTemplate(TemplDecl *TD) { |
| 9693 | if (TD->isMemberSpecialization()) { |
| 9694 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 9695 | diagnose(TD->getMostRecentDecl(), false); |
| 9696 | } |
| 9697 | } |
| 9698 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 9699 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9700 | |
| 9701 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 9702 | if (!getLangOpts().Modules) |
| 9703 | return; |
| 9704 | |
| 9705 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 9706 | } |
| 9707 | |
| 9708 | /// \brief Check whether a template partial specialization that we've discovered |
| 9709 | /// is hidden, and produce suitable diagnostics if so. |
| 9710 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 9711 | NamedDecl *Spec) { |
| 9712 | llvm::SmallVector<Module *, 8> Modules; |
| 9713 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 9714 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 9715 | MissingImportKind::PartialSpecialization, |
| 9716 | /*Recover*/true); |
| 9717 | } |