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 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 245 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 246 | SourceLocation IILoc, |
| 247 | Scope *S, |
| 248 | const CXXScopeSpec *SS, |
| 249 | TemplateTy &SuggestedTemplate, |
| 250 | TemplateNameKind &SuggestedKind) { |
| 251 | // We can't recover unless there's a dependent scope specifier preceding the |
| 252 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 253 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 254 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 255 | computeDeclContext(*SS)) |
| 256 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 257 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 258 | // The code is missing a 'template' keyword prior to the dependent template |
| 259 | // name. |
| 260 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 261 | Diag(IILoc, diag::err_template_kw_missing) |
| 262 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 263 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 264 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 265 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 266 | SuggestedKind = TNK_Dependent_template_name; |
| 267 | return true; |
| 268 | } |
| 269 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 270 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 271 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 272 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 273 | bool EnteringContext, |
| 274 | bool &MemberOfUnknownSpecialization) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 275 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 276 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 277 | DeclContext *LookupCtx = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 278 | bool isDependent = false; |
| 279 | if (!ObjectType.isNull()) { |
| 280 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 281 | // x->B::f, and we are looking into the type of the object. |
| 282 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 283 | LookupCtx = computeDeclContext(ObjectType); |
| 284 | isDependent = ObjectType->isDependentType(); |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 285 | assert((isDependent || !ObjectType->isIncompleteType() || |
| 286 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 287 | "Caller should have completed object type"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 289 | // Template names cannot appear inside an Objective-C class or object type. |
| 290 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 291 | Found.clear(); |
| 292 | return; |
| 293 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 294 | } else if (SS.isSet()) { |
| 295 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 296 | // so long into the context associated with the prior nested-name-specifier. |
| 297 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 298 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 299 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 300 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 301 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | |
| 305 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 306 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 307 | if (LookupCtx) { |
| 308 | // Perform "qualified" name lookup into the declaration context we |
| 309 | // computed, which is either the type of the base of a member access |
| 310 | // expression or the declaration context associated with a prior |
| 311 | // nested-name-specifier. |
| 312 | LookupQualifiedName(Found, LookupCtx); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 313 | if (!ObjectType.isNull() && Found.empty()) { |
| 314 | // C++ [basic.lookup.classref]p1: |
| 315 | // In a class member access expression (5.2.5), if the . or -> token is |
| 316 | // immediately followed by an identifier followed by a <, the |
| 317 | // identifier must be looked up to determine whether the < is the |
| 318 | // beginning of a template argument list (14.2) or a less-than operator. |
| 319 | // The identifier is first looked up in the class of the object |
| 320 | // expression. If the identifier is not found, it is then looked up in |
| 321 | // the context of the entire postfix-expression and shall name a class |
| 322 | // or function template. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 323 | if (S) LookupName(Found, S); |
| 324 | ObjectTypeSearchedInScope = true; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 325 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 326 | } |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 327 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 328 | // We cannot look into a dependent object type or nested nme |
| 329 | // specifier. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 330 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 331 | return; |
| 332 | } else { |
| 333 | // Perform unqualified name lookup in the current scope. |
| 334 | LookupName(Found, S); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 335 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 336 | if (!ObjectType.isNull()) |
| 337 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 340 | if (Found.empty() && !isDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 341 | // If we did not find any names, attempt to correct any typos. |
| 342 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 343 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 344 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 345 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 346 | FilterCCC->WantTypeSpecifiers = false; |
| 347 | FilterCCC->WantExpressionKeywords = false; |
| 348 | FilterCCC->WantRemainingKeywords = false; |
| 349 | FilterCCC->WantCXXNamedCasts = true; |
| 350 | if (TypoCorrection Corrected = CorrectTypo( |
| 351 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 352 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 353 | Found.setLookupName(Corrected.getCorrection()); |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 354 | if (auto *ND = Corrected.getFoundDecl()) |
| 355 | Found.addDecl(ND); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 356 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 357 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 358 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 359 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 360 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 361 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 362 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 363 | << Name << LookupCtx << DroppedSpecifier |
| 364 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 365 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 366 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 367 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 368 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 369 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 370 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 374 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 375 | if (Found.empty()) { |
| 376 | if (isDependent) |
| 377 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 378 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 379 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 380 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 381 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 382 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 383 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 384 | // [...] If the lookup in the class of the object expression finds a |
| 385 | // template, the name is also looked up in the context of the entire |
| 386 | // postfix-expression and [...] |
| 387 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 388 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 389 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 390 | LookupOrdinaryName); |
| 391 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 392 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 393 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 394 | if (FoundOuter.empty()) { |
| 395 | // - if the name is not found, the name found in the class of the |
| 396 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 397 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 398 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 399 | // - if the name is found in the context of the entire |
| 400 | // postfix-expression and does not name a class template, the name |
| 401 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 402 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 403 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 404 | // - if the name found is a class template, it must refer to the same |
| 405 | // entity as the one found in the class of the object expression, |
| 406 | // otherwise the program is ill-formed. |
| 407 | if (!Found.isSingleResult() || |
| 408 | Found.getFoundDecl()->getCanonicalDecl() |
| 409 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 410 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 411 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 412 | << Found.getLookupName() |
| 413 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 414 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 415 | diag::note_ambig_member_ref_object_type) |
| 416 | << ObjectType; |
| 417 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 418 | diag::note_ambig_member_ref_scope); |
| 419 | |
| 420 | // Recover by taking the template that we found in the object |
| 421 | // expression's type. |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 427 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 428 | /// was just parsed. This is only possible with an explicit scope |
| 429 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 430 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 431 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 432 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 433 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 434 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 435 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 436 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 437 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 438 | // C++11 [expr.prim.general]p12: |
| 439 | // An id-expression that denotes a non-static data member or non-static |
| 440 | // member function of a class can only be used: |
| 441 | // (...) |
| 442 | // - if that id-expression denotes a non-static data member and it |
| 443 | // appears in an unevaluated operand. |
| 444 | // |
| 445 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 446 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 447 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 448 | // always a MemberExpr. |
| 449 | bool MightBeCxx11UnevalField = |
| 450 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 451 | |
Akira Hatanaka | d644e02 | 2016-12-16 03:19:41 +0000 | [diff] [blame] | 452 | // Check if the nested name specifier is an enum type. |
| 453 | bool IsEnum = false; |
| 454 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
| 455 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
| 456 | |
| 457 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 458 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 459 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 460 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 461 | // Since the 'this' expression is synthesized, we don't need to |
| 462 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 463 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 464 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 465 | return CXXDependentScopeMemberExpr::Create( |
| 466 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 467 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 468 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 471 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 472 | } |
| 473 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 474 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 475 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 476 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 477 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 478 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 479 | return DependentScopeDeclRefExpr::Create( |
| 480 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 481 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 484 | |
| 485 | /// Determine whether we would be unable to instantiate this template (because |
| 486 | /// it either has no definition, or is in the process of being instantiated). |
| 487 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 488 | NamedDecl *Instantiation, |
| 489 | bool InstantiatedFromMember, |
| 490 | const NamedDecl *Pattern, |
| 491 | const NamedDecl *PatternDef, |
| 492 | TemplateSpecializationKind TSK, |
| 493 | bool Complain /*= true*/) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 494 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 495 | isa<VarDecl>(Instantiation)); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 496 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 497 | bool IsEntityBeingDefined = false; |
| 498 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 499 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 500 | |
| 501 | if (PatternDef && !IsEntityBeingDefined) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 502 | NamedDecl *SuggestedDef = nullptr; |
| 503 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 504 | /*OnlyNeedComplete*/false)) { |
| 505 | // If we're allowed to diagnose this and recover, do so. |
| 506 | bool Recover = Complain && !isSFINAEContext(); |
| 507 | if (Complain) |
| 508 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 509 | Sema::MissingImportKind::Definition, Recover); |
| 510 | return !Recover; |
| 511 | } |
| 512 | return false; |
| 513 | } |
| 514 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 515 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 516 | return true; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 517 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 518 | llvm::Optional<unsigned> Note; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 519 | QualType InstantiationTy; |
| 520 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 521 | InstantiationTy = Context.getTypeDeclType(TD); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 522 | if (PatternDef) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 523 | Diag(PointOfInstantiation, |
| 524 | diag::err_template_instantiate_within_definition) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 525 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 526 | << InstantiationTy; |
| 527 | // Not much point in noting the template declaration here, since |
| 528 | // we're lexically inside it. |
| 529 | Instantiation->setInvalidDecl(); |
| 530 | } else if (InstantiatedFromMember) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 531 | if (isa<FunctionDecl>(Instantiation)) { |
| 532 | Diag(PointOfInstantiation, |
| 533 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 534 | << /*member function*/ 1 << Instantiation->getDeclName() |
| 535 | << Instantiation->getDeclContext(); |
| 536 | Note = diag::note_explicit_instantiation_here; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 537 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 538 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 539 | Diag(PointOfInstantiation, |
| 540 | diag::err_implicit_instantiate_member_undefined) |
| 541 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 542 | Note = diag::note_member_declared_at; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 543 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 544 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 545 | if (isa<FunctionDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 546 | Diag(PointOfInstantiation, |
| 547 | diag::err_explicit_instantiation_undefined_func_template) |
| 548 | << Pattern; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 549 | Note = diag::note_explicit_instantiation_here; |
| 550 | } else if (isa<TagDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 551 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 552 | << (TSK != TSK_ImplicitInstantiation) |
| 553 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 554 | Note = diag::note_template_decl_here; |
| 555 | } else { |
| 556 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 557 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 558 | Diag(PointOfInstantiation, |
| 559 | diag::err_explicit_instantiation_undefined_var_template) |
| 560 | << Instantiation; |
| 561 | Instantiation->setInvalidDecl(); |
| 562 | } else |
| 563 | Diag(PointOfInstantiation, |
| 564 | diag::err_explicit_instantiation_undefined_member) |
| 565 | << /*static data member*/ 2 << Instantiation->getDeclName() |
| 566 | << Instantiation->getDeclContext(); |
| 567 | Note = diag::note_explicit_instantiation_here; |
| 568 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 569 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 570 | if (Note) // Diagnostics were emitted. |
| 571 | Diag(Pattern->getLocation(), Note.getValue()); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 572 | |
| 573 | // In general, Instantiation isn't marked invalid to get more than one |
| 574 | // error for multiple undefined instantiations. But the code that does |
| 575 | // explicit declaration -> explicit definition conversion can't handle |
| 576 | // invalid declarations, so mark as invalid in that case. |
| 577 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 578 | Instantiation->setInvalidDecl(); |
| 579 | return true; |
| 580 | } |
| 581 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 582 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 583 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 584 | /// declaration at location Loc. Returns true to indicate that this is |
| 585 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 586 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 587 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 588 | |
| 589 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 590 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 591 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 592 | |
| 593 | // C++ [temp.local]p4: |
| 594 | // A template-parameter shall not be redeclared within its |
| 595 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 597 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 598 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 601 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 602 | /// the parameter D to reference the templated declaration and return a pointer |
| 603 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 604 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 605 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 606 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 607 | return Temp; |
| 608 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 609 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 612 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 613 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 614 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 615 | "Only template template arguments can be pack expansions here"); |
| 616 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 617 | "Template template argument pack expansion without packs"); |
| 618 | ParsedTemplateArgument Result(*this); |
| 619 | Result.EllipsisLoc = EllipsisLoc; |
| 620 | return Result; |
| 621 | } |
| 622 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 623 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 624 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 625 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 626 | switch (Arg.getKind()) { |
| 627 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 628 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 629 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 630 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 631 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 632 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 633 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 634 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 635 | case ParsedTemplateArgument::NonType: { |
| 636 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 637 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 638 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 639 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 640 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 641 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 642 | TemplateArgument TArg; |
| 643 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 644 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 645 | else |
| 646 | TArg = Template; |
| 647 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 648 | Arg.getScopeSpec().getWithLocInContext( |
| 649 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 650 | Arg.getLocation(), |
| 651 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 654 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 655 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 656 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 658 | /// \brief Translates template arguments as provided by the parser |
| 659 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 660 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 661 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 662 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 663 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 664 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 665 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 666 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 667 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 668 | SourceLocation Loc, |
| 669 | IdentifierInfo *Name) { |
| 670 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 671 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 672 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 673 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 674 | } |
| 675 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 676 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 677 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 678 | /// the keyword "typename" was used to declare the type parameter |
| 679 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 680 | /// "class" or "typename" keyword. ParamName is the name of the |
| 681 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 682 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 683 | /// If the type parameter has a default argument, it will be added |
| 684 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 685 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 686 | SourceLocation EllipsisLoc, |
| 687 | SourceLocation KeyLoc, |
| 688 | IdentifierInfo *ParamName, |
| 689 | SourceLocation ParamNameLoc, |
| 690 | unsigned Depth, unsigned Position, |
| 691 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 692 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | assert(S->isTemplateParamScope() && |
| 694 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 696 | SourceLocation Loc = ParamNameLoc; |
| 697 | if (!ParamName) |
| 698 | Loc = KeyLoc; |
| 699 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 700 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 701 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 702 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 703 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 704 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 705 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 706 | |
| 707 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 708 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 709 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 710 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 711 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 712 | IdResolver.AddDecl(Param); |
| 713 | } |
| 714 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 715 | // C++0x [temp.param]p9: |
| 716 | // A default template-argument may be specified for any kind of |
| 717 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 718 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 719 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 720 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 723 | // Handle the default argument, if provided. |
| 724 | if (DefaultArg) { |
| 725 | TypeSourceInfo *DefaultTInfo; |
| 726 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 727 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 728 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 729 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 730 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 731 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 732 | UPPC_DefaultArgument)) |
| 733 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 734 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 735 | // Check the template argument itself. |
| 736 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 737 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 738 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 739 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 740 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 741 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 742 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 743 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 744 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 747 | /// \brief Check that the type of a non-type template parameter is |
| 748 | /// well-formed. |
| 749 | /// |
| 750 | /// \returns the (possibly-promoted) parameter type if valid; |
| 751 | /// otherwise, produces a diagnostic and returns a NULL type. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 752 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
| 753 | SourceLocation Loc) { |
| 754 | if (TSI->getType()->isUndeducedType()) { |
| 755 | // C++1z [temp.dep.expr]p3: |
| 756 | // An id-expression is type-dependent if it contains |
| 757 | // - an identifier associated by name lookup with a non-type |
| 758 | // template-parameter declared with a type that contains a |
| 759 | // placeholder type (7.1.7.4), |
| 760 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
| 761 | } |
| 762 | |
| 763 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
| 764 | } |
| 765 | |
| 766 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
| 767 | SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 768 | // We don't allow variably-modified types as the type of non-type template |
| 769 | // parameters. |
| 770 | if (T->isVariablyModifiedType()) { |
| 771 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 772 | << T; |
| 773 | return QualType(); |
| 774 | } |
| 775 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 776 | // C++ [temp.param]p4: |
| 777 | // |
| 778 | // A non-type template-parameter shall have one of the following |
| 779 | // (optionally cv-qualified) types: |
| 780 | // |
| 781 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 782 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 784 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 786 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 787 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 788 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 789 | // -- std::nullptr_t. |
| 790 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 791 | // If T is a dependent type, we can't do the check now, so we |
| 792 | // assume that it is well-formed. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 793 | T->isDependentType() || |
| 794 | // Allow use of auto in template parameter declarations. |
| 795 | T->isUndeducedType()) { |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 796 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 797 | // are ignored when determining its type. |
| 798 | return T.getUnqualifiedType(); |
| 799 | } |
| 800 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 801 | // C++ [temp.param]p8: |
| 802 | // |
| 803 | // A non-type template-parameter of type "array of T" or |
| 804 | // "function returning T" is adjusted to be of type "pointer to |
| 805 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 806 | else if (T->isArrayType() || T->isFunctionType()) |
| 807 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 809 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 810 | << T; |
| 811 | |
| 812 | return QualType(); |
| 813 | } |
| 814 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 815 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 816 | unsigned Depth, |
| 817 | unsigned Position, |
| 818 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 819 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 820 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 821 | |
| 822 | if (TInfo->getType()->isUndeducedType()) { |
| 823 | Diag(D.getIdentifierLoc(), |
| 824 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 825 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
| 826 | } |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 828 | assert(S->isTemplateParamScope() && |
| 829 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 830 | bool Invalid = false; |
| 831 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 832 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 833 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 834 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 835 | Invalid = true; |
| 836 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 837 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 838 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 839 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 840 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 841 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 842 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 843 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 844 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 845 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 846 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 848 | if (Invalid) |
| 849 | Param->setInvalidDecl(); |
| 850 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 851 | if (ParamName) { |
| 852 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 853 | ParamName); |
| 854 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 855 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 856 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 857 | IdResolver.AddDecl(Param); |
| 858 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 860 | // C++0x [temp.param]p9: |
| 861 | // A default template-argument may be specified for any kind of |
| 862 | // template-parameter that is not a template parameter pack. |
| 863 | if (Default && IsParameterPack) { |
| 864 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 865 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 868 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 869 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 870 | // Check for unexpanded parameter packs. |
| 871 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 872 | return Param; |
| 873 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 874 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 875 | ExprResult DefaultRes = |
| 876 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 877 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 878 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 879 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 880 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 881 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 882 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 883 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 884 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 885 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 886 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 887 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 888 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 889 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 890 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 891 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 892 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 893 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 894 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 895 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 896 | IdentifierInfo *Name, |
| 897 | SourceLocation NameLoc, |
| 898 | unsigned Depth, |
| 899 | unsigned Position, |
| 900 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 901 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 902 | assert(S->isTemplateParamScope() && |
| 903 | "Template template parameter not in template parameter scope!"); |
| 904 | |
| 905 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 906 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 907 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 908 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 909 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 910 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 911 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 912 | Param->setAccess(AS_public); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 913 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 914 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 915 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 916 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 917 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 918 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 919 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 920 | IdResolver.AddDecl(Param); |
| 921 | } |
| 922 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 923 | if (Params->size() == 0) { |
| 924 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 925 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 926 | Param->setInvalidDecl(); |
| 927 | } |
| 928 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 929 | // C++0x [temp.param]p9: |
| 930 | // A default template-argument may be specified for any kind of |
| 931 | // template-parameter that is not a template parameter pack. |
| 932 | if (IsParameterPack && !Default.isInvalid()) { |
| 933 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 934 | Default = ParsedTemplateArgument(); |
| 935 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 937 | if (!Default.isInvalid()) { |
| 938 | // Check only that we have a template template argument. We don't want to |
| 939 | // try to check well-formedness now, because our template template parameter |
| 940 | // might have dependent types in its template parameters, which we wouldn't |
| 941 | // be able to match now. |
| 942 | // |
| 943 | // If none of the template template parameter's template arguments mention |
| 944 | // other template parameters, we could actually perform more checking here. |
| 945 | // However, it isn't worth doing. |
| 946 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 947 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 948 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 949 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 950 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 951 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 952 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 953 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 955 | DefaultArg.getArgument().getAsTemplate(), |
| 956 | UPPC_DefaultArgument)) |
| 957 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 958 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 959 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 960 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 961 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 962 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 965 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 966 | /// constrained by RequiresClause, that contains the template parameters in |
| 967 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 968 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 969 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 970 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 972 | SourceLocation LAngleLoc, |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 973 | ArrayRef<Decl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 974 | SourceLocation RAngleLoc, |
| 975 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 976 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 977 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 978 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 979 | return TemplateParameterList::Create( |
| 980 | Context, TemplateLoc, LAngleLoc, |
| 981 | llvm::makeArrayRef((NamedDecl *const *)Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 982 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 983 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 984 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 985 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 986 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 987 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 988 | } |
| 989 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 990 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 991 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 992 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 993 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 994 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 995 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 996 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 997 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 998 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 999 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1000 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1002 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1003 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1004 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1007 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1008 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1009 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1010 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 1011 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1012 | |
| 1013 | // There is no such thing as an unnamed class template. |
| 1014 | if (!Name) { |
| 1015 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1016 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1019 | // Find any previous declaration with this name. For a friend with no |
| 1020 | // scope explicitly specified, we only look for tag declarations (per |
| 1021 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1022 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1023 | LookupResult Previous(*this, Name, NameLoc, |
| 1024 | (SS.isEmpty() && TUK == TUK_Friend) |
| 1025 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1026 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1027 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 1028 | SemanticContext = computeDeclContext(SS, true); |
| 1029 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1030 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 1031 | // in the AST, and historically we have just ignored such friend |
| 1032 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1033 | Diag(NameLoc, TUK == TUK_Friend |
| 1034 | ? diag::warn_template_qualified_friend_ignored |
| 1035 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1036 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1037 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1038 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1040 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 1041 | return true; |
| 1042 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1043 | // If we're adding a template to a dependent context, we may need to |
| 1044 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 1045 | // now that we know what the current instantiation is. |
| 1046 | if (SemanticContext->isDependentContext()) { |
| 1047 | ContextRAII SavedContext(*this, SemanticContext); |
| 1048 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 1049 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1050 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 1051 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1052 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1053 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1054 | } else { |
| 1055 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 1056 | |
| 1057 | // C++14 [class.mem]p14: |
| 1058 | // If T is the name of a class, then each of the following shall have a |
| 1059 | // name different from T: |
| 1060 | // -- every member template of class T |
| 1061 | if (TUK != TUK_Friend && |
| 1062 | DiagnoseClassNameShadow(SemanticContext, |
| 1063 | DeclarationNameInfo(Name, NameLoc))) |
| 1064 | return true; |
| 1065 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1066 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1067 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1069 | if (Previous.isAmbiguous()) |
| 1070 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1071 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1072 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1073 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1074 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1075 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1076 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1077 | // Maybe we will complain about the shadowed template parameter. |
| 1078 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1079 | // Just pretend that we didn't see the previous declaration. |
| 1080 | PrevDecl = nullptr; |
| 1081 | } |
| 1082 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1083 | // If there is a previous declaration with the same name, check |
| 1084 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1086 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1087 | |
| 1088 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1089 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1090 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1091 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1092 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1093 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1094 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1095 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1096 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1097 | PrevClassTemplate |
| 1098 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1099 | ->getSpecializedTemplate(); |
| 1100 | } |
| 1101 | } |
| 1102 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1103 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1104 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1105 | // [...] When looking for a prior declaration of a class or a function |
| 1106 | // 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] | 1107 | // function is neither a qualified name nor a template-id, scopes outside |
| 1108 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1109 | if (!SS.isSet()) { |
| 1110 | DeclContext *OutermostContext = CurContext; |
| 1111 | while (!OutermostContext->isFileContext()) |
| 1112 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1113 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 1114 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1115 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1116 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1117 | SemanticContext = PrevDecl->getDeclContext(); |
| 1118 | } else { |
| 1119 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1120 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1121 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1122 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1123 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1124 | |
| 1125 | // Check that the chosen semantic context doesn't already contain a |
| 1126 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1127 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1128 | DeclContext *LookupContext = SemanticContext; |
| 1129 | while (LookupContext->isTransparentContext()) |
| 1130 | LookupContext = LookupContext->getLookupParent(); |
| 1131 | LookupQualifiedName(Previous, LookupContext); |
| 1132 | |
| 1133 | if (Previous.isAmbiguous()) |
| 1134 | return true; |
| 1135 | |
| 1136 | if (Previous.begin() != Previous.end()) |
| 1137 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1138 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1139 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1140 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1141 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1142 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1143 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1144 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1145 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1146 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1147 | if (SS.isEmpty() && |
| 1148 | !(PrevClassTemplate && |
| 1149 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1150 | SemanticContext->getRedeclContext()))) { |
| 1151 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1152 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1153 | diag::note_using_decl_target); |
| 1154 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1155 | // Recover by ignoring the old declaration. |
| 1156 | PrevDecl = PrevClassTemplate = nullptr; |
| 1157 | } |
| 1158 | } |
| 1159 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame^] | 1160 | // TODO Memory management; associated constraints are not always stored. |
| 1161 | Expr *const CurAC = formAssociatedConstraints(TemplateParams, nullptr); |
| 1162 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1163 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1164 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1165 | // for a friend in a dependent context: the template parameter list itself |
| 1166 | // could be dependent. |
| 1167 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1168 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1169 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1170 | /*Complain=*/true, |
| 1171 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1172 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1173 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame^] | 1174 | // Check for matching associated constraints on redeclarations. |
| 1175 | const Expr *const PrevAC = PrevClassTemplate->getAssociatedConstraints(); |
| 1176 | const bool RedeclACMismatch = [&] { |
| 1177 | if (!(CurAC || PrevAC)) |
| 1178 | return false; // Nothing to check; no mismatch. |
| 1179 | if (CurAC && PrevAC) { |
| 1180 | llvm::FoldingSetNodeID CurACInfo, PrevACInfo; |
| 1181 | CurAC->Profile(CurACInfo, Context, /*Canonical=*/true); |
| 1182 | PrevAC->Profile(PrevACInfo, Context, /*Canonical=*/true); |
| 1183 | if (CurACInfo == PrevACInfo) |
| 1184 | return false; // All good; no mismatch. |
| 1185 | } |
| 1186 | return true; |
| 1187 | }(); |
| 1188 | |
| 1189 | if (RedeclACMismatch) { |
| 1190 | Diag(CurAC ? CurAC->getLocStart() : NameLoc, |
| 1191 | diag::err_template_different_associated_constraints); |
| 1192 | Diag(PrevAC ? PrevAC->getLocStart() : PrevClassTemplate->getLocation(), |
| 1193 | diag::note_template_prev_declaration) << /*declaration*/0; |
| 1194 | return true; |
| 1195 | } |
| 1196 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1197 | // C++ [temp.class]p4: |
| 1198 | // In a redeclaration, partial specialization, explicit |
| 1199 | // specialization or explicit instantiation of a class template, |
| 1200 | // the class-key shall agree in kind with the original class |
| 1201 | // template declaration (7.1.5.3). |
| 1202 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1203 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1204 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1206 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1207 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1208 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1209 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1212 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1213 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1214 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1215 | // If we have a prior definition that is not visible, treat this as |
| 1216 | // simply making that previous definition visible. |
| 1217 | NamedDecl *Hidden = nullptr; |
| 1218 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1219 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1220 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1221 | assert(Tmpl && "original definition of a class template is not a " |
| 1222 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1223 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1224 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1225 | return Def; |
| 1226 | } |
| 1227 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1228 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1229 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1230 | // FIXME: Would it make sense to try to "forget" the previous |
| 1231 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1232 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1233 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1234 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1235 | } else if (PrevDecl) { |
| 1236 | // C++ [temp]p5: |
| 1237 | // A class template shall not have the same name as any other |
| 1238 | // template, class, function, object, enumeration, enumerator, |
| 1239 | // namespace, or type in the same scope (3.3), except as specified |
| 1240 | // in (14.5.4). |
| 1241 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1242 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1243 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1246 | // Check the template parameter list of this declaration, possibly |
| 1247 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1248 | // template declaration. Skip this check for a friend in a dependent |
| 1249 | // context, because the template parameter list might be dependent. |
| 1250 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1251 | CheckTemplateParameterList( |
| 1252 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1253 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1254 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1255 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1256 | SemanticContext->isDependentContext()) |
| 1257 | ? TPC_ClassTemplateMember |
| 1258 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1259 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1260 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1262 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1263 | // 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] | 1264 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1265 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1266 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1267 | : diag::err_member_decl_does_not_match) |
| 1268 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1269 | Invalid = true; |
| 1270 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1273 | // If this is a templated friend in a dependent context we should not put it |
| 1274 | // on the redecl chain. In some cases, the templated friend can be the most |
| 1275 | // recent declaration tricking the template instantiator to make substitutions |
| 1276 | // there. |
| 1277 | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
| 1278 | bool ShouldAddRedecl |
| 1279 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
| 1280 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1282 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1283 | PrevClassTemplate && ShouldAddRedecl ? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1284 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1285 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1286 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1287 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1288 | NewClass->setTemplateParameterListsInfo( |
| 1289 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1290 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1291 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1292 | // Add alignment attributes if necessary; these attributes are checked when |
| 1293 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1294 | if (TUK == TUK_Definition) { |
| 1295 | AddAlignmentAttributesForRecord(NewClass); |
| 1296 | AddMsStructLayoutForRecord(NewClass); |
| 1297 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1298 | |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame^] | 1299 | // Attach the associated constraints when the declaration will not be part of |
| 1300 | // a decl chain. |
| 1301 | Expr *const ACtoAttach = |
| 1302 | PrevClassTemplate && ShouldAddRedecl ? nullptr : CurAC; |
| 1303 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1304 | ClassTemplateDecl *NewTemplate |
| 1305 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1306 | DeclarationName(Name), TemplateParams, |
Hubert Tong | 5a8ec4e | 2017-02-10 02:46:19 +0000 | [diff] [blame^] | 1307 | NewClass, ACtoAttach); |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1308 | |
| 1309 | if (ShouldAddRedecl) |
| 1310 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
| 1311 | |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1312 | NewClass->setDescribedClassTemplate(NewTemplate); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1314 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1315 | NewTemplate->setModulePrivate(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1316 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1317 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1318 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1319 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1320 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1321 | (void)T; |
| 1322 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1323 | // 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] | 1324 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1325 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1326 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1327 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1328 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1329 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1330 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1331 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1333 | // Set the lexical context of these templates |
| 1334 | NewClass->setLexicalDeclContext(CurContext); |
| 1335 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1336 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1337 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1338 | NewClass->startDefinition(); |
| 1339 | |
| 1340 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1341 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1342 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1343 | if (PrevClassTemplate) |
| 1344 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1345 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1346 | AddPushedVisibilityAttribute(NewClass); |
| 1347 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1348 | if (TUK != TUK_Friend) { |
| 1349 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1350 | Scope *Outer = S; |
| 1351 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1352 | Outer = Outer->getParent(); |
| 1353 | PushOnScopeChains(NewTemplate, Outer); |
| 1354 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1355 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1356 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1357 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1358 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1359 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1360 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1361 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1362 | // Friend templates are visible in fairly strange ways. |
| 1363 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1364 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1365 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1366 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1367 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1368 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1369 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1370 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1371 | FriendDecl *Friend = FriendDecl::Create( |
| 1372 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1373 | Friend->setAccess(AS_public); |
| 1374 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1375 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1377 | if (Invalid) { |
| 1378 | NewTemplate->setInvalidDecl(); |
| 1379 | NewClass->setInvalidDecl(); |
| 1380 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1381 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1382 | ActOnDocumentableDecl(NewTemplate); |
| 1383 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1384 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1387 | /// \brief Diagnose the presence of a default template argument on a |
| 1388 | /// template parameter, which is ill-formed in certain contexts. |
| 1389 | /// |
| 1390 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1391 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1392 | Sema::TemplateParamListContext TPC, |
| 1393 | SourceLocation ParamLoc, |
| 1394 | SourceRange DefArgRange) { |
| 1395 | switch (TPC) { |
| 1396 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1397 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1398 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1399 | return false; |
| 1400 | |
| 1401 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1402 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1403 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1404 | // A default template-argument shall not be specified in a |
| 1405 | // function template declaration or a function template |
| 1406 | // definition [...] |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1407 | // If a friend function template declaration specifies a default |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1408 | // template-argument, that declaration shall be a definition and shall be |
| 1409 | // the only declaration of the function template in the translation unit. |
| 1410 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1411 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1412 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1413 | : diag::ext_template_parameter_default_in_function_template) |
| 1414 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1415 | return false; |
| 1416 | |
| 1417 | case Sema::TPC_ClassTemplateMember: |
| 1418 | // C++0x [temp.param]p9: |
| 1419 | // A default template-argument shall not be specified in the |
| 1420 | // template-parameter-lists of the definition of a member of a |
| 1421 | // class template that appears outside of the member's class. |
| 1422 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1423 | << DefArgRange; |
| 1424 | return true; |
| 1425 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1426 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1427 | case Sema::TPC_FriendFunctionTemplate: |
| 1428 | // C++ [temp.param]p9: |
| 1429 | // A default template-argument shall not be specified in a |
| 1430 | // friend template declaration. |
| 1431 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1432 | << DefArgRange; |
| 1433 | return true; |
| 1434 | |
| 1435 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1436 | // for friend function templates if there is only a single |
| 1437 | // declaration (and it is a definition). Strange! |
| 1438 | } |
| 1439 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1440 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1443 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1444 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1445 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1446 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1447 | // A template template parameter which is a parameter pack is also a pack |
| 1448 | // expansion. |
| 1449 | if (TTP->isParameterPack()) |
| 1450 | return false; |
| 1451 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1452 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1453 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1454 | NamedDecl *P = Params->getParam(I); |
| 1455 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1456 | if (!NTTP->isParameterPack() && |
| 1457 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1458 | NTTP->getTypeSourceInfo(), |
| 1459 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1460 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1461 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1462 | continue; |
| 1463 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1464 | |
| 1465 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1466 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1467 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1468 | return true; |
| 1469 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1471 | return false; |
| 1472 | } |
| 1473 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1474 | /// \brief Checks the validity of a template parameter list, possibly |
| 1475 | /// considering the template parameter list from a previous |
| 1476 | /// declaration. |
| 1477 | /// |
| 1478 | /// If an "old" template parameter list is provided, it must be |
| 1479 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1480 | /// template parameter list. |
| 1481 | /// |
| 1482 | /// \param NewParams Template parameter list for a new template |
| 1483 | /// declaration. This template parameter list will be updated with any |
| 1484 | /// default arguments that are carried through from the previous |
| 1485 | /// template parameter list. |
| 1486 | /// |
| 1487 | /// \param OldParams If provided, template parameter list from a |
| 1488 | /// previous declaration of the same template. Default template |
| 1489 | /// arguments will be merged from the old template parameter list to |
| 1490 | /// the new template parameter list. |
| 1491 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1492 | /// \param TPC Describes the context in which we are checking the given |
| 1493 | /// template parameter list. |
| 1494 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1495 | /// \returns true if an error occurred, false otherwise. |
| 1496 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1497 | TemplateParameterList *OldParams, |
| 1498 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1499 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1501 | // C++ [temp.param]p10: |
| 1502 | // The set of default template-arguments available for use with a |
| 1503 | // template declaration or definition is obtained by merging the |
| 1504 | // default arguments from the definition (if in scope) and all |
| 1505 | // declarations in scope in the same way default function |
| 1506 | // arguments are (8.3.6). |
| 1507 | bool SawDefaultArgument = false; |
| 1508 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1509 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1510 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1511 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1512 | if (OldParams) |
| 1513 | OldParam = OldParams->begin(); |
| 1514 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1515 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1516 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1517 | NewParamEnd = NewParams->end(); |
| 1518 | NewParam != NewParamEnd; ++NewParam) { |
| 1519 | // Variables used to diagnose redundant default arguments |
| 1520 | bool RedundantDefaultArg = false; |
| 1521 | SourceLocation OldDefaultLoc; |
| 1522 | SourceLocation NewDefaultLoc; |
| 1523 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1524 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1525 | bool MissingDefaultArg = false; |
| 1526 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1527 | // Variable used to diagnose non-final parameter packs |
| 1528 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1529 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1530 | if (TemplateTypeParmDecl *NewTypeParm |
| 1531 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1532 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1533 | if (NewTypeParm->hasDefaultArgument() && |
| 1534 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1535 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1536 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1537 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1538 | NewTypeParm->removeDefaultArgument(); |
| 1539 | |
| 1540 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1542 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1543 | if (NewTypeParm->isParameterPack()) { |
| 1544 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1545 | "Parameter packs can't have a default argument!"); |
| 1546 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1547 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1548 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1549 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1550 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1551 | SawDefaultArgument = true; |
| 1552 | RedundantDefaultArg = true; |
| 1553 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1554 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1555 | // Merge the default argument from the old declaration to the |
| 1556 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1557 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1558 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1559 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1560 | SawDefaultArgument = true; |
| 1561 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1562 | } else if (SawDefaultArgument) |
| 1563 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1564 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1565 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1566 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1567 | if (!NewNonTypeParm->isParameterPack() && |
| 1568 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1569 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1570 | UPPC_NonTypeTemplateParameterType)) { |
| 1571 | Invalid = true; |
| 1572 | continue; |
| 1573 | } |
| 1574 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1575 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1576 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1577 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1578 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1579 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1580 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1583 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1584 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1585 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1586 | if (NewNonTypeParm->isParameterPack()) { |
| 1587 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1588 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1589 | if (!NewNonTypeParm->isPackExpansion()) |
| 1590 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1591 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1592 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1593 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1594 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1595 | SawDefaultArgument = true; |
| 1596 | RedundantDefaultArg = true; |
| 1597 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1598 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1599 | // Merge the default argument from the old declaration to the |
| 1600 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1601 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1602 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1603 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1604 | SawDefaultArgument = true; |
| 1605 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1606 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1608 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1609 | TemplateTemplateParmDecl *NewTemplateParm |
| 1610 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1611 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1612 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1613 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1614 | Invalid = true; |
| 1615 | continue; |
| 1616 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1617 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1618 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1619 | if (NewTemplateParm->hasDefaultArgument() && |
| 1620 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1621 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1622 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1623 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1624 | |
| 1625 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1626 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1627 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1628 | if (NewTemplateParm->isParameterPack()) { |
| 1629 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1630 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1631 | if (!NewTemplateParm->isPackExpansion()) |
| 1632 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1633 | } else if (OldTemplateParm && |
| 1634 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 1635 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1636 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1637 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1638 | SawDefaultArgument = true; |
| 1639 | RedundantDefaultArg = true; |
| 1640 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1641 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1642 | // Merge the default argument from the old declaration to the |
| 1643 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1644 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1645 | PreviousDefaultArgLoc |
| 1646 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1647 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1648 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1649 | PreviousDefaultArgLoc |
| 1650 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1651 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1655 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1656 | // If a template parameter of a primary class template or alias template |
| 1657 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1658 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1659 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1660 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1661 | Diag((*NewParam)->getLocation(), |
| 1662 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1663 | Invalid = true; |
| 1664 | } |
| 1665 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1666 | if (RedundantDefaultArg) { |
| 1667 | // C++ [temp.param]p12: |
| 1668 | // A template-parameter shall not be given default arguments |
| 1669 | // by two different declarations in the same scope. |
| 1670 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1671 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1672 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1673 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1674 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1675 | // If a template-parameter of a class template has a default |
| 1676 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1677 | // have a default template-argument supplied or be a template parameter |
| 1678 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1680 | diag::err_template_param_default_arg_missing); |
| 1681 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1682 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1683 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | // If we have an old template parameter list that we're merging |
| 1687 | // in, move on to the next parameter. |
| 1688 | if (OldParams) |
| 1689 | ++OldParam; |
| 1690 | } |
| 1691 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1692 | // We were missing some default arguments at the end of the list, so remove |
| 1693 | // all of the default arguments. |
| 1694 | if (RemoveDefaultArguments) { |
| 1695 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1696 | NewParamEnd = NewParams->end(); |
| 1697 | NewParam != NewParamEnd; ++NewParam) { |
| 1698 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1699 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1700 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1701 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1702 | NTTP->removeDefaultArgument(); |
| 1703 | else |
| 1704 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1705 | } |
| 1706 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1707 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1708 | return Invalid; |
| 1709 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1710 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1711 | namespace { |
| 1712 | |
| 1713 | /// A class which looks for a use of a certain level of template |
| 1714 | /// parameter. |
| 1715 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1716 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1717 | |
| 1718 | unsigned Depth; |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 1719 | bool FindLessThanDepth; |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1720 | |
| 1721 | // Whether we're looking for a use of a template parameter that makes the |
| 1722 | // overall construct type-dependent / a dependent type. This is strictly |
| 1723 | // best-effort for now; we may fail to match at all for a dependent type |
| 1724 | // in some cases if this is set. |
| 1725 | bool IgnoreNonTypeDependent; |
| 1726 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1727 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1728 | SourceLocation MatchLoc; |
| 1729 | |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 1730 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent, |
| 1731 | bool FindLessThanDepth = false) |
| 1732 | : Depth(Depth), FindLessThanDepth(FindLessThanDepth), |
| 1733 | IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1734 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1735 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 1736 | : DependencyChecker(Params->getDepth(), IgnoreNonTypeDependent) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1737 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1738 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 1739 | if (FindLessThanDepth ^ (ParmDepth >= Depth)) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1740 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1741 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1742 | return true; |
| 1743 | } |
| 1744 | return false; |
| 1745 | } |
| 1746 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1747 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 1748 | // Prune out non-type-dependent expressions if requested. This can |
| 1749 | // sometimes result in us failing to find a template parameter reference |
| 1750 | // (if a value-dependent expression creates a dependent type), but this |
| 1751 | // mode is best-effort only. |
| 1752 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 1753 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 1754 | return true; |
| 1755 | return super::TraverseStmt(S, Q); |
| 1756 | } |
| 1757 | |
| 1758 | bool TraverseTypeLoc(TypeLoc TL) { |
| 1759 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 1760 | !TL.getType()->isDependentType()) |
| 1761 | return true; |
| 1762 | return super::TraverseTypeLoc(TL); |
| 1763 | } |
| 1764 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1765 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1766 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1767 | } |
| 1768 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1769 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1770 | // For a best-effort search, keep looking until we find a location. |
| 1771 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | bool TraverseTemplateName(TemplateName N) { |
| 1775 | if (TemplateTemplateParmDecl *PD = |
| 1776 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1777 | if (Matches(PD->getDepth())) |
| 1778 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1779 | return super::TraverseTemplateName(N); |
| 1780 | } |
| 1781 | |
| 1782 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1783 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1784 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1785 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1786 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1787 | return super::VisitDeclRefExpr(E); |
| 1788 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1789 | |
| 1790 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1791 | return TraverseType(T->getReplacementType()); |
| 1792 | } |
| 1793 | |
| 1794 | bool |
| 1795 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1796 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1797 | } |
| 1798 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1799 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1800 | return TraverseType(T->getInjectedSpecializationType()); |
| 1801 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1802 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1803 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1805 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1806 | /// list. |
| 1807 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1808 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1809 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1810 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1811 | return Checker.Match; |
| 1812 | } |
| 1813 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1814 | // Find the source range corresponding to the named type in the given |
| 1815 | // nested-name-specifier, if any. |
| 1816 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1817 | QualType T, |
| 1818 | const CXXScopeSpec &SS) { |
| 1819 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1820 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1821 | if (const Type *CurType = NNS->getAsType()) { |
| 1822 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1823 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1824 | } else |
| 1825 | break; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1826 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1827 | NNSLoc = NNSLoc.getPrefix(); |
| 1828 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1829 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1830 | return SourceRange(); |
| 1831 | } |
| 1832 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1834 | /// specifier, returning the template parameter list that applies to the |
| 1835 | /// name. |
| 1836 | /// |
| 1837 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1838 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1840 | /// \param DeclLoc The location of the declaration itself. |
| 1841 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1842 | /// \param SS the scope specifier that will be matched to the given template |
| 1843 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1844 | /// being declared. |
| 1845 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1846 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1847 | /// is one. Used to check for a missing 'template<>'. |
| 1848 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1849 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1850 | /// innermost template parameter lists. |
| 1851 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1852 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1853 | /// matching template parameters to scope specifiers in friend |
| 1854 | /// declarations. |
| 1855 | /// |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 1856 | /// \param IsMemberSpecialization will be set true if the scope specifier |
| 1857 | /// denotes a fully-specialized type, and therefore this is a declaration of |
| 1858 | /// a member specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1859 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1861 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1862 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1864 | /// 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] | 1865 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1866 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1867 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1868 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1869 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 1870 | bool &IsMemberSpecialization, bool &Invalid) { |
| 1871 | IsMemberSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1872 | Invalid = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1873 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1874 | // The sequence of nested types to which we will match up the template |
| 1875 | // parameter lists. We first build this list by starting with the type named |
| 1876 | // 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] | 1877 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1878 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1879 | if (SS.getScopeRep()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1880 | if (CXXRecordDecl *Record |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1881 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1882 | T = Context.getTypeDeclType(Record); |
| 1883 | else |
| 1884 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1885 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1886 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1887 | // If we found an explicit specialization that prevents us from needing |
| 1888 | // 'template<>' headers, this will be set to the location of that |
| 1889 | // explicit specialization. |
| 1890 | SourceLocation ExplicitSpecLoc; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1891 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1892 | while (!T.isNull()) { |
| 1893 | NestedTypes.push_back(T); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1894 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1895 | // Retrieve the parent of a record type. |
| 1896 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1897 | // If this type is an explicit specialization, we're done. |
| 1898 | if (ClassTemplateSpecializationDecl *Spec |
| 1899 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1900 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1901 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1902 | ExplicitSpecLoc = Spec->getLocation(); |
| 1903 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1904 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1905 | } else if (Record->getTemplateSpecializationKind() |
| 1906 | == TSK_ExplicitSpecialization) { |
| 1907 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1908 | break; |
| 1909 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1911 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1912 | T = Context.getTypeDeclType(Parent); |
| 1913 | else |
| 1914 | T = QualType(); |
| 1915 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1918 | if (const TemplateSpecializationType *TST |
| 1919 | = T->getAs<TemplateSpecializationType>()) { |
| 1920 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1921 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1922 | T = Context.getTypeDeclType(Parent); |
| 1923 | else |
| 1924 | T = QualType(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1925 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1926 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1927 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1928 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1929 | // Look one step prior in a dependent template specialization type. |
| 1930 | if (const DependentTemplateSpecializationType *DependentTST |
| 1931 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1932 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1933 | T = QualType(NNS->getAsType(), 0); |
| 1934 | else |
| 1935 | T = QualType(); |
| 1936 | continue; |
| 1937 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1939 | // Look one step prior in a dependent name type. |
| 1940 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1941 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1942 | T = QualType(NNS->getAsType(), 0); |
| 1943 | else |
| 1944 | T = QualType(); |
| 1945 | continue; |
| 1946 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1947 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1948 | // Retrieve the parent of an enumeration type. |
| 1949 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1950 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1951 | // check here. |
| 1952 | EnumDecl *Enum = EnumT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1954 | // Get to the parent type. |
| 1955 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1956 | T = Context.getTypeDeclType(Parent); |
| 1957 | else |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1958 | T = QualType(); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1959 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1960 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1962 | T = QualType(); |
| 1963 | } |
| 1964 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1965 | // to the innermost while checking template-parameter-lists. |
| 1966 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1967 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1968 | // C++0x [temp.expl.spec]p17: |
| 1969 | // A member or a member template may be nested within many |
| 1970 | // enclosing class templates. In an explicit specialization for |
| 1971 | // such a member, the member declaration shall be preceded by a |
| 1972 | // template<> for each enclosing class template that is |
| 1973 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1974 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1975 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1976 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1977 | if (SawNonEmptyTemplateParameterList) { |
| 1978 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1979 | << !Recovery << Range; |
| 1980 | Invalid = true; |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 1981 | IsMemberSpecialization = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1982 | return true; |
| 1983 | } |
| 1984 | |
| 1985 | return false; |
| 1986 | }; |
| 1987 | |
| 1988 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1989 | // Check that we can have an explicit specialization here. |
| 1990 | if (CheckExplicitSpecialization(Range, true)) |
| 1991 | return true; |
| 1992 | |
| 1993 | // We don't have a template header, but we should. |
| 1994 | SourceLocation ExpectedTemplateLoc; |
| 1995 | if (!ParamLists.empty()) |
| 1996 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1997 | else |
| 1998 | ExpectedTemplateLoc = DeclStartLoc; |
| 1999 | |
| 2000 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 2001 | << Range |
| 2002 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 2003 | return false; |
| 2004 | }; |
| 2005 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2006 | unsigned ParamIdx = 0; |
| 2007 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 2008 | ++TypeIdx) { |
| 2009 | T = NestedTypes[TypeIdx]; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2010 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2011 | // Whether we expect a 'template<>' header. |
| 2012 | bool NeedEmptyTemplateHeader = false; |
| 2013 | |
| 2014 | // Whether we expect a template header with parameters. |
| 2015 | bool NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2016 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2017 | // For a dependent type, the set of template parameters that we |
| 2018 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2019 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2020 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2021 | // C++0x [temp.expl.spec]p15: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2022 | // A member or a member template may be nested within many enclosing |
| 2023 | // class templates. In an explicit specialization for such a member, the |
| 2024 | // member declaration shall be preceded by a template<> for each |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2025 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2026 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2027 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 2028 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 2029 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 2030 | NeedNonemptyTemplateHeader = true; |
| 2031 | } else if (Record->isDependentType()) { |
| 2032 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2033 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2034 | ->getTemplateParameters(); |
| 2035 | NeedNonemptyTemplateHeader = true; |
| 2036 | } |
| 2037 | } else if (ClassTemplateSpecializationDecl *Spec |
| 2038 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 2039 | // C++0x [temp.expl.spec]p4: |
| 2040 | // Members of an explicitly specialized class template are defined |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2041 | // in the same manner as members of normal classes, and not using |
| 2042 | // the template<> syntax. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2043 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 2044 | NeedEmptyTemplateHeader = true; |
| 2045 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 2046 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2047 | } else if (Record->getTemplateSpecializationKind()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2048 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2049 | != TSK_ExplicitSpecialization && |
| 2050 | TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2051 | IsMemberSpecialization = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2052 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2053 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2054 | } |
| 2055 | } else if (const TemplateSpecializationType *TST |
| 2056 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 2057 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2058 | ExpectedTemplateParams = Template->getTemplateParameters(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2059 | NeedNonemptyTemplateHeader = true; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2060 | } |
| 2061 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2062 | // FIXME: We actually could/should check the template arguments here |
| 2063 | // against the corresponding template parameter list. |
| 2064 | NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2067 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2068 | // In an explicit specialization declaration for a member of a class |
| 2069 | // template or a member template that ap- pears in namespace scope, the |
| 2070 | // member template and some of its enclosing class templates may remain |
| 2071 | // unspecialized, except that the declaration shall not explicitly |
| 2072 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2073 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2074 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2075 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2076 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2077 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2078 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2079 | } else |
| 2080 | SawNonEmptyTemplateParameterList = true; |
| 2081 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2082 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2083 | if (NeedEmptyTemplateHeader) { |
| 2084 | // 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] | 2085 | // here, then it's a member specialization. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2086 | if (TypeIdx == NumTypes - 1) |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 2087 | IsMemberSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2088 | |
| 2089 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2090 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2091 | // The header has template parameters when it shouldn't. Complain. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2092 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2093 | diag::err_template_param_list_matches_nontemplate) |
| 2094 | << T |
| 2095 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2096 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2097 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2098 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2099 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2100 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2102 | // Consume this template header. |
| 2103 | ++ParamIdx; |
| 2104 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2105 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2106 | |
| 2107 | if (!IsFriend) |
| 2108 | if (DiagnoseMissingExplicitSpecialization( |
| 2109 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2110 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2111 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2112 | continue; |
| 2113 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2114 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2115 | if (NeedNonemptyTemplateHeader) { |
| 2116 | // In friend declarations we can have template-ids which don't |
| 2117 | // depend on the corresponding template parameter lists. But |
| 2118 | // assume that empty parameter lists are supposed to match this |
| 2119 | // template-id. |
| 2120 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2121 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2122 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2123 | ExpectedTemplateParams = nullptr; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2124 | else |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2125 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2127 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2128 | if (ParamIdx < ParamLists.size()) { |
| 2129 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2130 | if (ExpectedTemplateParams && |
| 2131 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2132 | ExpectedTemplateParams, |
| 2133 | true, TPL_TemplateMatch)) |
| 2134 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2135 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2136 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2137 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2138 | TPC_ClassTemplateMember)) |
| 2139 | Invalid = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2141 | ++ParamIdx; |
| 2142 | continue; |
| 2143 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2144 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2145 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2146 | << T |
| 2147 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2148 | Invalid = true; |
| 2149 | continue; |
| 2150 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2151 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2153 | // If there were at least as many template-ids as there were template |
| 2154 | // parameter lists, then there are no template parameter lists remaining for |
| 2155 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2156 | if (ParamIdx >= ParamLists.size()) { |
| 2157 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2158 | // We don't have a template header for the declaration itself, but we |
| 2159 | // should. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2160 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2161 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2162 | |
| 2163 | // Fabricate an empty template parameter list for the invented header. |
| 2164 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2165 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2166 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2169 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2170 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2172 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2173 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2174 | bool HasAnyExplicitSpecHeader = false; |
| 2175 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2176 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2177 | if (ParamLists[I]->size() == 0) |
| 2178 | HasAnyExplicitSpecHeader = true; |
| 2179 | else |
| 2180 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2181 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2182 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2183 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2184 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2185 | : diag::err_template_spec_extra_headers) |
| 2186 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2187 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2188 | |
| 2189 | // If there was a specialization somewhere, such that 'template<>' is |
| 2190 | // not required, and there were any 'template<>' headers, note where the |
| 2191 | // specialization occurred. |
| 2192 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2193 | Diag(ExplicitSpecLoc, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2194 | diag::note_explicit_template_spec_does_not_need_header) |
| 2195 | << NestedTypes.back(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2196 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2197 | // We have a template parameter list with no corresponding scope, which |
| 2198 | // means that the resulting template declaration can't be instantiated |
| 2199 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2200 | if (!AllExplicitSpecHeaders) |
| 2201 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2202 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2204 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2205 | // In an explicit specialization declaration for a member of a class |
| 2206 | // template or a member template that ap- pears in namespace scope, the |
| 2207 | // member template and some of its enclosing class templates may remain |
| 2208 | // unspecialized, except that the declaration shall not explicitly |
| 2209 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2210 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2211 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2212 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2213 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2214 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2215 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2216 | // Return the last template parameter list, which corresponds to the |
| 2217 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2218 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2221 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2222 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2223 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2224 | << (isa<FunctionTemplateDecl>(Template) |
| 2225 | ? 0 |
| 2226 | : isa<ClassTemplateDecl>(Template) |
| 2227 | ? 1 |
| 2228 | : isa<VarTemplateDecl>(Template) |
| 2229 | ? 2 |
| 2230 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2231 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2232 | return; |
| 2233 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2234 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2235 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2236 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2237 | IEnd = OST->end(); |
| 2238 | I != IEnd; ++I) |
| 2239 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2240 | << 0 << (*I)->getDeclName(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2241 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2242 | return; |
| 2243 | } |
| 2244 | } |
| 2245 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2246 | static QualType |
| 2247 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2248 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2249 | SourceLocation TemplateLoc, |
| 2250 | TemplateArgumentListInfo &TemplateArgs) { |
| 2251 | ASTContext &Context = SemaRef.getASTContext(); |
| 2252 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2253 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2254 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2255 | // S<T, 0, ..., N-1>. |
| 2256 | |
| 2257 | // C++14 [inteseq.intseq]p1: |
| 2258 | // T shall be an integer type. |
| 2259 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2260 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2261 | diag::err_integer_sequence_integral_element_type); |
| 2262 | return QualType(); |
| 2263 | } |
| 2264 | |
| 2265 | // C++14 [inteseq.make]p1: |
| 2266 | // If N is negative the program is ill-formed. |
| 2267 | TemplateArgument NumArgsArg = Converted[2]; |
| 2268 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2269 | if (NumArgs < 0) { |
| 2270 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2271 | diag::err_integer_sequence_negative_length); |
| 2272 | return QualType(); |
| 2273 | } |
| 2274 | |
| 2275 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2276 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2277 | // The type argument gets reused as the first template argument in the |
| 2278 | // synthetic template argument list. |
| 2279 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2280 | // Expand N into 0 ... N-1. |
| 2281 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2282 | I < NumArgs; ++I) { |
| 2283 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2284 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2285 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2286 | } |
| 2287 | // The first template argument will be reused as the template decl that |
| 2288 | // our synthetic template arguments will be applied to. |
| 2289 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2290 | TemplateLoc, SyntheticTemplateArgs); |
| 2291 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2292 | |
| 2293 | case BTK__type_pack_element: |
| 2294 | // Specializations of |
| 2295 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2296 | // are treated like T_Index. |
| 2297 | assert(Converted.size() == 2 && |
| 2298 | "__type_pack_element should be given an index and a parameter pack"); |
| 2299 | |
| 2300 | // If the Index is out of bounds, the program is ill-formed. |
| 2301 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2302 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2303 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2304 | "type std::size_t, and hence be non-negative"); |
| 2305 | if (Index >= Ts.pack_size()) { |
| 2306 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2307 | diag::err_type_pack_element_out_of_bounds); |
| 2308 | return QualType(); |
| 2309 | } |
| 2310 | |
| 2311 | // We simply return the type at index `Index`. |
| 2312 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2313 | return Nth->getAsType(); |
| 2314 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2315 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2316 | } |
| 2317 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2318 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2319 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2320 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2321 | DependentTemplateName *DTN |
| 2322 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2323 | if (DTN && DTN->isIdentifier()) |
| 2324 | // When building a template-id where the template-name is dependent, |
| 2325 | // assume the template is a type template. Either our assumption is |
| 2326 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2327 | // dependent name is substituted. |
| 2328 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2329 | DTN->getQualifier(), |
| 2330 | DTN->getIdentifier(), |
| 2331 | TemplateArgs); |
| 2332 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2333 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2334 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2335 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2336 | // We might have a substituted template template parameter pack. If so, |
| 2337 | // build a template specialization type for it. |
| 2338 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2339 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2341 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2342 | << Name; |
| 2343 | NoteAllFoundTemplates(Name); |
| 2344 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2345 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2347 | // Check that the template argument list is well-formed for this |
| 2348 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2349 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2350 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2351 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2352 | return QualType(); |
| 2353 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2354 | QualType CanonType; |
| 2355 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2356 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2357 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2358 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2359 | // Find the canonical type for this type alias template specialization. |
| 2360 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2361 | if (Pattern->isInvalidDecl()) |
| 2362 | return QualType(); |
| 2363 | |
| 2364 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2365 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2366 | |
| 2367 | // Only substitute for the innermost template argument list. |
| 2368 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2369 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2370 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2371 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2372 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2373 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2374 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2375 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2376 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2377 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2378 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2379 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2380 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2381 | AliasTemplate->getDeclName()); |
| 2382 | if (CanonType.isNull()) |
| 2383 | return QualType(); |
| 2384 | } else if (Name.isDependent() || |
| 2385 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2386 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2387 | // This class template specialization is a dependent |
| 2388 | // type. Therefore, its canonical type is another class template |
| 2389 | // specialization type that contains all of the converted |
| 2390 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2391 | // A<T, T> have identical types when A is declared as: |
| 2392 | // |
| 2393 | // template<typename T, typename U = T> struct A; |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 2394 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2395 | |
| 2396 | // This might work out to be a current instantiation, in which |
| 2397 | // case the canonical type needs to be the InjectedClassNameType. |
| 2398 | // |
| 2399 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2400 | // changes to CurContext don't change the set of current |
| 2401 | // instantiations. |
| 2402 | if (isa<ClassTemplateDecl>(Template)) { |
| 2403 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2404 | // If we get out to a namespace, we're done. |
| 2405 | if (Ctx->isFileContext()) break; |
| 2406 | |
| 2407 | // If this isn't a record, keep looking. |
| 2408 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2409 | if (!Record) continue; |
| 2410 | |
| 2411 | // Look for one of the two cases with InjectedClassNameTypes |
| 2412 | // and check whether it's the same template. |
| 2413 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2414 | !Record->getDescribedClassTemplate()) |
| 2415 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2416 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2417 | // Fetch the injected class name type and check whether its |
| 2418 | // injected type is equal to the type we just built. |
| 2419 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2420 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2421 | ->getInjectedSpecializationType(); |
| 2422 | |
| 2423 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2424 | continue; |
| 2425 | |
| 2426 | // If so, the canonical type of this TST is the injected |
| 2427 | // class name type of the record we just found. |
| 2428 | assert(ICNT.isCanonical()); |
| 2429 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2430 | break; |
| 2431 | } |
| 2432 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2433 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2434 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2435 | // Find the class template specialization declaration that |
| 2436 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2437 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2438 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2439 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2440 | if (!Decl) { |
| 2441 | // This is the first time we have referenced this class template |
| 2442 | // specialization. Create the canonical declaration and add it to |
| 2443 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2444 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2445 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2446 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2447 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2448 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2449 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2450 | Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2451 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2452 | if (ClassTemplate->isOutOfLine()) |
| 2453 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2456 | // Diagnose uses of this specialization. |
| 2457 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2458 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2459 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2460 | assert(isa<RecordType>(CanonType) && |
| 2461 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2462 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2463 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2464 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2465 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2466 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2467 | // Build the fully-sugared type for this class template |
| 2468 | // specialization, which refers back to the class template |
| 2469 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2470 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2473 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2474 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2475 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
| 2476 | SourceLocation TemplateIILoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2478 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2479 | SourceLocation RAngleLoc, |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 2480 | bool IsCtorOrDtorName, bool IsClassName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2481 | if (SS.isInvalid()) |
| 2482 | return true; |
| 2483 | |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 2484 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { |
| 2485 | DeclContext *LookupCtx = computeDeclContext(SS, /*EnteringContext*/false); |
| 2486 | |
| 2487 | // C++ [temp.res]p3: |
| 2488 | // A qualified-id that refers to a type and in which the |
| 2489 | // nested-name-specifier depends on a template-parameter (14.6.2) |
| 2490 | // shall be prefixed by the keyword typename to indicate that the |
| 2491 | // qualified-id denotes a type, forming an |
| 2492 | // elaborated-type-specifier (7.1.5.3). |
| 2493 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { |
Richard Smith | 3411fbf | 2017-02-01 21:41:18 +0000 | [diff] [blame] | 2494 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 2495 | << SS.getScopeRep() << TemplateII->getName(); |
| 2496 | // Recover as if 'typename' were specified. |
| 2497 | // FIXME: This is not quite correct recovery as we don't transform SS |
| 2498 | // into the corresponding dependent form (and we don't diagnose missing |
| 2499 | // 'template' keywords within SS as a result). |
| 2500 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
| 2501 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
| 2502 | TemplateArgsIn, RAngleLoc); |
| 2503 | } |
| 2504 | |
| 2505 | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
| 2506 | // it's not actually allowed to be used as a type in most cases. Because |
| 2507 | // we annotate it before we know whether it's valid, we have to check for |
| 2508 | // this case here. |
| 2509 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2510 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 2511 | Diag(TemplateIILoc, |
| 2512 | TemplateKWLoc.isInvalid() |
| 2513 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
| 2514 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 2515 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 2516 | << 1 /*if any keyword was present, it was 'template'*/; |
| 2517 | } |
| 2518 | } |
| 2519 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2520 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2521 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2522 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2523 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2524 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2525 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2526 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2527 | QualType T |
| 2528 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2529 | DTN->getQualifier(), |
| 2530 | DTN->getIdentifier(), |
| 2531 | TemplateArgs); |
| 2532 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2533 | TypeLocBuilder TLB; |
| 2534 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2535 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2536 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2537 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2538 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2539 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2540 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2541 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2542 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2543 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2544 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2545 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2546 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2547 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2548 | if (Result.isNull()) |
| 2549 | return true; |
| 2550 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2551 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2552 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2553 | TemplateSpecializationTypeLoc SpecTL |
| 2554 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2555 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2556 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2557 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2558 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2559 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2560 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2561 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2562 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2563 | // constructor or destructor name (in such a case, the scope specifier |
| 2564 | // will be attached to the enclosing Decl or Expr node). |
| 2565 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2566 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2567 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2568 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2569 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2570 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2571 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2572 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2573 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2574 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2575 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2576 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2577 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2578 | SourceLocation TagLoc, |
| 2579 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2580 | SourceLocation TemplateKWLoc, |
| 2581 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2582 | SourceLocation TemplateLoc, |
| 2583 | SourceLocation LAngleLoc, |
| 2584 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2585 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2586 | TemplateName Template = TemplateD.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2587 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2588 | // Translate the parser's template argument list in our AST format. |
| 2589 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2590 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2591 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2592 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2593 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2594 | ElaboratedTypeKeyword Keyword |
| 2595 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2597 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2598 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2599 | DTN->getQualifier(), |
| 2600 | DTN->getIdentifier(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2601 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2602 | |
| 2603 | // Build type-source information. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2604 | TypeLocBuilder TLB; |
| 2605 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2606 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2607 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2608 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2609 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2610 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2611 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2612 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2613 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2614 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2615 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2616 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2617 | |
| 2618 | if (TypeAliasTemplateDecl *TAT = |
| 2619 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2620 | // C++0x [dcl.type.elab]p2: |
| 2621 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2622 | // resolves to an alias template specialization, the |
| 2623 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 2624 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 2625 | << TAT << NTK_TypeAliasTemplate << TagKind; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2626 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2627 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2628 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2629 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2630 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2631 | return TypeResult(true); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2632 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2633 | // Check the tag kind |
| 2634 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2635 | RecordDecl *D = RT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2636 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2637 | IdentifierInfo *Id = D->getIdentifier(); |
| 2638 | assert(Id && "templated class must have an identifier"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2639 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2640 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 2641 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2642 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2643 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2644 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2645 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2646 | } |
| 2647 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2648 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2649 | // Provide source-location information for the template specialization. |
| 2650 | TypeLocBuilder TLB; |
| 2651 | TemplateSpecializationTypeLoc SpecTL |
| 2652 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2653 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2654 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2655 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2656 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2657 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2658 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2659 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2660 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2661 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2662 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2663 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2664 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2665 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2666 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2669 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2670 | NamedDecl *PrevDecl, |
| 2671 | SourceLocation Loc, |
| 2672 | bool IsPartialSpecialization); |
| 2673 | |
| 2674 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2675 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2676 | static bool isTemplateArgumentTemplateParameter( |
| 2677 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2678 | switch (Arg.getKind()) { |
| 2679 | case TemplateArgument::Null: |
| 2680 | case TemplateArgument::NullPtr: |
| 2681 | case TemplateArgument::Integral: |
| 2682 | case TemplateArgument::Declaration: |
| 2683 | case TemplateArgument::Pack: |
| 2684 | case TemplateArgument::TemplateExpansion: |
| 2685 | return false; |
| 2686 | |
| 2687 | case TemplateArgument::Type: { |
| 2688 | QualType Type = Arg.getAsType(); |
| 2689 | const TemplateTypeParmType *TPT = |
| 2690 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2691 | return TPT && !Type.hasQualifiers() && |
| 2692 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2693 | } |
| 2694 | |
| 2695 | case TemplateArgument::Expression: { |
| 2696 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2697 | if (!DRE || !DRE->getDecl()) |
| 2698 | return false; |
| 2699 | const NonTypeTemplateParmDecl *NTTP = |
| 2700 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2701 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2702 | } |
| 2703 | |
| 2704 | case TemplateArgument::Template: |
| 2705 | const TemplateTemplateParmDecl *TTP = |
| 2706 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2707 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2708 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2709 | } |
| 2710 | llvm_unreachable("unexpected kind of template argument"); |
| 2711 | } |
| 2712 | |
| 2713 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2714 | ArrayRef<TemplateArgument> Args) { |
| 2715 | if (Params->size() != Args.size()) |
| 2716 | return false; |
| 2717 | |
| 2718 | unsigned Depth = Params->getDepth(); |
| 2719 | |
| 2720 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2721 | TemplateArgument Arg = Args[I]; |
| 2722 | |
| 2723 | // If the parameter is a pack expansion, the argument must be a pack |
| 2724 | // whose only element is a pack expansion. |
| 2725 | if (Params->getParam(I)->isParameterPack()) { |
| 2726 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2727 | !Arg.pack_begin()->isPackExpansion()) |
| 2728 | return false; |
| 2729 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2730 | } |
| 2731 | |
| 2732 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2733 | return false; |
| 2734 | } |
| 2735 | |
| 2736 | return true; |
| 2737 | } |
| 2738 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2739 | /// Convert the parser's template argument list representation into our form. |
| 2740 | static TemplateArgumentListInfo |
| 2741 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2742 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2743 | TemplateId.RAngleLoc); |
| 2744 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2745 | TemplateId.NumArgs); |
| 2746 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2747 | return TemplateArgs; |
| 2748 | } |
| 2749 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2750 | template<typename PartialSpecDecl> |
| 2751 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 2752 | if (Partial->getDeclContext()->isDependentContext()) |
| 2753 | return; |
| 2754 | |
| 2755 | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
| 2756 | // for non-substitution-failure issues? |
| 2757 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 2758 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 2759 | return; |
| 2760 | |
| 2761 | auto *Template = Partial->getSpecializedTemplate(); |
| 2762 | S.Diag(Partial->getLocation(), |
Richard Smith | fa4a09d | 2016-12-27 20:03:09 +0000 | [diff] [blame] | 2763 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 2764 | << isa<VarTemplateDecl>(Template); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2765 | |
| 2766 | if (Info.hasSFINAEDiagnostic()) { |
| 2767 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 2768 | PartialDiagnostic::NullDiagnostic()}; |
| 2769 | Info.takeSFINAEDiagnostic(Diag); |
| 2770 | SmallString<128> SFINAEArgString; |
| 2771 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 2772 | S.Diag(Diag.first, |
| 2773 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 2774 | << SFINAEArgString; |
| 2775 | } |
| 2776 | |
| 2777 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2778 | } |
| 2779 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2780 | template<typename PartialSpecDecl> |
| 2781 | static void checkTemplatePartialSpecialization(Sema &S, |
| 2782 | PartialSpecDecl *Partial) { |
| 2783 | // C++1z [temp.class.spec]p8: (DR1495) |
| 2784 | // - The specialization shall be more specialized than the primary |
| 2785 | // template (14.5.5.2). |
| 2786 | checkMoreSpecializedThanPrimary(S, Partial); |
| 2787 | |
| 2788 | // C++ [temp.class.spec]p8: (DR1315) |
| 2789 | // - Each template-parameter shall appear at least once in the |
| 2790 | // template-id outside a non-deduced context. |
| 2791 | // C++1z [temp.class.spec.match]p3 (P0127R2) |
| 2792 | // If the template arguments of a partial specialization cannot be |
| 2793 | // deduced because of the structure of its template-parameter-list |
| 2794 | // and the template-id, the program is ill-formed. |
| 2795 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 2796 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2797 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2798 | TemplateParams->getDepth(), DeducibleParams); |
| 2799 | |
| 2800 | if (!DeducibleParams.all()) { |
| 2801 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 2802 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 2803 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 2804 | << (NumNonDeducible > 1) |
| 2805 | << SourceRange(Partial->getLocation(), |
| 2806 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
| 2807 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2808 | if (!DeducibleParams[I]) { |
| 2809 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2810 | if (Param->getDeclName()) |
| 2811 | S.Diag(Param->getLocation(), |
| 2812 | diag::note_partial_spec_unused_parameter) |
| 2813 | << Param->getDeclName(); |
| 2814 | else |
| 2815 | S.Diag(Param->getLocation(), |
| 2816 | diag::note_partial_spec_unused_parameter) |
| 2817 | << "(anonymous)"; |
| 2818 | } |
| 2819 | } |
| 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | void Sema::CheckTemplatePartialSpecialization( |
| 2824 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 2825 | checkTemplatePartialSpecialization(*this, Partial); |
| 2826 | } |
| 2827 | |
| 2828 | void Sema::CheckTemplatePartialSpecialization( |
| 2829 | VarTemplatePartialSpecializationDecl *Partial) { |
| 2830 | checkTemplatePartialSpecialization(*this, Partial); |
| 2831 | } |
| 2832 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2833 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2834 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2835 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2836 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2837 | // D must be variable template id. |
| 2838 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2839 | "Variable template specialization is declared with a template it."); |
| 2840 | |
| 2841 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2842 | TemplateArgumentListInfo TemplateArgs = |
| 2843 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2844 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2845 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2846 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2847 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2848 | TemplateName Name = TemplateId->Template.get(); |
| 2849 | |
| 2850 | // The template-id must name a variable template. |
| 2851 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2852 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2853 | if (!VarTemplate) { |
| 2854 | NamedDecl *FnTemplate; |
| 2855 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2856 | FnTemplate = *OTS->begin(); |
| 2857 | else |
| 2858 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2859 | if (FnTemplate) |
| 2860 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2861 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2862 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2863 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2864 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2865 | |
| 2866 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2867 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2868 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2869 | UPPC_PartialSpecialization)) |
| 2870 | return true; |
| 2871 | |
| 2872 | // Check that the template argument list is well-formed for this |
| 2873 | // template. |
| 2874 | SmallVector<TemplateArgument, 4> Converted; |
| 2875 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2876 | false, Converted)) |
| 2877 | return true; |
| 2878 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2879 | // Find the variable template (partial) specialization declaration that |
| 2880 | // corresponds to these arguments. |
| 2881 | if (IsPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2882 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 2883 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2884 | return true; |
| 2885 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2886 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
| 2887 | // also do them during instantiation. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2888 | bool InstantiationDependent; |
| 2889 | if (!Name.isDependent() && |
| 2890 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2891 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2892 | InstantiationDependent)) { |
| 2893 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2894 | << VarTemplate->getDeclName(); |
| 2895 | IsPartialSpecialization = false; |
| 2896 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2897 | |
| 2898 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2899 | Converted)) { |
| 2900 | // C++ [temp.class.spec]p9b3: |
| 2901 | // |
| 2902 | // -- The argument list of the specialization shall not be identical |
| 2903 | // to the implicit argument list of the primary template. |
| 2904 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2905 | << /*variable template*/ 1 |
| 2906 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2907 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2908 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2909 | // of the primary template. |
| 2910 | return true; |
| 2911 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2914 | void *InsertPos = nullptr; |
| 2915 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2916 | |
| 2917 | if (IsPartialSpecialization) |
| 2918 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2919 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2920 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2921 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2922 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2923 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2924 | |
| 2925 | // Check whether we can declare a variable template specialization in |
| 2926 | // the current scope. |
| 2927 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2928 | TemplateNameLoc, |
| 2929 | IsPartialSpecialization)) |
| 2930 | return true; |
| 2931 | |
| 2932 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2933 | // Since the only prior variable template specialization with these |
| 2934 | // arguments was referenced but not declared, reuse that |
| 2935 | // declaration node as our own, updating its source location and |
| 2936 | // the list of outer template parameters to reflect our new declaration. |
| 2937 | Specialization = PrevDecl; |
| 2938 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2939 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2940 | } else if (IsPartialSpecialization) { |
| 2941 | // Create a new class template partial specialization declaration node. |
| 2942 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2943 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2944 | VarTemplatePartialSpecializationDecl *Partial = |
| 2945 | VarTemplatePartialSpecializationDecl::Create( |
| 2946 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2947 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2948 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2949 | |
| 2950 | if (!PrevPartial) |
| 2951 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2952 | Specialization = Partial; |
| 2953 | |
| 2954 | // If we are providing an explicit specialization of a member variable |
| 2955 | // template specialization, make a note of that. |
| 2956 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2957 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2958 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2959 | CheckTemplatePartialSpecialization(Partial); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2960 | } else { |
| 2961 | // Create a new class template specialization declaration node for |
| 2962 | // this explicit specialization or friend declaration. |
| 2963 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2964 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2965 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2966 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2967 | |
| 2968 | if (!PrevDecl) |
| 2969 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2970 | } |
| 2971 | |
| 2972 | // C++ [temp.expl.spec]p6: |
| 2973 | // If a template, a member template or the member of a class template is |
| 2974 | // explicitly specialized then that specialization shall be declared |
| 2975 | // before the first use of that specialization that would cause an implicit |
| 2976 | // instantiation to take place, in every translation unit in which such a |
| 2977 | // use occurs; no diagnostic is required. |
| 2978 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2979 | bool Okay = false; |
| 2980 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2981 | // Is there any previous explicit specialization declaration? |
| 2982 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2983 | Okay = true; |
| 2984 | break; |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | if (!Okay) { |
| 2989 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2990 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2991 | << Name << Range; |
| 2992 | |
| 2993 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2994 | diag::note_instantiation_required_here) |
| 2995 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2996 | TSK_ImplicitInstantiation); |
| 2997 | return true; |
| 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 3002 | Specialization->setLexicalDeclContext(CurContext); |
| 3003 | |
| 3004 | // Add the specialization into its lexical context, so that it can |
| 3005 | // be seen when iterating through the list of declarations in that |
| 3006 | // context. However, specializations are not found by name lookup. |
| 3007 | CurContext->addDecl(Specialization); |
| 3008 | |
| 3009 | // Note that this is an explicit specialization. |
| 3010 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 3011 | |
| 3012 | if (PrevDecl) { |
| 3013 | // Check that this isn't a redefinition of this specialization, |
| 3014 | // merging with previous declarations. |
| 3015 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 3016 | ForRedeclaration); |
| 3017 | PrevSpec.addDecl(PrevDecl); |
| 3018 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3019 | } else if (Specialization->isStaticDataMember() && |
| 3020 | Specialization->isOutOfLine()) { |
| 3021 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | // Link instantiations of static data members back to the template from |
| 3025 | // which they were instantiated. |
| 3026 | if (Specialization->isStaticDataMember()) |
| 3027 | Specialization->setInstantiationOfStaticDataMember( |
| 3028 | VarTemplate->getTemplatedDecl(), |
| 3029 | Specialization->getSpecializationKind()); |
| 3030 | |
| 3031 | return Specialization; |
| 3032 | } |
| 3033 | |
| 3034 | namespace { |
| 3035 | /// \brief A partial specialization whose template arguments have matched |
| 3036 | /// a given template-id. |
| 3037 | struct PartialSpecMatchResult { |
| 3038 | VarTemplatePartialSpecializationDecl *Partial; |
| 3039 | TemplateArgumentList *Args; |
| 3040 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 3041 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3042 | |
| 3043 | DeclResult |
| 3044 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3045 | SourceLocation TemplateNameLoc, |
| 3046 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3047 | assert(Template && "A variable template id without template?"); |
| 3048 | |
| 3049 | // Check that the template argument list is well-formed for this template. |
| 3050 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3051 | if (CheckTemplateArgumentList( |
| 3052 | Template, TemplateNameLoc, |
| 3053 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3054 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3055 | return true; |
| 3056 | |
| 3057 | // Find the variable template specialization declaration that |
| 3058 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3059 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3060 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3061 | Converted, InsertPos)) { |
| 3062 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3063 | // If we already have a variable template specialization, return it. |
| 3064 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3065 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3066 | |
| 3067 | // This is the first time we have referenced this variable template |
| 3068 | // specialization. Create the canonical declaration and add it to |
| 3069 | // the set of specializations, based on the closest partial specialization |
| 3070 | // that it represents. That is, |
| 3071 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 3072 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3073 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3074 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 3075 | bool AmbiguousPartialSpec = false; |
| 3076 | typedef PartialSpecMatchResult MatchResult; |
| 3077 | SmallVector<MatchResult, 4> Matched; |
| 3078 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3079 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 3080 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3081 | |
| 3082 | // 1. Attempt to find the closest partial specialization that this |
| 3083 | // specializes, if any. |
| 3084 | // If any of the template arguments is dependent, then this is probably |
| 3085 | // a placeholder for an incomplete declarative context; which must be |
| 3086 | // complete by instantiation time. Thus, do not search through the partial |
| 3087 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3088 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 3089 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 3090 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3091 | bool InstantiationDependent = false; |
| 3092 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3093 | TemplateArgs, InstantiationDependent)) { |
| 3094 | |
| 3095 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3096 | Template->getPartialSpecializations(PartialSpecs); |
| 3097 | |
| 3098 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 3099 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 3100 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 3101 | |
| 3102 | if (TemplateDeductionResult Result = |
| 3103 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 3104 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3105 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3106 | FailedCandidates.addCandidate().set( |
| 3107 | DeclAccessPair::make(Template, AS_public), Partial, |
| 3108 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3109 | (void)Result; |
| 3110 | } else { |
| 3111 | Matched.push_back(PartialSpecMatchResult()); |
| 3112 | Matched.back().Partial = Partial; |
| 3113 | Matched.back().Args = Info.take(); |
| 3114 | } |
| 3115 | } |
| 3116 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3117 | if (Matched.size() >= 1) { |
| 3118 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 3119 | if (Matched.size() == 1) { |
| 3120 | // -- If exactly one matching specialization is found, the |
| 3121 | // instantiation is generated from that specialization. |
| 3122 | // We don't need to do anything for this. |
| 3123 | } else { |
| 3124 | // -- If more than one matching specialization is found, the |
| 3125 | // partial order rules (14.5.4.2) are used to determine |
| 3126 | // whether one of the specializations is more specialized |
| 3127 | // than the others. If none of the specializations is more |
| 3128 | // specialized than all of the other matching |
| 3129 | // specializations, then the use of the variable template is |
| 3130 | // ambiguous and the program is ill-formed. |
| 3131 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 3132 | PEnd = Matched.end(); |
| 3133 | P != PEnd; ++P) { |
| 3134 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 3135 | PointOfInstantiation) == |
| 3136 | P->Partial) |
| 3137 | Best = P; |
| 3138 | } |
| 3139 | |
| 3140 | // Determine if the best partial specialization is more specialized than |
| 3141 | // the others. |
| 3142 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 3143 | PEnd = Matched.end(); |
| 3144 | P != PEnd; ++P) { |
| 3145 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 3146 | P->Partial, Best->Partial, |
| 3147 | PointOfInstantiation) != Best->Partial) { |
| 3148 | AmbiguousPartialSpec = true; |
| 3149 | break; |
| 3150 | } |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | // Instantiate using the best variable template partial specialization. |
| 3155 | InstantiationPattern = Best->Partial; |
| 3156 | InstantiationArgs = Best->Args; |
| 3157 | } else { |
| 3158 | // -- If no match is found, the instantiation is generated |
| 3159 | // from the primary template. |
| 3160 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 3161 | } |
| 3162 | } |
| 3163 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3164 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3165 | // Note that we do not instantiate a definition until we see an odr-use |
| 3166 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3167 | // FIXME: LateAttrs et al.? |
| 3168 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 3169 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 3170 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 3171 | if (!Decl) |
| 3172 | return true; |
| 3173 | |
| 3174 | if (AmbiguousPartialSpec) { |
| 3175 | // Partial ordering did not produce a clear winner. Complain. |
| 3176 | Decl->setInvalidDecl(); |
| 3177 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 3178 | << Decl; |
| 3179 | |
| 3180 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 3181 | for (MatchResult P : Matched) |
| 3182 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 3183 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 3184 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3185 | return true; |
| 3186 | } |
| 3187 | |
| 3188 | if (VarTemplatePartialSpecializationDecl *D = |
| 3189 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 3190 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 3191 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3192 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 3193 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3194 | assert(Decl && "No variable template specialization?"); |
| 3195 | return Decl; |
| 3196 | } |
| 3197 | |
| 3198 | ExprResult |
| 3199 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 3200 | const DeclarationNameInfo &NameInfo, |
| 3201 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3202 | const TemplateArgumentListInfo *TemplateArgs) { |
| 3203 | |
| 3204 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 3205 | *TemplateArgs); |
| 3206 | if (Decl.isInvalid()) |
| 3207 | return ExprError(); |
| 3208 | |
| 3209 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 3210 | if (!Var->getTemplateSpecializationKind()) |
| 3211 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 3212 | NameInfo.getLoc()); |
| 3213 | |
| 3214 | // Build an ordinary singleton decl ref. |
| 3215 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3216 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3219 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3220 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3221 | LookupResult &R, |
| 3222 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3223 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3224 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 3225 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3226 | // 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] | 3227 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3228 | // foo<int> could identify a single function unambiguously |
| 3229 | // This approach does NOT work, since f<int>(1); |
| 3230 | // gets resolved prior to resorting to overload resolution |
| 3231 | // i.e., template<class T> void f(double); |
| 3232 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3233 | |
| 3234 | // These should be filtered out by our callers. |
| 3235 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 3236 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 3237 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3238 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 3239 | bool InstantiationDependent; |
| 3240 | if (R.getAsSingle<VarTemplateDecl>() && |
| 3241 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3242 | *TemplateArgs, InstantiationDependent)) { |
| 3243 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 3244 | R.getAsSingle<VarTemplateDecl>(), |
| 3245 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3248 | // We don't want lookup warnings at this point. |
| 3249 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3250 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3251 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3252 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3253 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3254 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3255 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3256 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3257 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3258 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3259 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3262 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3263 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3264 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3265 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3266 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3267 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3268 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3269 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3270 | DeclContext *DC; |
| 3271 | if (!(DC = computeDeclContext(SS, false)) || |
| 3272 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3273 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 3274 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3275 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3276 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3277 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3278 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3279 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3280 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3281 | if (R.isAmbiguous()) |
| 3282 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3283 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3284 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3285 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 3286 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3287 | return ExprError(); |
| 3288 | } |
| 3289 | |
| 3290 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3291 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3292 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 3293 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3294 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 3295 | return ExprError(); |
| 3296 | } |
| 3297 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3298 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3301 | /// \brief Form a dependent template name. |
| 3302 | /// |
| 3303 | /// This action forms a dependent template name given the template |
| 3304 | /// name and its (presumably dependent) scope specifier. For |
| 3305 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 3306 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 3307 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3308 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3309 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3310 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3311 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3312 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3313 | bool EnteringContext, |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 3314 | TemplateTy &Result, |
| 3315 | bool AllowInjectedClassName) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3316 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 3317 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3318 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3319 | diag::warn_cxx98_compat_template_outside_of_template : |
| 3320 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3321 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 3322 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3323 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3324 | if (SS.isSet()) |
| 3325 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 3326 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3327 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3328 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3329 | // C++0x [temp.names]p5: |
| 3330 | // If a name prefixed by the keyword template is not the name of |
| 3331 | // a template, the program is ill-formed. [Note: the keyword |
| 3332 | // template may not be applied to non-template members of class |
| 3333 | // templates. -end note ] [ Note: as is the case with the |
| 3334 | // typename prefix, the template prefix is allowed in cases |
| 3335 | // where it is not strictly necessary; i.e., when the |
| 3336 | // nested-name-specifier or the expression on the left of the -> |
| 3337 | // or . is not dependent on a template-parameter, or the use |
| 3338 | // does not appear in the scope of a template. -end note] |
| 3339 | // |
| 3340 | // Note: C++03 was more strict here, because it banned the use of |
| 3341 | // the "template" keyword prior to a template-name that was not a |
| 3342 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3343 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3344 | // 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] | 3345 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3346 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3347 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3348 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3349 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3350 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3351 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3352 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3353 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3354 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3355 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3356 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3357 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3358 | << Name.getSourceRange() |
| 3359 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3360 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3361 | } else { |
| 3362 | // We found something; return it. |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 3363 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
| 3364 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && |
| 3365 | Name.getKind() == UnqualifiedId::IK_Identifier && Name.Identifier && |
| 3366 | LookupRD->getIdentifier() == Name.Identifier) { |
| 3367 | // C++14 [class.qual]p2: |
| 3368 | // In a lookup in which function names are not ignored and the |
| 3369 | // nested-name-specifier nominates a class C, if the name specified |
| 3370 | // [...] is the injected-class-name of C, [...] the name is instead |
| 3371 | // considered to name the constructor |
| 3372 | // |
| 3373 | // We don't get here if naming the constructor would be valid, so we |
| 3374 | // just reject immediately and recover by treating the |
| 3375 | // injected-class-name as naming the template. |
| 3376 | Diag(Name.getLocStart(), |
| 3377 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 3378 | << Name.Identifier << 0 /*injected-class-name used as template name*/ |
| 3379 | << 1 /*'template' keyword was used*/; |
| 3380 | } |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3381 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3382 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3385 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3386 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3387 | switch (Name.getKind()) { |
| 3388 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3389 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3390 | Name.Identifier)); |
| 3391 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3393 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3394 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3395 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3396 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3397 | |
| 3398 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3399 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3401 | default: |
| 3402 | break; |
| 3403 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3404 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3405 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3406 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3407 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3408 | << Name.getSourceRange() |
| 3409 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3410 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3414 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3415 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3416 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3417 | QualType ArgType; |
| 3418 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3419 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3420 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3421 | switch(Arg.getKind()) { |
| 3422 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3423 | // C++ [temp.arg.type]p1: |
| 3424 | // A template-argument for a template-parameter which is a |
| 3425 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3426 | ArgType = Arg.getAsType(); |
| 3427 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3428 | break; |
| 3429 | case TemplateArgument::Template: { |
| 3430 | // We have a template type parameter but the template argument |
| 3431 | // is a template without any arguments. |
| 3432 | SourceRange SR = AL.getSourceRange(); |
| 3433 | TemplateName Name = Arg.getAsTemplate(); |
| 3434 | Diag(SR.getBegin(), diag::err_template_missing_args) |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 3435 | << (int)getTemplateNameKindForDiagnostics(Name) << Name << SR; |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3436 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3437 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3438 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3439 | return true; |
| 3440 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3441 | case TemplateArgument::Expression: { |
| 3442 | // We have a template type parameter but the template argument is an |
| 3443 | // expression; see if maybe it is missing the "typename" keyword. |
| 3444 | CXXScopeSpec SS; |
| 3445 | DeclarationNameInfo NameInfo; |
| 3446 | |
| 3447 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3448 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3449 | NameInfo = ArgExpr->getNameInfo(); |
| 3450 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3451 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3452 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3453 | NameInfo = ArgExpr->getNameInfo(); |
| 3454 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3455 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3456 | if (ArgExpr->isImplicitAccess()) { |
| 3457 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3458 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3459 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3462 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3463 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3464 | LookupParsedName(Result, CurScope, &SS); |
| 3465 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3466 | if (Result.getAsSingle<TypeDecl>() || |
| 3467 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3468 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3469 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3470 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3471 | Diag(Loc, getLangOpts().MSVCCompat |
| 3472 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3473 | : diag::err_template_arg_must_be_type_suggest) |
| 3474 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3475 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3476 | |
| 3477 | // Recover by synthesizing a type using the location information that we |
| 3478 | // already have. |
| 3479 | ArgType = |
| 3480 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3481 | TypeLocBuilder TLB; |
| 3482 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3483 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3484 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3485 | TL.setNameLoc(NameInfo.getLoc()); |
| 3486 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3487 | |
| 3488 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3489 | // properly. |
| 3490 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3491 | TemplateArgumentLocInfo(TSI)); |
| 3492 | |
| 3493 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3494 | } |
| 3495 | } |
| 3496 | // fallthrough |
| 3497 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3498 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3499 | // We have a template type parameter but the template argument |
| 3500 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3501 | SourceRange SR = AL.getSourceRange(); |
| 3502 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3503 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3505 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3506 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3507 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3508 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3509 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3510 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3511 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3512 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3513 | ArgType = Context.getCanonicalType(ArgType); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3514 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3515 | // Objective-C ARC: |
| 3516 | // If an explicitly-specified template argument type is a lifetime type |
| 3517 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3518 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3519 | ArgType->isObjCLifetimeType() && |
| 3520 | !ArgType.getObjCLifetime()) { |
| 3521 | Qualifiers Qs; |
| 3522 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3523 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3524 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3525 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3526 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3527 | return false; |
| 3528 | } |
| 3529 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3530 | /// \brief Substitute template arguments into the default template argument for |
| 3531 | /// the given template type parameter. |
| 3532 | /// |
| 3533 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3534 | /// the substitution. |
| 3535 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3536 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3537 | /// for. |
| 3538 | /// |
| 3539 | /// \param TemplateLoc the location of the template name that started the |
| 3540 | /// template-id we are checking. |
| 3541 | /// |
| 3542 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3543 | /// terminates the template-id. |
| 3544 | /// |
| 3545 | /// \param Param the template template parameter whose default we are |
| 3546 | /// substituting into. |
| 3547 | /// |
| 3548 | /// \param Converted the list of template arguments provided for template |
| 3549 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3550 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3551 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3552 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3553 | TemplateDecl *Template, |
| 3554 | SourceLocation TemplateLoc, |
| 3555 | SourceLocation RAngleLoc, |
| 3556 | TemplateTypeParmDecl *Param, |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 3557 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3558 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3559 | |
| 3560 | // If the argument type is dependent, instantiate it now based |
| 3561 | // on the previously-computed template arguments. |
| 3562 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3563 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3564 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3565 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3566 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3567 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3568 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3569 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3570 | |
| 3571 | // Only substitute for the innermost template argument list. |
| 3572 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3573 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3574 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3575 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3576 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3577 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3578 | ArgType = |
| 3579 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3580 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | return ArgType; |
| 3584 | } |
| 3585 | |
| 3586 | /// \brief Substitute template arguments into the default template argument for |
| 3587 | /// the given non-type template parameter. |
| 3588 | /// |
| 3589 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3590 | /// the substitution. |
| 3591 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3592 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3593 | /// for. |
| 3594 | /// |
| 3595 | /// \param TemplateLoc the location of the template name that started the |
| 3596 | /// template-id we are checking. |
| 3597 | /// |
| 3598 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3599 | /// terminates the template-id. |
| 3600 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3601 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3602 | /// substituting into. |
| 3603 | /// |
| 3604 | /// \param Converted the list of template arguments provided for template |
| 3605 | /// parameters that precede \p Param in the template parameter list. |
| 3606 | /// |
| 3607 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3608 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3609 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3610 | TemplateDecl *Template, |
| 3611 | SourceLocation TemplateLoc, |
| 3612 | SourceLocation RAngleLoc, |
| 3613 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3614 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3615 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3616 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3617 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3618 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3619 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3620 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3621 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3622 | |
| 3623 | // Only substitute for the innermost template argument list. |
| 3624 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3625 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3626 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3627 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3628 | |
Faisal Vali | 48401eb | 2015-11-19 19:20:17 +0000 | [diff] [blame] | 3629 | EnterExpressionEvaluationContext ConstantEvaluated(SemaRef, |
| 3630 | Sema::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3631 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3634 | /// \brief Substitute template arguments into the default template argument for |
| 3635 | /// the given template template parameter. |
| 3636 | /// |
| 3637 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3638 | /// the substitution. |
| 3639 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3640 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3641 | /// for. |
| 3642 | /// |
| 3643 | /// \param TemplateLoc the location of the template name that started the |
| 3644 | /// template-id we are checking. |
| 3645 | /// |
| 3646 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3647 | /// terminates the template-id. |
| 3648 | /// |
| 3649 | /// \param Param the template template parameter whose default we are |
| 3650 | /// substituting into. |
| 3651 | /// |
| 3652 | /// \param Converted the list of template arguments provided for template |
| 3653 | /// parameters that precede \p Param in the template parameter list. |
| 3654 | /// |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3655 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3656 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3657 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3658 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3659 | static TemplateName |
| 3660 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3661 | TemplateDecl *Template, |
| 3662 | SourceLocation TemplateLoc, |
| 3663 | SourceLocation RAngleLoc, |
| 3664 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3665 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3666 | NestedNameSpecifierLoc &QualifierLoc) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3667 | Sema::InstantiatingTemplate Inst( |
| 3668 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 3669 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3670 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3671 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3672 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3673 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3674 | |
| 3675 | // Only substitute for the innermost template argument list. |
| 3676 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3677 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3678 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3679 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3680 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3681 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3682 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3683 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3684 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3685 | QualifierLoc = |
| 3686 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3687 | if (!QualifierLoc) |
| 3688 | return TemplateName(); |
| 3689 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3690 | |
| 3691 | return SemaRef.SubstTemplateName( |
| 3692 | QualifierLoc, |
| 3693 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3694 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3695 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3698 | /// \brief If the given template parameter has a default template |
| 3699 | /// argument, substitute into that default template argument and |
| 3700 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3701 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3702 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3703 | SourceLocation TemplateLoc, |
| 3704 | SourceLocation RAngleLoc, |
| 3705 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3706 | SmallVectorImpl<TemplateArgument> |
| 3707 | &Converted, |
| 3708 | bool &HasDefaultArg) { |
| 3709 | HasDefaultArg = false; |
| 3710 | |
| 3711 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3712 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3713 | return TemplateArgumentLoc(); |
| 3714 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3715 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3716 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3717 | TemplateLoc, |
| 3718 | RAngleLoc, |
| 3719 | TypeParm, |
| 3720 | Converted); |
| 3721 | if (DI) |
| 3722 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3723 | |
| 3724 | return TemplateArgumentLoc(); |
| 3725 | } |
| 3726 | |
| 3727 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3728 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3729 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3730 | return TemplateArgumentLoc(); |
| 3731 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3732 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3733 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3734 | TemplateLoc, |
| 3735 | RAngleLoc, |
| 3736 | NonTypeParm, |
| 3737 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3738 | if (Arg.isInvalid()) |
| 3739 | return TemplateArgumentLoc(); |
| 3740 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3741 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3742 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3743 | } |
| 3744 | |
| 3745 | TemplateTemplateParmDecl *TempTempParm |
| 3746 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3747 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3748 | return TemplateArgumentLoc(); |
| 3749 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3750 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3751 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3752 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3753 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3754 | RAngleLoc, |
| 3755 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3756 | Converted, |
| 3757 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3758 | if (TName.isNull()) |
| 3759 | return TemplateArgumentLoc(); |
| 3760 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3761 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3762 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3763 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3764 | } |
| 3765 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 3766 | /// Convert a template-argument that we parsed as a type into a template, if |
| 3767 | /// possible. C++ permits injected-class-names to perform dual service as |
| 3768 | /// template template arguments and as template type arguments. |
| 3769 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { |
| 3770 | // Extract and step over any surrounding nested-name-specifier. |
| 3771 | NestedNameSpecifierLoc QualLoc; |
| 3772 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { |
| 3773 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
| 3774 | return TemplateArgumentLoc(); |
| 3775 | |
| 3776 | QualLoc = ETLoc.getQualifierLoc(); |
| 3777 | TLoc = ETLoc.getNamedTypeLoc(); |
| 3778 | } |
| 3779 | |
| 3780 | // If this type was written as an injected-class-name, it can be used as a |
| 3781 | // template template argument. |
| 3782 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) |
| 3783 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), |
| 3784 | QualLoc, InjLoc.getNameLoc()); |
| 3785 | |
| 3786 | // If this type was written as an injected-class-name, it may have been |
| 3787 | // converted to a RecordType during instantiation. If the RecordType is |
| 3788 | // *not* wrapped in a TemplateSpecializationType and denotes a class |
| 3789 | // template specialization, it must have come from an injected-class-name. |
| 3790 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) |
| 3791 | if (auto *CTSD = |
| 3792 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
| 3793 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), |
| 3794 | QualLoc, RecLoc.getNameLoc()); |
| 3795 | |
| 3796 | return TemplateArgumentLoc(); |
| 3797 | } |
| 3798 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3799 | /// \brief Check that the given template argument corresponds to the given |
| 3800 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3801 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3802 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3803 | /// checked. |
| 3804 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3805 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3806 | /// |
| 3807 | /// \param Template The template in which the template argument resides. |
| 3808 | /// |
| 3809 | /// \param TemplateLoc The location of the template name for the template |
| 3810 | /// whose argument list we're matching. |
| 3811 | /// |
| 3812 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3813 | /// the template argument list. |
| 3814 | /// |
| 3815 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3816 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3817 | /// |
| 3818 | /// \param Converted The checked, converted argument will be added to the |
| 3819 | /// end of this small vector. |
| 3820 | /// |
| 3821 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3822 | /// explicitly written, deduced, etc. |
| 3823 | /// |
| 3824 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3825 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3826 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3827 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3828 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3829 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3830 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3831 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3832 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3833 | // Check template type parameters. |
| 3834 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3835 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3836 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3837 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3838 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3839 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3840 | // with the template arguments we've seen thus far. But if the |
| 3841 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3842 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3843 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3844 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3845 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3846 | if (NTTPType->isDependentType() && |
| 3847 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3848 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3849 | // Do substitution on the type of the non-type template parameter. |
| 3850 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3851 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3852 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3853 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3854 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3855 | |
| 3856 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3857 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3858 | NTTPType = SubstType(NTTPType, |
| 3859 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3860 | NTTP->getLocation(), |
| 3861 | NTTP->getDeclName()); |
| 3862 | // If that worked, check the non-type template parameter type |
| 3863 | // for validity. |
| 3864 | if (!NTTPType.isNull()) |
| 3865 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3866 | NTTP->getLocation()); |
| 3867 | if (NTTPType.isNull()) |
| 3868 | return true; |
| 3869 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3870 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3871 | switch (Arg.getArgument().getKind()) { |
| 3872 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3873 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3874 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3875 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3876 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3877 | ExprResult Res = |
| 3878 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3879 | Result, CTAK); |
| 3880 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3881 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3882 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3883 | // If the resulting expression is new, then use it in place of the |
| 3884 | // old expression in the template argument. |
| 3885 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3886 | TemplateArgument TA(Res.get()); |
| 3887 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3888 | } |
| 3889 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3890 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3891 | break; |
| 3892 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3893 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3894 | case TemplateArgument::Declaration: |
| 3895 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3896 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3897 | // We've already checked this template argument, so just copy |
| 3898 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3899 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3900 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3901 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3902 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3903 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3904 | // We were given a template template argument. It may not be ill-formed; |
| 3905 | // see below. |
| 3906 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3907 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3908 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3909 | // We have a template argument such as \c T::template X, which we |
| 3910 | // parsed as a template template argument. However, since we now |
| 3911 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3912 | // template name into an expression. |
| 3913 | |
| 3914 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3915 | Arg.getTemplateNameLoc()); |
| 3916 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3917 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3918 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3919 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3920 | // so it was provided with a template keyword. However, its source |
| 3921 | // location is not stored in the template argument structure. |
| 3922 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3923 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3924 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3925 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3926 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3927 | // If we parsed the template argument as a pack expansion, create a |
| 3928 | // pack expansion expression. |
| 3929 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3930 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3931 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3932 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3933 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3934 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3935 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3936 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3937 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3938 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3939 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3940 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3941 | break; |
| 3942 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3943 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3944 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3945 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3946 | // therefore cannot be a non-type template argument. |
| 3947 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3948 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3950 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3951 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3952 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3953 | case TemplateArgument::Type: { |
| 3954 | // We have a non-type template parameter but the template |
| 3955 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3956 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3957 | // C++ [temp.arg]p2: |
| 3958 | // In a template-argument, an ambiguity between a type-id and |
| 3959 | // an expression is resolved to a type-id, regardless of the |
| 3960 | // form of the corresponding template-parameter. |
| 3961 | // |
| 3962 | // We warn specifically about this case, since it can be rather |
| 3963 | // confusing for users. |
| 3964 | QualType T = Arg.getArgument().getAsType(); |
| 3965 | SourceRange SR = Arg.getSourceRange(); |
| 3966 | if (T->isFunctionType()) |
| 3967 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3968 | else |
| 3969 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3970 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3971 | return true; |
| 3972 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3973 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3974 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3975 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3976 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3977 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3978 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3982 | // Check template template parameters. |
| 3983 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3984 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3985 | // Substitute into the template parameter list of the template |
| 3986 | // template parameter, since previously-supplied template arguments |
| 3987 | // may appear within the template template parameter. |
| 3988 | { |
| 3989 | // Set up a template instantiation context. |
| 3990 | LocalInstantiationScope Scope(*this); |
| 3991 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3992 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3993 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3994 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3995 | return true; |
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); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3998 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3999 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4000 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 4001 | if (!TempParm) |
| 4002 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4003 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4004 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4005 | // C++1z [temp.local]p1: (DR1004) |
| 4006 | // When [the injected-class-name] is used [...] as a template-argument for |
| 4007 | // a template template-parameter [...] it refers to the class template |
| 4008 | // itself. |
| 4009 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { |
| 4010 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
| 4011 | Arg.getTypeSourceInfo()->getTypeLoc()); |
| 4012 | if (!ConvertedArg.getArgument().isNull()) |
| 4013 | Arg = ConvertedArg; |
| 4014 | } |
| 4015 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4016 | switch (Arg.getArgument().getKind()) { |
| 4017 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4018 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4019 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4020 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4021 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4022 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4023 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4024 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4025 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4026 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4027 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4028 | case TemplateArgument::Expression: |
| 4029 | case TemplateArgument::Type: |
| 4030 | // We have a template template parameter but the template |
| 4031 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4032 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4033 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4034 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4035 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4036 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4037 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4038 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4039 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4040 | case TemplateArgument::NullPtr: |
| 4041 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4042 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4043 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4044 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4045 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4046 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4047 | return false; |
| 4048 | } |
| 4049 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4050 | /// \brief Diagnose an arity mismatch in the |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4051 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 4052 | SourceLocation TemplateLoc, |
| 4053 | TemplateArgumentListInfo &TemplateArgs) { |
| 4054 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 4055 | unsigned NumParams = Params->size(); |
| 4056 | unsigned NumArgs = TemplateArgs.size(); |
| 4057 | |
| 4058 | SourceRange Range; |
| 4059 | if (NumArgs > NumParams) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4060 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4061 | TemplateArgs.getRAngleLoc()); |
| 4062 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 4063 | << (NumArgs > NumParams) |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 4064 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4065 | << Template << Range; |
| 4066 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 4067 | << Params->getSourceRange(); |
| 4068 | return true; |
| 4069 | } |
| 4070 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4071 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 4072 | /// determine the number of parameters produced by that expansion. For instance: |
| 4073 | /// |
| 4074 | /// \code |
| 4075 | /// template<typename ...Ts> struct A { |
| 4076 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 4077 | /// }; |
| 4078 | /// \endcode |
| 4079 | /// |
| 4080 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 4081 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4082 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4083 | if (NonTypeTemplateParmDecl *NTTP |
| 4084 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4085 | if (NTTP->isExpandedParameterPack()) |
| 4086 | return NTTP->getNumExpansionTypes(); |
| 4087 | } |
| 4088 | |
| 4089 | if (TemplateTemplateParmDecl *TTP |
| 4090 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 4091 | if (TTP->isExpandedParameterPack()) |
| 4092 | return TTP->getNumExpansionTemplateParameters(); |
| 4093 | } |
| 4094 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 4095 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4098 | /// Diagnose a missing template argument. |
| 4099 | template<typename TemplateParmDecl> |
| 4100 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 4101 | TemplateDecl *TD, |
| 4102 | const TemplateParmDecl *D, |
| 4103 | TemplateArgumentListInfo &Args) { |
| 4104 | // Dig out the most recent declaration of the template parameter; there may be |
| 4105 | // declarations of the template that are more recent than TD. |
| 4106 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 4107 | ->getTemplateParameters() |
| 4108 | ->getParam(D->getIndex())); |
| 4109 | |
| 4110 | // If there's a default argument that's not visible, diagnose that we're |
| 4111 | // missing a module import. |
| 4112 | llvm::SmallVector<Module*, 8> Modules; |
| 4113 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 4114 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 4115 | D->getDefaultArgumentLoc(), Modules, |
| 4116 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4117 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4118 | return true; |
| 4119 | } |
| 4120 | |
| 4121 | // FIXME: If there's a more recent default argument that *is* visible, |
| 4122 | // diagnose that it was declared too late. |
| 4123 | |
| 4124 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 4125 | } |
| 4126 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4127 | /// \brief Check that the given template argument list is well-formed |
| 4128 | /// for specializing the given template. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4129 | bool Sema::CheckTemplateArgumentList( |
| 4130 | TemplateDecl *Template, SourceLocation TemplateLoc, |
| 4131 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
| 4132 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4133 | bool UpdateArgsWithConversions) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4134 | // Make a copy of the template arguments for processing. Only make the |
| 4135 | // changes at the end when successful in matching the arguments to the |
| 4136 | // template. |
| 4137 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 4138 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4139 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4140 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4141 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4142 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4143 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4144 | // [...] The type and form of each template-argument specified in |
| 4145 | // a template-id shall match the type and form specified for the |
| 4146 | // corresponding parameter declared by the template in its |
| 4147 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4148 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4149 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4150 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4151 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4152 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4153 | ParamEnd = Params->end(); |
| 4154 | Param != ParamEnd; /* increment in loop */) { |
| 4155 | // If we have an expanded parameter pack, make sure we don't have too |
| 4156 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4157 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4158 | if (*Expansions == ArgumentPack.size()) { |
| 4159 | // We're done with this parameter pack. Pack up its arguments and add |
| 4160 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4161 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4162 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4163 | ArgumentPack.clear(); |
| 4164 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4165 | // This argument is assigned to the next parameter. |
| 4166 | ++Param; |
| 4167 | continue; |
| 4168 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 4169 | // Not enough arguments for this parameter pack. |
| 4170 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 4171 | << false |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 4172 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4173 | << Template; |
| 4174 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 4175 | << Params->getSourceRange(); |
| 4176 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4177 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4178 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4179 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4180 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4181 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4182 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4183 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4184 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4185 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4186 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4187 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4188 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4189 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 4190 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4191 | // 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] | 4192 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4193 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4194 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4195 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4196 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4197 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 4198 | return true; |
| 4199 | } |
| 4200 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4201 | // We're now done with this argument. |
| 4202 | ++ArgIdx; |
| 4203 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4204 | if ((*Param)->isTemplateParameterPack()) { |
| 4205 | // The template parameter was a template parameter pack, so take the |
| 4206 | // deduced argument and place it on the argument pack. Note that we |
| 4207 | // stay on the same template parameter so that we can deduce more |
| 4208 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 4209 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4210 | } else { |
| 4211 | // Move to the next template parameter. |
| 4212 | ++Param; |
| 4213 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4214 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4215 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 4216 | // the remaining arguments, because we don't know what parameters they'll |
| 4217 | // match up with. |
| 4218 | if (PackExpansionIntoNonPack) { |
| 4219 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4220 | // If we were part way through filling in an expanded parameter pack, |
| 4221 | // fall back to just producing individual arguments. |
| 4222 | Converted.insert(Converted.end(), |
| 4223 | ArgumentPack.begin(), ArgumentPack.end()); |
| 4224 | ArgumentPack.clear(); |
| 4225 | } |
| 4226 | |
| 4227 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4228 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4229 | ++ArgIdx; |
| 4230 | } |
| 4231 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4232 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4233 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4234 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4235 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4236 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4237 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 4238 | // If we're checking a partial template argument list, we're done. |
| 4239 | if (PartialTemplateArgs) { |
| 4240 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4241 | Converted.push_back( |
| 4242 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 4243 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4244 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4247 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4248 | // 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] | 4249 | if ((*Param)->isTemplateParameterPack()) { |
| 4250 | assert(!getExpandedPackSize(*Param) && |
| 4251 | "Should have dealt with this already"); |
| 4252 | |
| 4253 | // A non-expanded parameter pack before the end of the parameter list |
| 4254 | // only occurs for an ill-formed template parameter list, unless we've |
| 4255 | // got a partial argument list for a function template, so just bail out. |
| 4256 | if (Param + 1 != ParamEnd) |
| 4257 | return true; |
| 4258 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4259 | Converted.push_back( |
| 4260 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4261 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4262 | |
| 4263 | ++Param; |
| 4264 | continue; |
| 4265 | } |
| 4266 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4267 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4268 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4269 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4270 | // Retrieve the default template argument from the template |
| 4271 | // parameter. For each kind of template parameter, we substitute the |
| 4272 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4274 | // the default argument. |
| 4275 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4276 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4277 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 4278 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4279 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4280 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4281 | Template, |
| 4282 | TemplateLoc, |
| 4283 | RAngleLoc, |
| 4284 | TTP, |
| 4285 | Converted); |
| 4286 | if (!ArgType) |
| 4287 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4288 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4289 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 4290 | ArgType); |
| 4291 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4292 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4293 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4294 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 4295 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4296 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4297 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4298 | TemplateLoc, |
| 4299 | RAngleLoc, |
| 4300 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4301 | Converted); |
| 4302 | if (E.isInvalid()) |
| 4303 | return true; |
| 4304 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4305 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4306 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 4307 | } else { |
| 4308 | TemplateTemplateParmDecl *TempParm |
| 4309 | = cast<TemplateTemplateParmDecl>(*Param); |
| 4310 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4311 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4312 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 4313 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4314 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4315 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4316 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4317 | TemplateLoc, |
| 4318 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4319 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4320 | Converted, |
| 4321 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4322 | if (Name.isNull()) |
| 4323 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4324 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4325 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 4326 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4327 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4329 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4330 | // the default template argument. We're not actually instantiating a |
| 4331 | // template here, we just create this object to put a note into the |
| 4332 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4333 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 4334 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4335 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4336 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4337 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4338 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4339 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4340 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4341 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4342 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4343 | // Core issue 150 (assumed resolution): if this is a template template |
| 4344 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4345 | // template definition. |
| 4346 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4347 | NewArgs.addArgument(Arg); |
| 4348 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4349 | // Move to the next template parameter and argument. |
| 4350 | ++Param; |
| 4351 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4352 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4353 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4354 | // If we're performing a partial argument substitution, allow any trailing |
| 4355 | // pack expansions; they might be empty. This can happen even if |
| 4356 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 4357 | // still dependent). |
| 4358 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 4359 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4360 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 4361 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4364 | // If we have any leftover arguments, then there were too many arguments. |
| 4365 | // Complain and fail. |
| 4366 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4367 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 4368 | |
| 4369 | // No problems found with the new argument list, propagate changes back |
| 4370 | // to caller. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4371 | if (UpdateArgsWithConversions) |
| 4372 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4373 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4374 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4377 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 | class UnnamedLocalNoLinkageFinder |
| 4379 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4380 | { |
| 4381 | Sema &S; |
| 4382 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4383 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4384 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4385 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4386 | public: |
| 4387 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 4388 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4389 | bool Visit(QualType T) { |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 4390 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4391 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4392 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4393 | #define TYPE(Class, Parent) \ |
| 4394 | bool Visit##Class##Type(const Class##Type *); |
| 4395 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4396 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4397 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4398 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4399 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4400 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4401 | bool VisitTagDecl(const TagDecl *Tag); |
| 4402 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4403 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4404 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4405 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4406 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4407 | return false; |
| 4408 | } |
| 4409 | |
| 4410 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4411 | return Visit(T->getElementType()); |
| 4412 | } |
| 4413 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4414 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4415 | return Visit(T->getPointeeType()); |
| 4416 | } |
| 4417 | |
| 4418 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4419 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4420 | return Visit(T->getPointeeType()); |
| 4421 | } |
| 4422 | |
| 4423 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4425 | return Visit(T->getPointeeType()); |
| 4426 | } |
| 4427 | |
| 4428 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4430 | return Visit(T->getPointeeType()); |
| 4431 | } |
| 4432 | |
| 4433 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4434 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4435 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4436 | } |
| 4437 | |
| 4438 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4439 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4440 | return Visit(T->getElementType()); |
| 4441 | } |
| 4442 | |
| 4443 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4444 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4445 | return Visit(T->getElementType()); |
| 4446 | } |
| 4447 | |
| 4448 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4449 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4450 | return Visit(T->getElementType()); |
| 4451 | } |
| 4452 | |
| 4453 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4454 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4455 | return Visit(T->getElementType()); |
| 4456 | } |
| 4457 | |
| 4458 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4459 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4460 | return Visit(T->getElementType()); |
| 4461 | } |
| 4462 | |
| 4463 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4464 | return Visit(T->getElementType()); |
| 4465 | } |
| 4466 | |
| 4467 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4468 | return Visit(T->getElementType()); |
| 4469 | } |
| 4470 | |
| 4471 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4472 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4473 | for (const auto &A : T->param_types()) { |
| 4474 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4475 | return true; |
| 4476 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4477 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4478 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4482 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4483 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4487 | const UnresolvedUsingType*) { |
| 4488 | return false; |
| 4489 | } |
| 4490 | |
| 4491 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4492 | return false; |
| 4493 | } |
| 4494 | |
| 4495 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4496 | return Visit(T->getUnderlyingType()); |
| 4497 | } |
| 4498 | |
| 4499 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4500 | return false; |
| 4501 | } |
| 4502 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4503 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4504 | const UnaryTransformType*) { |
| 4505 | return false; |
| 4506 | } |
| 4507 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4508 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4509 | return Visit(T->getDeducedType()); |
| 4510 | } |
| 4511 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 4512 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
| 4513 | const DeducedTemplateSpecializationType *T) { |
| 4514 | return Visit(T->getDeducedType()); |
| 4515 | } |
| 4516 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4517 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4518 | return VisitTagDecl(T->getDecl()); |
| 4519 | } |
| 4520 | |
| 4521 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4522 | return VisitTagDecl(T->getDecl()); |
| 4523 | } |
| 4524 | |
| 4525 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4526 | const TemplateTypeParmType*) { |
| 4527 | return false; |
| 4528 | } |
| 4529 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4530 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4531 | const SubstTemplateTypeParmPackType *) { |
| 4532 | return false; |
| 4533 | } |
| 4534 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4535 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4536 | const TemplateSpecializationType*) { |
| 4537 | return false; |
| 4538 | } |
| 4539 | |
| 4540 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4541 | const InjectedClassNameType* T) { |
| 4542 | return VisitTagDecl(T->getDecl()); |
| 4543 | } |
| 4544 | |
| 4545 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4546 | const DependentNameType* T) { |
| 4547 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4548 | } |
| 4549 | |
| 4550 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4551 | const DependentTemplateSpecializationType* T) { |
| 4552 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4553 | } |
| 4554 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4555 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4556 | const PackExpansionType* T) { |
| 4557 | return Visit(T->getPattern()); |
| 4558 | } |
| 4559 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4560 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4561 | return false; |
| 4562 | } |
| 4563 | |
| 4564 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4565 | const ObjCInterfaceType *) { |
| 4566 | return false; |
| 4567 | } |
| 4568 | |
| 4569 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4570 | const ObjCObjectPointerType *) { |
| 4571 | return false; |
| 4572 | } |
| 4573 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4574 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4575 | return Visit(T->getValueType()); |
| 4576 | } |
| 4577 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4578 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 4579 | return false; |
| 4580 | } |
| 4581 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4582 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4583 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4584 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4585 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4586 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4587 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4588 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4589 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4592 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4593 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4594 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4595 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4596 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4597 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4598 | return true; |
| 4599 | } |
| 4600 | |
| 4601 | return false; |
| 4602 | } |
| 4603 | |
| 4604 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4605 | NestedNameSpecifier *NNS) { |
| 4606 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4607 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4608 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4609 | switch (NNS->getKind()) { |
| 4610 | case NestedNameSpecifier::Identifier: |
| 4611 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4612 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4613 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4614 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4615 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4616 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4617 | case NestedNameSpecifier::TypeSpec: |
| 4618 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4619 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4620 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4621 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4624 | /// \brief Check a template argument against its corresponding |
| 4625 | /// template type parameter. |
| 4626 | /// |
| 4627 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4628 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4629 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4630 | TypeSourceInfo *ArgInfo) { |
| 4631 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4632 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4633 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4634 | |
| 4635 | if (Arg->isVariablyModifiedType()) { |
| 4636 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4637 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4638 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4641 | // C++03 [temp.arg.type]p2: |
| 4642 | // A local type, a type with no linkage, an unnamed type or a type |
| 4643 | // compounded from any of these types shall not be used as a |
| 4644 | // template-argument for a template type-parameter. |
| 4645 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4646 | // 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] | 4647 | // a warning. |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 4648 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4649 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4650 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4651 | } |
| 4652 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4653 | return false; |
| 4654 | } |
| 4655 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4656 | enum NullPointerValueKind { |
| 4657 | NPV_NotNullPointer, |
| 4658 | NPV_NullPointer, |
| 4659 | NPV_Error |
| 4660 | }; |
| 4661 | |
| 4662 | /// \brief Determine whether the given template argument is a null pointer |
| 4663 | /// value of the appropriate type. |
| 4664 | static NullPointerValueKind |
| 4665 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4666 | QualType ParamType, Expr *Arg) { |
| 4667 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4668 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4669 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4670 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 4671 | llvm_unreachable( |
| 4672 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4673 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4674 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4675 | return NPV_NotNullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4676 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4677 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4678 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4679 | if (ArgRV.isInvalid()) |
| 4680 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4681 | Arg = ArgRV.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4682 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4683 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4684 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4685 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4686 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4687 | EvalResult.HasSideEffects) { |
| 4688 | SourceLocation DiagLoc = Arg->getExprLoc(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4689 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4690 | // If our only note is the usual "invalid subexpression" note, just point |
| 4691 | // the caret at its location rather than producing an essentially |
| 4692 | // redundant note. |
| 4693 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4694 | diag::note_invalid_subexpr_in_const_expr) { |
| 4695 | DiagLoc = Notes[0].first; |
| 4696 | Notes.clear(); |
| 4697 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4698 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4699 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4700 | << Arg->getType() << Arg->getSourceRange(); |
| 4701 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4702 | S.Diag(Notes[I].first, Notes[I].second); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4703 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4704 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4705 | return NPV_Error; |
| 4706 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4707 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4708 | // C++11 [temp.arg.nontype]p1: |
| 4709 | // - an address constant expression of type std::nullptr_t |
| 4710 | if (Arg->getType()->isNullPtrType()) |
| 4711 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4712 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4713 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4714 | // - a constant expression that evaluates to a null member pointer value |
| 4715 | // (4.11); or |
| 4716 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4717 | (EvalResult.Val.isMemberPointer() && |
| 4718 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4719 | // If our expression has an appropriate type, we've succeeded. |
| 4720 | bool ObjCLifetimeConversion; |
| 4721 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4722 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4723 | ObjCLifetimeConversion)) |
| 4724 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4725 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4726 | // The types didn't match, but we know we got a null pointer; complain, |
| 4727 | // then recover as if the types were correct. |
| 4728 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4729 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4730 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4731 | return NPV_NullPointer; |
| 4732 | } |
| 4733 | |
| 4734 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4735 | // constant, suggest a cast to the appropriate type. |
| 4736 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4737 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4738 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4739 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4740 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4741 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4742 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4743 | return NPV_NullPointer; |
| 4744 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4745 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4746 | // FIXME: If we ever want to support general, address-constant expressions |
| 4747 | // as non-type template arguments, we should return the ExprResult here to |
| 4748 | // be interpreted by the caller. |
| 4749 | return NPV_NotNullPointer; |
| 4750 | } |
| 4751 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4752 | /// \brief Checks whether the given template argument is compatible with its |
| 4753 | /// template parameter. |
| 4754 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4755 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4756 | Expr *Arg, QualType ArgType) { |
| 4757 | bool ObjCLifetimeConversion; |
| 4758 | if (ParamType->isPointerType() && |
| 4759 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4760 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4761 | ObjCLifetimeConversion)) { |
| 4762 | // For pointer-to-object types, qualification conversions are |
| 4763 | // permitted. |
| 4764 | } else { |
| 4765 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4766 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4767 | // C++ [temp.arg.nontype]p5b3: |
| 4768 | // For a non-type template-parameter of type reference to |
| 4769 | // object, no conversions apply. The type referred to by the |
| 4770 | // reference may be more cv-qualified than the (otherwise |
| 4771 | // identical) type of the template- argument. The |
| 4772 | // template-parameter is bound directly to the |
| 4773 | // template-argument, which shall be an lvalue. |
| 4774 | |
| 4775 | // FIXME: Other qualifiers? |
| 4776 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4777 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4778 | |
| 4779 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4780 | S.Diag(Arg->getLocStart(), |
| 4781 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4782 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4783 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4784 | return true; |
| 4785 | } |
| 4786 | } |
| 4787 | } |
| 4788 | |
| 4789 | // At this point, the template argument refers to an object or |
| 4790 | // function with external linkage. We now need to check whether the |
| 4791 | // argument and parameter types are compatible. |
| 4792 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4793 | ParamType.getNonReferenceType())) { |
| 4794 | // We can't perform this conversion or binding. |
| 4795 | if (ParamType->isReferenceType()) |
| 4796 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4797 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4798 | else |
| 4799 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4800 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4801 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4802 | return true; |
| 4803 | } |
| 4804 | } |
| 4805 | |
| 4806 | return false; |
| 4807 | } |
| 4808 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4809 | /// \brief Checks whether the given template argument is the address |
| 4810 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4811 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4812 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4813 | NonTypeTemplateParmDecl *Param, |
| 4814 | QualType ParamType, |
| 4815 | Expr *ArgIn, |
| 4816 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4817 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4818 | Expr *Arg = ArgIn; |
| 4819 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4820 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4821 | bool AddressTaken = false; |
| 4822 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4823 | if (S.getLangOpts().MicrosoftExt) { |
| 4824 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4825 | // dereference and address-of operators. |
| 4826 | Arg = Arg->IgnoreParenCasts(); |
| 4827 | |
| 4828 | bool ExtWarnMSTemplateArg = false; |
| 4829 | UnaryOperatorKind FirstOpKind; |
| 4830 | SourceLocation FirstOpLoc; |
| 4831 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4832 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4833 | if (UnOpKind == UO_Deref) |
| 4834 | ExtWarnMSTemplateArg = true; |
| 4835 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4836 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4837 | if (!AddrOpLoc.isValid()) { |
| 4838 | FirstOpKind = UnOpKind; |
| 4839 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4840 | } |
| 4841 | } else |
| 4842 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4843 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4844 | if (FirstOpLoc.isValid()) { |
| 4845 | if (ExtWarnMSTemplateArg) |
| 4846 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4847 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4848 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4849 | if (FirstOpKind == UO_AddrOf) |
| 4850 | AddressTaken = true; |
| 4851 | else if (Arg->getType()->isPointerType()) { |
| 4852 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4853 | // constant expression. |
| 4854 | assert(FirstOpKind == UO_Deref); |
| 4855 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4856 | << Arg->getSourceRange(); |
| 4857 | } |
| 4858 | } |
| 4859 | } else { |
| 4860 | // See through any implicit casts we added to fix the type. |
| 4861 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4862 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4863 | // C++ [temp.arg.nontype]p1: |
| 4864 | // |
| 4865 | // A template-argument for a non-type, non-template |
| 4866 | // template-parameter shall be one of: [...] |
| 4867 | // |
| 4868 | // -- the address of an object or function with external |
| 4869 | // linkage, including function templates and function |
| 4870 | // template-ids but excluding non-static class members, |
| 4871 | // expressed as & id-expression where the & is optional if |
| 4872 | // the name refers to a function or array, or if the |
| 4873 | // corresponding template-parameter is a reference; or |
| 4874 | |
| 4875 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4876 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4877 | bool ExtraParens = false; |
| 4878 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4879 | if (!Invalid && !ExtraParens) { |
| 4880 | S.Diag(Arg->getLocStart(), |
| 4881 | S.getLangOpts().CPlusPlus11 |
| 4882 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4883 | : diag::ext_template_arg_extra_parens) |
| 4884 | << Arg->getSourceRange(); |
| 4885 | ExtraParens = true; |
| 4886 | } |
| 4887 | |
| 4888 | Arg = Parens->getSubExpr(); |
| 4889 | } |
| 4890 | |
| 4891 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4892 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4893 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4894 | |
| 4895 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4896 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4897 | Arg = UnOp->getSubExpr(); |
| 4898 | AddressTaken = true; |
| 4899 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4900 | } |
| 4901 | } |
| 4902 | |
| 4903 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4904 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4905 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4906 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4907 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4908 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4909 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4910 | |
| 4911 | // If our parameter has pointer type, check for a null template value. |
| 4912 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4913 | NullPointerValueKind NPV; |
| 4914 | // dllimport'd entities aren't constant but are available inside of template |
| 4915 | // arguments. |
| 4916 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4917 | NPV = NPV_NotNullPointer; |
| 4918 | else |
| 4919 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4920 | switch (NPV) { |
| 4921 | case NPV_NullPointer: |
| 4922 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4923 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4924 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4925 | return false; |
| 4926 | |
| 4927 | case NPV_Error: |
| 4928 | return true; |
| 4929 | |
| 4930 | case NPV_NotNullPointer: |
| 4931 | break; |
| 4932 | } |
| 4933 | } |
| 4934 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4935 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4936 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4937 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4938 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4939 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4940 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4941 | |
| 4942 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4943 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4944 | ArgIn, Arg, ArgType)) |
| 4945 | return true; |
| 4946 | |
| 4947 | Converted = TemplateArgument(ArgIn); |
| 4948 | return false; |
| 4949 | } |
| 4950 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4951 | if (!DRE) { |
| 4952 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4953 | << Arg->getSourceRange(); |
| 4954 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4955 | return true; |
| 4956 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4958 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4959 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4960 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4961 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4962 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4963 | return true; |
| 4964 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4965 | |
| 4966 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4967 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4968 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4969 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4970 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4971 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4972 | return true; |
| 4973 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4974 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4975 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4976 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4977 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4978 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4979 | // A non-type template argument must refer to an object or function. |
| 4980 | if (!Func && !Var) { |
| 4981 | // We found something, but we don't know specifically what it is. |
| 4982 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4983 | << Arg->getSourceRange(); |
| 4984 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4985 | return true; |
| 4986 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4987 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4988 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4989 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4990 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4991 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4992 | diag::ext_template_arg_object_internal) |
| 4993 | << !Func << Entity << Arg->getSourceRange(); |
| 4994 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4995 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4996 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4997 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4998 | << !Func << Entity << Arg->getSourceRange(); |
| 4999 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5000 | << !Func; |
| 5001 | return true; |
| 5002 | } |
| 5003 | |
| 5004 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5005 | // If the template parameter has pointer type, the function decays. |
| 5006 | if (ParamType->isPointerType() && !AddressTaken) |
| 5007 | ArgType = S.Context.getPointerType(Func->getType()); |
| 5008 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 5009 | // If we originally had an address-of operator, but the |
| 5010 | // parameter has reference type, complain and (if things look |
| 5011 | // like they will work) drop the address-of operator. |
| 5012 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 5013 | ParamType.getNonReferenceType())) { |
| 5014 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5015 | << ParamType; |
| 5016 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5017 | return true; |
| 5018 | } |
| 5019 | |
| 5020 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5021 | << ParamType |
| 5022 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5023 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5024 | |
| 5025 | ArgType = Func->getType(); |
| 5026 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5027 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5028 | // A value of reference type is not an object. |
| 5029 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5030 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5031 | diag::err_template_arg_reference_var) |
| 5032 | << Var->getType() << Arg->getSourceRange(); |
| 5033 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5034 | return true; |
| 5035 | } |
| 5036 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5037 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 5038 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 5039 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 5040 | << Arg->getSourceRange(); |
| 5041 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 5042 | return true; |
| 5043 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5044 | |
| 5045 | // If the template parameter has pointer type, we must have taken |
| 5046 | // the address of this object. |
| 5047 | if (ParamType->isReferenceType()) { |
| 5048 | if (AddressTaken) { |
| 5049 | // If we originally had an address-of operator, but the |
| 5050 | // parameter has reference type, complain and (if things look |
| 5051 | // like they will work) drop the address-of operator. |
| 5052 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 5053 | ParamType.getNonReferenceType())) { |
| 5054 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5055 | << ParamType; |
| 5056 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5057 | return true; |
| 5058 | } |
| 5059 | |
| 5060 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5061 | << ParamType |
| 5062 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5063 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5064 | |
| 5065 | ArgType = Var->getType(); |
| 5066 | } |
| 5067 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 5068 | if (Var->getType()->isArrayType()) { |
| 5069 | // Array-to-pointer decay. |
| 5070 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 5071 | } else { |
| 5072 | // If the template parameter has pointer type but the address of |
| 5073 | // this object was not taken, complain and (possibly) recover by |
| 5074 | // taking the address of the entity. |
| 5075 | ArgType = S.Context.getPointerType(Var->getType()); |
| 5076 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 5077 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 5078 | << ParamType; |
| 5079 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5080 | return true; |
| 5081 | } |
| 5082 | |
| 5083 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 5084 | << ParamType |
| 5085 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 5086 | |
| 5087 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5088 | } |
| 5089 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5090 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5091 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5092 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 5093 | Arg, ArgType)) |
| 5094 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5095 | |
| 5096 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5097 | Converted = |
| 5098 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 5099 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5100 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | /// \brief Checks whether the given template argument is a pointer to |
| 5104 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5105 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 5106 | NonTypeTemplateParmDecl *Param, |
| 5107 | QualType ParamType, |
| 5108 | Expr *&ResultArg, |
| 5109 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5110 | bool Invalid = false; |
| 5111 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5112 | // Check for a null pointer value. |
| 5113 | Expr *Arg = ResultArg; |
| 5114 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 5115 | case NPV_Error: |
| 5116 | return true; |
| 5117 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5118 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5119 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 5120 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5121 | return false; |
| 5122 | case NPV_NotNullPointer: |
| 5123 | break; |
| 5124 | } |
| 5125 | |
| 5126 | bool ObjCLifetimeConversion; |
| 5127 | if (S.IsQualificationConversion(Arg->getType(), |
| 5128 | ParamType.getNonReferenceType(), |
| 5129 | false, ObjCLifetimeConversion)) { |
| 5130 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5131 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5132 | ResultArg = Arg; |
| 5133 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 5134 | ParamType.getNonReferenceType())) { |
| 5135 | // We can't perform this conversion. |
| 5136 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 5137 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 5138 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5139 | return true; |
| 5140 | } |
| 5141 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5142 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5143 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5144 | Arg = Cast->getSubExpr(); |
| 5145 | |
| 5146 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5147 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5148 | // A template-argument for a non-type, non-template |
| 5149 | // template-parameter shall be one of: [...] |
| 5150 | // |
| 5151 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5152 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5153 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 5154 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 5155 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 5156 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5157 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5158 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5159 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5160 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5161 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 5162 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5163 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 5164 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | Arg = Parens->getSubExpr(); |
| 5168 | } |
| 5169 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5170 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5171 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5172 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5173 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5174 | // A pointer-to-member constant written &Class::member. |
| 5175 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5176 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5177 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 5178 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5179 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5180 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5181 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5182 | // A constant of pointer-to-member type. |
| 5183 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 5184 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 5185 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 5186 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5187 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5188 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5189 | } else { |
| 5190 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5191 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5192 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5193 | return Invalid; |
| 5194 | } |
| 5195 | } |
| 5196 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5197 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5198 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5199 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5200 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5201 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5202 | return S.Diag(Arg->getLocStart(), |
| 5203 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5204 | << Arg->getSourceRange(); |
| 5205 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 5206 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 5207 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 5208 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5209 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 5210 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5211 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 5212 | "Only non-static member pointers can make it here"); |
| 5213 | |
| 5214 | // Okay: this is the address of a non-static member, and therefore |
| 5215 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5216 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5217 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5218 | } else { |
| 5219 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5220 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5221 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5222 | return Invalid; |
| 5223 | } |
| 5224 | |
| 5225 | // 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] | 5226 | S.Diag(Arg->getLocStart(), |
| 5227 | diag::err_template_arg_not_pointer_to_member_form) |
| 5228 | << Arg->getSourceRange(); |
| 5229 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5230 | return true; |
| 5231 | } |
| 5232 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5233 | /// \brief Check a template argument against its corresponding |
| 5234 | /// non-type template parameter. |
| 5235 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 5236 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5237 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5238 | /// returns the converted template argument. \p ParamType is the |
| 5239 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5240 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5241 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5242 | TemplateArgument &Converted, |
| 5243 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5244 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5245 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5246 | // If the parameter type somehow involves auto, deduce the type now. |
| 5247 | if (getLangOpts().CPlusPlus1z && ParamType->isUndeducedType()) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5248 | // When checking a deduced template argument, deduce from its type even if |
| 5249 | // the type is dependent, in order to check the types of non-type template |
| 5250 | // arguments line up properly in partial ordering. |
| 5251 | Optional<unsigned> Depth; |
| 5252 | if (CTAK != CTAK_Specified) |
| 5253 | Depth = Param->getDepth() + 1; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5254 | if (DeduceAutoType( |
| 5255 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5256 | Arg, ParamType, Depth) == DAR_Failed) { |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5257 | Diag(Arg->getExprLoc(), |
| 5258 | diag::err_non_type_template_parm_type_deduction_failure) |
| 5259 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 5260 | << Arg->getSourceRange(); |
| 5261 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5262 | return ExprError(); |
| 5263 | } |
| 5264 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 5265 | // an error. The error message normally references the parameter |
| 5266 | // declaration, but here we'll pass the argument location because that's |
| 5267 | // where the parameter type is deduced. |
| 5268 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 5269 | if (ParamType.isNull()) { |
| 5270 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5271 | return ExprError(); |
| 5272 | } |
| 5273 | } |
| 5274 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5275 | // We should have already dropped all cv-qualifiers by now. |
| 5276 | assert(!ParamType.hasQualifiers() && |
| 5277 | "non-type template parameter type cannot be qualified"); |
| 5278 | |
| 5279 | if (CTAK == CTAK_Deduced && |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 5280 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5281 | Arg->getType())) { |
Richard Smith | 957fbf1 | 2017-01-17 02:14:37 +0000 | [diff] [blame] | 5282 | // FIXME: If either type is dependent, we skip the check. This isn't |
| 5283 | // correct, since during deduction we're supposed to have replaced each |
| 5284 | // template parameter with some unique (non-dependent) placeholder. |
| 5285 | // FIXME: If the argument type contains 'auto', we carry on and fail the |
| 5286 | // type check in order to force specific types to be more specialized than |
| 5287 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to |
| 5288 | // work. |
| 5289 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
| 5290 | !Arg->getType()->getContainedAutoType()) { |
| 5291 | Converted = TemplateArgument(Arg); |
| 5292 | return Arg; |
| 5293 | } |
| 5294 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, |
| 5295 | // we should actually be checking the type of the template argument in P, |
| 5296 | // not the type of the template argument deduced from A, against the |
| 5297 | // template parameter type. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5298 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5299 | << Arg->getType() |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5300 | << ParamType.getUnqualifiedType(); |
| 5301 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5302 | return ExprError(); |
| 5303 | } |
| 5304 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5305 | // If either the parameter has a dependent type or the argument is |
| 5306 | // type-dependent, there's nothing we can check now. |
| 5307 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 5308 | // FIXME: Produce a cloned, canonical expression? |
| 5309 | Converted = TemplateArgument(Arg); |
| 5310 | return Arg; |
| 5311 | } |
| 5312 | |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 5313 | // The initialization of the parameter from the argument is |
| 5314 | // a constant-evaluated context. |
| 5315 | EnterExpressionEvaluationContext ConstantEvaluated(*this, |
| 5316 | Sema::ConstantEvaluated); |
| 5317 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5318 | if (getLangOpts().CPlusPlus1z) { |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5319 | // C++1z [temp.arg.nontype]p1: |
| 5320 | // A template-argument for a non-type template parameter shall be |
| 5321 | // a converted constant expression of the type of the template-parameter. |
| 5322 | APValue Value; |
| 5323 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 5324 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 5325 | if (ArgResult.isInvalid()) |
| 5326 | return ExprError(); |
| 5327 | |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5328 | // For a value-dependent argument, CheckConvertedConstantExpression is |
| 5329 | // permitted (and expected) to be unable to determine a value. |
| 5330 | if (ArgResult.get()->isValueDependent()) { |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 5331 | Converted = TemplateArgument(ArgResult.get()); |
| 5332 | return ArgResult; |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5333 | } |
| 5334 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5335 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 5336 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5337 | // Convert the APValue to a TemplateArgument. |
| 5338 | switch (Value.getKind()) { |
| 5339 | case APValue::Uninitialized: |
| 5340 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5341 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5342 | break; |
| 5343 | case APValue::Int: |
| 5344 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5345 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5346 | break; |
| 5347 | case APValue::MemberPointer: { |
| 5348 | assert(ParamType->isMemberPointerType()); |
| 5349 | |
| 5350 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 5351 | if (!Value.getMemberPointerPath().empty()) { |
| 5352 | Diag(Arg->getLocStart(), |
| 5353 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 5354 | << Value.getMemberPointerDecl() << ParamType |
| 5355 | << Arg->getSourceRange(); |
| 5356 | return ExprError(); |
| 5357 | } |
| 5358 | |
| 5359 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5360 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5361 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5362 | break; |
| 5363 | } |
| 5364 | case APValue::LValue: { |
| 5365 | // For a non-type template-parameter of pointer or reference type, |
| 5366 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5367 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 5368 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5369 | // -- a temporary object |
| 5370 | // -- a string literal |
| 5371 | // -- the result of a typeid expression, or |
| 5372 | // -- a predefind __func__ variable |
| 5373 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 5374 | if (isa<CXXUuidofExpr>(E)) { |
| 5375 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 5376 | break; |
| 5377 | } |
| 5378 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5379 | << Arg->getSourceRange(); |
| 5380 | return ExprError(); |
| 5381 | } |
| 5382 | auto *VD = const_cast<ValueDecl *>( |
| 5383 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 5384 | // -- a subobject |
| 5385 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 5386 | VD && VD->getType()->isArrayType() && |
| 5387 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 5388 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 5389 | // Per defect report (no number yet): |
| 5390 | // ... other than a pointer to the first element of a complete array |
| 5391 | // object. |
| 5392 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 5393 | Value.isLValueOnePastTheEnd()) { |
| 5394 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 5395 | << Value.getAsString(Context, ParamType); |
| 5396 | return ExprError(); |
| 5397 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5398 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5399 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5400 | assert((!VD || !ParamType->isNullPtrType()) && |
| 5401 | "non-null value of type nullptr_t?"); |
| 5402 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5403 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5404 | break; |
| 5405 | } |
| 5406 | case APValue::AddrLabelDiff: |
| 5407 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 5408 | case APValue::Float: |
| 5409 | case APValue::ComplexInt: |
| 5410 | case APValue::ComplexFloat: |
| 5411 | case APValue::Vector: |
| 5412 | case APValue::Array: |
| 5413 | case APValue::Struct: |
| 5414 | case APValue::Union: |
| 5415 | llvm_unreachable("invalid kind for template argument"); |
| 5416 | } |
| 5417 | |
| 5418 | return ArgResult.get(); |
| 5419 | } |
| 5420 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5421 | // C++ [temp.arg.nontype]p5: |
| 5422 | // The following conversions are performed on each expression used |
| 5423 | // as a non-type template-argument. If a non-type |
| 5424 | // template-argument cannot be converted to the type of the |
| 5425 | // corresponding template-parameter then the program is |
| 5426 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5427 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5428 | // C++11: |
| 5429 | // -- for a non-type template-parameter of integral or |
| 5430 | // enumeration type, conversions permitted in a converted |
| 5431 | // constant expression are applied. |
| 5432 | // |
| 5433 | // C++98: |
| 5434 | // -- for a non-type template-parameter of integral or |
| 5435 | // enumeration type, integral promotions (4.5) and integral |
| 5436 | // conversions (4.7) are applied. |
| 5437 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5438 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5439 | // C++ [temp.arg.nontype]p1: |
| 5440 | // A template-argument for a non-type, non-template template-parameter |
| 5441 | // shall be one of: |
| 5442 | // |
| 5443 | // -- for a non-type template-parameter of integral or enumeration |
| 5444 | // type, a converted constant expression of the type of the |
| 5445 | // template-parameter; or |
| 5446 | llvm::APSInt Value; |
| 5447 | ExprResult ArgResult = |
| 5448 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5449 | CCEK_TemplateArg); |
| 5450 | if (ArgResult.isInvalid()) |
| 5451 | return ExprError(); |
| 5452 | |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 5453 | // We can't check arbitrary value-dependent arguments. |
| 5454 | if (ArgResult.get()->isValueDependent()) { |
| 5455 | Converted = TemplateArgument(ArgResult.get()); |
| 5456 | return ArgResult; |
| 5457 | } |
| 5458 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5459 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5460 | // always a no-op, except when the parameter type is bool. In |
| 5461 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5462 | QualType IntegerType = ParamType; |
| 5463 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5464 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5465 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5466 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5467 | Converted = TemplateArgument(Context, Value, |
| 5468 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5469 | return ArgResult; |
| 5470 | } |
| 5471 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5472 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5473 | if (ArgResult.isInvalid()) |
| 5474 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5475 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5476 | |
| 5477 | QualType ArgType = Arg->getType(); |
| 5478 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5479 | // C++ [temp.arg.nontype]p1: |
| 5480 | // A template-argument for a non-type, non-template |
| 5481 | // template-parameter shall be one of: |
| 5482 | // |
| 5483 | // -- an integral constant-expression of integral or enumeration |
| 5484 | // type; or |
| 5485 | // -- the name of a non-type template-parameter; or |
| 5486 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5487 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5488 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5489 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5490 | diag::err_template_arg_not_integral_or_enumeral) |
| 5491 | << ArgType << Arg->getSourceRange(); |
| 5492 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5493 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5494 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5495 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5496 | QualType T; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5497 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5498 | public: |
| 5499 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5500 | |
| 5501 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5502 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5503 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5504 | } |
| 5505 | } Diagnoser(ArgType); |
| 5506 | |
| 5507 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5508 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5509 | if (!Arg) |
| 5510 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5513 | // From here on out, all we care about is the unqualified form |
| 5514 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5515 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5516 | |
| 5517 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5518 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5519 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5520 | } else if (ParamType->isBooleanType()) { |
| 5521 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5522 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5523 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5524 | !ParamType->isEnumeralType()) { |
| 5525 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5526 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5527 | } else { |
| 5528 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5529 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5530 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5531 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5532 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5533 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5536 | // Add the value of this argument to the list of converted |
| 5537 | // arguments. We use the bitwidth and signedness of the template |
| 5538 | // parameter. |
| 5539 | if (Arg->isValueDependent()) { |
| 5540 | // The argument is value-dependent. Create a new |
| 5541 | // TemplateArgument with the converted expression. |
| 5542 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5543 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5546 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5547 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5548 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5549 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5550 | if (ParamType->isBooleanType()) { |
| 5551 | // Value must be zero or one. |
| 5552 | Value = Value != 0; |
| 5553 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5554 | if (Value.getBitWidth() != AllowedBits) |
| 5555 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5556 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5557 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5558 | llvm::APSInt OldValue = Value; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5559 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5560 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5561 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5562 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5563 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5564 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5565 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5566 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5567 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5568 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5569 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5570 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5571 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5572 | << Arg->getSourceRange(); |
| 5573 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5574 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5575 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5576 | // Complain if we overflowed the template parameter's type. |
| 5577 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5578 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5579 | RequiredBits = OldValue.getActiveBits(); |
| 5580 | else if (OldValue.isUnsigned()) |
| 5581 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5582 | else |
| 5583 | RequiredBits = OldValue.getMinSignedBits(); |
| 5584 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5585 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5586 | diag::warn_template_arg_too_large) |
| 5587 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5588 | << Arg->getSourceRange(); |
| 5589 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5590 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5591 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5592 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5593 | Converted = TemplateArgument(Context, Value, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5594 | ParamType->isEnumeralType() |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5595 | ? Context.getCanonicalType(ParamType) |
| 5596 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5597 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5598 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5599 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5600 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5601 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5602 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5603 | // Handle pointer-to-function, reference-to-function, and |
| 5604 | // pointer-to-member-function all in (roughly) the same way. |
| 5605 | if (// -- For a non-type template-parameter of type pointer to |
| 5606 | // function, only the function-to-pointer conversion (4.3) is |
| 5607 | // applied. If the template-argument represents a set of |
| 5608 | // overloaded functions (or a pointer to such), the matching |
| 5609 | // function is selected from the set (13.4). |
| 5610 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5611 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5612 | // -- For a non-type template-parameter of type reference to |
| 5613 | // function, no conversions apply. If the template-argument |
| 5614 | // represents a set of overloaded functions, the matching |
| 5615 | // function is selected from the set (13.4). |
| 5616 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5617 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5618 | // -- For a non-type template-parameter of type pointer to |
| 5619 | // member function, no conversions apply. If the |
| 5620 | // template-argument represents a set of overloaded member |
| 5621 | // functions, the matching member function is selected from |
| 5622 | // the set (13.4). |
| 5623 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5624 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5625 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5626 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5627 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5628 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5629 | true, |
| 5630 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5631 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5632 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5633 | |
| 5634 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5635 | ArgType = Arg->getType(); |
| 5636 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5637 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5638 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5639 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5640 | if (!ParamType->isMemberPointerType()) { |
| 5641 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5642 | ParamType, |
| 5643 | Arg, Converted)) |
| 5644 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5645 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5646 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5647 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5648 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5649 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5650 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5651 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5654 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5655 | // -- for a non-type template-parameter of type pointer to |
| 5656 | // object, qualification conversions (4.4) and the |
| 5657 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5658 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5659 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5660 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5661 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5662 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5663 | ParamType, |
| 5664 | Arg, Converted)) |
| 5665 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5666 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5667 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5668 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5669 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5670 | // -- For a non-type template-parameter of type reference to |
| 5671 | // object, no conversions apply. The type referred to by the |
| 5672 | // reference may be more cv-qualified than the (otherwise |
| 5673 | // identical) type of the template-argument. The |
| 5674 | // template-parameter is bound directly to the |
| 5675 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5676 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5677 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5678 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5679 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5680 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5681 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5682 | true, |
| 5683 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5684 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5685 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5686 | |
| 5687 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5688 | ArgType = Arg->getType(); |
| 5689 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5690 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5691 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5692 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5693 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5694 | ParamType, |
| 5695 | Arg, Converted)) |
| 5696 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5697 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5698 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5699 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5700 | // Deal with parameters of type std::nullptr_t. |
| 5701 | if (ParamType->isNullPtrType()) { |
| 5702 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5703 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5704 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5705 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5706 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5707 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5708 | case NPV_NotNullPointer: |
| 5709 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5710 | << Arg->getType() << ParamType; |
| 5711 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5712 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5713 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5714 | case NPV_Error: |
| 5715 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5716 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5717 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5718 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5719 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5720 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5721 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5722 | } |
| 5723 | } |
| 5724 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5725 | // -- For a non-type template-parameter of type pointer to data |
| 5726 | // member, qualification conversions (4.4) are applied. |
| 5727 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5728 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5729 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5730 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5731 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5732 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5733 | } |
| 5734 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5735 | static void DiagnoseTemplateParameterListArityMismatch( |
| 5736 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 5737 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 5738 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5739 | /// \brief Check a template argument against its corresponding |
| 5740 | /// template template parameter. |
| 5741 | /// |
| 5742 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5743 | /// It returns true if an error occurred, and false otherwise. |
| 5744 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5745 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5746 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5747 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5748 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5749 | if (!Template) { |
| 5750 | // Any dependent template name is fine. |
| 5751 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5752 | return false; |
| 5753 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5754 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5755 | if (Template->isInvalidDecl()) |
| 5756 | return true; |
| 5757 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5758 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5759 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5760 | // the name of a class template or an alias template, expressed as an |
| 5761 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5762 | // primary class templates are considered when matching the |
| 5763 | // template template argument with the corresponding parameter; |
| 5764 | // partial specializations are not considered even if their |
| 5765 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5766 | // |
| 5767 | // Note that we also allow template template parameters here, which |
| 5768 | // will happen when we are dealing with, e.g., class template |
| 5769 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5770 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5771 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5772 | !isa<TypeAliasTemplateDecl>(Template) && |
| 5773 | !isa<BuiltinTemplateDecl>(Template)) { |
| 5774 | assert(isa<FunctionTemplateDecl>(Template) && |
| 5775 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 5776 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5777 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 5778 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5779 | } |
| 5780 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5781 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5782 | if (Param->isExpandedParameterPack()) |
| 5783 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5784 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5785 | // C++1z [temp.arg.template]p3: (DR 150) |
| 5786 | // A template-argument matches a template template-parameter P when P |
| 5787 | // is at least as specialized as the template-argument A. |
| 5788 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 5789 | // Quick check for the common case: |
| 5790 | // If P contains a parameter pack, then A [...] matches P if each of A's |
| 5791 | // template parameters matches the corresponding template parameter in |
| 5792 | // the template-parameter-list of P. |
| 5793 | if (TemplateParameterListsAreEqual( |
| 5794 | Template->getTemplateParameters(), Params, false, |
| 5795 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 5796 | return false; |
| 5797 | |
| 5798 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 5799 | Arg.getLocation())) |
| 5800 | return false; |
| 5801 | // FIXME: Produce better diagnostics for deduction failures. |
| 5802 | } |
| 5803 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5804 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5805 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5806 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5807 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5808 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5809 | } |
| 5810 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5811 | /// \brief Given a non-type template argument that refers to a |
| 5812 | /// declaration and the type of its corresponding non-type template |
| 5813 | /// parameter, produce an expression that properly refers to that |
| 5814 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5815 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5816 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5817 | QualType ParamType, |
| 5818 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5819 | // C++ [temp.param]p8: |
| 5820 | // |
| 5821 | // A non-type template-parameter of type "array of T" or |
| 5822 | // "function returning T" is adjusted to be of type "pointer to |
| 5823 | // T" or "pointer to function returning T", respectively. |
| 5824 | if (ParamType->isArrayType()) |
| 5825 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5826 | else if (ParamType->isFunctionType()) |
| 5827 | ParamType = Context.getPointerType(ParamType); |
| 5828 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5829 | // For a NULL non-type template argument, return nullptr casted to the |
| 5830 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5831 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5832 | return ImpCastExprToType( |
| 5833 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5834 | ParamType, |
| 5835 | ParamType->getAs<MemberPointerType>() |
| 5836 | ? CK_NullToMemberPointer |
| 5837 | : CK_NullToPointer); |
| 5838 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5839 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5840 | "Only declaration template arguments permitted here"); |
| 5841 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5842 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5843 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5844 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5845 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5846 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5847 | // If the value is a class member, we might have a pointer-to-member. |
| 5848 | // Determine whether the non-type template template parameter is of |
| 5849 | // pointer-to-member type. If so, we need to build an appropriate |
| 5850 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5851 | // would refer to the member itself. |
| 5852 | if (ParamType->isMemberPointerType()) { |
| 5853 | QualType ClassType |
| 5854 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5855 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5856 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5857 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5858 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5859 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5860 | |
| 5861 | // The actual value-ness of this is unimportant, but for |
| 5862 | // internal consistency's sake, references to instance methods |
| 5863 | // are r-values. |
| 5864 | ExprValueKind VK = VK_LValue; |
| 5865 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5866 | VK = VK_RValue; |
| 5867 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5868 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5869 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5870 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5871 | Loc, |
| 5872 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5873 | if (RefExpr.isInvalid()) |
| 5874 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5876 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5877 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5878 | // We might need to perform a trailing qualification conversion, since |
| 5879 | // the element type on the parameter could be more qualified than the |
| 5880 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5881 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5882 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5883 | ParamType.getUnqualifiedType(), false, |
| 5884 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5885 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5886 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5887 | assert(!RefExpr.isInvalid() && |
| 5888 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5889 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5890 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5891 | } |
| 5892 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5893 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5894 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5895 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5896 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5897 | // When the non-type template parameter is a pointer, take the |
| 5898 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5899 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5900 | if (RefExpr.isInvalid()) |
| 5901 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5902 | |
Richard Smith | fc6fca1 | 2017-01-28 00:38:35 +0000 | [diff] [blame] | 5903 | if (!Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
| 5904 | (T->isFunctionType() || T->isArrayType())) { |
| 5905 | // Decay functions and arrays unless we're forming a pointer to array. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5906 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5907 | if (RefExpr.isInvalid()) |
| 5908 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5909 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5910 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5911 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5912 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5913 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5914 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5917 | ExprValueKind VK = VK_RValue; |
| 5918 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5919 | // If the non-type template parameter has reference type, qualify the |
| 5920 | // resulting declaration reference with the extra qualifiers on the |
| 5921 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5922 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5923 | VK = VK_LValue; |
| 5924 | T = Context.getQualifiedType(T, |
| 5925 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5926 | } else if (isa<FunctionDecl>(VD)) { |
| 5927 | // References to functions are always lvalues. |
| 5928 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5929 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5930 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5931 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | /// \brief Construct a new expression that refers to the given |
| 5935 | /// integral template argument with the given source-location |
| 5936 | /// information. |
| 5937 | /// |
| 5938 | /// This routine takes care of the mapping from an integral template |
| 5939 | /// argument (which may have any integral type) to the appropriate |
| 5940 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5941 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5942 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5943 | SourceLocation Loc) { |
| 5944 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5945 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5946 | QualType OrigT = Arg.getIntegralType(); |
| 5947 | |
| 5948 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5949 | // type the same size as the enumerator. We don't want to build an |
| 5950 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5951 | // any integral type with C++11 enum classes, make sure we create the right |
| 5952 | // type of literal for it. |
| 5953 | QualType T = OrigT; |
| 5954 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5955 | T = ET->getDecl()->getIntegerType(); |
| 5956 | |
| 5957 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5958 | if (T->isAnyCharacterType()) { |
Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 5959 | // This does not need to handle u8 character literals because those are |
| 5960 | // 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] | 5961 | CharacterLiteral::CharacterKind Kind; |
| 5962 | if (T->isWideCharType()) |
| 5963 | Kind = CharacterLiteral::Wide; |
| 5964 | else if (T->isChar16Type()) |
| 5965 | Kind = CharacterLiteral::UTF16; |
| 5966 | else if (T->isChar32Type()) |
| 5967 | Kind = CharacterLiteral::UTF32; |
| 5968 | else |
| 5969 | Kind = CharacterLiteral::Ascii; |
| 5970 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5971 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5972 | Kind, T, Loc); |
| 5973 | } else if (T->isBooleanType()) { |
| 5974 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5975 | T, Loc); |
| 5976 | } else if (T->isNullPtrType()) { |
| 5977 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5978 | } else { |
| 5979 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5980 | } |
| 5981 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5982 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5983 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5984 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5985 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5986 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5987 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5988 | Loc, Loc); |
| 5989 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5990 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5991 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5992 | } |
| 5993 | |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 5994 | static bool isDependentOnOuter(NonTypeTemplateParmDecl *NTTP) { |
| 5995 | if (NTTP->getDepth() == 0 || !NTTP->getType()->isDependentType()) |
| 5996 | return false; |
| 5997 | DependencyChecker Checker(NTTP->getDepth(), /*IgnoreNonTypeDependent*/ false, |
| 5998 | /*FindLessThanDepth*/ true); |
| 5999 | Checker.TraverseType(NTTP->getType()); |
| 6000 | return Checker.Match; |
| 6001 | } |
| 6002 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6003 | /// \brief Match two template parameters within template parameter lists. |
| 6004 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 6005 | bool Complain, |
| 6006 | Sema::TemplateParameterListEqualKind Kind, |
| 6007 | SourceLocation TemplateArgLoc) { |
| 6008 | // Check the actual kind (type, non-type, template). |
| 6009 | if (Old->getKind() != New->getKind()) { |
| 6010 | if (Complain) { |
| 6011 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 6012 | if (TemplateArgLoc.isValid()) { |
| 6013 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6014 | NextDiag = diag::note_template_param_different_kind; |
| 6015 | } |
| 6016 | S.Diag(New->getLocation(), NextDiag) |
| 6017 | << (Kind != Sema::TPL_TemplateMatch); |
| 6018 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 6019 | << (Kind != Sema::TPL_TemplateMatch); |
| 6020 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6021 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6022 | return false; |
| 6023 | } |
| 6024 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 6025 | // Check that both are parameter packs or neither are parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6026 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6027 | // template template parameter, the template template parameter can have |
| 6028 | // a parameter pack where the template template argument does not. |
| 6029 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 6030 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 6031 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6032 | if (Complain) { |
| 6033 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 6034 | if (TemplateArgLoc.isValid()) { |
| 6035 | S.Diag(TemplateArgLoc, |
| 6036 | diag::err_template_arg_template_params_mismatch); |
| 6037 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 6038 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6039 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6040 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 6041 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 6042 | : 2; |
| 6043 | S.Diag(New->getLocation(), NextDiag) |
| 6044 | << ParamKind << New->isParameterPack(); |
| 6045 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 6046 | << ParamKind << Old->isParameterPack(); |
| 6047 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6048 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6049 | return false; |
| 6050 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6051 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6052 | // For non-type template parameters, check the type of the parameter. |
| 6053 | if (NonTypeTemplateParmDecl *OldNTTP |
| 6054 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 6055 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6056 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6057 | // If we are matching a template template argument to a template |
| 6058 | // template parameter and one of the non-type template parameter types |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 6059 | // is dependent on an outer template's parameter, then we must wait until |
| 6060 | // template instantiation time to actually compare the arguments. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6061 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 6062 | (isDependentOnOuter(OldNTTP) || isDependentOnOuter(NewNTTP))) |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6063 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6064 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6065 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 6066 | if (Complain) { |
| 6067 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 6068 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6069 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6070 | diag::err_template_arg_template_params_mismatch); |
| 6071 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 6072 | } |
| 6073 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 6074 | << NewNTTP->getType() |
| 6075 | << (Kind != Sema::TPL_TemplateMatch); |
| 6076 | S.Diag(OldNTTP->getLocation(), |
| 6077 | diag::note_template_nontype_parm_prev_declaration) |
| 6078 | << OldNTTP->getType(); |
| 6079 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6080 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6081 | return false; |
| 6082 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6083 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6084 | return true; |
| 6085 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6086 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6087 | // For template template parameters, check the template parameter types. |
| 6088 | // The template parameter lists of template template |
| 6089 | // parameters must agree. |
| 6090 | if (TemplateTemplateParmDecl *OldTTP |
| 6091 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6092 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6093 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 6094 | OldTTP->getTemplateParameters(), |
| 6095 | Complain, |
| 6096 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6097 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6098 | : Kind), |
| 6099 | TemplateArgLoc); |
| 6100 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6101 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6102 | return true; |
| 6103 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6104 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6105 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 6106 | /// lists. |
| 6107 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6108 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6109 | TemplateParameterList *New, |
| 6110 | TemplateParameterList *Old, |
| 6111 | Sema::TemplateParameterListEqualKind Kind, |
| 6112 | SourceLocation TemplateArgLoc) { |
| 6113 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 6114 | if (TemplateArgLoc.isValid()) { |
| 6115 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6116 | NextDiag = diag::note_template_param_list_different_arity; |
| 6117 | } |
| 6118 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 6119 | << (New->size() > Old->size()) |
| 6120 | << (Kind != Sema::TPL_TemplateMatch) |
| 6121 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 6122 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 6123 | << (Kind != Sema::TPL_TemplateMatch) |
| 6124 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 6125 | } |
| 6126 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6127 | /// \brief Determine whether the given template parameter lists are |
| 6128 | /// equivalent. |
| 6129 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6130 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6131 | /// source code as part of a new template declaration. |
| 6132 | /// |
| 6133 | /// \param Old The old template parameter list, typically found via |
| 6134 | /// name lookup of the template declared with this template parameter |
| 6135 | /// list. |
| 6136 | /// |
| 6137 | /// \param Complain If true, this routine will produce a diagnostic if |
| 6138 | /// the template parameter lists are not equivalent. |
| 6139 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6140 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6141 | /// |
| 6142 | /// \param TemplateArgLoc If this source location is valid, then we |
| 6143 | /// are actually checking the template parameter list of a template |
| 6144 | /// argument (New) against the template parameter list of its |
| 6145 | /// corresponding template template parameter (Old). We produce |
| 6146 | /// slightly different diagnostics in this scenario. |
| 6147 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6148 | /// \returns True if the template parameter lists are equal, false |
| 6149 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6150 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6151 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 6152 | TemplateParameterList *Old, |
| 6153 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6154 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6155 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6156 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 6157 | if (Complain) |
| 6158 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6159 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6160 | |
| 6161 | return false; |
| 6162 | } |
| 6163 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6164 | // C++0x [temp.arg.template]p3: |
| 6165 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6166 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6167 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6168 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6169 | // template-parameter-list of P. [...] |
| 6170 | TemplateParameterList::iterator NewParm = New->begin(); |
| 6171 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6172 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6173 | OldParmEnd = Old->end(); |
| 6174 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 6175 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 6176 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6177 | if (NewParm == NewParmEnd) { |
| 6178 | if (Complain) |
| 6179 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6180 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6181 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6182 | return false; |
| 6183 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6184 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6185 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 6186 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6187 | return false; |
| 6188 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6189 | ++NewParm; |
| 6190 | continue; |
| 6191 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6192 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6193 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6194 | // [...] When P's template- parameter-list contains a template parameter |
| 6195 | // pack (14.5.3), the template parameter pack will match zero or more |
| 6196 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6197 | // template-parameter-list of A with the same type and form as the |
| 6198 | // template parameter pack in P (ignoring whether those template |
| 6199 | // parameters are template parameter packs). |
| 6200 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 6201 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 6202 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6203 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6204 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6205 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6206 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6207 | // Make sure we exhausted all of the arguments. |
| 6208 | if (NewParm != NewParmEnd) { |
| 6209 | if (Complain) |
| 6210 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6211 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6213 | return false; |
| 6214 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6215 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6216 | return true; |
| 6217 | } |
| 6218 | |
| 6219 | /// \brief Check whether a template can be declared within this scope. |
| 6220 | /// |
| 6221 | /// If the template declaration is valid in this scope, returns |
| 6222 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6223 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6224 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 6225 | if (!S) |
| 6226 | return false; |
| 6227 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6228 | // Find the nearest enclosing declaration scope. |
| 6229 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6230 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6231 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6232 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 6233 | // C++ [temp]p4: |
| 6234 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 6235 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 6236 | if (Ctx && Ctx->isExternCContext()) { |
| 6237 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 6238 | << TemplateParams->getSourceRange(); |
| 6239 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 6240 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 6241 | return true; |
| 6242 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 6243 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6244 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 6245 | // C++ [temp]p2: |
| 6246 | // A template-declaration can appear only as a namespace scope or |
| 6247 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 6248 | if (Ctx) { |
| 6249 | if (Ctx->isFileContext()) |
| 6250 | return false; |
| 6251 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 6252 | // C++ [temp.mem]p2: |
| 6253 | // A local class shall not have member templates. |
| 6254 | if (RD->isLocalClass()) |
| 6255 | return Diag(TemplateParams->getTemplateLoc(), |
| 6256 | diag::err_template_inside_local_class) |
| 6257 | << TemplateParams->getSourceRange(); |
| 6258 | else |
| 6259 | return false; |
| 6260 | } |
| 6261 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6262 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6263 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6264 | diag::err_template_outside_namespace_or_class_scope) |
| 6265 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6266 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6267 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6268 | /// \brief Determine what kind of template specialization the given declaration |
| 6269 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6270 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6271 | if (!D) |
| 6272 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6273 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6274 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 6275 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6276 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 6277 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6278 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 6279 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6280 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6281 | return TSK_Undeclared; |
| 6282 | } |
| 6283 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6284 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6285 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6286 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6287 | /// This routine determines whether a template specialization can be declared |
| 6288 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6289 | /// |
| 6290 | /// \param S the semantic analysis object for which this check is being |
| 6291 | /// performed. |
| 6292 | /// |
| 6293 | /// \param Specialized the entity being specialized or instantiated, which |
| 6294 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6295 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6296 | /// member class). |
| 6297 | /// |
| 6298 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 6299 | /// |
| 6300 | /// \param Loc the location of the explicit specialization or instantiation of |
| 6301 | /// this entity. |
| 6302 | /// |
| 6303 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 6304 | /// a class template. |
| 6305 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6306 | /// \returns true if there was an error that we cannot recover from, false |
| 6307 | /// otherwise. |
| 6308 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 6309 | NamedDecl *Specialized, |
| 6310 | NamedDecl *PrevDecl, |
| 6311 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6312 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6313 | // Keep these "kind" numbers in sync with the %select statements in the |
| 6314 | // various diagnostics emitted by this routine. |
| 6315 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6316 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6317 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6318 | else if (isa<VarTemplateDecl>(Specialized)) |
| 6319 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6320 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6321 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6322 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6323 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6324 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6325 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6326 | else if (isa<RecordDecl>(Specialized)) |
| 6327 | EntityKind = 7; |
| 6328 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 6329 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6330 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6331 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6332 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6333 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6334 | return true; |
| 6335 | } |
| 6336 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6337 | // C++ [temp.expl.spec]p2: |
| 6338 | // An explicit specialization shall be declared in the namespace |
| 6339 | // of which the template is a member, or, for member templates, in |
| 6340 | // the namespace of which the enclosing class or enclosing class |
| 6341 | // template is a member. An explicit specialization of a member |
| 6342 | // function, member class or static data member of a class |
| 6343 | // template shall be declared in the namespace of which the class |
| 6344 | // template is a member. Such a declaration may also be a |
| 6345 | // definition. If the declaration is not a definition, the |
| 6346 | // specialization may be defined later in the name- space in which |
| 6347 | // the explicit specialization was declared, or in a namespace |
| 6348 | // that encloses the one in which the explicit specialization was |
| 6349 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6350 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6351 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6352 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6353 | return true; |
| 6354 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6355 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6356 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6357 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6358 | // Do not warn for class scope explicit specialization during |
| 6359 | // instantiation, warning was already emitted during pattern |
| 6360 | // semantic analysis. |
| 6361 | if (!S.ActiveTemplateInstantiations.size()) |
| 6362 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 6363 | << Specialized; |
| 6364 | } else { |
| 6365 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6366 | << Specialized; |
| 6367 | return true; |
| 6368 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6369 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6370 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 6371 | if (S.CurContext->isRecord() && |
| 6372 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 6373 | // Make sure that we're specializing in the right record context. |
| 6374 | // Otherwise, things can go horribly wrong. |
| 6375 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6376 | << Specialized; |
| 6377 | return true; |
| 6378 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6379 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6380 | // C++ [temp.class.spec]p6: |
| 6381 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6382 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 6383 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6384 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6385 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6386 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6387 | |
| 6388 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 6389 | // namespace. |
| 6390 | // Note that HandleDeclarator() performs this check for explicit |
| 6391 | // specializations of function templates, static data members, and member |
| 6392 | // functions, so we skip the check here for those kinds of entities. |
| 6393 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 6394 | // Should we refactor that check, so that it occurs later? |
| 6395 | if (!DC->Encloses(SpecializedContext) && |
| 6396 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 6397 | isa<FunctionDecl>(Specialized) || |
| 6398 | isa<VarTemplateDecl>(Specialized) || |
| 6399 | isa<VarDecl>(Specialized))) { |
| 6400 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 6401 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 6402 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 6403 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6404 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 6405 | if (S.getLangOpts().MicrosoftExt) |
| 6406 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 6407 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 6408 | << cast<NamedDecl>(SpecializedContext); |
| 6409 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6410 | llvm_unreachable("unexpected namespace context for specialization"); |
| 6411 | |
| 6412 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 6413 | } else if ((!PrevDecl || |
| 6414 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 6415 | getTemplateSpecializationKind(PrevDecl) == |
| 6416 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6417 | // C++ [temp.exp.spec]p2: |
| 6418 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6419 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6420 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6421 | // An explicit specialization of a member function, member class or |
| 6422 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6423 | // namespace of which the class template is a member. |
| 6424 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6425 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6426 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6427 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6428 | // C++11 [temp.explicit]p3: |
| 6429 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 6430 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6431 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6432 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6433 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6434 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6435 | "DC encloses TU but isn't in enclosing namespace set"); |
| 6436 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 6437 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6438 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6439 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6440 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6441 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6442 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6443 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 6444 | else |
| 6445 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 6446 | S.Diag(Loc, Diag) |
| 6447 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 6448 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6449 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6450 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6451 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6452 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6453 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6454 | return false; |
| 6455 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6456 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6457 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 6458 | if (!E->isTypeDependent()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6459 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6460 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6461 | Checker.TraverseStmt(E); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6462 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6463 | return E->getSourceRange(); |
| 6464 | return Checker.MatchLoc; |
| 6465 | } |
| 6466 | |
| 6467 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6468 | if (!TL.getType()->isDependentType()) |
| 6469 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6470 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6471 | Checker.TraverseTypeLoc(TL); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6472 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6473 | return TL.getSourceRange(); |
| 6474 | return Checker.MatchLoc; |
| 6475 | } |
| 6476 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6477 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6478 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6479 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6480 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6481 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6482 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6483 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6484 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6485 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6486 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6487 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6488 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6489 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6490 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6491 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6492 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6493 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6494 | |
| 6495 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6496 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6497 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6498 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6499 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6500 | |
| 6501 | // Strip off any implicit casts we added as part of type checking. |
| 6502 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6503 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6504 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6505 | // C++ [temp.class.spec]p8: |
| 6506 | // A non-type argument is non-specialized if it is the name of a |
| 6507 | // non-type parameter. All other non-type arguments are |
| 6508 | // specialized. |
| 6509 | // |
| 6510 | // Below, we check the two conditions that only apply to |
| 6511 | // specialized non-type arguments, so skip any non-specialized |
| 6512 | // arguments. |
| 6513 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6514 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6515 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6516 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6517 | // C++ [temp.class.spec]p9: |
| 6518 | // Within the argument list of a class template partial |
| 6519 | // specialization, the following restrictions apply: |
| 6520 | // -- A partially specialized non-type argument expression |
| 6521 | // shall not involve a template parameter of the partial |
| 6522 | // specialization except when the argument expression is a |
| 6523 | // simple identifier. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6524 | // -- The type of a template parameter corresponding to a |
| 6525 | // specialized non-type argument shall not be dependent on a |
| 6526 | // parameter of the specialization. |
| 6527 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
| 6528 | // We implement a compromise between the original rules and DR1315: |
| 6529 | // -- A specialized non-type template argument shall not be |
| 6530 | // type-dependent and the corresponding template parameter |
| 6531 | // shall have a non-dependent type. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6532 | SourceRange ParamUseRange = |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6533 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6534 | if (ParamUseRange.isValid()) { |
| 6535 | if (IsDefaultArgument) { |
| 6536 | S.Diag(TemplateNameLoc, |
| 6537 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6538 | S.Diag(ParamUseRange.getBegin(), |
| 6539 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6540 | << ParamUseRange; |
| 6541 | } else { |
| 6542 | S.Diag(ParamUseRange.getBegin(), |
| 6543 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6544 | << ParamUseRange; |
| 6545 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6546 | return true; |
| 6547 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6548 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6549 | ParamUseRange = findTemplateParameter( |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6550 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6551 | if (ParamUseRange.isValid()) { |
| 6552 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6553 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6554 | << Param->getType(); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6555 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6556 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 6557 | << ParamUseRange; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6558 | return true; |
| 6559 | } |
| 6560 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6561 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6562 | return false; |
| 6563 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6564 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6565 | /// \brief Check the non-type template arguments of a class template |
| 6566 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6567 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6568 | /// \param TemplateNameLoc the location of the template name. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6569 | /// \param PrimaryTemplate the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6570 | /// template. |
| 6571 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6572 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6573 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6574 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6575 | /// \returns \c true if there was an error, \c false otherwise. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6576 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 6577 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 6578 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 6579 | // We have to be conservative when checking a template in a dependent |
| 6580 | // context. |
| 6581 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 6582 | return false; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6583 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6584 | TemplateParameterList *TemplateParams = |
| 6585 | PrimaryTemplate->getTemplateParameters(); |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6586 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6587 | NonTypeTemplateParmDecl *Param |
| 6588 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6589 | if (!Param) |
| 6590 | continue; |
| 6591 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6592 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 6593 | Param, &TemplateArgs[I], |
| 6594 | 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6595 | return true; |
| 6596 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6597 | |
| 6598 | return false; |
| 6599 | } |
| 6600 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6601 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6602 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6603 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6604 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6605 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6606 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6607 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6608 | MultiTemplateParamsArg |
| 6609 | TemplateParameterLists, |
| 6610 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6611 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6612 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6613 | CXXScopeSpec &SS = TemplateId.SS; |
| 6614 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6615 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6616 | // store the location of the outermost template keyword in the declaration. |
| 6617 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6618 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6619 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6620 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6621 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6622 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6623 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6624 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6625 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6626 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6627 | |
| 6628 | if (!ClassTemplate) { |
| 6629 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6630 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6631 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6632 | return true; |
| 6633 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6634 | |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 6635 | bool isMemberSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6636 | bool isPartialSpecialization = false; |
| 6637 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6638 | // Check the validity of the template headers that introduce this |
| 6639 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6640 | // FIXME: We probably shouldn't complain about these headers for |
| 6641 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6642 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6643 | TemplateParameterList *TemplateParams = |
| 6644 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6645 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
Richard Smith | f445f19 | 2017-02-09 21:04:43 +0000 | [diff] [blame] | 6646 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6647 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6648 | if (Invalid) |
| 6649 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6650 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6651 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6652 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6653 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6654 | if (TUK == TUK_Friend) { |
| 6655 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6656 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6657 | return true; |
| 6658 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6659 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6660 | // C++ [temp.class.spec]p10: |
| 6661 | // The template parameter list of a specialization shall not |
| 6662 | // contain default template argument values. |
| 6663 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6664 | Decl *Param = TemplateParams->getParam(I); |
| 6665 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6666 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6667 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6668 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6669 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6670 | } |
| 6671 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6672 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6673 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6674 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6675 | diag::err_default_arg_in_partial_spec) |
| 6676 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6677 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6678 | } |
| 6679 | } else { |
| 6680 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6681 | if (TTP->hasDefaultArgument()) { |
| 6682 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6683 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6684 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6685 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6686 | } |
| 6687 | } |
| 6688 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6689 | } else if (TemplateParams) { |
| 6690 | if (TUK == TUK_Friend) |
| 6691 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6692 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6693 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6694 | TemplateParams->getRAngleLoc())) |
| 6695 | << SourceRange(LAngleLoc, RAngleLoc); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6696 | } else { |
| 6697 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6698 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6699 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6700 | // Check that the specialization uses the same tag kind as the |
| 6701 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6702 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6703 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6704 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6705 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 6706 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6707 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6708 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6709 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6710 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6711 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6712 | diag::note_previous_use); |
| 6713 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6714 | } |
| 6715 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6716 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6717 | TemplateArgumentListInfo TemplateArgs = |
| 6718 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6719 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6720 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6721 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6722 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6723 | UPPC_PartialSpecialization)) |
| 6724 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6725 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6726 | // Check that the template argument list is well-formed for this |
| 6727 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6728 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6729 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6730 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6731 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6732 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6733 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6734 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6735 | if (isPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6736 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 6737 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6738 | return true; |
| 6739 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6740 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
| 6741 | // also do it during instantiation. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6742 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6743 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6744 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6745 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6746 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6747 | << ClassTemplate->getDeclName(); |
| 6748 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6749 | } |
| 6750 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6751 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6752 | void *InsertPos = nullptr; |
| 6753 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6754 | |
| 6755 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6756 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6757 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6758 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6759 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6760 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6761 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6762 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6763 | // Check whether we can declare a class template specialization in |
| 6764 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6765 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6766 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6767 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6768 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6769 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6770 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6771 | // The canonical type |
| 6772 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6773 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6774 | // Build the canonical type that describes the converted template |
| 6775 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6776 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6777 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6778 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6779 | |
| 6780 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6781 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6782 | // C++ [temp.class.spec]p9b3: |
| 6783 | // |
| 6784 | // -- The argument list of the specialization shall not be identical |
| 6785 | // to the implicit argument list of the primary template. |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6786 | // |
| 6787 | // This rule has since been removed, because it's redundant given DR1495, |
| 6788 | // but we keep it because it produces better diagnostics and recovery. |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6789 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6790 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6791 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6792 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6793 | ClassTemplate->getIdentifier(), |
| 6794 | TemplateNameLoc, |
| 6795 | Attr, |
| 6796 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6797 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6798 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6799 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6800 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6801 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6802 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6803 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6804 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6805 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6806 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6807 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6808 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6809 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6810 | TemplateParams, |
| 6811 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6812 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6813 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6814 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6815 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6816 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6817 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6818 | Partial->setTemplateParameterListsInfo( |
| 6819 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6820 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6821 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6822 | if (!PrevPartial) |
| 6823 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6824 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6825 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6826 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6827 | // template specialization, make a note of that. |
| 6828 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6829 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6830 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6831 | CheckTemplatePartialSpecialization(Partial); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6832 | } else { |
| 6833 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6834 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6835 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6836 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6837 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6838 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6839 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6840 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6841 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6842 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6843 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6844 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6845 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6846 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6847 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6848 | if (!PrevDecl) |
| 6849 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6850 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6851 | if (CurContext->isDependentContext()) { |
| 6852 | // -fms-extensions permits specialization of nested classes without |
| 6853 | // fully specializing the outer class(es). |
| 6854 | assert(getLangOpts().MicrosoftExt && |
| 6855 | "Only possible with -fms-extensions!"); |
| 6856 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6857 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6858 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6859 | } else { |
| 6860 | CanonType = Context.getTypeDeclType(Specialization); |
| 6861 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6862 | } |
| 6863 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6864 | // C++ [temp.expl.spec]p6: |
| 6865 | // 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] | 6866 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6867 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6868 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6869 | // use occurs; no diagnostic is required. |
| 6870 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6871 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6872 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6873 | // Is there any previous explicit specialization declaration? |
| 6874 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6875 | Okay = true; |
| 6876 | break; |
| 6877 | } |
| 6878 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6879 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6880 | if (!Okay) { |
| 6881 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6882 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6883 | << Context.getTypeDeclType(Specialization) << Range; |
| 6884 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6885 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6886 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6887 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6888 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6889 | return true; |
| 6890 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6891 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6892 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6893 | // If this is not a friend, note that this is an explicit specialization. |
| 6894 | if (TUK != TUK_Friend) |
| 6895 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6896 | |
| 6897 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6898 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6899 | RecordDecl *Def = Specialization->getDefinition(); |
| 6900 | NamedDecl *Hidden = nullptr; |
| 6901 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6902 | SkipBody->ShouldSkip = true; |
| 6903 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6904 | // From here on out, treat this as just a redeclaration. |
| 6905 | TUK = TUK_Declaration; |
| 6906 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6907 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 6908 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6909 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6910 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6911 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6912 | } |
| 6913 | } |
| 6914 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6915 | if (Attr) |
| 6916 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6917 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6918 | // Add alignment attributes if necessary; these attributes are checked when |
| 6919 | // the ASTContext lays out the structure. |
| 6920 | if (TUK == TUK_Definition) { |
| 6921 | AddAlignmentAttributesForRecord(Specialization); |
| 6922 | AddMsStructLayoutForRecord(Specialization); |
| 6923 | } |
| 6924 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6925 | if (ModulePrivateLoc.isValid()) |
| 6926 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6927 | << (isPartialSpecialization? 1 : 0) |
| 6928 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6929 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6930 | // Build the fully-sugared type for this class template |
| 6931 | // specialization as the user wrote in the specialization |
| 6932 | // itself. This means that we'll pretty-print the type retrieved |
| 6933 | // from the specialization's declaration the way that the user |
| 6934 | // actually wrote the specialization, rather than formatting the |
| 6935 | // name based on the "canonical" representation used to store the |
| 6936 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6937 | TypeSourceInfo *WrittenTy |
| 6938 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6939 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6940 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6941 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6942 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6943 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6944 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6945 | // C++ [temp.expl.spec]p9: |
| 6946 | // A template explicit specialization is in the scope of the |
| 6947 | // namespace in which the template was defined. |
| 6948 | // |
| 6949 | // We actually implement this paragraph where we set the semantic |
| 6950 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6951 | // but we also maintain the lexical context where the actual |
| 6952 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6953 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6954 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6955 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6956 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6957 | Specialization->startDefinition(); |
| 6958 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6959 | if (TUK == TUK_Friend) { |
| 6960 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6961 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6962 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6963 | /*FIXME:*/KWLoc); |
| 6964 | Friend->setAccess(AS_public); |
| 6965 | CurContext->addDecl(Friend); |
| 6966 | } else { |
| 6967 | // Add the specialization into its lexical context, so that it can |
| 6968 | // be seen when iterating through the list of declarations in that |
| 6969 | // context. However, specializations are not found by name lookup. |
| 6970 | CurContext->addDecl(Specialization); |
| 6971 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6972 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6973 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6974 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6975 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6976 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6977 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6978 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6979 | ActOnDocumentableDecl(NewDecl); |
| 6980 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6981 | } |
| 6982 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6983 | /// \brief Strips various properties off an implicit instantiation |
| 6984 | /// that has just been explicitly specialized. |
| 6985 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6986 | D->dropAttr<DLLImportAttr>(); |
| 6987 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6988 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6989 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6990 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6991 | } |
| 6992 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6993 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6994 | // declaration or definition. |
| 6995 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6996 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6997 | // Explicit instantiations following a specialization have no effect and |
| 6998 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6999 | // until a valid name loc is found. |
| 7000 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7001 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 7002 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7003 | PrevDiagLoc = Prev->getLocation(); |
| 7004 | } |
| 7005 | assert(PrevDiagLoc.isValid() && |
| 7006 | "Explicit instantiation without point of instantiation?"); |
| 7007 | return PrevDiagLoc; |
| 7008 | } |
| 7009 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7010 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7011 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7012 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7013 | /// new specialization/instantiation will have any effect. |
| 7014 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7015 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7016 | /// instantiation. |
| 7017 | /// |
| 7018 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 7019 | /// |
| 7020 | /// \param PrevDecl the previous declaration of the entity. |
| 7021 | /// |
| 7022 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 7023 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7024 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7025 | /// declaration was instantiated (either implicitly or explicitly). |
| 7026 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7027 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7028 | /// specialization or instantiation has no effect and should be ignored. |
| 7029 | /// |
| 7030 | /// \returns true if there was an error that should prevent the introduction of |
| 7031 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7032 | bool |
| 7033 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 7034 | TemplateSpecializationKind NewTSK, |
| 7035 | NamedDecl *PrevDecl, |
| 7036 | TemplateSpecializationKind PrevTSK, |
| 7037 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7038 | bool &HasNoEffect) { |
| 7039 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7040 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7041 | switch (NewTSK) { |
| 7042 | case TSK_Undeclared: |
| 7043 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 7044 | assert( |
| 7045 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 7046 | "previous declaration must be implicit!"); |
| 7047 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7048 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7049 | case TSK_ExplicitSpecialization: |
| 7050 | switch (PrevTSK) { |
| 7051 | case TSK_Undeclared: |
| 7052 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7053 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7054 | // explicitly specialized or has merely been mentioned without any |
| 7055 | // instantiation. |
| 7056 | return false; |
| 7057 | |
| 7058 | case TSK_ImplicitInstantiation: |
| 7059 | if (PrevPointOfInstantiation.isInvalid()) { |
| 7060 | // The declaration itself has not actually been instantiated, so it is |
| 7061 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7062 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7063 | return false; |
| 7064 | } |
| 7065 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7066 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7067 | case TSK_ExplicitInstantiationDeclaration: |
| 7068 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7069 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 7070 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7071 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7072 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7073 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7074 | // 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] | 7075 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7076 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7077 | // implicit instantiation to take place, in every translation unit in |
| 7078 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7079 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7080 | // Is there any previous explicit specialization declaration? |
| 7081 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 7082 | return false; |
| 7083 | } |
| 7084 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7085 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7086 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7087 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7088 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7089 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7090 | return true; |
| 7091 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7092 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7093 | case TSK_ExplicitInstantiationDeclaration: |
| 7094 | switch (PrevTSK) { |
| 7095 | case TSK_ExplicitInstantiationDeclaration: |
| 7096 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7097 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7098 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7099 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7100 | case TSK_Undeclared: |
| 7101 | case TSK_ImplicitInstantiation: |
| 7102 | // We're explicitly instantiating something that may have already been |
| 7103 | // implicitly instantiated; that's fine. |
| 7104 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7105 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7106 | case TSK_ExplicitSpecialization: |
| 7107 | // C++0x [temp.explicit]p4: |
| 7108 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7109 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7110 | // specialization for that template, the explicit instantiation has no |
| 7111 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7112 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7113 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7114 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7115 | case TSK_ExplicitInstantiationDefinition: |
| 7116 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7117 | // If an entity is the subject of both an explicit instantiation |
| 7118 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7119 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7120 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7121 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7122 | |
| 7123 | // Explicit instantiations following a specialization have no effect and |
| 7124 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 7125 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7126 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 7127 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7128 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7129 | return false; |
| 7130 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7131 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7132 | case TSK_ExplicitInstantiationDefinition: |
| 7133 | switch (PrevTSK) { |
| 7134 | case TSK_Undeclared: |
| 7135 | case TSK_ImplicitInstantiation: |
| 7136 | // We're explicitly instantiating something that may have already been |
| 7137 | // implicitly instantiated; that's fine. |
| 7138 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7139 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7140 | case TSK_ExplicitSpecialization: |
| 7141 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 7142 | // For a given set of template parameters, if an explicit |
| 7143 | // instantiation of a template appears after a declaration of |
| 7144 | // an explicit specialization for that template, the explicit |
| 7145 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 7146 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7147 | << PrevDecl; |
| 7148 | Diag(PrevDecl->getLocation(), |
| 7149 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7150 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7151 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7152 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7153 | case TSK_ExplicitInstantiationDeclaration: |
| 7154 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7155 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7156 | |
| 7157 | // C++0x [temp.explicit]p4: |
| 7158 | // For a given set of template parameters, if an explicit instantiation |
| 7159 | // of a template appears after a declaration of an explicit |
| 7160 | // specialization for that template, the explicit instantiation has no |
| 7161 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7162 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7163 | // Is there any previous explicit specialization declaration? |
| 7164 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7165 | HasNoEffect = true; |
| 7166 | break; |
| 7167 | } |
| 7168 | } |
| 7169 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7170 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7171 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7172 | case TSK_ExplicitInstantiationDefinition: |
| 7173 | // C++0x [temp.spec]p5: |
| 7174 | // For a given template and a given set of template-arguments, |
| 7175 | // - an explicit instantiation definition shall appear at most once |
| 7176 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7177 | |
| 7178 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 7179 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 7180 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7181 | : diag::err_explicit_instantiation_duplicate) |
| 7182 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7183 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7184 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7185 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7186 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7187 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7188 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7189 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7190 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7191 | } |
| 7192 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7193 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7194 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7195 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7196 | /// The only possible way to get a dependent function template specialization |
| 7197 | /// is with a friend declaration, like so: |
| 7198 | /// |
| 7199 | /// \code |
| 7200 | /// template \<class T> void foo(T); |
| 7201 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7202 | /// friend void foo<>(T); |
| 7203 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7204 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7205 | /// |
| 7206 | /// There really isn't any useful analysis we can do here, so we |
| 7207 | /// just store the information. |
| 7208 | bool |
| 7209 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 7210 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 7211 | LookupResult &Previous) { |
| 7212 | // Remove anything from Previous that isn't a function template in |
| 7213 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7214 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7215 | LookupResult::Filter F = Previous.makeFilter(); |
| 7216 | while (F.hasNext()) { |
| 7217 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 7218 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7219 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 7220 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7221 | F.erase(); |
| 7222 | } |
| 7223 | F.done(); |
| 7224 | |
| 7225 | // Should this be diagnosed here? |
| 7226 | if (Previous.empty()) return true; |
| 7227 | |
| 7228 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 7229 | ExplicitTemplateArgs); |
| 7230 | return false; |
| 7231 | } |
| 7232 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7233 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7234 | /// specialization. |
| 7235 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7236 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7237 | /// explicit function template specialization. On successful completion, |
| 7238 | /// the function declaration \p FD will become a function template |
| 7239 | /// specialization. |
| 7240 | /// |
| 7241 | /// \param FD the function declaration, which will be updated to become a |
| 7242 | /// function template specialization. |
| 7243 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7244 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 7245 | /// if any. Note that this may be valid info even when 0 arguments are |
| 7246 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 7247 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7248 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7249 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7250 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7251 | bool Sema::CheckFunctionTemplateSpecialization( |
| 7252 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 7253 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7254 | // The set of function template specializations that could match this |
| 7255 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7256 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 7257 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 7258 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7259 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7260 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 7261 | ConvertedTemplateArgs; |
| 7262 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7263 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7264 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7265 | I != E; ++I) { |
| 7266 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 7267 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7268 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7269 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7270 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 7271 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7272 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7273 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7274 | // When matching a constexpr member function template specialization |
| 7275 | // against the primary template, we don't yet know whether the |
| 7276 | // specialization has an implicit 'const' (because we don't know whether |
| 7277 | // it will be a static member function until we know which template it |
| 7278 | // specializes), so adjust it now assuming it specializes this template. |
| 7279 | QualType FT = FD->getType(); |
| 7280 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7281 | CXXMethodDecl *OldMD = |
| 7282 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7283 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7284 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7285 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7286 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7287 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7288 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7289 | } |
| 7290 | } |
| 7291 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7292 | TemplateArgumentListInfo Args; |
| 7293 | if (ExplicitTemplateArgs) |
| 7294 | Args = *ExplicitTemplateArgs; |
| 7295 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7296 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7297 | // A trailing template-argument can be left unspecified in the |
| 7298 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7299 | // provided it can be deduced from the function argument type. |
| 7300 | // Perform template argument deduction to determine whether we may be |
| 7301 | // specializing this template. |
| 7302 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7303 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7304 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 7305 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 7306 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7307 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 7308 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7309 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7310 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7311 | FailedCandidates.addCandidate().set( |
| 7312 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 7313 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7314 | (void)TDK; |
| 7315 | continue; |
| 7316 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7317 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7318 | // Target attributes are part of the cuda function signature, so |
| 7319 | // the deduced template's cuda target must match that of the |
| 7320 | // specialization. Given that C++ template deduction does not |
| 7321 | // take target attributes into account, we reject candidates |
| 7322 | // here that have a different target. |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 7323 | if (LangOpts.CUDA && |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7324 | IdentifyCUDATarget(Specialization, |
| 7325 | /* IgnoreImplicitHDAttributes = */ true) != |
| 7326 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 7327 | FailedCandidates.addCandidate().set( |
| 7328 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 7329 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 7330 | continue; |
| 7331 | } |
| 7332 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7333 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7334 | if (ExplicitTemplateArgs) |
| 7335 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7336 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7337 | } |
| 7338 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7339 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 7340 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7341 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7342 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7343 | FD->getLocation(), |
| 7344 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 7345 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7346 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7347 | PDiag(diag::note_function_template_spec_matched)); |
| 7348 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7349 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7350 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7351 | |
| 7352 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 7353 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7354 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7355 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare [...] |
| 7356 | // an explicit specialization (14.8.3) [...] of a concept definition. |
| 7357 | if (Specialization->getPrimaryTemplate()->isConcept()) { |
| 7358 | Diag(FD->getLocation(), diag::err_concept_specialized) |
| 7359 | << 0 /*function*/ << 1 /*explicitly specialized*/; |
| 7360 | Diag(Specialization->getLocation(), diag::note_previous_declaration); |
| 7361 | return true; |
| 7362 | } |
| 7363 | |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7364 | FunctionTemplateSpecializationInfo *SpecInfo |
| 7365 | = Specialization->getTemplateSpecializationInfo(); |
| 7366 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7367 | |
| 7368 | // Note: do not overwrite location info if previous template |
| 7369 | // specialization kind was explicit. |
| 7370 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7371 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7372 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7373 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 7374 | // function can differ from the template declaration with respect to |
| 7375 | // the constexpr specifier. |
| 7376 | Specialization->setConstexpr(FD->isConstexpr()); |
| 7377 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7378 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7379 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7380 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7381 | |
| 7382 | // If this is a friend declaration, then we're not really declaring |
| 7383 | // an explicit specialization. |
| 7384 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7385 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7386 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7387 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7388 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7389 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7390 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7391 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7392 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7393 | |
| 7394 | // C++ [temp.expl.spec]p6: |
| 7395 | // 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] | 7396 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7397 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7398 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7399 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7400 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7401 | if (!isFriend && |
| 7402 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7403 | TSK_ExplicitSpecialization, |
| 7404 | Specialization, |
| 7405 | SpecInfo->getTemplateSpecializationKind(), |
| 7406 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7407 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7408 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7409 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7410 | // Mark the prior declaration as an explicit specialization, so that later |
| 7411 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7412 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 7413 | // Since explicit specializations do not inherit '=delete' from their |
| 7414 | // primary function template - check if the 'specialization' that was |
| 7415 | // implicitly generated (during template argument deduction for partial |
| 7416 | // ordering) from the most specialized of all the function templates that |
| 7417 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 7418 | // first check that it was implicitly generated during template argument |
| 7419 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 7420 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 7421 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 7422 | !Specialization->getCanonicalDecl()->isReferenced()) { |
| 7423 | assert( |
| 7424 | Specialization->getCanonicalDecl() == Specialization && |
| 7425 | "This must be the only existing declaration of this specialization"); |
| 7426 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7427 | } |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7428 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7429 | MarkUnusedFileScopedDecl(Specialization); |
| 7430 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7431 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7432 | // Turn the given function declaration into a function template |
| 7433 | // specialization, with the template arguments from the previous |
| 7434 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7435 | // Take copies of (semantic and syntactic) template argument lists. |
| 7436 | const TemplateArgumentList* TemplArgs = new (Context) |
| 7437 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7438 | FD->setFunctionTemplateSpecialization( |
| 7439 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 7440 | SpecInfo->getTemplateSpecializationKind(), |
| 7441 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7442 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7443 | // A function template specialization inherits the target attributes |
| 7444 | // of its template. (We require the attributes explicitly in the |
| 7445 | // code to match, but a template may have implicit attributes by |
| 7446 | // virtue e.g. of being constexpr, and it passes these implicit |
| 7447 | // attributes on to its specializations.) |
| 7448 | if (LangOpts.CUDA) |
| 7449 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 7450 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7451 | // The "previous declaration" for this function template specialization is |
| 7452 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7453 | Previous.clear(); |
| 7454 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7455 | return false; |
| 7456 | } |
| 7457 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7458 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7459 | /// specialization. |
| 7460 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7461 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7462 | /// explicit member function specialization. On successful completion, |
| 7463 | /// the function declaration \p FD will become a member function |
| 7464 | /// specialization. |
| 7465 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7466 | /// \param Member the member declaration, which will be updated to become a |
| 7467 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7468 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7469 | /// \param Previous the set of declarations, one of which may be specialized |
| 7470 | /// by this function specialization; the set will be modified to contain the |
| 7471 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7472 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7473 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7474 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7475 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7476 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7477 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7478 | NamedDecl *Instantiation = nullptr; |
| 7479 | NamedDecl *InstantiatedFrom = nullptr; |
| 7480 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7481 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7482 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7483 | // Nowhere to look anyway. |
| 7484 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7485 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7486 | I != E; ++I) { |
| 7487 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7488 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7489 | QualType Adjusted = Function->getType(); |
| 7490 | if (!hasExplicitCallingConv(Adjusted)) |
| 7491 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7492 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7493 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7494 | Instantiation = Method; |
| 7495 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7496 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7497 | break; |
| 7498 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7499 | } |
| 7500 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7501 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7502 | VarDecl *PrevVar; |
| 7503 | if (Previous.isSingleResult() && |
| 7504 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7505 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7506 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7507 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7508 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7509 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7510 | } |
| 7511 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7512 | CXXRecordDecl *PrevRecord; |
| 7513 | if (Previous.isSingleResult() && |
| 7514 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7515 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7516 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7517 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7518 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7519 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7520 | } else if (isa<EnumDecl>(Member)) { |
| 7521 | EnumDecl *PrevEnum; |
| 7522 | if (Previous.isSingleResult() && |
| 7523 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7524 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7525 | Instantiation = PrevEnum; |
| 7526 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7527 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7528 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7529 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7530 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7531 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7532 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7533 | // specializations are always out-of-line, the caller will complain about |
| 7534 | // this mismatch later. |
| 7535 | return false; |
| 7536 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7537 | |
| 7538 | // If this is a friend, just bail out here before we start turning |
| 7539 | // things into explicit specializations. |
| 7540 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7541 | // Preserve instantiation information. |
| 7542 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7543 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7544 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7545 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7546 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7547 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7548 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7549 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7550 | } |
| 7551 | |
| 7552 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7553 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7554 | return false; |
| 7555 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7556 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7557 | // Make sure that this is a specialization of a member. |
| 7558 | if (!InstantiatedFrom) { |
| 7559 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 7560 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7561 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 7562 | return true; |
| 7563 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7564 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7565 | // C++ [temp.expl.spec]p6: |
| 7566 | // 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] | 7567 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7568 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7569 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7570 | // use occurs; no diagnostic is required. |
| 7571 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7572 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7573 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7574 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7575 | TSK_ExplicitSpecialization, |
| 7576 | Instantiation, |
| 7577 | MSInfo->getTemplateSpecializationKind(), |
| 7578 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7579 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7580 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7581 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7582 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7583 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7584 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7585 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7586 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7587 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7588 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7589 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7590 | // the original declaration to note that it is an explicit specialization |
| 7591 | // (if it was previously an implicit instantiation). This latter step |
| 7592 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7593 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7594 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7595 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7596 | TSK_ImplicitInstantiation) { |
| 7597 | InstantiationFunction->setTemplateSpecializationKind( |
| 7598 | TSK_ExplicitSpecialization); |
| 7599 | InstantiationFunction->setLocation(Member->getLocation()); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7600 | // Explicit specializations of member functions of class templates do not |
| 7601 | // inherit '=delete' from the member function they are specializing. |
| 7602 | if (InstantiationFunction->isDeleted()) { |
| 7603 | assert(InstantiationFunction->getCanonicalDecl() == |
| 7604 | InstantiationFunction); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 7605 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7606 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7607 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7608 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7609 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7610 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7611 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7612 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7613 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7614 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7615 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7616 | TSK_ImplicitInstantiation) { |
| 7617 | InstantiationVar->setTemplateSpecializationKind( |
| 7618 | TSK_ExplicitSpecialization); |
| 7619 | InstantiationVar->setLocation(Member->getLocation()); |
| 7620 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7621 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7622 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7623 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7624 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7625 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7626 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7627 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7628 | TSK_ImplicitInstantiation) { |
| 7629 | InstantiationClass->setTemplateSpecializationKind( |
| 7630 | TSK_ExplicitSpecialization); |
| 7631 | InstantiationClass->setLocation(Member->getLocation()); |
| 7632 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7633 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7634 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7635 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7636 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7637 | } else { |
| 7638 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7639 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7640 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7641 | TSK_ImplicitInstantiation) { |
| 7642 | InstantiationEnum->setTemplateSpecializationKind( |
| 7643 | TSK_ExplicitSpecialization); |
| 7644 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7645 | } |
| 7646 | |
| 7647 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7648 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7649 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7650 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7651 | // Save the caller the trouble of having to figure out which declaration |
| 7652 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7653 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7654 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7655 | return false; |
| 7656 | } |
| 7657 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7658 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7659 | /// |
| 7660 | /// \returns true if a serious error occurs, false otherwise. |
| 7661 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7662 | SourceLocation InstLoc, |
| 7663 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7664 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7665 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7666 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7667 | if (CurContext->isRecord()) { |
| 7668 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7669 | << D; |
| 7670 | return true; |
| 7671 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7672 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7673 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7674 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7675 | // template. If the name declared in the explicit instantiation is an |
| 7676 | // unqualified name, the explicit instantiation shall appear in the |
| 7677 | // namespace where its template is declared or, if that namespace is inline |
| 7678 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7679 | // |
| 7680 | // 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] | 7681 | if (WasQualifiedName) { |
| 7682 | if (CurContext->Encloses(OrigContext)) |
| 7683 | return false; |
| 7684 | } else { |
| 7685 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7686 | return false; |
| 7687 | } |
| 7688 | |
| 7689 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7690 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7691 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7692 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7693 | diag::err_explicit_instantiation_out_of_scope : |
| 7694 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7695 | << D << NS; |
| 7696 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7697 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7698 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7699 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7700 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7701 | << D << NS; |
| 7702 | } else |
| 7703 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7704 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7705 | diag::err_explicit_instantiation_must_be_global : |
| 7706 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7707 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7708 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7709 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7710 | } |
| 7711 | |
| 7712 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7713 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7714 | if (!SS.isSet()) |
| 7715 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7716 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7717 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7718 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7719 | // or a static data member of a class template specialization, the name of |
| 7720 | // the class template specialization in the qualified-id for the member |
| 7721 | // name shall be a simple-template-id. |
| 7722 | // |
| 7723 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7724 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7725 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7726 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7727 | if (isa<TemplateSpecializationType>(T)) |
| 7728 | return true; |
| 7729 | |
| 7730 | return false; |
| 7731 | } |
| 7732 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 7733 | /// Make a dllexport or dllimport attr on a class template specialization take |
| 7734 | /// effect. |
| 7735 | static void dllExportImportClassTemplateSpecialization( |
| 7736 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 7737 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 7738 | assert(A && "dllExportImportClassTemplateSpecialization called " |
| 7739 | "on Def without dllexport or dllimport"); |
| 7740 | |
| 7741 | // We reject explicit instantiations in class scope, so there should |
| 7742 | // never be any delayed exported classes to worry about. |
| 7743 | assert(S.DelayedDllExportClasses.empty() && |
| 7744 | "delayed exports present at explicit instantiation"); |
| 7745 | S.checkClassLevelDLLAttribute(Def); |
| 7746 | |
| 7747 | // Propagate attribute to base class templates. |
| 7748 | for (auto &B : Def->bases()) { |
| 7749 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 7750 | B.getType()->getAsCXXRecordDecl())) |
| 7751 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 7752 | } |
| 7753 | |
| 7754 | S.referenceDLLExportedClassMethods(); |
| 7755 | } |
| 7756 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7757 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7758 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7759 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7760 | SourceLocation ExternLoc, |
| 7761 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7762 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7763 | SourceLocation KWLoc, |
| 7764 | const CXXScopeSpec &SS, |
| 7765 | TemplateTy TemplateD, |
| 7766 | SourceLocation TemplateNameLoc, |
| 7767 | SourceLocation LAngleLoc, |
| 7768 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7769 | SourceLocation RAngleLoc, |
| 7770 | AttributeList *Attr) { |
| 7771 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7772 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7773 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7774 | // Check that the specialization uses the same tag kind as the |
| 7775 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7776 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7777 | assert(Kind != TTK_Enum && |
| 7778 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7779 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7780 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 7781 | |
| 7782 | if (!ClassTemplate) { |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 7783 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 7784 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7785 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7786 | return true; |
| 7787 | } |
| 7788 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7789 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7790 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7791 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7792 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7793 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7794 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7795 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7796 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7797 | diag::note_previous_use); |
| 7798 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7799 | } |
| 7800 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7801 | // C++0x [temp.explicit]p2: |
| 7802 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7803 | // definition and an explicit instantiation declaration. An explicit |
| 7804 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7805 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7806 | ? TSK_ExplicitInstantiationDefinition |
| 7807 | : TSK_ExplicitInstantiationDeclaration; |
| 7808 | |
| 7809 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7810 | // Check for dllexport class template instantiation declarations. |
| 7811 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7812 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7813 | Diag(ExternLoc, |
| 7814 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7815 | Diag(A->getLoc(), diag::note_attribute); |
| 7816 | break; |
| 7817 | } |
| 7818 | } |
| 7819 | |
| 7820 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7821 | Diag(ExternLoc, |
| 7822 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7823 | Diag(A->getLocation(), diag::note_attribute); |
| 7824 | } |
| 7825 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7826 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7827 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 7828 | // instantiation declarations for most purposes. |
| 7829 | bool DLLImportExplicitInstantiationDef = false; |
| 7830 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 7831 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7832 | // Check for dllimport class template instantiation definitions. |
| 7833 | bool DLLImport = |
| 7834 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 7835 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7836 | if (A->getKind() == AttributeList::AT_DLLImport) |
| 7837 | DLLImport = true; |
| 7838 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7839 | // dllexport trumps dllimport here. |
| 7840 | DLLImport = false; |
| 7841 | break; |
| 7842 | } |
| 7843 | } |
| 7844 | if (DLLImport) { |
| 7845 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 7846 | DLLImportExplicitInstantiationDef = true; |
| 7847 | } |
| 7848 | } |
| 7849 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7850 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7851 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7852 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7853 | |
| 7854 | // Check that the template argument list is well-formed for this |
| 7855 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7856 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7857 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7858 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7859 | return true; |
| 7860 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7861 | // Find the class template specialization declaration that |
| 7862 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7863 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7864 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7865 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7866 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7867 | TemplateSpecializationKind PrevDecl_TSK |
| 7868 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7869 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7870 | // C++0x [temp.explicit]p2: |
| 7871 | // [...] An explicit instantiation shall appear in an enclosing |
| 7872 | // namespace of its template. [...] |
| 7873 | // |
| 7874 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7875 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7876 | SS.isSet())) |
| 7877 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7878 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7879 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7880 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7881 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7882 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7883 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7884 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7885 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7886 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7887 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7888 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7889 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7890 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7891 | |
| 7892 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7893 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7894 | // Since the only prior class template specialization with these |
| 7895 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7896 | // declaration node as our own, updating the source location |
| 7897 | // for the template name to reflect our new declaration. |
| 7898 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7899 | Specialization = PrevDecl; |
| 7900 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7901 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7902 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7903 | |
| 7904 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7905 | DLLImportExplicitInstantiationDef) { |
| 7906 | // The new specialization might add a dllimport attribute. |
| 7907 | HasNoEffect = false; |
| 7908 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7909 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7910 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7911 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7912 | // Create a new class template specialization declaration node for |
| 7913 | // this explicit specialization. |
| 7914 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7915 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7916 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7917 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7918 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7919 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7920 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7921 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7922 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7923 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7924 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7925 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7926 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7927 | } |
| 7928 | |
| 7929 | // Build the fully-sugared type for this explicit instantiation as |
| 7930 | // the user wrote in the explicit instantiation itself. This means |
| 7931 | // that we'll pretty-print the type retrieved from the |
| 7932 | // specialization's declaration the way that the user actually wrote |
| 7933 | // the explicit instantiation, rather than formatting the name based |
| 7934 | // on the "canonical" representation used to store the template |
| 7935 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7936 | TypeSourceInfo *WrittenTy |
| 7937 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7938 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7939 | Context.getTypeDeclType(Specialization)); |
| 7940 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7941 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7942 | // Set source locations for keywords. |
| 7943 | Specialization->setExternLoc(ExternLoc); |
| 7944 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 7945 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7946 | |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 7947 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7948 | if (Attr) |
| 7949 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7950 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7951 | // Add the explicit instantiation into its lexical context. However, |
| 7952 | // since explicit instantiations are never found by name lookup, we |
| 7953 | // just put it into the declaration context directly. |
| 7954 | Specialization->setLexicalDeclContext(CurContext); |
| 7955 | CurContext->addDecl(Specialization); |
| 7956 | |
| 7957 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7958 | if (HasNoEffect) { |
| 7959 | // Set the template specialization kind. |
| 7960 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7961 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7962 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7963 | |
| 7964 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7965 | // A definition of a class template or class member template |
| 7966 | // shall be in scope at the point of the explicit instantiation of |
| 7967 | // the class template or class member template. |
| 7968 | // |
| 7969 | // This check comes when we actually try to perform the |
| 7970 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7971 | ClassTemplateSpecializationDecl *Def |
| 7972 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7973 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7974 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7975 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7976 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7977 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7978 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7979 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7980 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7981 | // Instantiate the members of this class template specialization. |
| 7982 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7983 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7984 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7985 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7986 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7987 | // TSK_ExplicitInstantiationDefinition |
| 7988 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7989 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 7990 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7991 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7992 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7993 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7994 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
Shoaib Meenai | ab3f96c | 2016-11-09 23:52:20 +0000 | [diff] [blame] | 7995 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 7996 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7997 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7998 | // attribute to a template with a previous instantiation declaration. |
| 7999 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8000 | auto *A = cast<InheritableAttr>( |
| 8001 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 8002 | A->setInherited(true); |
| 8003 | Def->addAttr(A); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8004 | dllExportImportClassTemplateSpecialization(*this, Def); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 8005 | } |
| 8006 | } |
| 8007 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8008 | // Fix a TSK_ImplicitInstantiation followed by a |
| 8009 | // TSK_ExplicitInstantiationDefinition |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 8010 | bool NewlyDLLExported = |
| 8011 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
| 8012 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 8013 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8014 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 8015 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 8016 | // attribute to a template with a previous implicit instantiation. |
| 8017 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to |
| 8018 | // avoid potentially strange codegen behavior. For example, if we extend |
| 8019 | // this conditional to dllimport, and we have a source file calling a |
| 8020 | // method on an implicitly instantiated template class instance and then |
| 8021 | // declaring a dllimport explicit instantiation definition for the same |
| 8022 | // template class, the codegen for the method call will not respect the |
| 8023 | // dllimport, while it will with cl. The Def will already have the DLL |
| 8024 | // attribute, since the Def and Specialization will be the same in the |
| 8025 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the |
| 8026 | // attribute to the Specialization; we just need to make it take effect. |
| 8027 | assert(Def == Specialization && |
| 8028 | "Def and Specialization should match for implicit instantiation"); |
| 8029 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 8030 | } |
| 8031 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8032 | // Set the template specialization kind. Make sure it is set before |
| 8033 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 8034 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8035 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 8036 | } else { |
| 8037 | |
| 8038 | // Set the template specialization kind. |
| 8039 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 8040 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8041 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8042 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 8043 | } |
| 8044 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8045 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8046 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8047 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 8048 | SourceLocation ExternLoc, |
| 8049 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8050 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8051 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 8052 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8053 | IdentifierInfo *Name, |
| 8054 | SourceLocation NameLoc, |
| 8055 | AttributeList *Attr) { |
| 8056 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 8057 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8058 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8059 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8060 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 8061 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8062 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 8063 | SourceLocation(), false, TypeResult(), |
| 8064 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8065 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 8066 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8067 | if (!TagD) |
| 8068 | return true; |
| 8069 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8070 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8071 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8072 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 8073 | if (Tag->isInvalidDecl()) |
| 8074 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8075 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8076 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 8077 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 8078 | if (!Pattern) { |
| 8079 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 8080 | << Context.getTypeDeclType(Record); |
| 8081 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 8082 | return true; |
| 8083 | } |
| 8084 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8085 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8086 | // If the explicit instantiation is for a class or member class, the |
| 8087 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8088 | // simple-template-id. |
| 8089 | // |
| 8090 | // C++98 has the same restriction, just worded differently. |
| 8091 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8092 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8093 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8094 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8095 | // C++0x [temp.explicit]p2: |
| 8096 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8097 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8098 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 8099 | TemplateSpecializationKind TSK |
| 8100 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8101 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8102 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8103 | // C++0x [temp.explicit]p2: |
| 8104 | // [...] An explicit instantiation shall appear in an enclosing |
| 8105 | // namespace of its template. [...] |
| 8106 | // |
| 8107 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8108 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8109 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8110 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8111 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8112 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8113 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8114 | PrevDecl = Record; |
| 8115 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8116 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8117 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8118 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8119 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8120 | PrevDecl, |
| 8121 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8122 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8123 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8124 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8125 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8126 | return TagD; |
| 8127 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8128 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8129 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8130 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8131 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8132 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8133 | // 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] | 8134 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8135 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8136 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8137 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 8138 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 8139 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8140 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 8141 | << Pattern; |
| 8142 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8143 | } else { |
| 8144 | if (InstantiateClass(NameLoc, Record, Def, |
| 8145 | getTemplateInstantiationArgs(Record), |
| 8146 | TSK)) |
| 8147 | return true; |
| 8148 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8149 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8150 | if (!RecordDef) |
| 8151 | return true; |
| 8152 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8153 | } |
| 8154 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8155 | // Instantiate all of the members of the class. |
| 8156 | InstantiateClassMembers(NameLoc, RecordDef, |
| 8157 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8158 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8159 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8160 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 8161 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8162 | // FIXME: We don't have any representation for explicit instantiations of |
| 8163 | // member classes. Such a representation is not needed for compilation, but it |
| 8164 | // should be available for clients that want to see all of the declarations in |
| 8165 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8166 | return TagD; |
| 8167 | } |
| 8168 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8169 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 8170 | SourceLocation ExternLoc, |
| 8171 | SourceLocation TemplateLoc, |
| 8172 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8173 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8174 | // TODO: check if/when DNInfo should replace Name. |
| 8175 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 8176 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8177 | if (!Name) { |
| 8178 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8179 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8180 | diag::err_explicit_instantiation_requires_name) |
| 8181 | << D.getDeclSpec().getSourceRange() |
| 8182 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8183 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8184 | return true; |
| 8185 | } |
| 8186 | |
| 8187 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 8188 | // we find one that is. |
| 8189 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 8190 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 8191 | S = S->getParent(); |
| 8192 | |
| 8193 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8194 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 8195 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8196 | if (R.isNull()) |
| 8197 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8198 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8199 | // C++ [dcl.stc]p1: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8200 | // A storage-class-specifier shall not be specified in [...] an explicit |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8201 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8202 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8203 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 8204 | << Name; |
| 8205 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8206 | } else if (D.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8207 | != DeclSpec::SCS_unspecified) { |
| 8208 | // Complain about then remove the storage class specifier. |
| 8209 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 8210 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8211 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8212 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8213 | } |
| 8214 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 8215 | // C++0x [temp.explicit]p1: |
| 8216 | // [...] An explicit instantiation of a function template shall not use the |
| 8217 | // inline or constexpr specifiers. |
| 8218 | // Presumably, this also applies to member functions of class templates as |
| 8219 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 8220 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8221 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8222 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 8223 | diag::err_explicit_instantiation_inline : |
| 8224 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 8225 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8226 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 8227 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 8228 | // not already specified. |
| 8229 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 8230 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8231 | |
Nathan Wilson | de49845 | 2016-02-08 05:34:00 +0000 | [diff] [blame] | 8232 | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
| 8233 | // applied only to the definition of a function template or variable template, |
| 8234 | // declared in namespace scope. |
| 8235 | if (D.getDeclSpec().isConceptSpecified()) { |
| 8236 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
| 8237 | diag::err_concept_specified_specialization) << 0; |
| 8238 | return true; |
| 8239 | } |
| 8240 | |
Richard Smith | 19a311a | 2017-02-09 22:47:51 +0000 | [diff] [blame] | 8241 | // A deduction guide is not on the list of entities that can be explicitly |
| 8242 | // instantiated. |
| 8243 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
| 8244 | Diag(D.getDeclSpec().getLocStart(), diag::err_deduction_guide_specialized) |
| 8245 | << /*explicit instantiation*/ 0; |
| 8246 | return true; |
| 8247 | } |
| 8248 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8249 | // C++0x [temp.explicit]p2: |
| 8250 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8251 | // definition and an explicit instantiation declaration. An explicit |
| 8252 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8253 | TemplateSpecializationKind TSK |
| 8254 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8255 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8256 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8257 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8258 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8259 | |
| 8260 | if (!R->isFunctionType()) { |
| 8261 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8262 | // A [...] static data member of a class template can be explicitly |
| 8263 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8264 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8265 | // C++1y [temp.explicit]p1: |
| 8266 | // A [...] variable [...] template specialization can be explicitly |
| 8267 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8268 | if (Previous.isAmbiguous()) |
| 8269 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8270 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 8271 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8272 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8273 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8274 | if (!PrevTemplate) { |
| 8275 | if (!Prev || !Prev->isStaticDataMember()) { |
| 8276 | // We expect to see a data data member here. |
| 8277 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 8278 | << Name; |
| 8279 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8280 | P != PEnd; ++P) |
| 8281 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 8282 | return true; |
| 8283 | } |
| 8284 | |
| 8285 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 8286 | // FIXME: Check for explicit specialization? |
| 8287 | Diag(D.getIdentifierLoc(), |
| 8288 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 8289 | << Prev; |
| 8290 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 8291 | // FIXME: Can we provide a note showing where this was declared? |
| 8292 | return true; |
| 8293 | } |
| 8294 | } else { |
| 8295 | // Explicitly instantiate a variable template. |
| 8296 | |
| 8297 | // C++1y [dcl.spec.auto]p6: |
| 8298 | // ... A program that uses auto or decltype(auto) in a context not |
| 8299 | // explicitly allowed in this section is ill-formed. |
| 8300 | // |
| 8301 | // This includes auto-typed variable template instantiations. |
| 8302 | if (R->isUndeducedType()) { |
| 8303 | Diag(T->getTypeLoc().getLocStart(), |
| 8304 | diag::err_auto_not_allowed_var_inst); |
| 8305 | return true; |
| 8306 | } |
| 8307 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8308 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 8309 | // C++1y [temp.explicit]p3: |
| 8310 | // If the explicit instantiation is for a variable, the unqualified-id |
| 8311 | // in the declaration shall be a template-id. |
| 8312 | Diag(D.getIdentifierLoc(), |
| 8313 | diag::err_explicit_instantiation_without_template_id) |
| 8314 | << PrevTemplate; |
| 8315 | Diag(PrevTemplate->getLocation(), |
| 8316 | diag::note_explicit_instantiation_here); |
| 8317 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8318 | } |
| 8319 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8320 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8321 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8322 | if (PrevTemplate->isConcept()) { |
| 8323 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8324 | << 1 /*variable*/ << 0 /*explicitly instantiated*/; |
| 8325 | Diag(PrevTemplate->getLocation(), diag::note_previous_declaration); |
| 8326 | return true; |
| 8327 | } |
| 8328 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8329 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8330 | TemplateArgumentListInfo TemplateArgs = |
| 8331 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8332 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8333 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 8334 | D.getIdentifierLoc(), TemplateArgs); |
| 8335 | if (Res.isInvalid()) |
| 8336 | return true; |
| 8337 | |
| 8338 | // Ignore access control bits, we don't need them for redeclaration |
| 8339 | // checking. |
| 8340 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8341 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8342 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8343 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8344 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8345 | // or a static data member of a class template specialization, the name of |
| 8346 | // the class template specialization in the qualified-id for the member |
| 8347 | // name shall be a simple-template-id. |
| 8348 | // |
| 8349 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8350 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 8351 | // This does not apply to variable template specializations, where the |
| 8352 | // template-id is in the unqualified-id instead. |
| 8353 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8354 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8355 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8356 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8357 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8358 | // Check the scope of this explicit instantiation. |
| 8359 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8360 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8361 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8362 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 8363 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8364 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8365 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8366 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8367 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8368 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8369 | if (!HasNoEffect) { |
| 8370 | // Instantiate static data member or variable template. |
| 8371 | |
| 8372 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 8373 | if (PrevTemplate) { |
| 8374 | // Merge attributes. |
| 8375 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 8376 | ProcessDeclAttributeList(S, Prev, Attr); |
| 8377 | } |
| 8378 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8379 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 8380 | } |
| 8381 | |
| 8382 | // Check the new variable specialization against the parsed input. |
| 8383 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 8384 | Diag(T->getTypeLoc().getLocStart(), |
| 8385 | diag::err_invalid_var_template_spec_type) |
| 8386 | << 0 << PrevTemplate << R << Prev->getType(); |
| 8387 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 8388 | << 2 << PrevTemplate->getDeclName(); |
| 8389 | return true; |
| 8390 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8391 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8392 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8393 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8394 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8395 | |
| 8396 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 8397 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8398 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8399 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8400 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8401 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8402 | HasExplicitTemplateArgs = true; |
| 8403 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8404 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8405 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8406 | // A [...] function [...] can be explicitly instantiated from its template. |
| 8407 | // A member function [...] of a class template can be explicitly |
| 8408 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8409 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8410 | UnresolvedSet<8> Matches; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8411 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8412 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8413 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8414 | P != PEnd; ++P) { |
| 8415 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8416 | if (!HasExplicitTemplateArgs) { |
| 8417 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 8418 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 8419 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 8420 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8421 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8422 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8423 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8424 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 8425 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8426 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8427 | } |
| 8428 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8429 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8430 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 8431 | if (!FunTmpl) |
| 8432 | continue; |
| 8433 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8434 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8435 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8436 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8437 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8438 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 8439 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8440 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8441 | // Keep track of almost-matches. |
| 8442 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8443 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8444 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8445 | (void)TDK; |
| 8446 | continue; |
| 8447 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8448 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8449 | // Target attributes are part of the cuda function signature, so |
| 8450 | // the cuda target of the instantiated function must match that of its |
| 8451 | // template. Given that C++ template deduction does not take |
| 8452 | // target attributes into account, we reject candidates here that |
| 8453 | // have a different target. |
| 8454 | if (LangOpts.CUDA && |
| 8455 | IdentifyCUDATarget(Specialization, |
| 8456 | /* IgnoreImplicitHDAttributes = */ true) != |
| 8457 | IdentifyCUDATarget(Attr)) { |
| 8458 | FailedCandidates.addCandidate().set( |
| 8459 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 8460 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8461 | continue; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8462 | } |
| 8463 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8464 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8465 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8466 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8467 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8468 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 8469 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8470 | D.getIdentifierLoc(), |
| 8471 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 8472 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 8473 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8474 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8475 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8476 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8477 | |
| 8478 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 8479 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8480 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 8481 | // C++11 [except.spec]p4 |
| 8482 | // In an explicit instantiation an exception-specification may be specified, |
| 8483 | // but is not required. |
| 8484 | // If an exception-specification is specified in an explicit instantiation |
| 8485 | // directive, it shall be compatible with the exception-specifications of |
| 8486 | // other declarations of that function. |
| 8487 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 8488 | if (FPT->hasExceptionSpec()) { |
| 8489 | unsigned DiagID = |
| 8490 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 8491 | if (getLangOpts().MicrosoftExt) |
| 8492 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 8493 | bool Result = CheckEquivalentExceptionSpec( |
| 8494 | PDiag(DiagID) << Specialization->getType(), |
| 8495 | PDiag(diag::note_explicit_instantiation_here), |
| 8496 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 8497 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 8498 | // In Microsoft mode, mismatching exception specifications just cause a |
| 8499 | // warning. |
| 8500 | if (!getLangOpts().MicrosoftExt && Result) |
| 8501 | return true; |
| 8502 | } |
| 8503 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8504 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8505 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8506 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 8507 | << Specialization |
| 8508 | << (Specialization->getTemplateSpecializationKind() == |
| 8509 | TSK_ExplicitSpecialization); |
| 8510 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 8511 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8512 | } |
| 8513 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8514 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8515 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 8516 | PrevDecl = Specialization; |
| 8517 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8518 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8519 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8520 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8521 | PrevDecl, |
| 8522 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8523 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8524 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8525 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8526 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8527 | // FIXME: We may still want to build some representation of this |
| 8528 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8529 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8530 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8531 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 8532 | |
| 8533 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 8534 | if (Attr) |
| 8535 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8536 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8537 | if (Specialization->isDefined()) { |
| 8538 | // Let the ASTConsumer know that this function has been explicitly |
| 8539 | // instantiated now, and its linkage might have changed. |
| 8540 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 8541 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 8542 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8543 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8544 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8545 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8546 | // or a static data member of a class template specialization, the name of |
| 8547 | // the class template specialization in the qualified-id for the member |
| 8548 | // name shall be a simple-template-id. |
| 8549 | // |
| 8550 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8551 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8552 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8553 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8554 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8555 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8556 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8557 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8558 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8559 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8560 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8561 | if (FunTmpl && FunTmpl->isConcept() && |
| 8562 | !D.getDeclSpec().isConceptSpecified()) { |
| 8563 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8564 | << 0 /*function*/ << 0 /*explicitly instantiated*/; |
| 8565 | Diag(FunTmpl->getLocation(), diag::note_previous_declaration); |
| 8566 | return true; |
| 8567 | } |
| 8568 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8569 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8570 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8571 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8572 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8573 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8574 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8575 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8576 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8577 | } |
| 8578 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8579 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8580 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 8581 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 8582 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 8583 | // This has to hold, because SS is expected to be defined. |
| 8584 | assert(Name && "Expected a name in a dependent tag"); |
| 8585 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8586 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8587 | if (!NNS) |
| 8588 | return true; |
| 8589 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8590 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 8591 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8592 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 8593 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8594 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8595 | return true; |
| 8596 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8597 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8598 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8599 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8600 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8601 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8602 | // Create type-source location information for this type. |
| 8603 | TypeLocBuilder TLB; |
| 8604 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8605 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8606 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8607 | TL.setNameLoc(NameLoc); |
| 8608 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8609 | } |
| 8610 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8611 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8612 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 8613 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 8614 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8615 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8616 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8617 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8618 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8619 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8620 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8621 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8622 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8623 | << FixItHint::CreateRemoval(TypenameLoc); |
| 8624 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8625 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8626 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 8627 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 8628 | if (T.isNull()) |
| 8629 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8630 | |
| 8631 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8632 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8633 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8634 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8635 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8636 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8637 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8638 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8639 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8640 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8641 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8642 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8643 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8644 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8645 | } |
| 8646 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8647 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8648 | Sema::ActOnTypenameType(Scope *S, |
| 8649 | SourceLocation TypenameLoc, |
| 8650 | const CXXScopeSpec &SS, |
| 8651 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8652 | TemplateTy TemplateIn, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8653 | IdentifierInfo *TemplateII, |
| 8654 | SourceLocation TemplateIILoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8655 | SourceLocation LAngleLoc, |
| 8656 | ASTTemplateArgsPtr TemplateArgsIn, |
| 8657 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8658 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8659 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8660 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8661 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8662 | diag::ext_typename_outside_of_template) |
| 8663 | << FixItHint::CreateRemoval(TypenameLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8664 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8665 | // Strangely, non-type results are not ignored by this lookup, so the |
| 8666 | // program is ill-formed if it finds an injected-class-name. |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 8667 | if (TypenameLoc.isValid()) { |
| 8668 | auto *LookupRD = |
| 8669 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
| 8670 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 8671 | Diag(TemplateIILoc, |
| 8672 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 8673 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 8674 | << (TemplateKWLoc.isValid() ? 1 : 0 /*'template'/'typename' keyword*/); |
| 8675 | } |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8676 | } |
| 8677 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8678 | // Translate the parser's template argument list in our AST format. |
| 8679 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8680 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8681 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8682 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8683 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 8684 | // Construct a dependent template specialization type. |
| 8685 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8686 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8687 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 8688 | DTN->getQualifier(), |
| 8689 | DTN->getIdentifier(), |
| 8690 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8691 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8692 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8693 | TypeLocBuilder Builder; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8694 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8695 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8696 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 8697 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 8698 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8699 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8700 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8701 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8702 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8703 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8704 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 8705 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8706 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8707 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8708 | if (T.isNull()) |
| 8709 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8710 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8711 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8712 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8713 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8714 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8715 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8716 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8717 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8718 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8719 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8720 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8721 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8722 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 8723 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8724 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8725 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8726 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8727 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 8728 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 8729 | } |
| 8730 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8731 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8732 | /// Determine whether this failed name lookup should be treated as being |
| 8733 | /// disabled by a usage of std::enable_if. |
| 8734 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8735 | SourceRange &CondRange) { |
| 8736 | // We must be looking for a ::type... |
| 8737 | if (!II.isStr("type")) |
| 8738 | return false; |
| 8739 | |
| 8740 | // ... within an explicitly-written template specialization... |
| 8741 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8742 | return false; |
| 8743 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8744 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8745 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8746 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8747 | return false; |
| 8748 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8749 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8750 | |
| 8751 | // ... which names a complete class template declaration... |
| 8752 | const TemplateDecl *EnableIfDecl = |
| 8753 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8754 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8755 | return false; |
| 8756 | |
| 8757 | // ... called "enable_if". |
| 8758 | const IdentifierInfo *EnableIfII = |
| 8759 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8760 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8761 | return false; |
| 8762 | |
| 8763 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8764 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8765 | return true; |
| 8766 | } |
| 8767 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8768 | /// \brief Build the type that describes a C++ typename specifier, |
| 8769 | /// e.g., "typename T::type". |
| 8770 | QualType |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8771 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8772 | SourceLocation KeywordLoc, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8773 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8774 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8775 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8776 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8777 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8778 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8779 | DeclContext *Ctx = computeDeclContext(SS); |
| 8780 | if (!Ctx) { |
| 8781 | // If the nested-name-specifier is dependent and couldn't be |
| 8782 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8783 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8784 | return Context.getDependentNameType(Keyword, |
| 8785 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8786 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8787 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8788 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8789 | // If the nested-name-specifier refers to the current instantiation, |
| 8790 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8791 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8792 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8793 | // 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] | 8794 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8795 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8796 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8797 | |
| 8798 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8799 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8800 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8801 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8802 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8803 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8804 | case LookupResult::NotFound: { |
| 8805 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8806 | // a more specific diagnostic. |
| 8807 | SourceRange CondRange; |
| 8808 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8809 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8810 | << Ctx << CondRange; |
| 8811 | return QualType(); |
| 8812 | } |
| 8813 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8814 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8815 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8816 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8817 | |
| 8818 | case LookupResult::FoundUnresolvedValue: { |
| 8819 | // We found a using declaration that is a value. Most likely, the using |
| 8820 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8821 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8822 | IILoc); |
| 8823 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8824 | << Name << Ctx << FullRange; |
| 8825 | if (UnresolvedUsingValueDecl *Using |
| 8826 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8827 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8828 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8829 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8830 | } |
| 8831 | } |
| 8832 | // Fall through to create a dependent typename type, from which we can recover |
| 8833 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8834 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8835 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8836 | // Okay, it's a member of an unknown instantiation. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8837 | return Context.getDependentNameType(Keyword, |
| 8838 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8839 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8840 | |
| 8841 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8842 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8843 | // C++ [class.qual]p2: |
| 8844 | // In a lookup in which function names are not ignored and the |
| 8845 | // nested-name-specifier nominates a class C, if the name specified |
| 8846 | // after the nested-name-specifier, when looked up in C, is the |
| 8847 | // injected-class-name of C [...] then the name is instead considered |
| 8848 | // to name the constructor of class C. |
| 8849 | // |
| 8850 | // Unlike in an elaborated-type-specifier, function names are not ignored |
| 8851 | // in typename-specifier lookup. However, they are ignored in all the |
| 8852 | // contexts where we form a typename type with no keyword (that is, in |
| 8853 | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). |
| 8854 | // |
| 8855 | // FIXME: That's not strictly true: mem-initializer-id lookup does not |
| 8856 | // ignore functions, but that appears to be an oversight. |
| 8857 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
| 8858 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
| 8859 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
| 8860 | FoundRD->isInjectedClassName() && |
| 8861 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
| 8862 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 8863 | << &II << 1 << 0 /*'typename' keyword used*/; |
| 8864 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8865 | // We found a type. Build an ElaboratedType, since the |
| 8866 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8867 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8868 | return Context.getElaboratedType(Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8869 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8870 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8871 | } |
| 8872 | |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 8873 | // C++ [dcl.type.simple]p2: |
| 8874 | // A type-specifier of the form |
| 8875 | // typename[opt] nested-name-specifier[opt] template-name |
| 8876 | // is a placeholder for a deduced class type [...]. |
| 8877 | if (getLangOpts().CPlusPlus1z) { |
| 8878 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
| 8879 | return Context.getElaboratedType( |
| 8880 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
| 8881 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
| 8882 | QualType(), false)); |
| 8883 | } |
| 8884 | } |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 8885 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8886 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8887 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8888 | break; |
| 8889 | |
| 8890 | case LookupResult::FoundOverloaded: |
| 8891 | DiagID = diag::err_typename_nested_not_type; |
| 8892 | Referenced = *Result.begin(); |
| 8893 | break; |
| 8894 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8895 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8896 | return QualType(); |
| 8897 | } |
| 8898 | |
| 8899 | // If we get here, it's because name lookup did not find a |
| 8900 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8901 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8902 | IILoc); |
| 8903 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8904 | if (Referenced) |
| 8905 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8906 | << Name; |
| 8907 | return QualType(); |
| 8908 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8909 | |
| 8910 | namespace { |
| 8911 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8912 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8913 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8914 | SourceLocation Loc; |
| 8915 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8916 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8917 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8918 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8919 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8920 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8921 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8922 | DeclarationName Entity) |
| 8923 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8924 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8925 | |
| 8926 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8927 | /// transformed. |
| 8928 | /// |
| 8929 | /// For the purposes of type reconstruction, a type has already been |
| 8930 | /// transformed if it is NULL or if it is not dependent. |
| 8931 | bool AlreadyTransformed(QualType T) { |
| 8932 | return T.isNull() || !T->isDependentType(); |
| 8933 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8934 | |
| 8935 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8936 | /// rebuilt. |
| 8937 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8938 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8939 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8940 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8941 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8942 | /// \brief Sets the "base" location and entity when that |
| 8943 | /// information is known based on another transformation. |
| 8944 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8945 | this->Loc = Loc; |
| 8946 | this->Entity = Entity; |
| 8947 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8948 | |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8949 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8950 | // Lambdas never need to be transformed. |
| 8951 | return E; |
| 8952 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8953 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 8954 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8955 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8956 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8957 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8958 | /// 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] | 8959 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8960 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8961 | /// partial specialization thereof). This routine will rebuild that type now |
| 8962 | /// that we have entered the declarator's scope, which may produce different |
| 8963 | /// canonical types, e.g., |
| 8964 | /// |
| 8965 | /// \code |
| 8966 | /// template<typename T> |
| 8967 | /// struct X { |
| 8968 | /// typedef T* pointer; |
| 8969 | /// pointer data(); |
| 8970 | /// }; |
| 8971 | /// |
| 8972 | /// template<typename T> |
| 8973 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8974 | /// \endcode |
| 8975 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8976 | /// 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] | 8977 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8978 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8979 | /// 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] | 8980 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8981 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8982 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8983 | SourceLocation Loc, |
| 8984 | DeclarationName Name) { |
| 8985 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8986 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8987 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8988 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8989 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8990 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8991 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8992 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8993 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8994 | DeclarationName()); |
| 8995 | return Rebuilder.TransformExpr(E); |
| 8996 | } |
| 8997 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8998 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8999 | if (SS.isInvalid()) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9000 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9001 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9002 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9003 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 9004 | DeclarationName()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9005 | NestedNameSpecifierLoc Rebuilt |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9006 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9007 | if (!Rebuilt) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9008 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9009 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 9010 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 9011 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 9012 | } |
| 9013 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9014 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 9015 | /// instantiation. |
| 9016 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 9017 | TemplateParameterList *Params) { |
| 9018 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 9019 | Decl *Param = Params->getParam(I); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9020 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9021 | // There is nothing to rebuild in a type parameter. |
| 9022 | if (isa<TemplateTypeParmDecl>(Param)) |
| 9023 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9024 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9025 | // Rebuild the template parameter list of a template template parameter. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9026 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9027 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 9028 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 9029 | TTP->getTemplateParameters())) |
| 9030 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9031 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9032 | continue; |
| 9033 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9034 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9035 | // Rebuild the type of a non-type template parameter. |
| 9036 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9037 | TypeSourceInfo *NewTSI |
| 9038 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 9039 | NTTP->getLocation(), |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9040 | NTTP->getDeclName()); |
| 9041 | if (!NewTSI) |
| 9042 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9043 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9044 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 9045 | NTTP->setTypeSourceInfo(NewTSI); |
| 9046 | NTTP->setType(NewTSI->getType()); |
| 9047 | } |
| 9048 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 9049 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 9050 | return false; |
| 9051 | } |
| 9052 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9053 | /// \brief Produces a formatted string that describes the binding of |
| 9054 | /// template parameters to template arguments. |
| 9055 | std::string |
| 9056 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9057 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9058 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9059 | } |
| 9060 | |
| 9061 | std::string |
| 9062 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9063 | const TemplateArgument *Args, |
| 9064 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9065 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9066 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9067 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9068 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9069 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9070 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9071 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 9072 | if (I >= NumArgs) |
| 9073 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9074 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9075 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9076 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9077 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9078 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9079 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9080 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9081 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9082 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9083 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9084 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9085 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9086 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 9087 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9088 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9089 | |
| 9090 | Out << ']'; |
| 9091 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9092 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9093 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9094 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 9095 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9096 | if (!FD) |
| 9097 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9098 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9099 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9100 | |
| 9101 | // Take tokens to avoid allocations |
| 9102 | LPT->Toks.swap(Toks); |
| 9103 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9104 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9105 | |
| 9106 | FD->setLateTemplateParsed(true); |
| 9107 | } |
| 9108 | |
| 9109 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 9110 | if (!FD) |
| 9111 | return; |
| 9112 | FD->setLateTemplateParsed(false); |
| 9113 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9114 | |
| 9115 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 9116 | DeclContext *DC = CurContext; |
| 9117 | |
| 9118 | while (DC) { |
| 9119 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 9120 | const FunctionDecl *FD = RD->isLocalClass(); |
| 9121 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 9122 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 9123 | return false; |
| 9124 | |
| 9125 | DC = DC->getParent(); |
| 9126 | } |
| 9127 | return false; |
| 9128 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9129 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 9130 | namespace { |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9131 | /// \brief Walk the path from which a declaration was instantiated, and check |
| 9132 | /// that every explicit specialization along that path is visible. This enforces |
| 9133 | /// C++ [temp.expl.spec]/6: |
| 9134 | /// |
| 9135 | /// If a template, a member template or a member of a class template is |
| 9136 | /// explicitly specialized then that specialization shall be declared before |
| 9137 | /// the first use of that specialization that would cause an implicit |
| 9138 | /// instantiation to take place, in every translation unit in which such a |
| 9139 | /// use occurs; no diagnostic is required. |
| 9140 | /// |
| 9141 | /// and also C++ [temp.class.spec]/1: |
| 9142 | /// |
| 9143 | /// A partial specialization shall be declared before the first use of a |
| 9144 | /// class template specialization that would make use of the partial |
| 9145 | /// specialization as the result of an implicit or explicit instantiation |
| 9146 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 9147 | /// required. |
| 9148 | class ExplicitSpecializationVisibilityChecker { |
| 9149 | Sema &S; |
| 9150 | SourceLocation Loc; |
| 9151 | llvm::SmallVector<Module *, 8> Modules; |
| 9152 | |
| 9153 | public: |
| 9154 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 9155 | : S(S), Loc(Loc) {} |
| 9156 | |
| 9157 | void check(NamedDecl *ND) { |
| 9158 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 9159 | return checkImpl(FD); |
| 9160 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 9161 | return checkImpl(RD); |
| 9162 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 9163 | return checkImpl(VD); |
| 9164 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 9165 | return checkImpl(ED); |
| 9166 | } |
| 9167 | |
| 9168 | private: |
| 9169 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 9170 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 9171 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 9172 | const bool Recover = true; |
| 9173 | |
| 9174 | // If we got a custom set of modules (because only a subset of the |
| 9175 | // declarations are interesting), use them, otherwise let |
| 9176 | // diagnoseMissingImport intelligently pick some. |
| 9177 | if (Modules.empty()) |
| 9178 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 9179 | else |
| 9180 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 9181 | } |
| 9182 | |
| 9183 | // Check a specific declaration. There are three problematic cases: |
| 9184 | // |
| 9185 | // 1) The declaration is an explicit specialization of a template |
| 9186 | // specialization. |
| 9187 | // 2) The declaration is an explicit specialization of a member of an |
| 9188 | // templated class. |
| 9189 | // 3) The declaration is an instantiation of a template, and that template |
| 9190 | // is an explicit specialization of a member of a templated class. |
| 9191 | // |
| 9192 | // We don't need to go any deeper than that, as the instantiation of the |
| 9193 | // surrounding class / etc is not triggered by whatever triggered this |
| 9194 | // instantiation, and thus should be checked elsewhere. |
| 9195 | template<typename SpecDecl> |
| 9196 | void checkImpl(SpecDecl *Spec) { |
| 9197 | bool IsHiddenExplicitSpecialization = false; |
| 9198 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 9199 | IsHiddenExplicitSpecialization = |
| 9200 | Spec->getMemberSpecializationInfo() |
| 9201 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
| 9202 | : !S.hasVisibleDeclaration(Spec); |
| 9203 | } else { |
| 9204 | checkInstantiated(Spec); |
| 9205 | } |
| 9206 | |
| 9207 | if (IsHiddenExplicitSpecialization) |
| 9208 | diagnose(Spec->getMostRecentDecl(), false); |
| 9209 | } |
| 9210 | |
| 9211 | void checkInstantiated(FunctionDecl *FD) { |
| 9212 | if (auto *TD = FD->getPrimaryTemplate()) |
| 9213 | checkTemplate(TD); |
| 9214 | } |
| 9215 | |
| 9216 | void checkInstantiated(CXXRecordDecl *RD) { |
| 9217 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 9218 | if (!SD) |
| 9219 | return; |
| 9220 | |
| 9221 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 9222 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 9223 | checkTemplate(TD); |
| 9224 | else if (auto *TD = |
| 9225 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 9226 | if (!S.hasVisibleDeclaration(TD)) |
| 9227 | diagnose(TD, true); |
| 9228 | checkTemplate(TD); |
| 9229 | } |
| 9230 | } |
| 9231 | |
| 9232 | void checkInstantiated(VarDecl *RD) { |
| 9233 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 9234 | if (!SD) |
| 9235 | return; |
| 9236 | |
| 9237 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 9238 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 9239 | checkTemplate(TD); |
| 9240 | else if (auto *TD = |
| 9241 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 9242 | if (!S.hasVisibleDeclaration(TD)) |
| 9243 | diagnose(TD, true); |
| 9244 | checkTemplate(TD); |
| 9245 | } |
| 9246 | } |
| 9247 | |
| 9248 | void checkInstantiated(EnumDecl *FD) {} |
| 9249 | |
| 9250 | template<typename TemplDecl> |
| 9251 | void checkTemplate(TemplDecl *TD) { |
| 9252 | if (TD->isMemberSpecialization()) { |
| 9253 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 9254 | diagnose(TD->getMostRecentDecl(), false); |
| 9255 | } |
| 9256 | } |
| 9257 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 9258 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9259 | |
| 9260 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 9261 | if (!getLangOpts().Modules) |
| 9262 | return; |
| 9263 | |
| 9264 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 9265 | } |
| 9266 | |
| 9267 | /// \brief Check whether a template partial specialization that we've discovered |
| 9268 | /// is hidden, and produce suitable diagnostics if so. |
| 9269 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 9270 | NamedDecl *Spec) { |
| 9271 | llvm::SmallVector<Module *, 8> Modules; |
| 9272 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 9273 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 9274 | MissingImportKind::PartialSpecialization, |
| 9275 | /*Recover*/true); |
| 9276 | } |