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 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 48 | /// \brief Determine whether the declaration found is acceptable as the name |
| 49 | /// of a template and, if so, return that template declaration. Otherwise, |
| 50 | /// returns NULL. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 51 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 52 | NamedDecl *Orig, |
| 53 | bool AllowFunctionTemplates) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 54 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 56 | if (isa<TemplateDecl>(D)) { |
| 57 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 58 | return nullptr; |
| 59 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 60 | return Orig; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 61 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 63 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 64 | // C++ [temp.local]p1: |
| 65 | // Like normal (non-template) classes, class templates have an |
| 66 | // injected-class-name (Clause 9). The injected-class-name |
| 67 | // can be used with or without a template-argument-list. When |
| 68 | // it is used without a template-argument-list, it is |
| 69 | // equivalent to the injected-class-name followed by the |
| 70 | // template-parameters of the class template enclosed in |
| 71 | // <>. When it is used with a template-argument-list, it |
| 72 | // refers to the specified class template specialization, |
| 73 | // which could be the current specialization or another |
| 74 | // specialization. |
| 75 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 76 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 77 | if (Record->getDescribedClassTemplate()) |
| 78 | return Record->getDescribedClassTemplate(); |
| 79 | |
| 80 | if (ClassTemplateSpecializationDecl *Spec |
| 81 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 82 | return Spec->getSpecializedTemplate(); |
| 83 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 85 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 86 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 88 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 91 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 92 | bool AllowFunctionTemplates) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 93 | // The set of class templates we've already seen. |
| 94 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 95 | LookupResult::Filter filter = R.makeFilter(); |
| 96 | while (filter.hasNext()) { |
| 97 | NamedDecl *Orig = filter.next(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 98 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 99 | AllowFunctionTemplates); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 100 | if (!Repl) |
| 101 | filter.erase(); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 102 | else if (Repl != Orig) { |
| 103 | |
| 104 | // C++ [temp.local]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 105 | // 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] | 106 | // 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] | 107 | // one base class). If all of the injected-class-names that are found |
| 108 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 109 | // is used as a template-name, the reference refers to the class |
| 110 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 111 | // ambiguous. |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 112 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 113 | if (!ClassTemplates.insert(ClassTmpl).second) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 114 | filter.erase(); |
| 115 | continue; |
| 116 | } |
John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 117 | |
| 118 | // FIXME: we promote access to public here as a workaround to |
| 119 | // the fact that LookupResult doesn't let us remember that we |
| 120 | // found this template through a particular injected class name, |
| 121 | // which means we end up doing nasty things to the invariants. |
| 122 | // Pretending that access is public is *much* safer. |
| 123 | filter.replace(Repl, AS_public); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 124 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 125 | } |
| 126 | filter.done(); |
| 127 | } |
| 128 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 129 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 130 | bool AllowFunctionTemplates) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 131 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 132 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 133 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 134 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 135 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 138 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 139 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 140 | bool hasTemplateKeyword, |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 141 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 142 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 143 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 144 | TemplateTy &TemplateResult, |
| 145 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 146 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 148 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 149 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 151 | switch (Name.getKind()) { |
| 152 | case UnqualifiedId::IK_Identifier: |
| 153 | TName = DeclarationName(Name.Identifier); |
| 154 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 155 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 156 | case UnqualifiedId::IK_OperatorFunctionId: |
| 157 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 158 | Name.OperatorFunctionId.Operator); |
| 159 | break; |
| 160 | |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 161 | case UnqualifiedId::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 162 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 163 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 165 | default: |
| 166 | return TNK_Non_template; |
| 167 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 169 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 171 | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 172 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 173 | MemberOfUnknownSpecialization); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 174 | if (R.empty()) return TNK_Non_template; |
| 175 | if (R.isAmbiguous()) { |
| 176 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 177 | R.suppressDiagnostics(); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 178 | |
| 179 | // FIXME: we might have ambiguous templates, in which case we |
| 180 | // should at least parse them properly! |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 181 | return TNK_Non_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 182 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 183 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 184 | TemplateName Template; |
| 185 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 187 | unsigned ResultCount = R.end() - R.begin(); |
| 188 | if (ResultCount > 1) { |
| 189 | // We assume that we'll preserve the qualifier from a function |
| 190 | // template name in other ways. |
| 191 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 192 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 193 | |
| 194 | // We'll do this lookup again later. |
| 195 | R.suppressDiagnostics(); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 196 | } else { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 197 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 198 | |
| 199 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 200 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 201 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 202 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 203 | } else { |
| 204 | Template = TemplateName(TD); |
| 205 | } |
| 206 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 207 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 208 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 209 | |
| 210 | // We'll do this lookup again later. |
| 211 | R.suppressDiagnostics(); |
| 212 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 213 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 214 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
| 215 | isa<BuiltinTemplateDecl>(TD)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 216 | TemplateKind = |
| 217 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 218 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 219 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 221 | TemplateResult = TemplateTy::make(Template); |
| 222 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 223 | } |
| 224 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 225 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 226 | SourceLocation IILoc, |
| 227 | Scope *S, |
| 228 | const CXXScopeSpec *SS, |
| 229 | TemplateTy &SuggestedTemplate, |
| 230 | TemplateNameKind &SuggestedKind) { |
| 231 | // We can't recover unless there's a dependent scope specifier preceding the |
| 232 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 233 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 234 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 235 | computeDeclContext(*SS)) |
| 236 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 237 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 238 | // The code is missing a 'template' keyword prior to the dependent template |
| 239 | // name. |
| 240 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 241 | Diag(IILoc, diag::err_template_kw_missing) |
| 242 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 243 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 244 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 245 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 246 | SuggestedKind = TNK_Dependent_template_name; |
| 247 | return true; |
| 248 | } |
| 249 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 250 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 251 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 252 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 253 | bool EnteringContext, |
| 254 | bool &MemberOfUnknownSpecialization) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 255 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 256 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 257 | DeclContext *LookupCtx = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 258 | bool isDependent = false; |
| 259 | if (!ObjectType.isNull()) { |
| 260 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 261 | // x->B::f, and we are looking into the type of the object. |
| 262 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 263 | LookupCtx = computeDeclContext(ObjectType); |
| 264 | isDependent = ObjectType->isDependentType(); |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 265 | assert((isDependent || !ObjectType->isIncompleteType() || |
| 266 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 267 | "Caller should have completed object type"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 268 | |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 269 | // Template names cannot appear inside an Objective-C class or object type. |
| 270 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 271 | Found.clear(); |
| 272 | return; |
| 273 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 274 | } else if (SS.isSet()) { |
| 275 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 276 | // so long into the context associated with the prior nested-name-specifier. |
| 277 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 278 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 279 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 280 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 281 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | |
| 285 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 286 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 287 | if (LookupCtx) { |
| 288 | // Perform "qualified" name lookup into the declaration context we |
| 289 | // computed, which is either the type of the base of a member access |
| 290 | // expression or the declaration context associated with a prior |
| 291 | // nested-name-specifier. |
| 292 | LookupQualifiedName(Found, LookupCtx); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 293 | if (!ObjectType.isNull() && Found.empty()) { |
| 294 | // C++ [basic.lookup.classref]p1: |
| 295 | // In a class member access expression (5.2.5), if the . or -> token is |
| 296 | // immediately followed by an identifier followed by a <, the |
| 297 | // identifier must be looked up to determine whether the < is the |
| 298 | // beginning of a template argument list (14.2) or a less-than operator. |
| 299 | // The identifier is first looked up in the class of the object |
| 300 | // expression. If the identifier is not found, it is then looked up in |
| 301 | // the context of the entire postfix-expression and shall name a class |
| 302 | // or function template. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 303 | if (S) LookupName(Found, S); |
| 304 | ObjectTypeSearchedInScope = true; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 305 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 306 | } |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 307 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 308 | // We cannot look into a dependent object type or nested nme |
| 309 | // specifier. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 310 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 311 | return; |
| 312 | } else { |
| 313 | // Perform unqualified name lookup in the current scope. |
| 314 | LookupName(Found, S); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 315 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 316 | if (!ObjectType.isNull()) |
| 317 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 320 | if (Found.empty() && !isDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 321 | // If we did not find any names, attempt to correct any typos. |
| 322 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 323 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 324 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 325 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 326 | FilterCCC->WantTypeSpecifiers = false; |
| 327 | FilterCCC->WantExpressionKeywords = false; |
| 328 | FilterCCC->WantRemainingKeywords = false; |
| 329 | FilterCCC->WantCXXNamedCasts = true; |
| 330 | if (TypoCorrection Corrected = CorrectTypo( |
| 331 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 332 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 333 | Found.setLookupName(Corrected.getCorrection()); |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 334 | if (auto *ND = Corrected.getFoundDecl()) |
| 335 | Found.addDecl(ND); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 336 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 337 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 338 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 339 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 340 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 341 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 342 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 343 | << Name << LookupCtx << DroppedSpecifier |
| 344 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 345 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 346 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 347 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 348 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 349 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 350 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 354 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 355 | if (Found.empty()) { |
| 356 | if (isDependent) |
| 357 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 358 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 359 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 361 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 362 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 363 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 364 | // [...] If the lookup in the class of the object expression finds a |
| 365 | // template, the name is also looked up in the context of the entire |
| 366 | // postfix-expression and [...] |
| 367 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 368 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 369 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 370 | LookupOrdinaryName); |
| 371 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 372 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 373 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 374 | if (FoundOuter.empty()) { |
| 375 | // - if the name is not found, the name found in the class of the |
| 376 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 377 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 378 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 379 | // - if the name is found in the context of the entire |
| 380 | // postfix-expression and does not name a class template, the name |
| 381 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 382 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 383 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 384 | // - if the name found is a class template, it must refer to the same |
| 385 | // entity as the one found in the class of the object expression, |
| 386 | // otherwise the program is ill-formed. |
| 387 | if (!Found.isSingleResult() || |
| 388 | Found.getFoundDecl()->getCanonicalDecl() |
| 389 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 390 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 391 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 392 | << Found.getLookupName() |
| 393 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 394 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 395 | diag::note_ambig_member_ref_object_type) |
| 396 | << ObjectType; |
| 397 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 398 | diag::note_ambig_member_ref_scope); |
| 399 | |
| 400 | // Recover by taking the template that we found in the object |
| 401 | // expression's type. |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 407 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 408 | /// was just parsed. This is only possible with an explicit scope |
| 409 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 410 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 411 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 412 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 413 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 414 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 415 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 416 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 417 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 418 | // C++11 [expr.prim.general]p12: |
| 419 | // An id-expression that denotes a non-static data member or non-static |
| 420 | // member function of a class can only be used: |
| 421 | // (...) |
| 422 | // - if that id-expression denotes a non-static data member and it |
| 423 | // appears in an unevaluated operand. |
| 424 | // |
| 425 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 426 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 427 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 428 | // always a MemberExpr. |
| 429 | bool MightBeCxx11UnevalField = |
| 430 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 431 | |
Akira Hatanaka | d644e02 | 2016-12-16 03:19:41 +0000 | [diff] [blame] | 432 | // Check if the nested name specifier is an enum type. |
| 433 | bool IsEnum = false; |
| 434 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
| 435 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
| 436 | |
| 437 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 438 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 439 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 440 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 441 | // Since the 'this' expression is synthesized, we don't need to |
| 442 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 443 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 444 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 445 | return CXXDependentScopeMemberExpr::Create( |
| 446 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 447 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 448 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 451 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 452 | } |
| 453 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 454 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 455 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 456 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 457 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 458 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 459 | return DependentScopeDeclRefExpr::Create( |
| 460 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 461 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 464 | |
| 465 | /// Determine whether we would be unable to instantiate this template (because |
| 466 | /// it either has no definition, or is in the process of being instantiated). |
| 467 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 468 | NamedDecl *Instantiation, |
| 469 | bool InstantiatedFromMember, |
| 470 | const NamedDecl *Pattern, |
| 471 | const NamedDecl *PatternDef, |
| 472 | TemplateSpecializationKind TSK, |
| 473 | bool Complain /*= true*/) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 474 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 475 | isa<VarDecl>(Instantiation)); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 476 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 477 | bool IsEntityBeingDefined = false; |
| 478 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 479 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 480 | |
| 481 | if (PatternDef && !IsEntityBeingDefined) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 482 | NamedDecl *SuggestedDef = nullptr; |
| 483 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 484 | /*OnlyNeedComplete*/false)) { |
| 485 | // If we're allowed to diagnose this and recover, do so. |
| 486 | bool Recover = Complain && !isSFINAEContext(); |
| 487 | if (Complain) |
| 488 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 489 | Sema::MissingImportKind::Definition, Recover); |
| 490 | return !Recover; |
| 491 | } |
| 492 | return false; |
| 493 | } |
| 494 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 495 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 496 | return true; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 497 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 498 | llvm::Optional<unsigned> Note; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 499 | QualType InstantiationTy; |
| 500 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 501 | InstantiationTy = Context.getTypeDeclType(TD); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 502 | if (PatternDef) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 503 | Diag(PointOfInstantiation, |
| 504 | diag::err_template_instantiate_within_definition) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 505 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 506 | << InstantiationTy; |
| 507 | // Not much point in noting the template declaration here, since |
| 508 | // we're lexically inside it. |
| 509 | Instantiation->setInvalidDecl(); |
| 510 | } else if (InstantiatedFromMember) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 511 | if (isa<FunctionDecl>(Instantiation)) { |
| 512 | Diag(PointOfInstantiation, |
| 513 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 514 | << /*member function*/ 1 << Instantiation->getDeclName() |
| 515 | << Instantiation->getDeclContext(); |
| 516 | Note = diag::note_explicit_instantiation_here; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 517 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 518 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 519 | Diag(PointOfInstantiation, |
| 520 | diag::err_implicit_instantiate_member_undefined) |
| 521 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 522 | Note = diag::note_member_declared_at; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 523 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 524 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 525 | if (isa<FunctionDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 526 | Diag(PointOfInstantiation, |
| 527 | diag::err_explicit_instantiation_undefined_func_template) |
| 528 | << Pattern; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 529 | Note = diag::note_explicit_instantiation_here; |
| 530 | } else if (isa<TagDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 531 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 532 | << (TSK != TSK_ImplicitInstantiation) |
| 533 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 534 | Note = diag::note_template_decl_here; |
| 535 | } else { |
| 536 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 537 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 538 | Diag(PointOfInstantiation, |
| 539 | diag::err_explicit_instantiation_undefined_var_template) |
| 540 | << Instantiation; |
| 541 | Instantiation->setInvalidDecl(); |
| 542 | } else |
| 543 | Diag(PointOfInstantiation, |
| 544 | diag::err_explicit_instantiation_undefined_member) |
| 545 | << /*static data member*/ 2 << Instantiation->getDeclName() |
| 546 | << Instantiation->getDeclContext(); |
| 547 | Note = diag::note_explicit_instantiation_here; |
| 548 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 549 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 550 | if (Note) // Diagnostics were emitted. |
| 551 | Diag(Pattern->getLocation(), Note.getValue()); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 552 | |
| 553 | // In general, Instantiation isn't marked invalid to get more than one |
| 554 | // error for multiple undefined instantiations. But the code that does |
| 555 | // explicit declaration -> explicit definition conversion can't handle |
| 556 | // invalid declarations, so mark as invalid in that case. |
| 557 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 558 | Instantiation->setInvalidDecl(); |
| 559 | return true; |
| 560 | } |
| 561 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 562 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 563 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 564 | /// declaration at location Loc. Returns true to indicate that this is |
| 565 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 566 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 567 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 568 | |
| 569 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 570 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 571 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 572 | |
| 573 | // C++ [temp.local]p4: |
| 574 | // A template-parameter shall not be redeclared within its |
| 575 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 577 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 578 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 581 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 582 | /// the parameter D to reference the templated declaration and return a pointer |
| 583 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 584 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 585 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 586 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 587 | return Temp; |
| 588 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 589 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 592 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 593 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 594 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 595 | "Only template template arguments can be pack expansions here"); |
| 596 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 597 | "Template template argument pack expansion without packs"); |
| 598 | ParsedTemplateArgument Result(*this); |
| 599 | Result.EllipsisLoc = EllipsisLoc; |
| 600 | return Result; |
| 601 | } |
| 602 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 603 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 604 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 605 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 606 | switch (Arg.getKind()) { |
| 607 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 608 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 609 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 610 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 611 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 612 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 613 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 614 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 615 | case ParsedTemplateArgument::NonType: { |
| 616 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 617 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 618 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 619 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 620 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 621 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 622 | TemplateArgument TArg; |
| 623 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 624 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 625 | else |
| 626 | TArg = Template; |
| 627 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 628 | Arg.getScopeSpec().getWithLocInContext( |
| 629 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 630 | Arg.getLocation(), |
| 631 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 634 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 635 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 636 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 637 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 638 | /// \brief Translates template arguments as provided by the parser |
| 639 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 640 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 641 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 642 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 643 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 644 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 645 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 646 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 647 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 648 | SourceLocation Loc, |
| 649 | IdentifierInfo *Name) { |
| 650 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 651 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 652 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 653 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 654 | } |
| 655 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 656 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 657 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 658 | /// the keyword "typename" was used to declare the type parameter |
| 659 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 660 | /// "class" or "typename" keyword. ParamName is the name of the |
| 661 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 662 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 663 | /// If the type parameter has a default argument, it will be added |
| 664 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 665 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 666 | SourceLocation EllipsisLoc, |
| 667 | SourceLocation KeyLoc, |
| 668 | IdentifierInfo *ParamName, |
| 669 | SourceLocation ParamNameLoc, |
| 670 | unsigned Depth, unsigned Position, |
| 671 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 672 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | assert(S->isTemplateParamScope() && |
| 674 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 676 | SourceLocation Loc = ParamNameLoc; |
| 677 | if (!ParamName) |
| 678 | Loc = KeyLoc; |
| 679 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 680 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 681 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 682 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 683 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 684 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 685 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 686 | |
| 687 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 688 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 689 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 690 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 691 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 692 | IdResolver.AddDecl(Param); |
| 693 | } |
| 694 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 695 | // C++0x [temp.param]p9: |
| 696 | // A default template-argument may be specified for any kind of |
| 697 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 698 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 699 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 700 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 703 | // Handle the default argument, if provided. |
| 704 | if (DefaultArg) { |
| 705 | TypeSourceInfo *DefaultTInfo; |
| 706 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 707 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 708 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 709 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 710 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 711 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 712 | UPPC_DefaultArgument)) |
| 713 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 715 | // Check the template argument itself. |
| 716 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 717 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 718 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 719 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 720 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 721 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 722 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 723 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 724 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 727 | /// \brief Check that the type of a non-type template parameter is |
| 728 | /// well-formed. |
| 729 | /// |
| 730 | /// \returns the (possibly-promoted) parameter type if valid; |
| 731 | /// otherwise, produces a diagnostic and returns a NULL type. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 732 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
| 733 | SourceLocation Loc) { |
| 734 | if (TSI->getType()->isUndeducedType()) { |
| 735 | // C++1z [temp.dep.expr]p3: |
| 736 | // An id-expression is type-dependent if it contains |
| 737 | // - an identifier associated by name lookup with a non-type |
| 738 | // template-parameter declared with a type that contains a |
| 739 | // placeholder type (7.1.7.4), |
| 740 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
| 741 | } |
| 742 | |
| 743 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
| 744 | } |
| 745 | |
| 746 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
| 747 | SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 748 | // We don't allow variably-modified types as the type of non-type template |
| 749 | // parameters. |
| 750 | if (T->isVariablyModifiedType()) { |
| 751 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 752 | << T; |
| 753 | return QualType(); |
| 754 | } |
| 755 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 756 | // C++ [temp.param]p4: |
| 757 | // |
| 758 | // A non-type template-parameter shall have one of the following |
| 759 | // (optionally cv-qualified) types: |
| 760 | // |
| 761 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 762 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 764 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 766 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 767 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 768 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 769 | // -- std::nullptr_t. |
| 770 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 771 | // If T is a dependent type, we can't do the check now, so we |
| 772 | // assume that it is well-formed. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 773 | T->isDependentType() || |
| 774 | // Allow use of auto in template parameter declarations. |
| 775 | T->isUndeducedType()) { |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 776 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 777 | // are ignored when determining its type. |
| 778 | return T.getUnqualifiedType(); |
| 779 | } |
| 780 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 781 | // C++ [temp.param]p8: |
| 782 | // |
| 783 | // A non-type template-parameter of type "array of T" or |
| 784 | // "function returning T" is adjusted to be of type "pointer to |
| 785 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 786 | else if (T->isArrayType() || T->isFunctionType()) |
| 787 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 788 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 789 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 790 | << T; |
| 791 | |
| 792 | return QualType(); |
| 793 | } |
| 794 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 795 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 796 | unsigned Depth, |
| 797 | unsigned Position, |
| 798 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 799 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 800 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 801 | |
| 802 | if (TInfo->getType()->isUndeducedType()) { |
| 803 | Diag(D.getIdentifierLoc(), |
| 804 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 805 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
| 806 | } |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 808 | assert(S->isTemplateParamScope() && |
| 809 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 810 | bool Invalid = false; |
| 811 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 812 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 813 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 814 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 815 | Invalid = true; |
| 816 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 817 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 818 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 819 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 820 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 821 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 822 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 823 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 824 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 825 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 826 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 828 | if (Invalid) |
| 829 | Param->setInvalidDecl(); |
| 830 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 831 | if (ParamName) { |
| 832 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 833 | ParamName); |
| 834 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 835 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 836 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 837 | IdResolver.AddDecl(Param); |
| 838 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 840 | // C++0x [temp.param]p9: |
| 841 | // A default template-argument may be specified for any kind of |
| 842 | // template-parameter that is not a template parameter pack. |
| 843 | if (Default && IsParameterPack) { |
| 844 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 845 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 848 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 849 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 850 | // Check for unexpanded parameter packs. |
| 851 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 852 | return Param; |
| 853 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 854 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 855 | ExprResult DefaultRes = |
| 856 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 857 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 858 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 859 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 860 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 861 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 862 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 863 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 864 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 865 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 866 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 867 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 868 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 869 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 870 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 871 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 872 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 873 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 874 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 875 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 876 | IdentifierInfo *Name, |
| 877 | SourceLocation NameLoc, |
| 878 | unsigned Depth, |
| 879 | unsigned Position, |
| 880 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 881 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 882 | assert(S->isTemplateParamScope() && |
| 883 | "Template template parameter not in template parameter scope!"); |
| 884 | |
| 885 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 886 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 887 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 888 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 889 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 890 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 891 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 892 | Param->setAccess(AS_public); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 893 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 894 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 895 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 896 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 897 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 898 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 899 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 900 | IdResolver.AddDecl(Param); |
| 901 | } |
| 902 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 903 | if (Params->size() == 0) { |
| 904 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 905 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 906 | Param->setInvalidDecl(); |
| 907 | } |
| 908 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 909 | // C++0x [temp.param]p9: |
| 910 | // A default template-argument may be specified for any kind of |
| 911 | // template-parameter that is not a template parameter pack. |
| 912 | if (IsParameterPack && !Default.isInvalid()) { |
| 913 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 914 | Default = ParsedTemplateArgument(); |
| 915 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 917 | if (!Default.isInvalid()) { |
| 918 | // Check only that we have a template template argument. We don't want to |
| 919 | // try to check well-formedness now, because our template template parameter |
| 920 | // might have dependent types in its template parameters, which we wouldn't |
| 921 | // be able to match now. |
| 922 | // |
| 923 | // If none of the template template parameter's template arguments mention |
| 924 | // other template parameters, we could actually perform more checking here. |
| 925 | // However, it isn't worth doing. |
| 926 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 927 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 928 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 929 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 930 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 931 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 932 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 933 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 934 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 935 | DefaultArg.getArgument().getAsTemplate(), |
| 936 | UPPC_DefaultArgument)) |
| 937 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 938 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 939 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 940 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 941 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 942 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 945 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 946 | /// constrained by RequiresClause, that contains the template parameters in |
| 947 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 948 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 949 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 950 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 952 | SourceLocation LAngleLoc, |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 953 | ArrayRef<Decl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 954 | SourceLocation RAngleLoc, |
| 955 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 956 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 957 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 958 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 959 | return TemplateParameterList::Create( |
| 960 | Context, TemplateLoc, LAngleLoc, |
| 961 | llvm::makeArrayRef((NamedDecl *const *)Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 962 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 963 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 964 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 965 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 966 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 967 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 968 | } |
| 969 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 970 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 971 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 972 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 973 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 974 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 975 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 976 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 977 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 978 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 979 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 980 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 982 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 983 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 984 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 985 | |
| 986 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 987 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 988 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 989 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 990 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 991 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 992 | |
| 993 | // There is no such thing as an unnamed class template. |
| 994 | if (!Name) { |
| 995 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 996 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 999 | // Find any previous declaration with this name. For a friend with no |
| 1000 | // scope explicitly specified, we only look for tag declarations (per |
| 1001 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1002 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1003 | LookupResult Previous(*this, Name, NameLoc, |
| 1004 | (SS.isEmpty() && TUK == TUK_Friend) |
| 1005 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 1006 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1007 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 1008 | SemanticContext = computeDeclContext(SS, true); |
| 1009 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1010 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 1011 | // in the AST, and historically we have just ignored such friend |
| 1012 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1013 | Diag(NameLoc, TUK == TUK_Friend |
| 1014 | ? diag::warn_template_qualified_friend_ignored |
| 1015 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 1016 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 1017 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1018 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1020 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 1021 | return true; |
| 1022 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1023 | // If we're adding a template to a dependent context, we may need to |
| 1024 | // rebuilding some of the types used within the template parameter list, |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 1025 | // now that we know what the current instantiation is. |
| 1026 | if (SemanticContext->isDependentContext()) { |
| 1027 | ContextRAII SavedContext(*this, SemanticContext); |
| 1028 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 1029 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1030 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 1031 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1032 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1033 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1034 | } else { |
| 1035 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 1036 | |
| 1037 | // C++14 [class.mem]p14: |
| 1038 | // If T is the name of a class, then each of the following shall have a |
| 1039 | // name different from T: |
| 1040 | // -- every member template of class T |
| 1041 | if (TUK != TUK_Friend && |
| 1042 | DiagnoseClassNameShadow(SemanticContext, |
| 1043 | DeclarationNameInfo(Name, NameLoc))) |
| 1044 | return true; |
| 1045 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1046 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1047 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1049 | if (Previous.isAmbiguous()) |
| 1050 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1051 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1052 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1053 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1054 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1055 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1056 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1057 | // Maybe we will complain about the shadowed template parameter. |
| 1058 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1059 | // Just pretend that we didn't see the previous declaration. |
| 1060 | PrevDecl = nullptr; |
| 1061 | } |
| 1062 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1063 | // If there is a previous declaration with the same name, check |
| 1064 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1066 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1067 | |
| 1068 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1070 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1071 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1072 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1073 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1074 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1075 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1076 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1077 | PrevClassTemplate |
| 1078 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1079 | ->getSpecializedTemplate(); |
| 1080 | } |
| 1081 | } |
| 1082 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1083 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1084 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1085 | // [...] When looking for a prior declaration of a class or a function |
| 1086 | // 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] | 1087 | // function is neither a qualified name nor a template-id, scopes outside |
| 1088 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1089 | if (!SS.isSet()) { |
| 1090 | DeclContext *OutermostContext = CurContext; |
| 1091 | while (!OutermostContext->isFileContext()) |
| 1092 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1093 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 1094 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1095 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1096 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1097 | SemanticContext = PrevDecl->getDeclContext(); |
| 1098 | } else { |
| 1099 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1100 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1101 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1102 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1103 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Check that the chosen semantic context doesn't already contain a |
| 1106 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1107 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1108 | DeclContext *LookupContext = SemanticContext; |
| 1109 | while (LookupContext->isTransparentContext()) |
| 1110 | LookupContext = LookupContext->getLookupParent(); |
| 1111 | LookupQualifiedName(Previous, LookupContext); |
| 1112 | |
| 1113 | if (Previous.isAmbiguous()) |
| 1114 | return true; |
| 1115 | |
| 1116 | if (Previous.begin() != Previous.end()) |
| 1117 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1118 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1119 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1120 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1121 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1122 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1123 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1124 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1125 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1126 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1127 | if (SS.isEmpty() && |
| 1128 | !(PrevClassTemplate && |
| 1129 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1130 | SemanticContext->getRedeclContext()))) { |
| 1131 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1132 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1133 | diag::note_using_decl_target); |
| 1134 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1135 | // Recover by ignoring the old declaration. |
| 1136 | PrevDecl = PrevClassTemplate = nullptr; |
| 1137 | } |
| 1138 | } |
| 1139 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1140 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1141 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1142 | // for a friend in a dependent context: the template parameter list itself |
| 1143 | // could be dependent. |
| 1144 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1145 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1146 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1147 | /*Complain=*/true, |
| 1148 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1149 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1150 | |
| 1151 | // C++ [temp.class]p4: |
| 1152 | // In a redeclaration, partial specialization, explicit |
| 1153 | // specialization or explicit instantiation of a class template, |
| 1154 | // the class-key shall agree in kind with the original class |
| 1155 | // template declaration (7.1.5.3). |
| 1156 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1157 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1158 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1160 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1161 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1162 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1163 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1166 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1167 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1168 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1169 | // If we have a prior definition that is not visible, treat this as |
| 1170 | // simply making that previous definition visible. |
| 1171 | NamedDecl *Hidden = nullptr; |
| 1172 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1173 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1174 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1175 | assert(Tmpl && "original definition of a class template is not a " |
| 1176 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1177 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1178 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1179 | return Def; |
| 1180 | } |
| 1181 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1182 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1183 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1184 | // FIXME: Would it make sense to try to "forget" the previous |
| 1185 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1186 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1187 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1188 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1189 | } else if (PrevDecl) { |
| 1190 | // C++ [temp]p5: |
| 1191 | // A class template shall not have the same name as any other |
| 1192 | // template, class, function, object, enumeration, enumerator, |
| 1193 | // namespace, or type in the same scope (3.3), except as specified |
| 1194 | // in (14.5.4). |
| 1195 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1196 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1197 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1200 | // Check the template parameter list of this declaration, possibly |
| 1201 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1202 | // template declaration. Skip this check for a friend in a dependent |
| 1203 | // context, because the template parameter list might be dependent. |
| 1204 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1205 | CheckTemplateParameterList( |
| 1206 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1207 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1208 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1209 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1210 | SemanticContext->isDependentContext()) |
| 1211 | ? TPC_ClassTemplateMember |
| 1212 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1213 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1214 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1216 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1217 | // 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] | 1218 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1219 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1220 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1221 | : diag::err_member_decl_does_not_match) |
| 1222 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1223 | Invalid = true; |
| 1224 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1227 | // If this is a templated friend in a dependent context we should not put it |
| 1228 | // on the redecl chain. In some cases, the templated friend can be the most |
| 1229 | // recent declaration tricking the template instantiator to make substitutions |
| 1230 | // there. |
| 1231 | // FIXME: Figure out how to combine with shouldLinkDependentDeclWithPrevious |
| 1232 | bool ShouldAddRedecl |
| 1233 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
| 1234 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1236 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1237 | PrevClassTemplate && ShouldAddRedecl ? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1238 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1239 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1240 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1241 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1242 | NewClass->setTemplateParameterListsInfo( |
| 1243 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1244 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1245 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1246 | // Add alignment attributes if necessary; these attributes are checked when |
| 1247 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1248 | if (TUK == TUK_Definition) { |
| 1249 | AddAlignmentAttributesForRecord(NewClass); |
| 1250 | AddMsStructLayoutForRecord(NewClass); |
| 1251 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1253 | ClassTemplateDecl *NewTemplate |
| 1254 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1255 | DeclarationName(Name), TemplateParams, |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1256 | NewClass); |
| 1257 | |
| 1258 | if (ShouldAddRedecl) |
| 1259 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
| 1260 | |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1261 | NewClass->setDescribedClassTemplate(NewTemplate); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1262 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1263 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1264 | NewTemplate->setModulePrivate(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1265 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1266 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1267 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1268 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1269 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1270 | (void)T; |
| 1271 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1272 | // 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] | 1273 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1274 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1275 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1276 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1277 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1278 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1279 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1280 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1282 | // Set the lexical context of these templates |
| 1283 | NewClass->setLexicalDeclContext(CurContext); |
| 1284 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1285 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1286 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1287 | NewClass->startDefinition(); |
| 1288 | |
| 1289 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1290 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1291 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1292 | if (PrevClassTemplate) |
| 1293 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1294 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1295 | AddPushedVisibilityAttribute(NewClass); |
| 1296 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1297 | if (TUK != TUK_Friend) { |
| 1298 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1299 | Scope *Outer = S; |
| 1300 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1301 | Outer = Outer->getParent(); |
| 1302 | PushOnScopeChains(NewTemplate, Outer); |
| 1303 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1304 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1305 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1306 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1307 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1308 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1309 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1310 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1311 | // Friend templates are visible in fairly strange ways. |
| 1312 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1313 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1314 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1315 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1316 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1317 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1318 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1319 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1320 | FriendDecl *Friend = FriendDecl::Create( |
| 1321 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1322 | Friend->setAccess(AS_public); |
| 1323 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1324 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1325 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1326 | if (Invalid) { |
| 1327 | NewTemplate->setInvalidDecl(); |
| 1328 | NewClass->setInvalidDecl(); |
| 1329 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1330 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1331 | ActOnDocumentableDecl(NewTemplate); |
| 1332 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1333 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1336 | /// \brief Diagnose the presence of a default template argument on a |
| 1337 | /// template parameter, which is ill-formed in certain contexts. |
| 1338 | /// |
| 1339 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1340 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1341 | Sema::TemplateParamListContext TPC, |
| 1342 | SourceLocation ParamLoc, |
| 1343 | SourceRange DefArgRange) { |
| 1344 | switch (TPC) { |
| 1345 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1346 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1347 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1348 | return false; |
| 1349 | |
| 1350 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1351 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1352 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1353 | // A default template-argument shall not be specified in a |
| 1354 | // function template declaration or a function template |
| 1355 | // definition [...] |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1356 | // If a friend function template declaration specifies a default |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1357 | // template-argument, that declaration shall be a definition and shall be |
| 1358 | // the only declaration of the function template in the translation unit. |
| 1359 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1360 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1361 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1362 | : diag::ext_template_parameter_default_in_function_template) |
| 1363 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1364 | return false; |
| 1365 | |
| 1366 | case Sema::TPC_ClassTemplateMember: |
| 1367 | // C++0x [temp.param]p9: |
| 1368 | // A default template-argument shall not be specified in the |
| 1369 | // template-parameter-lists of the definition of a member of a |
| 1370 | // class template that appears outside of the member's class. |
| 1371 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1372 | << DefArgRange; |
| 1373 | return true; |
| 1374 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1375 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1376 | case Sema::TPC_FriendFunctionTemplate: |
| 1377 | // C++ [temp.param]p9: |
| 1378 | // A default template-argument shall not be specified in a |
| 1379 | // friend template declaration. |
| 1380 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1381 | << DefArgRange; |
| 1382 | return true; |
| 1383 | |
| 1384 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1385 | // for friend function templates if there is only a single |
| 1386 | // declaration (and it is a definition). Strange! |
| 1387 | } |
| 1388 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1389 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1392 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1393 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1394 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1395 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1396 | // A template template parameter which is a parameter pack is also a pack |
| 1397 | // expansion. |
| 1398 | if (TTP->isParameterPack()) |
| 1399 | return false; |
| 1400 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1401 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1402 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1403 | NamedDecl *P = Params->getParam(I); |
| 1404 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1405 | if (!NTTP->isParameterPack() && |
| 1406 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1407 | NTTP->getTypeSourceInfo(), |
| 1408 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1409 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1411 | continue; |
| 1412 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1415 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1416 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1417 | return true; |
| 1418 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1419 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1420 | return false; |
| 1421 | } |
| 1422 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1423 | /// \brief Checks the validity of a template parameter list, possibly |
| 1424 | /// considering the template parameter list from a previous |
| 1425 | /// declaration. |
| 1426 | /// |
| 1427 | /// If an "old" template parameter list is provided, it must be |
| 1428 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1429 | /// template parameter list. |
| 1430 | /// |
| 1431 | /// \param NewParams Template parameter list for a new template |
| 1432 | /// declaration. This template parameter list will be updated with any |
| 1433 | /// default arguments that are carried through from the previous |
| 1434 | /// template parameter list. |
| 1435 | /// |
| 1436 | /// \param OldParams If provided, template parameter list from a |
| 1437 | /// previous declaration of the same template. Default template |
| 1438 | /// arguments will be merged from the old template parameter list to |
| 1439 | /// the new template parameter list. |
| 1440 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1441 | /// \param TPC Describes the context in which we are checking the given |
| 1442 | /// template parameter list. |
| 1443 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1444 | /// \returns true if an error occurred, false otherwise. |
| 1445 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1446 | TemplateParameterList *OldParams, |
| 1447 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1448 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1450 | // C++ [temp.param]p10: |
| 1451 | // The set of default template-arguments available for use with a |
| 1452 | // template declaration or definition is obtained by merging the |
| 1453 | // default arguments from the definition (if in scope) and all |
| 1454 | // declarations in scope in the same way default function |
| 1455 | // arguments are (8.3.6). |
| 1456 | bool SawDefaultArgument = false; |
| 1457 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1458 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1459 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1460 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1461 | if (OldParams) |
| 1462 | OldParam = OldParams->begin(); |
| 1463 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1464 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1465 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1466 | NewParamEnd = NewParams->end(); |
| 1467 | NewParam != NewParamEnd; ++NewParam) { |
| 1468 | // Variables used to diagnose redundant default arguments |
| 1469 | bool RedundantDefaultArg = false; |
| 1470 | SourceLocation OldDefaultLoc; |
| 1471 | SourceLocation NewDefaultLoc; |
| 1472 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1473 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1474 | bool MissingDefaultArg = false; |
| 1475 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1476 | // Variable used to diagnose non-final parameter packs |
| 1477 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1478 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1479 | if (TemplateTypeParmDecl *NewTypeParm |
| 1480 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1481 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1482 | if (NewTypeParm->hasDefaultArgument() && |
| 1483 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1484 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1485 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1486 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1487 | NewTypeParm->removeDefaultArgument(); |
| 1488 | |
| 1489 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1491 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1492 | if (NewTypeParm->isParameterPack()) { |
| 1493 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1494 | "Parameter packs can't have a default argument!"); |
| 1495 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1496 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1497 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1498 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1499 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1500 | SawDefaultArgument = true; |
| 1501 | RedundantDefaultArg = true; |
| 1502 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1503 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1504 | // Merge the default argument from the old declaration to the |
| 1505 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1506 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1507 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1508 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1509 | SawDefaultArgument = true; |
| 1510 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1511 | } else if (SawDefaultArgument) |
| 1512 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1513 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1514 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1515 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1516 | if (!NewNonTypeParm->isParameterPack() && |
| 1517 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1518 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1519 | UPPC_NonTypeTemplateParameterType)) { |
| 1520 | Invalid = true; |
| 1521 | continue; |
| 1522 | } |
| 1523 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1524 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1525 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1526 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1527 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1528 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1529 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1532 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1533 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1534 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1535 | if (NewNonTypeParm->isParameterPack()) { |
| 1536 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1537 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1538 | if (!NewNonTypeParm->isPackExpansion()) |
| 1539 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1540 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1541 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1542 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1543 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1544 | SawDefaultArgument = true; |
| 1545 | RedundantDefaultArg = true; |
| 1546 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1547 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1548 | // Merge the default argument from the old declaration to the |
| 1549 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1550 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1551 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1552 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1553 | SawDefaultArgument = true; |
| 1554 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1555 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1557 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1558 | TemplateTemplateParmDecl *NewTemplateParm |
| 1559 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1560 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1561 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1562 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1563 | Invalid = true; |
| 1564 | continue; |
| 1565 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1566 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1567 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1568 | if (NewTemplateParm->hasDefaultArgument() && |
| 1569 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1570 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1571 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1572 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1573 | |
| 1574 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1575 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1576 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1577 | if (NewTemplateParm->isParameterPack()) { |
| 1578 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1579 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1580 | if (!NewTemplateParm->isPackExpansion()) |
| 1581 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1582 | } else if (OldTemplateParm && |
| 1583 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 1584 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1585 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1586 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1587 | SawDefaultArgument = true; |
| 1588 | RedundantDefaultArg = true; |
| 1589 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1590 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1591 | // Merge the default argument from the old declaration to the |
| 1592 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1593 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1594 | PreviousDefaultArgLoc |
| 1595 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1596 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1597 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1598 | PreviousDefaultArgLoc |
| 1599 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1600 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1604 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1605 | // If a template parameter of a primary class template or alias template |
| 1606 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1607 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1608 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1609 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1610 | Diag((*NewParam)->getLocation(), |
| 1611 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1612 | Invalid = true; |
| 1613 | } |
| 1614 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1615 | if (RedundantDefaultArg) { |
| 1616 | // C++ [temp.param]p12: |
| 1617 | // A template-parameter shall not be given default arguments |
| 1618 | // by two different declarations in the same scope. |
| 1619 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1620 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1621 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1622 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1623 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1624 | // If a template-parameter of a class template has a default |
| 1625 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1626 | // have a default template-argument supplied or be a template parameter |
| 1627 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1629 | diag::err_template_param_default_arg_missing); |
| 1630 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1631 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1632 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | // If we have an old template parameter list that we're merging |
| 1636 | // in, move on to the next parameter. |
| 1637 | if (OldParams) |
| 1638 | ++OldParam; |
| 1639 | } |
| 1640 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1641 | // We were missing some default arguments at the end of the list, so remove |
| 1642 | // all of the default arguments. |
| 1643 | if (RemoveDefaultArguments) { |
| 1644 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1645 | NewParamEnd = NewParams->end(); |
| 1646 | NewParam != NewParamEnd; ++NewParam) { |
| 1647 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1648 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1649 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1650 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1651 | NTTP->removeDefaultArgument(); |
| 1652 | else |
| 1653 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1654 | } |
| 1655 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1657 | return Invalid; |
| 1658 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1659 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1660 | namespace { |
| 1661 | |
| 1662 | /// A class which looks for a use of a certain level of template |
| 1663 | /// parameter. |
| 1664 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1665 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1666 | |
| 1667 | unsigned Depth; |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 1668 | bool FindLessThanDepth; |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1669 | |
| 1670 | // Whether we're looking for a use of a template parameter that makes the |
| 1671 | // overall construct type-dependent / a dependent type. This is strictly |
| 1672 | // best-effort for now; we may fail to match at all for a dependent type |
| 1673 | // in some cases if this is set. |
| 1674 | bool IgnoreNonTypeDependent; |
| 1675 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1676 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1677 | SourceLocation MatchLoc; |
| 1678 | |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 1679 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent, |
| 1680 | bool FindLessThanDepth = false) |
| 1681 | : Depth(Depth), FindLessThanDepth(FindLessThanDepth), |
| 1682 | IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1683 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1684 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 1685 | : DependencyChecker(Params->getDepth(), IgnoreNonTypeDependent) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1686 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1687 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 1688 | if (FindLessThanDepth ^ (ParmDepth >= Depth)) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1689 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1690 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1691 | return true; |
| 1692 | } |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1696 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 1697 | // Prune out non-type-dependent expressions if requested. This can |
| 1698 | // sometimes result in us failing to find a template parameter reference |
| 1699 | // (if a value-dependent expression creates a dependent type), but this |
| 1700 | // mode is best-effort only. |
| 1701 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 1702 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 1703 | return true; |
| 1704 | return super::TraverseStmt(S, Q); |
| 1705 | } |
| 1706 | |
| 1707 | bool TraverseTypeLoc(TypeLoc TL) { |
| 1708 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 1709 | !TL.getType()->isDependentType()) |
| 1710 | return true; |
| 1711 | return super::TraverseTypeLoc(TL); |
| 1712 | } |
| 1713 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1714 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1715 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1716 | } |
| 1717 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1718 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1719 | // For a best-effort search, keep looking until we find a location. |
| 1720 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | bool TraverseTemplateName(TemplateName N) { |
| 1724 | if (TemplateTemplateParmDecl *PD = |
| 1725 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1726 | if (Matches(PD->getDepth())) |
| 1727 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1728 | return super::TraverseTemplateName(N); |
| 1729 | } |
| 1730 | |
| 1731 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1732 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1733 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1734 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1735 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1736 | return super::VisitDeclRefExpr(E); |
| 1737 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1738 | |
| 1739 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1740 | return TraverseType(T->getReplacementType()); |
| 1741 | } |
| 1742 | |
| 1743 | bool |
| 1744 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1745 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1746 | } |
| 1747 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1748 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1749 | return TraverseType(T->getInjectedSpecializationType()); |
| 1750 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1751 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1752 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1753 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1754 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1755 | /// list. |
| 1756 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1757 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1758 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1759 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1760 | return Checker.Match; |
| 1761 | } |
| 1762 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1763 | // Find the source range corresponding to the named type in the given |
| 1764 | // nested-name-specifier, if any. |
| 1765 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1766 | QualType T, |
| 1767 | const CXXScopeSpec &SS) { |
| 1768 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1769 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1770 | if (const Type *CurType = NNS->getAsType()) { |
| 1771 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1772 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1773 | } else |
| 1774 | break; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1775 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1776 | NNSLoc = NNSLoc.getPrefix(); |
| 1777 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1778 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1779 | return SourceRange(); |
| 1780 | } |
| 1781 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1783 | /// specifier, returning the template parameter list that applies to the |
| 1784 | /// name. |
| 1785 | /// |
| 1786 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1787 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1789 | /// \param DeclLoc The location of the declaration itself. |
| 1790 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1791 | /// \param SS the scope specifier that will be matched to the given template |
| 1792 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1793 | /// being declared. |
| 1794 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1795 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1796 | /// is one. Used to check for a missing 'template<>'. |
| 1797 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1798 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1799 | /// innermost template parameter lists. |
| 1800 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1801 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1802 | /// matching template parameters to scope specifiers in friend |
| 1803 | /// declarations. |
| 1804 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1805 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1806 | /// declared is an explicit specialization, false otherwise. |
| 1807 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1809 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1810 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1812 | /// 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] | 1813 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1814 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1815 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1816 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1817 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1818 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1819 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1820 | Invalid = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1821 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1822 | // The sequence of nested types to which we will match up the template |
| 1823 | // parameter lists. We first build this list by starting with the type named |
| 1824 | // 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] | 1825 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1826 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1827 | if (SS.getScopeRep()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1828 | if (CXXRecordDecl *Record |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1829 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1830 | T = Context.getTypeDeclType(Record); |
| 1831 | else |
| 1832 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1833 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1834 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1835 | // If we found an explicit specialization that prevents us from needing |
| 1836 | // 'template<>' headers, this will be set to the location of that |
| 1837 | // explicit specialization. |
| 1838 | SourceLocation ExplicitSpecLoc; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1839 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1840 | while (!T.isNull()) { |
| 1841 | NestedTypes.push_back(T); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1842 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1843 | // Retrieve the parent of a record type. |
| 1844 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1845 | // If this type is an explicit specialization, we're done. |
| 1846 | if (ClassTemplateSpecializationDecl *Spec |
| 1847 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1848 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1849 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1850 | ExplicitSpecLoc = Spec->getLocation(); |
| 1851 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1852 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1853 | } else if (Record->getTemplateSpecializationKind() |
| 1854 | == TSK_ExplicitSpecialization) { |
| 1855 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1856 | break; |
| 1857 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1859 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1860 | T = Context.getTypeDeclType(Parent); |
| 1861 | else |
| 1862 | T = QualType(); |
| 1863 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1866 | if (const TemplateSpecializationType *TST |
| 1867 | = T->getAs<TemplateSpecializationType>()) { |
| 1868 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1869 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1870 | T = Context.getTypeDeclType(Parent); |
| 1871 | else |
| 1872 | T = QualType(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1873 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1874 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1875 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1876 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1877 | // Look one step prior in a dependent template specialization type. |
| 1878 | if (const DependentTemplateSpecializationType *DependentTST |
| 1879 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1880 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1881 | T = QualType(NNS->getAsType(), 0); |
| 1882 | else |
| 1883 | T = QualType(); |
| 1884 | continue; |
| 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 | // Look one step prior in a dependent name type. |
| 1888 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1889 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1890 | T = QualType(NNS->getAsType(), 0); |
| 1891 | else |
| 1892 | T = QualType(); |
| 1893 | continue; |
| 1894 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1895 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1896 | // Retrieve the parent of an enumeration type. |
| 1897 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1898 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1899 | // check here. |
| 1900 | EnumDecl *Enum = EnumT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1901 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1902 | // Get to the parent type. |
| 1903 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1904 | T = Context.getTypeDeclType(Parent); |
| 1905 | else |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1906 | T = QualType(); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1907 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1908 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1910 | T = QualType(); |
| 1911 | } |
| 1912 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1913 | // to the innermost while checking template-parameter-lists. |
| 1914 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1915 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1916 | // C++0x [temp.expl.spec]p17: |
| 1917 | // A member or a member template may be nested within many |
| 1918 | // enclosing class templates. In an explicit specialization for |
| 1919 | // such a member, the member declaration shall be preceded by a |
| 1920 | // template<> for each enclosing class template that is |
| 1921 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1922 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1923 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1924 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1925 | if (SawNonEmptyTemplateParameterList) { |
| 1926 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1927 | << !Recovery << Range; |
| 1928 | Invalid = true; |
| 1929 | IsExplicitSpecialization = false; |
| 1930 | return true; |
| 1931 | } |
| 1932 | |
| 1933 | return false; |
| 1934 | }; |
| 1935 | |
| 1936 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1937 | // Check that we can have an explicit specialization here. |
| 1938 | if (CheckExplicitSpecialization(Range, true)) |
| 1939 | return true; |
| 1940 | |
| 1941 | // We don't have a template header, but we should. |
| 1942 | SourceLocation ExpectedTemplateLoc; |
| 1943 | if (!ParamLists.empty()) |
| 1944 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1945 | else |
| 1946 | ExpectedTemplateLoc = DeclStartLoc; |
| 1947 | |
| 1948 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1949 | << Range |
| 1950 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1951 | return false; |
| 1952 | }; |
| 1953 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1954 | unsigned ParamIdx = 0; |
| 1955 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1956 | ++TypeIdx) { |
| 1957 | T = NestedTypes[TypeIdx]; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1959 | // Whether we expect a 'template<>' header. |
| 1960 | bool NeedEmptyTemplateHeader = false; |
| 1961 | |
| 1962 | // Whether we expect a template header with parameters. |
| 1963 | bool NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1965 | // For a dependent type, the set of template parameters that we |
| 1966 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1967 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1969 | // C++0x [temp.expl.spec]p15: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1970 | // A member or a member template may be nested within many enclosing |
| 1971 | // class templates. In an explicit specialization for such a member, the |
| 1972 | // member declaration shall be preceded by a template<> for each |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1973 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1974 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1975 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1976 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1977 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1978 | NeedNonemptyTemplateHeader = true; |
| 1979 | } else if (Record->isDependentType()) { |
| 1980 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1981 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1982 | ->getTemplateParameters(); |
| 1983 | NeedNonemptyTemplateHeader = true; |
| 1984 | } |
| 1985 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1986 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1987 | // C++0x [temp.expl.spec]p4: |
| 1988 | // Members of an explicitly specialized class template are defined |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1989 | // in the same manner as members of normal classes, and not using |
| 1990 | // the template<> syntax. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1991 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1992 | NeedEmptyTemplateHeader = true; |
| 1993 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1994 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1995 | } else if (Record->getTemplateSpecializationKind()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1996 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1997 | != TSK_ExplicitSpecialization && |
| 1998 | TypeIdx == NumTypes - 1) |
| 1999 | IsExplicitSpecialization = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2000 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 2001 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2002 | } |
| 2003 | } else if (const TemplateSpecializationType *TST |
| 2004 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 2005 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2006 | ExpectedTemplateParams = Template->getTemplateParameters(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2007 | NeedNonemptyTemplateHeader = true; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2008 | } |
| 2009 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2010 | // FIXME: We actually could/should check the template arguments here |
| 2011 | // against the corresponding template parameter list. |
| 2012 | NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2015 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2016 | // In an explicit specialization declaration for a member of a class |
| 2017 | // template or a member template that ap- pears in namespace scope, the |
| 2018 | // member template and some of its enclosing class templates may remain |
| 2019 | // unspecialized, except that the declaration shall not explicitly |
| 2020 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2021 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2022 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2023 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2024 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2025 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2026 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2027 | } else |
| 2028 | SawNonEmptyTemplateParameterList = true; |
| 2029 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2030 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2031 | if (NeedEmptyTemplateHeader) { |
| 2032 | // If we're on the last of the types, and we need a 'template<>' header |
| 2033 | // here, then it's an explicit specialization. |
| 2034 | if (TypeIdx == NumTypes - 1) |
| 2035 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2036 | |
| 2037 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2038 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2039 | // The header has template parameters when it shouldn't. Complain. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2040 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2041 | diag::err_template_param_list_matches_nontemplate) |
| 2042 | << T |
| 2043 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2044 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2045 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2046 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2047 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2048 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2049 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2050 | // Consume this template header. |
| 2051 | ++ParamIdx; |
| 2052 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2053 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2054 | |
| 2055 | if (!IsFriend) |
| 2056 | if (DiagnoseMissingExplicitSpecialization( |
| 2057 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2058 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2060 | continue; |
| 2061 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2062 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2063 | if (NeedNonemptyTemplateHeader) { |
| 2064 | // In friend declarations we can have template-ids which don't |
| 2065 | // depend on the corresponding template parameter lists. But |
| 2066 | // assume that empty parameter lists are supposed to match this |
| 2067 | // template-id. |
| 2068 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2069 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2070 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2071 | ExpectedTemplateParams = nullptr; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2072 | else |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2073 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2075 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2076 | if (ParamIdx < ParamLists.size()) { |
| 2077 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2078 | if (ExpectedTemplateParams && |
| 2079 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2080 | ExpectedTemplateParams, |
| 2081 | true, TPL_TemplateMatch)) |
| 2082 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2083 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2084 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2085 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2086 | TPC_ClassTemplateMember)) |
| 2087 | Invalid = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2088 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2089 | ++ParamIdx; |
| 2090 | continue; |
| 2091 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2092 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2093 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2094 | << T |
| 2095 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2096 | Invalid = true; |
| 2097 | continue; |
| 2098 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2099 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2100 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2101 | // If there were at least as many template-ids as there were template |
| 2102 | // parameter lists, then there are no template parameter lists remaining for |
| 2103 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2104 | if (ParamIdx >= ParamLists.size()) { |
| 2105 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2106 | // We don't have a template header for the declaration itself, but we |
| 2107 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2108 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2109 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2110 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2111 | |
| 2112 | // Fabricate an empty template parameter list for the invented header. |
| 2113 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2114 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2115 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2118 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2119 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2121 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2122 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2123 | bool HasAnyExplicitSpecHeader = false; |
| 2124 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2125 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2126 | if (ParamLists[I]->size() == 0) |
| 2127 | HasAnyExplicitSpecHeader = true; |
| 2128 | else |
| 2129 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2130 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2131 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2132 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2133 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2134 | : diag::err_template_spec_extra_headers) |
| 2135 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2136 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2137 | |
| 2138 | // If there was a specialization somewhere, such that 'template<>' is |
| 2139 | // not required, and there were any 'template<>' headers, note where the |
| 2140 | // specialization occurred. |
| 2141 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2142 | Diag(ExplicitSpecLoc, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2143 | diag::note_explicit_template_spec_does_not_need_header) |
| 2144 | << NestedTypes.back(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2146 | // We have a template parameter list with no corresponding scope, which |
| 2147 | // means that the resulting template declaration can't be instantiated |
| 2148 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2149 | if (!AllExplicitSpecHeaders) |
| 2150 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2151 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2153 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2154 | // In an explicit specialization declaration for a member of a class |
| 2155 | // template or a member template that ap- pears in namespace scope, the |
| 2156 | // member template and some of its enclosing class templates may remain |
| 2157 | // unspecialized, except that the declaration shall not explicitly |
| 2158 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2159 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2160 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2161 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2162 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2163 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2165 | // Return the last template parameter list, which corresponds to the |
| 2166 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2167 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2170 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2171 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2172 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2173 | << (isa<FunctionTemplateDecl>(Template) |
| 2174 | ? 0 |
| 2175 | : isa<ClassTemplateDecl>(Template) |
| 2176 | ? 1 |
| 2177 | : isa<VarTemplateDecl>(Template) |
| 2178 | ? 2 |
| 2179 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2180 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2181 | return; |
| 2182 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2183 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2184 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2185 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2186 | IEnd = OST->end(); |
| 2187 | I != IEnd; ++I) |
| 2188 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2189 | << 0 << (*I)->getDeclName(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2190 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2191 | return; |
| 2192 | } |
| 2193 | } |
| 2194 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2195 | static QualType |
| 2196 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2197 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2198 | SourceLocation TemplateLoc, |
| 2199 | TemplateArgumentListInfo &TemplateArgs) { |
| 2200 | ASTContext &Context = SemaRef.getASTContext(); |
| 2201 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2202 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2203 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2204 | // S<T, 0, ..., N-1>. |
| 2205 | |
| 2206 | // C++14 [inteseq.intseq]p1: |
| 2207 | // T shall be an integer type. |
| 2208 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2209 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2210 | diag::err_integer_sequence_integral_element_type); |
| 2211 | return QualType(); |
| 2212 | } |
| 2213 | |
| 2214 | // C++14 [inteseq.make]p1: |
| 2215 | // If N is negative the program is ill-formed. |
| 2216 | TemplateArgument NumArgsArg = Converted[2]; |
| 2217 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2218 | if (NumArgs < 0) { |
| 2219 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2220 | diag::err_integer_sequence_negative_length); |
| 2221 | return QualType(); |
| 2222 | } |
| 2223 | |
| 2224 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2225 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2226 | // The type argument gets reused as the first template argument in the |
| 2227 | // synthetic template argument list. |
| 2228 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2229 | // Expand N into 0 ... N-1. |
| 2230 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2231 | I < NumArgs; ++I) { |
| 2232 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2233 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2234 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2235 | } |
| 2236 | // The first template argument will be reused as the template decl that |
| 2237 | // our synthetic template arguments will be applied to. |
| 2238 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2239 | TemplateLoc, SyntheticTemplateArgs); |
| 2240 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2241 | |
| 2242 | case BTK__type_pack_element: |
| 2243 | // Specializations of |
| 2244 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2245 | // are treated like T_Index. |
| 2246 | assert(Converted.size() == 2 && |
| 2247 | "__type_pack_element should be given an index and a parameter pack"); |
| 2248 | |
| 2249 | // If the Index is out of bounds, the program is ill-formed. |
| 2250 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2251 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2252 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2253 | "type std::size_t, and hence be non-negative"); |
| 2254 | if (Index >= Ts.pack_size()) { |
| 2255 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2256 | diag::err_type_pack_element_out_of_bounds); |
| 2257 | return QualType(); |
| 2258 | } |
| 2259 | |
| 2260 | // We simply return the type at index `Index`. |
| 2261 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2262 | return Nth->getAsType(); |
| 2263 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2264 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2265 | } |
| 2266 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2267 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2268 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2269 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2270 | DependentTemplateName *DTN |
| 2271 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2272 | if (DTN && DTN->isIdentifier()) |
| 2273 | // When building a template-id where the template-name is dependent, |
| 2274 | // assume the template is a type template. Either our assumption is |
| 2275 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2276 | // dependent name is substituted. |
| 2277 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2278 | DTN->getQualifier(), |
| 2279 | DTN->getIdentifier(), |
| 2280 | TemplateArgs); |
| 2281 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2282 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2283 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2284 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2285 | // We might have a substituted template template parameter pack. If so, |
| 2286 | // build a template specialization type for it. |
| 2287 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2288 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2289 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2290 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2291 | << Name; |
| 2292 | NoteAllFoundTemplates(Name); |
| 2293 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2294 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2295 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2296 | // Check that the template argument list is well-formed for this |
| 2297 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2298 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2299 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2300 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2301 | return QualType(); |
| 2302 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2303 | QualType CanonType; |
| 2304 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2305 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2306 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2307 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2308 | // Find the canonical type for this type alias template specialization. |
| 2309 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2310 | if (Pattern->isInvalidDecl()) |
| 2311 | return QualType(); |
| 2312 | |
| 2313 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2314 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2315 | |
| 2316 | // Only substitute for the innermost template argument list. |
| 2317 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2318 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2319 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2320 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2321 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2322 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2323 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2324 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2325 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2326 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2327 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2328 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2329 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2330 | AliasTemplate->getDeclName()); |
| 2331 | if (CanonType.isNull()) |
| 2332 | return QualType(); |
| 2333 | } else if (Name.isDependent() || |
| 2334 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2335 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2336 | // This class template specialization is a dependent |
| 2337 | // type. Therefore, its canonical type is another class template |
| 2338 | // specialization type that contains all of the converted |
| 2339 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2340 | // A<T, T> have identical types when A is declared as: |
| 2341 | // |
| 2342 | // template<typename T, typename U = T> struct A; |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 2343 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2344 | |
| 2345 | // This might work out to be a current instantiation, in which |
| 2346 | // case the canonical type needs to be the InjectedClassNameType. |
| 2347 | // |
| 2348 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2349 | // changes to CurContext don't change the set of current |
| 2350 | // instantiations. |
| 2351 | if (isa<ClassTemplateDecl>(Template)) { |
| 2352 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2353 | // If we get out to a namespace, we're done. |
| 2354 | if (Ctx->isFileContext()) break; |
| 2355 | |
| 2356 | // If this isn't a record, keep looking. |
| 2357 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2358 | if (!Record) continue; |
| 2359 | |
| 2360 | // Look for one of the two cases with InjectedClassNameTypes |
| 2361 | // and check whether it's the same template. |
| 2362 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2363 | !Record->getDescribedClassTemplate()) |
| 2364 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2365 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2366 | // Fetch the injected class name type and check whether its |
| 2367 | // injected type is equal to the type we just built. |
| 2368 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2369 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2370 | ->getInjectedSpecializationType(); |
| 2371 | |
| 2372 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2373 | continue; |
| 2374 | |
| 2375 | // If so, the canonical type of this TST is the injected |
| 2376 | // class name type of the record we just found. |
| 2377 | assert(ICNT.isCanonical()); |
| 2378 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2379 | break; |
| 2380 | } |
| 2381 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2383 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2384 | // Find the class template specialization declaration that |
| 2385 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2386 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2387 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2388 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2389 | if (!Decl) { |
| 2390 | // This is the first time we have referenced this class template |
| 2391 | // specialization. Create the canonical declaration and add it to |
| 2392 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2394 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2395 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2396 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2397 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2398 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2399 | Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2400 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2401 | if (ClassTemplate->isOutOfLine()) |
| 2402 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2405 | // Diagnose uses of this specialization. |
| 2406 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2407 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2408 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2409 | assert(isa<RecordType>(CanonType) && |
| 2410 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2411 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2412 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2413 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2414 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2416 | // Build the fully-sugared type for this class template |
| 2417 | // specialization, which refers back to the class template |
| 2418 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2419 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2422 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2423 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2424 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
| 2425 | SourceLocation TemplateIILoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2426 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2427 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2428 | SourceLocation RAngleLoc, |
| 2429 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2430 | if (SS.isInvalid()) |
| 2431 | return true; |
| 2432 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2433 | // Per C++ [class.qual]p2, if the template-id was an injected-class-name, |
| 2434 | // it's not actually allowed to be used as a type in most cases. Because |
| 2435 | // we annotate it before we know whether it's valid, we have to check for |
| 2436 | // this case here. |
| 2437 | if (!IsCtorOrDtorName) { |
| 2438 | auto *LookupRD = |
| 2439 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)); |
| 2440 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 2441 | Diag(TemplateIILoc, |
| 2442 | TemplateKWLoc.isInvalid() |
| 2443 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
| 2444 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 2445 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 2446 | << 1 /*if any keyword was present, it was 'template'*/; |
| 2447 | } |
| 2448 | } |
| 2449 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2450 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2451 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2452 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2453 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2454 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2455 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2456 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2457 | QualType T |
| 2458 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2459 | DTN->getQualifier(), |
| 2460 | DTN->getIdentifier(), |
| 2461 | TemplateArgs); |
| 2462 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2463 | TypeLocBuilder TLB; |
| 2464 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2465 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2466 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2467 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2468 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2469 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2470 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2471 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2472 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2473 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2474 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2475 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2476 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2477 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2478 | if (Result.isNull()) |
| 2479 | return true; |
| 2480 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2481 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2482 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2483 | TemplateSpecializationTypeLoc SpecTL |
| 2484 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2485 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2486 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2487 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2488 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2489 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2490 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2491 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2492 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2493 | // constructor or destructor name (in such a case, the scope specifier |
| 2494 | // will be attached to the enclosing Decl or Expr node). |
| 2495 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2496 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2497 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2498 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2499 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2500 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2501 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2502 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2503 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2504 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2505 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2506 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2507 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2508 | SourceLocation TagLoc, |
| 2509 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2510 | SourceLocation TemplateKWLoc, |
| 2511 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2512 | SourceLocation TemplateLoc, |
| 2513 | SourceLocation LAngleLoc, |
| 2514 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2515 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2516 | TemplateName Template = TemplateD.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2517 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2518 | // Translate the parser's template argument list in our AST format. |
| 2519 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2520 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2521 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2522 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2523 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2524 | ElaboratedTypeKeyword Keyword |
| 2525 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2527 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2528 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2529 | DTN->getQualifier(), |
| 2530 | DTN->getIdentifier(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2531 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2532 | |
| 2533 | // Build type-source information. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2534 | TypeLocBuilder TLB; |
| 2535 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2536 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2537 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2538 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2539 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2540 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2541 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2542 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2543 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2544 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2545 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2546 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2547 | |
| 2548 | if (TypeAliasTemplateDecl *TAT = |
| 2549 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2550 | // C++0x [dcl.type.elab]p2: |
| 2551 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2552 | // resolves to an alias template specialization, the |
| 2553 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 2554 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 2555 | << TAT << NTK_TypeAliasTemplate << TagKind; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2556 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2557 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2558 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2559 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2560 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2561 | return TypeResult(true); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2562 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2563 | // Check the tag kind |
| 2564 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2565 | RecordDecl *D = RT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2566 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2567 | IdentifierInfo *Id = D->getIdentifier(); |
| 2568 | assert(Id && "templated class must have an identifier"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2569 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2570 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 2571 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2572 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2573 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2574 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2575 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2576 | } |
| 2577 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2578 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2579 | // Provide source-location information for the template specialization. |
| 2580 | TypeLocBuilder TLB; |
| 2581 | TemplateSpecializationTypeLoc SpecTL |
| 2582 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2583 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2584 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2585 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2586 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2587 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2588 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2589 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2590 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2591 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2592 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2593 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2594 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2595 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2596 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2599 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2600 | NamedDecl *PrevDecl, |
| 2601 | SourceLocation Loc, |
| 2602 | bool IsPartialSpecialization); |
| 2603 | |
| 2604 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2605 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2606 | static bool isTemplateArgumentTemplateParameter( |
| 2607 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2608 | switch (Arg.getKind()) { |
| 2609 | case TemplateArgument::Null: |
| 2610 | case TemplateArgument::NullPtr: |
| 2611 | case TemplateArgument::Integral: |
| 2612 | case TemplateArgument::Declaration: |
| 2613 | case TemplateArgument::Pack: |
| 2614 | case TemplateArgument::TemplateExpansion: |
| 2615 | return false; |
| 2616 | |
| 2617 | case TemplateArgument::Type: { |
| 2618 | QualType Type = Arg.getAsType(); |
| 2619 | const TemplateTypeParmType *TPT = |
| 2620 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2621 | return TPT && !Type.hasQualifiers() && |
| 2622 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2623 | } |
| 2624 | |
| 2625 | case TemplateArgument::Expression: { |
| 2626 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2627 | if (!DRE || !DRE->getDecl()) |
| 2628 | return false; |
| 2629 | const NonTypeTemplateParmDecl *NTTP = |
| 2630 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2631 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2632 | } |
| 2633 | |
| 2634 | case TemplateArgument::Template: |
| 2635 | const TemplateTemplateParmDecl *TTP = |
| 2636 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2637 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2638 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2639 | } |
| 2640 | llvm_unreachable("unexpected kind of template argument"); |
| 2641 | } |
| 2642 | |
| 2643 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2644 | ArrayRef<TemplateArgument> Args) { |
| 2645 | if (Params->size() != Args.size()) |
| 2646 | return false; |
| 2647 | |
| 2648 | unsigned Depth = Params->getDepth(); |
| 2649 | |
| 2650 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2651 | TemplateArgument Arg = Args[I]; |
| 2652 | |
| 2653 | // If the parameter is a pack expansion, the argument must be a pack |
| 2654 | // whose only element is a pack expansion. |
| 2655 | if (Params->getParam(I)->isParameterPack()) { |
| 2656 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2657 | !Arg.pack_begin()->isPackExpansion()) |
| 2658 | return false; |
| 2659 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2660 | } |
| 2661 | |
| 2662 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2663 | return false; |
| 2664 | } |
| 2665 | |
| 2666 | return true; |
| 2667 | } |
| 2668 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2669 | /// Convert the parser's template argument list representation into our form. |
| 2670 | static TemplateArgumentListInfo |
| 2671 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2672 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2673 | TemplateId.RAngleLoc); |
| 2674 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2675 | TemplateId.NumArgs); |
| 2676 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2677 | return TemplateArgs; |
| 2678 | } |
| 2679 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2680 | template<typename PartialSpecDecl> |
| 2681 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 2682 | if (Partial->getDeclContext()->isDependentContext()) |
| 2683 | return; |
| 2684 | |
| 2685 | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
| 2686 | // for non-substitution-failure issues? |
| 2687 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 2688 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 2689 | return; |
| 2690 | |
| 2691 | auto *Template = Partial->getSpecializedTemplate(); |
| 2692 | S.Diag(Partial->getLocation(), |
Richard Smith | fa4a09d | 2016-12-27 20:03:09 +0000 | [diff] [blame] | 2693 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 2694 | << isa<VarTemplateDecl>(Template); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2695 | |
| 2696 | if (Info.hasSFINAEDiagnostic()) { |
| 2697 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 2698 | PartialDiagnostic::NullDiagnostic()}; |
| 2699 | Info.takeSFINAEDiagnostic(Diag); |
| 2700 | SmallString<128> SFINAEArgString; |
| 2701 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 2702 | S.Diag(Diag.first, |
| 2703 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 2704 | << SFINAEArgString; |
| 2705 | } |
| 2706 | |
| 2707 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2708 | } |
| 2709 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2710 | template<typename PartialSpecDecl> |
| 2711 | static void checkTemplatePartialSpecialization(Sema &S, |
| 2712 | PartialSpecDecl *Partial) { |
| 2713 | // C++1z [temp.class.spec]p8: (DR1495) |
| 2714 | // - The specialization shall be more specialized than the primary |
| 2715 | // template (14.5.5.2). |
| 2716 | checkMoreSpecializedThanPrimary(S, Partial); |
| 2717 | |
| 2718 | // C++ [temp.class.spec]p8: (DR1315) |
| 2719 | // - Each template-parameter shall appear at least once in the |
| 2720 | // template-id outside a non-deduced context. |
| 2721 | // C++1z [temp.class.spec.match]p3 (P0127R2) |
| 2722 | // If the template arguments of a partial specialization cannot be |
| 2723 | // deduced because of the structure of its template-parameter-list |
| 2724 | // and the template-id, the program is ill-formed. |
| 2725 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 2726 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2727 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2728 | TemplateParams->getDepth(), DeducibleParams); |
| 2729 | |
| 2730 | if (!DeducibleParams.all()) { |
| 2731 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 2732 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 2733 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 2734 | << (NumNonDeducible > 1) |
| 2735 | << SourceRange(Partial->getLocation(), |
| 2736 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
| 2737 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2738 | if (!DeducibleParams[I]) { |
| 2739 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2740 | if (Param->getDeclName()) |
| 2741 | S.Diag(Param->getLocation(), |
| 2742 | diag::note_partial_spec_unused_parameter) |
| 2743 | << Param->getDeclName(); |
| 2744 | else |
| 2745 | S.Diag(Param->getLocation(), |
| 2746 | diag::note_partial_spec_unused_parameter) |
| 2747 | << "(anonymous)"; |
| 2748 | } |
| 2749 | } |
| 2750 | } |
| 2751 | } |
| 2752 | |
| 2753 | void Sema::CheckTemplatePartialSpecialization( |
| 2754 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 2755 | checkTemplatePartialSpecialization(*this, Partial); |
| 2756 | } |
| 2757 | |
| 2758 | void Sema::CheckTemplatePartialSpecialization( |
| 2759 | VarTemplatePartialSpecializationDecl *Partial) { |
| 2760 | checkTemplatePartialSpecialization(*this, Partial); |
| 2761 | } |
| 2762 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2763 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2764 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2765 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2766 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2767 | // D must be variable template id. |
| 2768 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2769 | "Variable template specialization is declared with a template it."); |
| 2770 | |
| 2771 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2772 | TemplateArgumentListInfo TemplateArgs = |
| 2773 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2774 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2775 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2776 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2777 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2778 | TemplateName Name = TemplateId->Template.get(); |
| 2779 | |
| 2780 | // The template-id must name a variable template. |
| 2781 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2782 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2783 | if (!VarTemplate) { |
| 2784 | NamedDecl *FnTemplate; |
| 2785 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2786 | FnTemplate = *OTS->begin(); |
| 2787 | else |
| 2788 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2789 | if (FnTemplate) |
| 2790 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2791 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2792 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2793 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2794 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2795 | |
| 2796 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2797 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2798 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2799 | UPPC_PartialSpecialization)) |
| 2800 | return true; |
| 2801 | |
| 2802 | // Check that the template argument list is well-formed for this |
| 2803 | // template. |
| 2804 | SmallVector<TemplateArgument, 4> Converted; |
| 2805 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2806 | false, Converted)) |
| 2807 | return true; |
| 2808 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2809 | // Find the variable template (partial) specialization declaration that |
| 2810 | // corresponds to these arguments. |
| 2811 | if (IsPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2812 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 2813 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2814 | return true; |
| 2815 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2816 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
| 2817 | // also do them during instantiation. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2818 | bool InstantiationDependent; |
| 2819 | if (!Name.isDependent() && |
| 2820 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2821 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2822 | InstantiationDependent)) { |
| 2823 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2824 | << VarTemplate->getDeclName(); |
| 2825 | IsPartialSpecialization = false; |
| 2826 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2827 | |
| 2828 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2829 | Converted)) { |
| 2830 | // C++ [temp.class.spec]p9b3: |
| 2831 | // |
| 2832 | // -- The argument list of the specialization shall not be identical |
| 2833 | // to the implicit argument list of the primary template. |
| 2834 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2835 | << /*variable template*/ 1 |
| 2836 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2837 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2838 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2839 | // of the primary template. |
| 2840 | return true; |
| 2841 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2844 | void *InsertPos = nullptr; |
| 2845 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2846 | |
| 2847 | if (IsPartialSpecialization) |
| 2848 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2849 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2850 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2851 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2852 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2853 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2854 | |
| 2855 | // Check whether we can declare a variable template specialization in |
| 2856 | // the current scope. |
| 2857 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2858 | TemplateNameLoc, |
| 2859 | IsPartialSpecialization)) |
| 2860 | return true; |
| 2861 | |
| 2862 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2863 | // Since the only prior variable template specialization with these |
| 2864 | // arguments was referenced but not declared, reuse that |
| 2865 | // declaration node as our own, updating its source location and |
| 2866 | // the list of outer template parameters to reflect our new declaration. |
| 2867 | Specialization = PrevDecl; |
| 2868 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2869 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2870 | } else if (IsPartialSpecialization) { |
| 2871 | // Create a new class template partial specialization declaration node. |
| 2872 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2873 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2874 | VarTemplatePartialSpecializationDecl *Partial = |
| 2875 | VarTemplatePartialSpecializationDecl::Create( |
| 2876 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2877 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2878 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2879 | |
| 2880 | if (!PrevPartial) |
| 2881 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2882 | Specialization = Partial; |
| 2883 | |
| 2884 | // If we are providing an explicit specialization of a member variable |
| 2885 | // template specialization, make a note of that. |
| 2886 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2887 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2888 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2889 | CheckTemplatePartialSpecialization(Partial); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2890 | } else { |
| 2891 | // Create a new class template specialization declaration node for |
| 2892 | // this explicit specialization or friend declaration. |
| 2893 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2894 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2895 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2896 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2897 | |
| 2898 | if (!PrevDecl) |
| 2899 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2900 | } |
| 2901 | |
| 2902 | // C++ [temp.expl.spec]p6: |
| 2903 | // If a template, a member template or the member of a class template is |
| 2904 | // explicitly specialized then that specialization shall be declared |
| 2905 | // before the first use of that specialization that would cause an implicit |
| 2906 | // instantiation to take place, in every translation unit in which such a |
| 2907 | // use occurs; no diagnostic is required. |
| 2908 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2909 | bool Okay = false; |
| 2910 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2911 | // Is there any previous explicit specialization declaration? |
| 2912 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2913 | Okay = true; |
| 2914 | break; |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | if (!Okay) { |
| 2919 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2920 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2921 | << Name << Range; |
| 2922 | |
| 2923 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2924 | diag::note_instantiation_required_here) |
| 2925 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2926 | TSK_ImplicitInstantiation); |
| 2927 | return true; |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2932 | Specialization->setLexicalDeclContext(CurContext); |
| 2933 | |
| 2934 | // Add the specialization into its lexical context, so that it can |
| 2935 | // be seen when iterating through the list of declarations in that |
| 2936 | // context. However, specializations are not found by name lookup. |
| 2937 | CurContext->addDecl(Specialization); |
| 2938 | |
| 2939 | // Note that this is an explicit specialization. |
| 2940 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2941 | |
| 2942 | if (PrevDecl) { |
| 2943 | // Check that this isn't a redefinition of this specialization, |
| 2944 | // merging with previous declarations. |
| 2945 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2946 | ForRedeclaration); |
| 2947 | PrevSpec.addDecl(PrevDecl); |
| 2948 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2949 | } else if (Specialization->isStaticDataMember() && |
| 2950 | Specialization->isOutOfLine()) { |
| 2951 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | // Link instantiations of static data members back to the template from |
| 2955 | // which they were instantiated. |
| 2956 | if (Specialization->isStaticDataMember()) |
| 2957 | Specialization->setInstantiationOfStaticDataMember( |
| 2958 | VarTemplate->getTemplatedDecl(), |
| 2959 | Specialization->getSpecializationKind()); |
| 2960 | |
| 2961 | return Specialization; |
| 2962 | } |
| 2963 | |
| 2964 | namespace { |
| 2965 | /// \brief A partial specialization whose template arguments have matched |
| 2966 | /// a given template-id. |
| 2967 | struct PartialSpecMatchResult { |
| 2968 | VarTemplatePartialSpecializationDecl *Partial; |
| 2969 | TemplateArgumentList *Args; |
| 2970 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2971 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2972 | |
| 2973 | DeclResult |
| 2974 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2975 | SourceLocation TemplateNameLoc, |
| 2976 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2977 | assert(Template && "A variable template id without template?"); |
| 2978 | |
| 2979 | // Check that the template argument list is well-formed for this template. |
| 2980 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2981 | if (CheckTemplateArgumentList( |
| 2982 | Template, TemplateNameLoc, |
| 2983 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2984 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2985 | return true; |
| 2986 | |
| 2987 | // Find the variable template specialization declaration that |
| 2988 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2989 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2990 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2991 | Converted, InsertPos)) { |
| 2992 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2993 | // If we already have a variable template specialization, return it. |
| 2994 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2995 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2996 | |
| 2997 | // This is the first time we have referenced this variable template |
| 2998 | // specialization. Create the canonical declaration and add it to |
| 2999 | // the set of specializations, based on the closest partial specialization |
| 3000 | // that it represents. That is, |
| 3001 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 3002 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3003 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3004 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 3005 | bool AmbiguousPartialSpec = false; |
| 3006 | typedef PartialSpecMatchResult MatchResult; |
| 3007 | SmallVector<MatchResult, 4> Matched; |
| 3008 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3009 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 3010 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3011 | |
| 3012 | // 1. Attempt to find the closest partial specialization that this |
| 3013 | // specializes, if any. |
| 3014 | // If any of the template arguments is dependent, then this is probably |
| 3015 | // a placeholder for an incomplete declarative context; which must be |
| 3016 | // complete by instantiation time. Thus, do not search through the partial |
| 3017 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3018 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 3019 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 3020 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3021 | bool InstantiationDependent = false; |
| 3022 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3023 | TemplateArgs, InstantiationDependent)) { |
| 3024 | |
| 3025 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3026 | Template->getPartialSpecializations(PartialSpecs); |
| 3027 | |
| 3028 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 3029 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 3030 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 3031 | |
| 3032 | if (TemplateDeductionResult Result = |
| 3033 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 3034 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3035 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3036 | FailedCandidates.addCandidate().set( |
| 3037 | DeclAccessPair::make(Template, AS_public), Partial, |
| 3038 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3039 | (void)Result; |
| 3040 | } else { |
| 3041 | Matched.push_back(PartialSpecMatchResult()); |
| 3042 | Matched.back().Partial = Partial; |
| 3043 | Matched.back().Args = Info.take(); |
| 3044 | } |
| 3045 | } |
| 3046 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3047 | if (Matched.size() >= 1) { |
| 3048 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 3049 | if (Matched.size() == 1) { |
| 3050 | // -- If exactly one matching specialization is found, the |
| 3051 | // instantiation is generated from that specialization. |
| 3052 | // We don't need to do anything for this. |
| 3053 | } else { |
| 3054 | // -- If more than one matching specialization is found, the |
| 3055 | // partial order rules (14.5.4.2) are used to determine |
| 3056 | // whether one of the specializations is more specialized |
| 3057 | // than the others. If none of the specializations is more |
| 3058 | // specialized than all of the other matching |
| 3059 | // specializations, then the use of the variable template is |
| 3060 | // ambiguous and the program is ill-formed. |
| 3061 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 3062 | PEnd = Matched.end(); |
| 3063 | P != PEnd; ++P) { |
| 3064 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 3065 | PointOfInstantiation) == |
| 3066 | P->Partial) |
| 3067 | Best = P; |
| 3068 | } |
| 3069 | |
| 3070 | // Determine if the best partial specialization is more specialized than |
| 3071 | // the others. |
| 3072 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 3073 | PEnd = Matched.end(); |
| 3074 | P != PEnd; ++P) { |
| 3075 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 3076 | P->Partial, Best->Partial, |
| 3077 | PointOfInstantiation) != Best->Partial) { |
| 3078 | AmbiguousPartialSpec = true; |
| 3079 | break; |
| 3080 | } |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | // Instantiate using the best variable template partial specialization. |
| 3085 | InstantiationPattern = Best->Partial; |
| 3086 | InstantiationArgs = Best->Args; |
| 3087 | } else { |
| 3088 | // -- If no match is found, the instantiation is generated |
| 3089 | // from the primary template. |
| 3090 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 3091 | } |
| 3092 | } |
| 3093 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3094 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3095 | // Note that we do not instantiate a definition until we see an odr-use |
| 3096 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3097 | // FIXME: LateAttrs et al.? |
| 3098 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 3099 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 3100 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 3101 | if (!Decl) |
| 3102 | return true; |
| 3103 | |
| 3104 | if (AmbiguousPartialSpec) { |
| 3105 | // Partial ordering did not produce a clear winner. Complain. |
| 3106 | Decl->setInvalidDecl(); |
| 3107 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 3108 | << Decl; |
| 3109 | |
| 3110 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 3111 | for (MatchResult P : Matched) |
| 3112 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 3113 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 3114 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3115 | return true; |
| 3116 | } |
| 3117 | |
| 3118 | if (VarTemplatePartialSpecializationDecl *D = |
| 3119 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 3120 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 3121 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3122 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 3123 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3124 | assert(Decl && "No variable template specialization?"); |
| 3125 | return Decl; |
| 3126 | } |
| 3127 | |
| 3128 | ExprResult |
| 3129 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 3130 | const DeclarationNameInfo &NameInfo, |
| 3131 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3132 | const TemplateArgumentListInfo *TemplateArgs) { |
| 3133 | |
| 3134 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 3135 | *TemplateArgs); |
| 3136 | if (Decl.isInvalid()) |
| 3137 | return ExprError(); |
| 3138 | |
| 3139 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 3140 | if (!Var->getTemplateSpecializationKind()) |
| 3141 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 3142 | NameInfo.getLoc()); |
| 3143 | |
| 3144 | // Build an ordinary singleton decl ref. |
| 3145 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3146 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3149 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3150 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3151 | LookupResult &R, |
| 3152 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3153 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3154 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 3155 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3156 | // 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] | 3157 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3158 | // foo<int> could identify a single function unambiguously |
| 3159 | // This approach does NOT work, since f<int>(1); |
| 3160 | // gets resolved prior to resorting to overload resolution |
| 3161 | // i.e., template<class T> void f(double); |
| 3162 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3163 | |
| 3164 | // These should be filtered out by our callers. |
| 3165 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 3166 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 3167 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3168 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 3169 | bool InstantiationDependent; |
| 3170 | if (R.getAsSingle<VarTemplateDecl>() && |
| 3171 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3172 | *TemplateArgs, InstantiationDependent)) { |
| 3173 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 3174 | R.getAsSingle<VarTemplateDecl>(), |
| 3175 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3178 | // We don't want lookup warnings at this point. |
| 3179 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3180 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3181 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3182 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3183 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3184 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3185 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3186 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3187 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3188 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3189 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3192 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3193 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3194 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3195 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3196 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3197 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3198 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3199 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3200 | DeclContext *DC; |
| 3201 | if (!(DC = computeDeclContext(SS, false)) || |
| 3202 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3203 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 3204 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3206 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3207 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3208 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3209 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3210 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3211 | if (R.isAmbiguous()) |
| 3212 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3214 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3215 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 3216 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3217 | return ExprError(); |
| 3218 | } |
| 3219 | |
| 3220 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3221 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3222 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 3223 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3224 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 3225 | return ExprError(); |
| 3226 | } |
| 3227 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3228 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3231 | /// \brief Form a dependent template name. |
| 3232 | /// |
| 3233 | /// This action forms a dependent template name given the template |
| 3234 | /// name and its (presumably dependent) scope specifier. For |
| 3235 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 3236 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 3237 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3238 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3239 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3240 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3241 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3242 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3243 | bool EnteringContext, |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 3244 | TemplateTy &Result, |
| 3245 | bool AllowInjectedClassName) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3246 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 3247 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3248 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3249 | diag::warn_cxx98_compat_template_outside_of_template : |
| 3250 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3251 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 3252 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3253 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3254 | if (SS.isSet()) |
| 3255 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 3256 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3257 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3258 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3259 | // C++0x [temp.names]p5: |
| 3260 | // If a name prefixed by the keyword template is not the name of |
| 3261 | // a template, the program is ill-formed. [Note: the keyword |
| 3262 | // template may not be applied to non-template members of class |
| 3263 | // templates. -end note ] [ Note: as is the case with the |
| 3264 | // typename prefix, the template prefix is allowed in cases |
| 3265 | // where it is not strictly necessary; i.e., when the |
| 3266 | // nested-name-specifier or the expression on the left of the -> |
| 3267 | // or . is not dependent on a template-parameter, or the use |
| 3268 | // does not appear in the scope of a template. -end note] |
| 3269 | // |
| 3270 | // Note: C++03 was more strict here, because it banned the use of |
| 3271 | // the "template" keyword prior to a template-name that was not a |
| 3272 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3273 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3274 | // 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] | 3275 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3276 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3277 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3278 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3279 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3280 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3281 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3282 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3283 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3284 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3285 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3286 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3287 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3288 | << Name.getSourceRange() |
| 3289 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3290 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3291 | } else { |
| 3292 | // We found something; return it. |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 3293 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
| 3294 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && |
| 3295 | Name.getKind() == UnqualifiedId::IK_Identifier && Name.Identifier && |
| 3296 | LookupRD->getIdentifier() == Name.Identifier) { |
| 3297 | // C++14 [class.qual]p2: |
| 3298 | // In a lookup in which function names are not ignored and the |
| 3299 | // nested-name-specifier nominates a class C, if the name specified |
| 3300 | // [...] is the injected-class-name of C, [...] the name is instead |
| 3301 | // considered to name the constructor |
| 3302 | // |
| 3303 | // We don't get here if naming the constructor would be valid, so we |
| 3304 | // just reject immediately and recover by treating the |
| 3305 | // injected-class-name as naming the template. |
| 3306 | Diag(Name.getLocStart(), |
| 3307 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 3308 | << Name.Identifier << 0 /*injected-class-name used as template name*/ |
| 3309 | << 1 /*'template' keyword was used*/; |
| 3310 | } |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3311 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3312 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3315 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3316 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3317 | switch (Name.getKind()) { |
| 3318 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3319 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3320 | Name.Identifier)); |
| 3321 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3322 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3323 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3324 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3325 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3326 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3327 | |
| 3328 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3329 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3330 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3331 | default: |
| 3332 | break; |
| 3333 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3334 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3335 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3336 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3337 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3338 | << Name.getSourceRange() |
| 3339 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3340 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3343 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3344 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3345 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3346 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3347 | QualType ArgType; |
| 3348 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3349 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3350 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3351 | switch(Arg.getKind()) { |
| 3352 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3353 | // C++ [temp.arg.type]p1: |
| 3354 | // A template-argument for a template-parameter which is a |
| 3355 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3356 | ArgType = Arg.getAsType(); |
| 3357 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3358 | break; |
| 3359 | case TemplateArgument::Template: { |
| 3360 | // We have a template type parameter but the template argument |
| 3361 | // is a template without any arguments. |
| 3362 | SourceRange SR = AL.getSourceRange(); |
| 3363 | TemplateName Name = Arg.getAsTemplate(); |
| 3364 | Diag(SR.getBegin(), diag::err_template_missing_args) |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 3365 | << (int)getTemplateNameKindForDiagnostics(Name) << Name << SR; |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3366 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3367 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3368 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3369 | return true; |
| 3370 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3371 | case TemplateArgument::Expression: { |
| 3372 | // We have a template type parameter but the template argument is an |
| 3373 | // expression; see if maybe it is missing the "typename" keyword. |
| 3374 | CXXScopeSpec SS; |
| 3375 | DeclarationNameInfo NameInfo; |
| 3376 | |
| 3377 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3378 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3379 | NameInfo = ArgExpr->getNameInfo(); |
| 3380 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3381 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3382 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3383 | NameInfo = ArgExpr->getNameInfo(); |
| 3384 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3385 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3386 | if (ArgExpr->isImplicitAccess()) { |
| 3387 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3388 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3389 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3392 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3393 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3394 | LookupParsedName(Result, CurScope, &SS); |
| 3395 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3396 | if (Result.getAsSingle<TypeDecl>() || |
| 3397 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3398 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3399 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3400 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3401 | Diag(Loc, getLangOpts().MSVCCompat |
| 3402 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3403 | : diag::err_template_arg_must_be_type_suggest) |
| 3404 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3405 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3406 | |
| 3407 | // Recover by synthesizing a type using the location information that we |
| 3408 | // already have. |
| 3409 | ArgType = |
| 3410 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3411 | TypeLocBuilder TLB; |
| 3412 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3413 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3414 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3415 | TL.setNameLoc(NameInfo.getLoc()); |
| 3416 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3417 | |
| 3418 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3419 | // properly. |
| 3420 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3421 | TemplateArgumentLocInfo(TSI)); |
| 3422 | |
| 3423 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3424 | } |
| 3425 | } |
| 3426 | // fallthrough |
| 3427 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3428 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3429 | // We have a template type parameter but the template argument |
| 3430 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3431 | SourceRange SR = AL.getSourceRange(); |
| 3432 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3433 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3435 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3437 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3438 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3439 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3440 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3442 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3443 | ArgType = Context.getCanonicalType(ArgType); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3445 | // Objective-C ARC: |
| 3446 | // If an explicitly-specified template argument type is a lifetime type |
| 3447 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3448 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3449 | ArgType->isObjCLifetimeType() && |
| 3450 | !ArgType.getObjCLifetime()) { |
| 3451 | Qualifiers Qs; |
| 3452 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3453 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3454 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3455 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3456 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3457 | return false; |
| 3458 | } |
| 3459 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3460 | /// \brief Substitute template arguments into the default template argument for |
| 3461 | /// the given template type parameter. |
| 3462 | /// |
| 3463 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3464 | /// the substitution. |
| 3465 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3466 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3467 | /// for. |
| 3468 | /// |
| 3469 | /// \param TemplateLoc the location of the template name that started the |
| 3470 | /// template-id we are checking. |
| 3471 | /// |
| 3472 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3473 | /// terminates the template-id. |
| 3474 | /// |
| 3475 | /// \param Param the template template parameter whose default we are |
| 3476 | /// substituting into. |
| 3477 | /// |
| 3478 | /// \param Converted the list of template arguments provided for template |
| 3479 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3480 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3481 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3482 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3483 | TemplateDecl *Template, |
| 3484 | SourceLocation TemplateLoc, |
| 3485 | SourceLocation RAngleLoc, |
| 3486 | TemplateTypeParmDecl *Param, |
Vassil Vassilev | 2999d0e | 2017-01-10 09:09:09 +0000 | [diff] [blame] | 3487 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3488 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3489 | |
| 3490 | // If the argument type is dependent, instantiate it now based |
| 3491 | // on the previously-computed template arguments. |
| 3492 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3493 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3494 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3495 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3496 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3497 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3498 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3499 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3500 | |
| 3501 | // Only substitute for the innermost template argument list. |
| 3502 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3503 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3504 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3505 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3506 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3507 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3508 | ArgType = |
| 3509 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3510 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | return ArgType; |
| 3514 | } |
| 3515 | |
| 3516 | /// \brief Substitute template arguments into the default template argument for |
| 3517 | /// the given non-type template parameter. |
| 3518 | /// |
| 3519 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3520 | /// the substitution. |
| 3521 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3522 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3523 | /// for. |
| 3524 | /// |
| 3525 | /// \param TemplateLoc the location of the template name that started the |
| 3526 | /// template-id we are checking. |
| 3527 | /// |
| 3528 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3529 | /// terminates the template-id. |
| 3530 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3531 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3532 | /// substituting into. |
| 3533 | /// |
| 3534 | /// \param Converted the list of template arguments provided for template |
| 3535 | /// parameters that precede \p Param in the template parameter list. |
| 3536 | /// |
| 3537 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3538 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3539 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3540 | TemplateDecl *Template, |
| 3541 | SourceLocation TemplateLoc, |
| 3542 | SourceLocation RAngleLoc, |
| 3543 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3544 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3545 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3546 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3547 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3548 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3549 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3550 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3551 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3552 | |
| 3553 | // Only substitute for the innermost template argument list. |
| 3554 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3555 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3556 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3557 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3558 | |
Faisal Vali | 48401eb | 2015-11-19 19:20:17 +0000 | [diff] [blame] | 3559 | EnterExpressionEvaluationContext ConstantEvaluated(SemaRef, |
| 3560 | Sema::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3561 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3564 | /// \brief Substitute template arguments into the default template argument for |
| 3565 | /// the given template template parameter. |
| 3566 | /// |
| 3567 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3568 | /// the substitution. |
| 3569 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3570 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3571 | /// for. |
| 3572 | /// |
| 3573 | /// \param TemplateLoc the location of the template name that started the |
| 3574 | /// template-id we are checking. |
| 3575 | /// |
| 3576 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3577 | /// terminates the template-id. |
| 3578 | /// |
| 3579 | /// \param Param the template template parameter whose default we are |
| 3580 | /// substituting into. |
| 3581 | /// |
| 3582 | /// \param Converted the list of template arguments provided for template |
| 3583 | /// parameters that precede \p Param in the template parameter list. |
| 3584 | /// |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3585 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3586 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3587 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3588 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3589 | static TemplateName |
| 3590 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3591 | TemplateDecl *Template, |
| 3592 | SourceLocation TemplateLoc, |
| 3593 | SourceLocation RAngleLoc, |
| 3594 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3595 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3596 | NestedNameSpecifierLoc &QualifierLoc) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3597 | Sema::InstantiatingTemplate Inst( |
| 3598 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 3599 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3600 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3601 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3602 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3603 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3604 | |
| 3605 | // Only substitute for the innermost template argument list. |
| 3606 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3607 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3608 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3609 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3610 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3611 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3612 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3613 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3614 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3615 | QualifierLoc = |
| 3616 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3617 | if (!QualifierLoc) |
| 3618 | return TemplateName(); |
| 3619 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3620 | |
| 3621 | return SemaRef.SubstTemplateName( |
| 3622 | QualifierLoc, |
| 3623 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3624 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3625 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3628 | /// \brief If the given template parameter has a default template |
| 3629 | /// argument, substitute into that default template argument and |
| 3630 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3631 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3632 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3633 | SourceLocation TemplateLoc, |
| 3634 | SourceLocation RAngleLoc, |
| 3635 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3636 | SmallVectorImpl<TemplateArgument> |
| 3637 | &Converted, |
| 3638 | bool &HasDefaultArg) { |
| 3639 | HasDefaultArg = false; |
| 3640 | |
| 3641 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3642 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3643 | return TemplateArgumentLoc(); |
| 3644 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3645 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3646 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3647 | TemplateLoc, |
| 3648 | RAngleLoc, |
| 3649 | TypeParm, |
| 3650 | Converted); |
| 3651 | if (DI) |
| 3652 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3653 | |
| 3654 | return TemplateArgumentLoc(); |
| 3655 | } |
| 3656 | |
| 3657 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3658 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3659 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3660 | return TemplateArgumentLoc(); |
| 3661 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3662 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3663 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3664 | TemplateLoc, |
| 3665 | RAngleLoc, |
| 3666 | NonTypeParm, |
| 3667 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3668 | if (Arg.isInvalid()) |
| 3669 | return TemplateArgumentLoc(); |
| 3670 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3671 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3672 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3673 | } |
| 3674 | |
| 3675 | TemplateTemplateParmDecl *TempTempParm |
| 3676 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3677 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3678 | return TemplateArgumentLoc(); |
| 3679 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3680 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3681 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3682 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3683 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3684 | RAngleLoc, |
| 3685 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3686 | Converted, |
| 3687 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3688 | if (TName.isNull()) |
| 3689 | return TemplateArgumentLoc(); |
| 3690 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3691 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3692 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3693 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3694 | } |
| 3695 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 3696 | /// Convert a template-argument that we parsed as a type into a template, if |
| 3697 | /// possible. C++ permits injected-class-names to perform dual service as |
| 3698 | /// template template arguments and as template type arguments. |
| 3699 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { |
| 3700 | // Extract and step over any surrounding nested-name-specifier. |
| 3701 | NestedNameSpecifierLoc QualLoc; |
| 3702 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { |
| 3703 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
| 3704 | return TemplateArgumentLoc(); |
| 3705 | |
| 3706 | QualLoc = ETLoc.getQualifierLoc(); |
| 3707 | TLoc = ETLoc.getNamedTypeLoc(); |
| 3708 | } |
| 3709 | |
| 3710 | // If this type was written as an injected-class-name, it can be used as a |
| 3711 | // template template argument. |
| 3712 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) |
| 3713 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), |
| 3714 | QualLoc, InjLoc.getNameLoc()); |
| 3715 | |
| 3716 | // If this type was written as an injected-class-name, it may have been |
| 3717 | // converted to a RecordType during instantiation. If the RecordType is |
| 3718 | // *not* wrapped in a TemplateSpecializationType and denotes a class |
| 3719 | // template specialization, it must have come from an injected-class-name. |
| 3720 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) |
| 3721 | if (auto *CTSD = |
| 3722 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
| 3723 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), |
| 3724 | QualLoc, RecLoc.getNameLoc()); |
| 3725 | |
| 3726 | return TemplateArgumentLoc(); |
| 3727 | } |
| 3728 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3729 | /// \brief Check that the given template argument corresponds to the given |
| 3730 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3731 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3732 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3733 | /// checked. |
| 3734 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3735 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3736 | /// |
| 3737 | /// \param Template The template in which the template argument resides. |
| 3738 | /// |
| 3739 | /// \param TemplateLoc The location of the template name for the template |
| 3740 | /// whose argument list we're matching. |
| 3741 | /// |
| 3742 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3743 | /// the template argument list. |
| 3744 | /// |
| 3745 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3746 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3747 | /// |
| 3748 | /// \param Converted The checked, converted argument will be added to the |
| 3749 | /// end of this small vector. |
| 3750 | /// |
| 3751 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3752 | /// explicitly written, deduced, etc. |
| 3753 | /// |
| 3754 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3755 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3756 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3757 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3758 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3759 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3760 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3761 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3762 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3763 | // Check template type parameters. |
| 3764 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3765 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3767 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3768 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3769 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3770 | // with the template arguments we've seen thus far. But if the |
| 3771 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3772 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3773 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3774 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3775 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3776 | if (NTTPType->isDependentType() && |
| 3777 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3778 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3779 | // Do substitution on the type of the non-type template parameter. |
| 3780 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3781 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3782 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3783 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3784 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3785 | |
| 3786 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3787 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3788 | NTTPType = SubstType(NTTPType, |
| 3789 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3790 | NTTP->getLocation(), |
| 3791 | NTTP->getDeclName()); |
| 3792 | // If that worked, check the non-type template parameter type |
| 3793 | // for validity. |
| 3794 | if (!NTTPType.isNull()) |
| 3795 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3796 | NTTP->getLocation()); |
| 3797 | if (NTTPType.isNull()) |
| 3798 | return true; |
| 3799 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3801 | switch (Arg.getArgument().getKind()) { |
| 3802 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3803 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3804 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3805 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3806 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3807 | ExprResult Res = |
| 3808 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3809 | Result, CTAK); |
| 3810 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3811 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3812 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3813 | // If the resulting expression is new, then use it in place of the |
| 3814 | // old expression in the template argument. |
| 3815 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3816 | TemplateArgument TA(Res.get()); |
| 3817 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3818 | } |
| 3819 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3820 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3821 | break; |
| 3822 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3823 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3824 | case TemplateArgument::Declaration: |
| 3825 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3826 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3827 | // We've already checked this template argument, so just copy |
| 3828 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3829 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3830 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3831 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3832 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3833 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3834 | // We were given a template template argument. It may not be ill-formed; |
| 3835 | // see below. |
| 3836 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3837 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3838 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3839 | // We have a template argument such as \c T::template X, which we |
| 3840 | // parsed as a template template argument. However, since we now |
| 3841 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3842 | // template name into an expression. |
| 3843 | |
| 3844 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3845 | Arg.getTemplateNameLoc()); |
| 3846 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3847 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3848 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3849 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3850 | // so it was provided with a template keyword. However, its source |
| 3851 | // location is not stored in the template argument structure. |
| 3852 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3853 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3854 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3855 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3856 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3857 | // If we parsed the template argument as a pack expansion, create a |
| 3858 | // pack expansion expression. |
| 3859 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3860 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3861 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3862 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3863 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3864 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3865 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3866 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3867 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3868 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3870 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3871 | break; |
| 3872 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3873 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3874 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3875 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3876 | // therefore cannot be a non-type template argument. |
| 3877 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3878 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3879 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3880 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3881 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3882 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3883 | case TemplateArgument::Type: { |
| 3884 | // We have a non-type template parameter but the template |
| 3885 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3887 | // C++ [temp.arg]p2: |
| 3888 | // In a template-argument, an ambiguity between a type-id and |
| 3889 | // an expression is resolved to a type-id, regardless of the |
| 3890 | // form of the corresponding template-parameter. |
| 3891 | // |
| 3892 | // We warn specifically about this case, since it can be rather |
| 3893 | // confusing for users. |
| 3894 | QualType T = Arg.getArgument().getAsType(); |
| 3895 | SourceRange SR = Arg.getSourceRange(); |
| 3896 | if (T->isFunctionType()) |
| 3897 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3898 | else |
| 3899 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3900 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3901 | return true; |
| 3902 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3903 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3904 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3905 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3906 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3907 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3908 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3912 | // Check template template parameters. |
| 3913 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3914 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3915 | // Substitute into the template parameter list of the template |
| 3916 | // template parameter, since previously-supplied template arguments |
| 3917 | // may appear within the template template parameter. |
| 3918 | { |
| 3919 | // Set up a template instantiation context. |
| 3920 | LocalInstantiationScope Scope(*this); |
| 3921 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3922 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3923 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3924 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3925 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3926 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3927 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3928 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3929 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3930 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3931 | if (!TempParm) |
| 3932 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3933 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3934 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 3935 | // C++1z [temp.local]p1: (DR1004) |
| 3936 | // When [the injected-class-name] is used [...] as a template-argument for |
| 3937 | // a template template-parameter [...] it refers to the class template |
| 3938 | // itself. |
| 3939 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { |
| 3940 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
| 3941 | Arg.getTypeSourceInfo()->getTypeLoc()); |
| 3942 | if (!ConvertedArg.getArgument().isNull()) |
| 3943 | Arg = ConvertedArg; |
| 3944 | } |
| 3945 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3946 | switch (Arg.getArgument().getKind()) { |
| 3947 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3948 | llvm_unreachable("Should never see a NULL template argument here"); |
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 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3951 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3952 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3953 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3954 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3955 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3956 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3957 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3958 | case TemplateArgument::Expression: |
| 3959 | case TemplateArgument::Type: |
| 3960 | // We have a template template parameter but the template |
| 3961 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3962 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3963 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3964 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3965 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3966 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3967 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3968 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3969 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3970 | case TemplateArgument::NullPtr: |
| 3971 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3972 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3973 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3974 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3975 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3976 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3977 | return false; |
| 3978 | } |
| 3979 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3980 | /// \brief Diagnose an arity mismatch in the |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3981 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3982 | SourceLocation TemplateLoc, |
| 3983 | TemplateArgumentListInfo &TemplateArgs) { |
| 3984 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3985 | unsigned NumParams = Params->size(); |
| 3986 | unsigned NumArgs = TemplateArgs.size(); |
| 3987 | |
| 3988 | SourceRange Range; |
| 3989 | if (NumArgs > NumParams) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3990 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3991 | TemplateArgs.getRAngleLoc()); |
| 3992 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3993 | << (NumArgs > NumParams) |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 3994 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3995 | << Template << Range; |
| 3996 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3997 | << Params->getSourceRange(); |
| 3998 | return true; |
| 3999 | } |
| 4000 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4001 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 4002 | /// determine the number of parameters produced by that expansion. For instance: |
| 4003 | /// |
| 4004 | /// \code |
| 4005 | /// template<typename ...Ts> struct A { |
| 4006 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 4007 | /// }; |
| 4008 | /// \endcode |
| 4009 | /// |
| 4010 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 4011 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4012 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4013 | if (NonTypeTemplateParmDecl *NTTP |
| 4014 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4015 | if (NTTP->isExpandedParameterPack()) |
| 4016 | return NTTP->getNumExpansionTypes(); |
| 4017 | } |
| 4018 | |
| 4019 | if (TemplateTemplateParmDecl *TTP |
| 4020 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 4021 | if (TTP->isExpandedParameterPack()) |
| 4022 | return TTP->getNumExpansionTemplateParameters(); |
| 4023 | } |
| 4024 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 4025 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4028 | /// Diagnose a missing template argument. |
| 4029 | template<typename TemplateParmDecl> |
| 4030 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 4031 | TemplateDecl *TD, |
| 4032 | const TemplateParmDecl *D, |
| 4033 | TemplateArgumentListInfo &Args) { |
| 4034 | // Dig out the most recent declaration of the template parameter; there may be |
| 4035 | // declarations of the template that are more recent than TD. |
| 4036 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 4037 | ->getTemplateParameters() |
| 4038 | ->getParam(D->getIndex())); |
| 4039 | |
| 4040 | // If there's a default argument that's not visible, diagnose that we're |
| 4041 | // missing a module import. |
| 4042 | llvm::SmallVector<Module*, 8> Modules; |
| 4043 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 4044 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 4045 | D->getDefaultArgumentLoc(), Modules, |
| 4046 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4047 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4048 | return true; |
| 4049 | } |
| 4050 | |
| 4051 | // FIXME: If there's a more recent default argument that *is* visible, |
| 4052 | // diagnose that it was declared too late. |
| 4053 | |
| 4054 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 4055 | } |
| 4056 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4057 | /// \brief Check that the given template argument list is well-formed |
| 4058 | /// for specializing the given template. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4059 | bool Sema::CheckTemplateArgumentList( |
| 4060 | TemplateDecl *Template, SourceLocation TemplateLoc, |
| 4061 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
| 4062 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4063 | bool UpdateArgsWithConversions) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4064 | // Make a copy of the template arguments for processing. Only make the |
| 4065 | // changes at the end when successful in matching the arguments to the |
| 4066 | // template. |
| 4067 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 4068 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4069 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4070 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4071 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4072 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4074 | // [...] The type and form of each template-argument specified in |
| 4075 | // a template-id shall match the type and form specified for the |
| 4076 | // corresponding parameter declared by the template in its |
| 4077 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4078 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4079 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4080 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4081 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4082 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4083 | ParamEnd = Params->end(); |
| 4084 | Param != ParamEnd; /* increment in loop */) { |
| 4085 | // If we have an expanded parameter pack, make sure we don't have too |
| 4086 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4087 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4088 | if (*Expansions == ArgumentPack.size()) { |
| 4089 | // We're done with this parameter pack. Pack up its arguments and add |
| 4090 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4091 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4092 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4093 | ArgumentPack.clear(); |
| 4094 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4095 | // This argument is assigned to the next parameter. |
| 4096 | ++Param; |
| 4097 | continue; |
| 4098 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 4099 | // Not enough arguments for this parameter pack. |
| 4100 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 4101 | << false |
Richard Smith | 0c062b4 | 2017-01-14 02:19:59 +0000 | [diff] [blame] | 4102 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4103 | << Template; |
| 4104 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 4105 | << Params->getSourceRange(); |
| 4106 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4107 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4108 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4109 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4110 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4111 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4112 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4113 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4114 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4115 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4116 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4117 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4118 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4119 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 4120 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4121 | // 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] | 4122 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4123 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4124 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4125 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4126 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4127 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 4128 | return true; |
| 4129 | } |
| 4130 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4131 | // We're now done with this argument. |
| 4132 | ++ArgIdx; |
| 4133 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4134 | if ((*Param)->isTemplateParameterPack()) { |
| 4135 | // The template parameter was a template parameter pack, so take the |
| 4136 | // deduced argument and place it on the argument pack. Note that we |
| 4137 | // stay on the same template parameter so that we can deduce more |
| 4138 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 4139 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4140 | } else { |
| 4141 | // Move to the next template parameter. |
| 4142 | ++Param; |
| 4143 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4144 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4145 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 4146 | // the remaining arguments, because we don't know what parameters they'll |
| 4147 | // match up with. |
| 4148 | if (PackExpansionIntoNonPack) { |
| 4149 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4150 | // If we were part way through filling in an expanded parameter pack, |
| 4151 | // fall back to just producing individual arguments. |
| 4152 | Converted.insert(Converted.end(), |
| 4153 | ArgumentPack.begin(), ArgumentPack.end()); |
| 4154 | ArgumentPack.clear(); |
| 4155 | } |
| 4156 | |
| 4157 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4158 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4159 | ++ArgIdx; |
| 4160 | } |
| 4161 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4162 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4163 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4164 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4165 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4166 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4167 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 4168 | // If we're checking a partial template argument list, we're done. |
| 4169 | if (PartialTemplateArgs) { |
| 4170 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4171 | Converted.push_back( |
| 4172 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 4173 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4174 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4177 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4178 | // 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] | 4179 | if ((*Param)->isTemplateParameterPack()) { |
| 4180 | assert(!getExpandedPackSize(*Param) && |
| 4181 | "Should have dealt with this already"); |
| 4182 | |
| 4183 | // A non-expanded parameter pack before the end of the parameter list |
| 4184 | // only occurs for an ill-formed template parameter list, unless we've |
| 4185 | // got a partial argument list for a function template, so just bail out. |
| 4186 | if (Param + 1 != ParamEnd) |
| 4187 | return true; |
| 4188 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4189 | Converted.push_back( |
| 4190 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4191 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4192 | |
| 4193 | ++Param; |
| 4194 | continue; |
| 4195 | } |
| 4196 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4197 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4198 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4199 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4200 | // Retrieve the default template argument from the template |
| 4201 | // parameter. For each kind of template parameter, we substitute the |
| 4202 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4203 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4204 | // the default argument. |
| 4205 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4206 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4207 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 4208 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4209 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4210 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4211 | Template, |
| 4212 | TemplateLoc, |
| 4213 | RAngleLoc, |
| 4214 | TTP, |
| 4215 | Converted); |
| 4216 | if (!ArgType) |
| 4217 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4218 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4219 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 4220 | ArgType); |
| 4221 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4222 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4223 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4224 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 4225 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4226 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4227 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4228 | TemplateLoc, |
| 4229 | RAngleLoc, |
| 4230 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4231 | Converted); |
| 4232 | if (E.isInvalid()) |
| 4233 | return true; |
| 4234 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4235 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4236 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 4237 | } else { |
| 4238 | TemplateTemplateParmDecl *TempParm |
| 4239 | = cast<TemplateTemplateParmDecl>(*Param); |
| 4240 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4241 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4242 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 4243 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4244 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4245 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4246 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4247 | TemplateLoc, |
| 4248 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4249 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4250 | Converted, |
| 4251 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4252 | if (Name.isNull()) |
| 4253 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4254 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4255 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 4256 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4257 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4259 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4260 | // the default template argument. We're not actually instantiating a |
| 4261 | // template here, we just create this object to put a note into the |
| 4262 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4263 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 4264 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4265 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4266 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4268 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4269 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4270 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4271 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4272 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4273 | // Core issue 150 (assumed resolution): if this is a template template |
| 4274 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4275 | // template definition. |
| 4276 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4277 | NewArgs.addArgument(Arg); |
| 4278 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4279 | // Move to the next template parameter and argument. |
| 4280 | ++Param; |
| 4281 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4282 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4283 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4284 | // If we're performing a partial argument substitution, allow any trailing |
| 4285 | // pack expansions; they might be empty. This can happen even if |
| 4286 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 4287 | // still dependent). |
| 4288 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 4289 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4290 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 4291 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4294 | // If we have any leftover arguments, then there were too many arguments. |
| 4295 | // Complain and fail. |
| 4296 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4297 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 4298 | |
| 4299 | // No problems found with the new argument list, propagate changes back |
| 4300 | // to caller. |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 4301 | if (UpdateArgsWithConversions) |
| 4302 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4303 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4304 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4307 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | class UnnamedLocalNoLinkageFinder |
| 4309 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4310 | { |
| 4311 | Sema &S; |
| 4312 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4313 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4314 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4315 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4316 | public: |
| 4317 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 4318 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4319 | bool Visit(QualType T) { |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 4320 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4321 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4322 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4323 | #define TYPE(Class, Parent) \ |
| 4324 | bool Visit##Class##Type(const Class##Type *); |
| 4325 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4326 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4327 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4328 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4329 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4330 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4331 | bool VisitTagDecl(const TagDecl *Tag); |
| 4332 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4333 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4334 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4335 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4336 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4337 | return false; |
| 4338 | } |
| 4339 | |
| 4340 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4341 | return Visit(T->getElementType()); |
| 4342 | } |
| 4343 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4344 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4345 | return Visit(T->getPointeeType()); |
| 4346 | } |
| 4347 | |
| 4348 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4349 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4350 | return Visit(T->getPointeeType()); |
| 4351 | } |
| 4352 | |
| 4353 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4354 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4355 | return Visit(T->getPointeeType()); |
| 4356 | } |
| 4357 | |
| 4358 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4359 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4360 | return Visit(T->getPointeeType()); |
| 4361 | } |
| 4362 | |
| 4363 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4364 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4365 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4366 | } |
| 4367 | |
| 4368 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4369 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4370 | return Visit(T->getElementType()); |
| 4371 | } |
| 4372 | |
| 4373 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4374 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4375 | return Visit(T->getElementType()); |
| 4376 | } |
| 4377 | |
| 4378 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4379 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4380 | return Visit(T->getElementType()); |
| 4381 | } |
| 4382 | |
| 4383 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4384 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4385 | return Visit(T->getElementType()); |
| 4386 | } |
| 4387 | |
| 4388 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4389 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4390 | return Visit(T->getElementType()); |
| 4391 | } |
| 4392 | |
| 4393 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4394 | return Visit(T->getElementType()); |
| 4395 | } |
| 4396 | |
| 4397 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4398 | return Visit(T->getElementType()); |
| 4399 | } |
| 4400 | |
| 4401 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4402 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4403 | for (const auto &A : T->param_types()) { |
| 4404 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4405 | return true; |
| 4406 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4407 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4408 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4412 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4413 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4417 | const UnresolvedUsingType*) { |
| 4418 | return false; |
| 4419 | } |
| 4420 | |
| 4421 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4422 | return false; |
| 4423 | } |
| 4424 | |
| 4425 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4426 | return Visit(T->getUnderlyingType()); |
| 4427 | } |
| 4428 | |
| 4429 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4430 | return false; |
| 4431 | } |
| 4432 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4433 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4434 | const UnaryTransformType*) { |
| 4435 | return false; |
| 4436 | } |
| 4437 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4438 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4439 | return Visit(T->getDeducedType()); |
| 4440 | } |
| 4441 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 4442 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
| 4443 | const DeducedTemplateSpecializationType *T) { |
| 4444 | return Visit(T->getDeducedType()); |
| 4445 | } |
| 4446 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4447 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4448 | return VisitTagDecl(T->getDecl()); |
| 4449 | } |
| 4450 | |
| 4451 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4452 | return VisitTagDecl(T->getDecl()); |
| 4453 | } |
| 4454 | |
| 4455 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4456 | const TemplateTypeParmType*) { |
| 4457 | return false; |
| 4458 | } |
| 4459 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4460 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4461 | const SubstTemplateTypeParmPackType *) { |
| 4462 | return false; |
| 4463 | } |
| 4464 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4465 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4466 | const TemplateSpecializationType*) { |
| 4467 | return false; |
| 4468 | } |
| 4469 | |
| 4470 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4471 | const InjectedClassNameType* T) { |
| 4472 | return VisitTagDecl(T->getDecl()); |
| 4473 | } |
| 4474 | |
| 4475 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4476 | const DependentNameType* T) { |
| 4477 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4478 | } |
| 4479 | |
| 4480 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4481 | const DependentTemplateSpecializationType* T) { |
| 4482 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4483 | } |
| 4484 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4485 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4486 | const PackExpansionType* T) { |
| 4487 | return Visit(T->getPattern()); |
| 4488 | } |
| 4489 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4490 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4491 | return false; |
| 4492 | } |
| 4493 | |
| 4494 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4495 | const ObjCInterfaceType *) { |
| 4496 | return false; |
| 4497 | } |
| 4498 | |
| 4499 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4500 | const ObjCObjectPointerType *) { |
| 4501 | return false; |
| 4502 | } |
| 4503 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4504 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4505 | return Visit(T->getValueType()); |
| 4506 | } |
| 4507 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4508 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 4509 | return false; |
| 4510 | } |
| 4511 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4512 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4513 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4514 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4515 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4516 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4517 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4518 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4519 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4522 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4523 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4524 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4525 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4526 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4527 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4528 | return true; |
| 4529 | } |
| 4530 | |
| 4531 | return false; |
| 4532 | } |
| 4533 | |
| 4534 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4535 | NestedNameSpecifier *NNS) { |
| 4536 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4537 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4538 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4539 | switch (NNS->getKind()) { |
| 4540 | case NestedNameSpecifier::Identifier: |
| 4541 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4542 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4543 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4544 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4545 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4546 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4547 | case NestedNameSpecifier::TypeSpec: |
| 4548 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4549 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4550 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4551 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4552 | } |
| 4553 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4554 | /// \brief Check a template argument against its corresponding |
| 4555 | /// template type parameter. |
| 4556 | /// |
| 4557 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4558 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4559 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4560 | TypeSourceInfo *ArgInfo) { |
| 4561 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4562 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4563 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4564 | |
| 4565 | if (Arg->isVariablyModifiedType()) { |
| 4566 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4567 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4568 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4571 | // C++03 [temp.arg.type]p2: |
| 4572 | // A local type, a type with no linkage, an unnamed type or a type |
| 4573 | // compounded from any of these types shall not be used as a |
| 4574 | // template-argument for a template type-parameter. |
| 4575 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4576 | // 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] | 4577 | // a warning. |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame] | 4578 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4579 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4580 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4581 | } |
| 4582 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4583 | return false; |
| 4584 | } |
| 4585 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4586 | enum NullPointerValueKind { |
| 4587 | NPV_NotNullPointer, |
| 4588 | NPV_NullPointer, |
| 4589 | NPV_Error |
| 4590 | }; |
| 4591 | |
| 4592 | /// \brief Determine whether the given template argument is a null pointer |
| 4593 | /// value of the appropriate type. |
| 4594 | static NullPointerValueKind |
| 4595 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4596 | QualType ParamType, Expr *Arg) { |
| 4597 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4598 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4599 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4600 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 4601 | llvm_unreachable( |
| 4602 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4603 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4604 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4605 | return NPV_NotNullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4606 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4607 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4608 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4609 | if (ArgRV.isInvalid()) |
| 4610 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4611 | Arg = ArgRV.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4612 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4613 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4614 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4615 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4616 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4617 | EvalResult.HasSideEffects) { |
| 4618 | SourceLocation DiagLoc = Arg->getExprLoc(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4619 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4620 | // If our only note is the usual "invalid subexpression" note, just point |
| 4621 | // the caret at its location rather than producing an essentially |
| 4622 | // redundant note. |
| 4623 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4624 | diag::note_invalid_subexpr_in_const_expr) { |
| 4625 | DiagLoc = Notes[0].first; |
| 4626 | Notes.clear(); |
| 4627 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4628 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4629 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4630 | << Arg->getType() << Arg->getSourceRange(); |
| 4631 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4632 | S.Diag(Notes[I].first, Notes[I].second); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4633 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4634 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4635 | return NPV_Error; |
| 4636 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4637 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4638 | // C++11 [temp.arg.nontype]p1: |
| 4639 | // - an address constant expression of type std::nullptr_t |
| 4640 | if (Arg->getType()->isNullPtrType()) |
| 4641 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4642 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4643 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4644 | // - a constant expression that evaluates to a null member pointer value |
| 4645 | // (4.11); or |
| 4646 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4647 | (EvalResult.Val.isMemberPointer() && |
| 4648 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4649 | // If our expression has an appropriate type, we've succeeded. |
| 4650 | bool ObjCLifetimeConversion; |
| 4651 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4652 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4653 | ObjCLifetimeConversion)) |
| 4654 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4655 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4656 | // The types didn't match, but we know we got a null pointer; complain, |
| 4657 | // then recover as if the types were correct. |
| 4658 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4659 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4660 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4661 | return NPV_NullPointer; |
| 4662 | } |
| 4663 | |
| 4664 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4665 | // constant, suggest a cast to the appropriate type. |
| 4666 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4667 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4668 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4669 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4670 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4671 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4672 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4673 | return NPV_NullPointer; |
| 4674 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4676 | // FIXME: If we ever want to support general, address-constant expressions |
| 4677 | // as non-type template arguments, we should return the ExprResult here to |
| 4678 | // be interpreted by the caller. |
| 4679 | return NPV_NotNullPointer; |
| 4680 | } |
| 4681 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4682 | /// \brief Checks whether the given template argument is compatible with its |
| 4683 | /// template parameter. |
| 4684 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4685 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4686 | Expr *Arg, QualType ArgType) { |
| 4687 | bool ObjCLifetimeConversion; |
| 4688 | if (ParamType->isPointerType() && |
| 4689 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4690 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4691 | ObjCLifetimeConversion)) { |
| 4692 | // For pointer-to-object types, qualification conversions are |
| 4693 | // permitted. |
| 4694 | } else { |
| 4695 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4696 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4697 | // C++ [temp.arg.nontype]p5b3: |
| 4698 | // For a non-type template-parameter of type reference to |
| 4699 | // object, no conversions apply. The type referred to by the |
| 4700 | // reference may be more cv-qualified than the (otherwise |
| 4701 | // identical) type of the template- argument. The |
| 4702 | // template-parameter is bound directly to the |
| 4703 | // template-argument, which shall be an lvalue. |
| 4704 | |
| 4705 | // FIXME: Other qualifiers? |
| 4706 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4707 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4708 | |
| 4709 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4710 | S.Diag(Arg->getLocStart(), |
| 4711 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4712 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4713 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4714 | return true; |
| 4715 | } |
| 4716 | } |
| 4717 | } |
| 4718 | |
| 4719 | // At this point, the template argument refers to an object or |
| 4720 | // function with external linkage. We now need to check whether the |
| 4721 | // argument and parameter types are compatible. |
| 4722 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4723 | ParamType.getNonReferenceType())) { |
| 4724 | // We can't perform this conversion or binding. |
| 4725 | if (ParamType->isReferenceType()) |
| 4726 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4727 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4728 | else |
| 4729 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4730 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4731 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4732 | return true; |
| 4733 | } |
| 4734 | } |
| 4735 | |
| 4736 | return false; |
| 4737 | } |
| 4738 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4739 | /// \brief Checks whether the given template argument is the address |
| 4740 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4741 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4742 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4743 | NonTypeTemplateParmDecl *Param, |
| 4744 | QualType ParamType, |
| 4745 | Expr *ArgIn, |
| 4746 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4747 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4748 | Expr *Arg = ArgIn; |
| 4749 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4750 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4751 | bool AddressTaken = false; |
| 4752 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4753 | if (S.getLangOpts().MicrosoftExt) { |
| 4754 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4755 | // dereference and address-of operators. |
| 4756 | Arg = Arg->IgnoreParenCasts(); |
| 4757 | |
| 4758 | bool ExtWarnMSTemplateArg = false; |
| 4759 | UnaryOperatorKind FirstOpKind; |
| 4760 | SourceLocation FirstOpLoc; |
| 4761 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4762 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4763 | if (UnOpKind == UO_Deref) |
| 4764 | ExtWarnMSTemplateArg = true; |
| 4765 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4766 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4767 | if (!AddrOpLoc.isValid()) { |
| 4768 | FirstOpKind = UnOpKind; |
| 4769 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4770 | } |
| 4771 | } else |
| 4772 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4773 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4774 | if (FirstOpLoc.isValid()) { |
| 4775 | if (ExtWarnMSTemplateArg) |
| 4776 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4777 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4778 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4779 | if (FirstOpKind == UO_AddrOf) |
| 4780 | AddressTaken = true; |
| 4781 | else if (Arg->getType()->isPointerType()) { |
| 4782 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4783 | // constant expression. |
| 4784 | assert(FirstOpKind == UO_Deref); |
| 4785 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4786 | << Arg->getSourceRange(); |
| 4787 | } |
| 4788 | } |
| 4789 | } else { |
| 4790 | // See through any implicit casts we added to fix the type. |
| 4791 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4792 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4793 | // C++ [temp.arg.nontype]p1: |
| 4794 | // |
| 4795 | // A template-argument for a non-type, non-template |
| 4796 | // template-parameter shall be one of: [...] |
| 4797 | // |
| 4798 | // -- the address of an object or function with external |
| 4799 | // linkage, including function templates and function |
| 4800 | // template-ids but excluding non-static class members, |
| 4801 | // expressed as & id-expression where the & is optional if |
| 4802 | // the name refers to a function or array, or if the |
| 4803 | // corresponding template-parameter is a reference; or |
| 4804 | |
| 4805 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4806 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4807 | bool ExtraParens = false; |
| 4808 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4809 | if (!Invalid && !ExtraParens) { |
| 4810 | S.Diag(Arg->getLocStart(), |
| 4811 | S.getLangOpts().CPlusPlus11 |
| 4812 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4813 | : diag::ext_template_arg_extra_parens) |
| 4814 | << Arg->getSourceRange(); |
| 4815 | ExtraParens = true; |
| 4816 | } |
| 4817 | |
| 4818 | Arg = Parens->getSubExpr(); |
| 4819 | } |
| 4820 | |
| 4821 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4822 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4823 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4824 | |
| 4825 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4826 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4827 | Arg = UnOp->getSubExpr(); |
| 4828 | AddressTaken = true; |
| 4829 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4830 | } |
| 4831 | } |
| 4832 | |
| 4833 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4834 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4835 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4836 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4837 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4838 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4839 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4840 | |
| 4841 | // If our parameter has pointer type, check for a null template value. |
| 4842 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4843 | NullPointerValueKind NPV; |
| 4844 | // dllimport'd entities aren't constant but are available inside of template |
| 4845 | // arguments. |
| 4846 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4847 | NPV = NPV_NotNullPointer; |
| 4848 | else |
| 4849 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4850 | switch (NPV) { |
| 4851 | case NPV_NullPointer: |
| 4852 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4853 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4854 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4855 | return false; |
| 4856 | |
| 4857 | case NPV_Error: |
| 4858 | return true; |
| 4859 | |
| 4860 | case NPV_NotNullPointer: |
| 4861 | break; |
| 4862 | } |
| 4863 | } |
| 4864 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4865 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4866 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4867 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4868 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4869 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4870 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4871 | |
| 4872 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4873 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4874 | ArgIn, Arg, ArgType)) |
| 4875 | return true; |
| 4876 | |
| 4877 | Converted = TemplateArgument(ArgIn); |
| 4878 | return false; |
| 4879 | } |
| 4880 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4881 | if (!DRE) { |
| 4882 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4883 | << Arg->getSourceRange(); |
| 4884 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4885 | return true; |
| 4886 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4887 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4888 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4889 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4890 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4891 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4892 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4893 | return true; |
| 4894 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4895 | |
| 4896 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4897 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4898 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4899 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4900 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4901 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4902 | return true; |
| 4903 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4904 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4905 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4906 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4907 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4908 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4909 | // A non-type template argument must refer to an object or function. |
| 4910 | if (!Func && !Var) { |
| 4911 | // We found something, but we don't know specifically what it is. |
| 4912 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4913 | << Arg->getSourceRange(); |
| 4914 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4915 | return true; |
| 4916 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4917 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4918 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4919 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4920 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4921 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4922 | diag::ext_template_arg_object_internal) |
| 4923 | << !Func << Entity << Arg->getSourceRange(); |
| 4924 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4925 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4926 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4927 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4928 | << !Func << Entity << Arg->getSourceRange(); |
| 4929 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4930 | << !Func; |
| 4931 | return true; |
| 4932 | } |
| 4933 | |
| 4934 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4935 | // If the template parameter has pointer type, the function decays. |
| 4936 | if (ParamType->isPointerType() && !AddressTaken) |
| 4937 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4938 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4939 | // If we originally had an address-of operator, but the |
| 4940 | // parameter has reference type, complain and (if things look |
| 4941 | // like they will work) drop the address-of operator. |
| 4942 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4943 | ParamType.getNonReferenceType())) { |
| 4944 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4945 | << ParamType; |
| 4946 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4947 | return true; |
| 4948 | } |
| 4949 | |
| 4950 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4951 | << ParamType |
| 4952 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4953 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4954 | |
| 4955 | ArgType = Func->getType(); |
| 4956 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4957 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4958 | // A value of reference type is not an object. |
| 4959 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4960 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4961 | diag::err_template_arg_reference_var) |
| 4962 | << Var->getType() << Arg->getSourceRange(); |
| 4963 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4964 | return true; |
| 4965 | } |
| 4966 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4967 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4968 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4969 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4970 | << Arg->getSourceRange(); |
| 4971 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4972 | return true; |
| 4973 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4974 | |
| 4975 | // If the template parameter has pointer type, we must have taken |
| 4976 | // the address of this object. |
| 4977 | if (ParamType->isReferenceType()) { |
| 4978 | if (AddressTaken) { |
| 4979 | // If we originally had an address-of operator, but the |
| 4980 | // parameter has reference type, complain and (if things look |
| 4981 | // like they will work) drop the address-of operator. |
| 4982 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4983 | ParamType.getNonReferenceType())) { |
| 4984 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4985 | << ParamType; |
| 4986 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4987 | return true; |
| 4988 | } |
| 4989 | |
| 4990 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4991 | << ParamType |
| 4992 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4993 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4994 | |
| 4995 | ArgType = Var->getType(); |
| 4996 | } |
| 4997 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4998 | if (Var->getType()->isArrayType()) { |
| 4999 | // Array-to-pointer decay. |
| 5000 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 5001 | } else { |
| 5002 | // If the template parameter has pointer type but the address of |
| 5003 | // this object was not taken, complain and (possibly) recover by |
| 5004 | // taking the address of the entity. |
| 5005 | ArgType = S.Context.getPointerType(Var->getType()); |
| 5006 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 5007 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 5008 | << ParamType; |
| 5009 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5010 | return true; |
| 5011 | } |
| 5012 | |
| 5013 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 5014 | << ParamType |
| 5015 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 5016 | |
| 5017 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5018 | } |
| 5019 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5020 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5021 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 5022 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 5023 | Arg, ArgType)) |
| 5024 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5025 | |
| 5026 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5027 | Converted = |
| 5028 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 5029 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5030 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | /// \brief Checks whether the given template argument is a pointer to |
| 5034 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5035 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 5036 | NonTypeTemplateParmDecl *Param, |
| 5037 | QualType ParamType, |
| 5038 | Expr *&ResultArg, |
| 5039 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5040 | bool Invalid = false; |
| 5041 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5042 | // Check for a null pointer value. |
| 5043 | Expr *Arg = ResultArg; |
| 5044 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 5045 | case NPV_Error: |
| 5046 | return true; |
| 5047 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5048 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5049 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 5050 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5051 | return false; |
| 5052 | case NPV_NotNullPointer: |
| 5053 | break; |
| 5054 | } |
| 5055 | |
| 5056 | bool ObjCLifetimeConversion; |
| 5057 | if (S.IsQualificationConversion(Arg->getType(), |
| 5058 | ParamType.getNonReferenceType(), |
| 5059 | false, ObjCLifetimeConversion)) { |
| 5060 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5061 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5062 | ResultArg = Arg; |
| 5063 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 5064 | ParamType.getNonReferenceType())) { |
| 5065 | // We can't perform this conversion. |
| 5066 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 5067 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 5068 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5069 | return true; |
| 5070 | } |
| 5071 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5072 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5073 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5074 | Arg = Cast->getSubExpr(); |
| 5075 | |
| 5076 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5077 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5078 | // A template-argument for a non-type, non-template |
| 5079 | // template-parameter shall be one of: [...] |
| 5080 | // |
| 5081 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5082 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5083 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 5084 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 5085 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 5086 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5087 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5088 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5089 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5090 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5091 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 5092 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5093 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 5094 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | Arg = Parens->getSubExpr(); |
| 5098 | } |
| 5099 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5100 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5101 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5102 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5103 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5104 | // A pointer-to-member constant written &Class::member. |
| 5105 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5106 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5107 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 5108 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5109 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5110 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5111 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5112 | // A constant of pointer-to-member type. |
| 5113 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 5114 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 5115 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 5116 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5117 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5118 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5119 | } else { |
| 5120 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5121 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5122 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5123 | return Invalid; |
| 5124 | } |
| 5125 | } |
| 5126 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5127 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5128 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5129 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5130 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5131 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5132 | return S.Diag(Arg->getLocStart(), |
| 5133 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5134 | << Arg->getSourceRange(); |
| 5135 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 5136 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 5137 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 5138 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5139 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 5140 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5141 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 5142 | "Only non-static member pointers can make it here"); |
| 5143 | |
| 5144 | // Okay: this is the address of a non-static member, and therefore |
| 5145 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5146 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5147 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5148 | } else { |
| 5149 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5150 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5151 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5152 | return Invalid; |
| 5153 | } |
| 5154 | |
| 5155 | // 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] | 5156 | S.Diag(Arg->getLocStart(), |
| 5157 | diag::err_template_arg_not_pointer_to_member_form) |
| 5158 | << Arg->getSourceRange(); |
| 5159 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5160 | return true; |
| 5161 | } |
| 5162 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5163 | /// \brief Check a template argument against its corresponding |
| 5164 | /// non-type template parameter. |
| 5165 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 5166 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5167 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5168 | /// returns the converted template argument. \p ParamType is the |
| 5169 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5170 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5171 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5172 | TemplateArgument &Converted, |
| 5173 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5174 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5175 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5176 | // If the parameter type somehow involves auto, deduce the type now. |
| 5177 | if (getLangOpts().CPlusPlus1z && ParamType->isUndeducedType()) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5178 | // When checking a deduced template argument, deduce from its type even if |
| 5179 | // the type is dependent, in order to check the types of non-type template |
| 5180 | // arguments line up properly in partial ordering. |
| 5181 | Optional<unsigned> Depth; |
| 5182 | if (CTAK != CTAK_Specified) |
| 5183 | Depth = Param->getDepth() + 1; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5184 | if (DeduceAutoType( |
| 5185 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5186 | Arg, ParamType, Depth) == DAR_Failed) { |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5187 | Diag(Arg->getExprLoc(), |
| 5188 | diag::err_non_type_template_parm_type_deduction_failure) |
| 5189 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 5190 | << Arg->getSourceRange(); |
| 5191 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5192 | return ExprError(); |
| 5193 | } |
| 5194 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 5195 | // an error. The error message normally references the parameter |
| 5196 | // declaration, but here we'll pass the argument location because that's |
| 5197 | // where the parameter type is deduced. |
| 5198 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 5199 | if (ParamType.isNull()) { |
| 5200 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5201 | return ExprError(); |
| 5202 | } |
| 5203 | } |
| 5204 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5205 | // We should have already dropped all cv-qualifiers by now. |
| 5206 | assert(!ParamType.hasQualifiers() && |
| 5207 | "non-type template parameter type cannot be qualified"); |
| 5208 | |
| 5209 | if (CTAK == CTAK_Deduced && |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 5210 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5211 | Arg->getType())) { |
Richard Smith | 957fbf1 | 2017-01-17 02:14:37 +0000 | [diff] [blame] | 5212 | // FIXME: If either type is dependent, we skip the check. This isn't |
| 5213 | // correct, since during deduction we're supposed to have replaced each |
| 5214 | // template parameter with some unique (non-dependent) placeholder. |
| 5215 | // FIXME: If the argument type contains 'auto', we carry on and fail the |
| 5216 | // type check in order to force specific types to be more specialized than |
| 5217 | // 'auto'. It's not clear how partial ordering with 'auto' is supposed to |
| 5218 | // work. |
| 5219 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
| 5220 | !Arg->getType()->getContainedAutoType()) { |
| 5221 | Converted = TemplateArgument(Arg); |
| 5222 | return Arg; |
| 5223 | } |
| 5224 | // FIXME: This attempts to implement C++ [temp.deduct.type]p17. Per DR1770, |
| 5225 | // we should actually be checking the type of the template argument in P, |
| 5226 | // not the type of the template argument deduced from A, against the |
| 5227 | // template parameter type. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5228 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5229 | << Arg->getType() |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5230 | << ParamType.getUnqualifiedType(); |
| 5231 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5232 | return ExprError(); |
| 5233 | } |
| 5234 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5235 | // If either the parameter has a dependent type or the argument is |
| 5236 | // type-dependent, there's nothing we can check now. |
| 5237 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 5238 | // FIXME: Produce a cloned, canonical expression? |
| 5239 | Converted = TemplateArgument(Arg); |
| 5240 | return Arg; |
| 5241 | } |
| 5242 | |
Richard Smith | e594587 | 2017-01-06 22:52:53 +0000 | [diff] [blame] | 5243 | // The initialization of the parameter from the argument is |
| 5244 | // a constant-evaluated context. |
| 5245 | EnterExpressionEvaluationContext ConstantEvaluated(*this, |
| 5246 | Sema::ConstantEvaluated); |
| 5247 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5248 | if (getLangOpts().CPlusPlus1z) { |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5249 | // C++1z [temp.arg.nontype]p1: |
| 5250 | // A template-argument for a non-type template parameter shall be |
| 5251 | // a converted constant expression of the type of the template-parameter. |
| 5252 | APValue Value; |
| 5253 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 5254 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 5255 | if (ArgResult.isInvalid()) |
| 5256 | return ExprError(); |
| 5257 | |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5258 | // For a value-dependent argument, CheckConvertedConstantExpression is |
| 5259 | // permitted (and expected) to be unable to determine a value. |
| 5260 | if (ArgResult.get()->isValueDependent()) { |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 5261 | Converted = TemplateArgument(ArgResult.get()); |
| 5262 | return ArgResult; |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5265 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 5266 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5267 | // Convert the APValue to a TemplateArgument. |
| 5268 | switch (Value.getKind()) { |
| 5269 | case APValue::Uninitialized: |
| 5270 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5271 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5272 | break; |
| 5273 | case APValue::Int: |
| 5274 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5275 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5276 | break; |
| 5277 | case APValue::MemberPointer: { |
| 5278 | assert(ParamType->isMemberPointerType()); |
| 5279 | |
| 5280 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 5281 | if (!Value.getMemberPointerPath().empty()) { |
| 5282 | Diag(Arg->getLocStart(), |
| 5283 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 5284 | << Value.getMemberPointerDecl() << ParamType |
| 5285 | << Arg->getSourceRange(); |
| 5286 | return ExprError(); |
| 5287 | } |
| 5288 | |
| 5289 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5290 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5291 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5292 | break; |
| 5293 | } |
| 5294 | case APValue::LValue: { |
| 5295 | // For a non-type template-parameter of pointer or reference type, |
| 5296 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5297 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 5298 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5299 | // -- a temporary object |
| 5300 | // -- a string literal |
| 5301 | // -- the result of a typeid expression, or |
| 5302 | // -- a predefind __func__ variable |
| 5303 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 5304 | if (isa<CXXUuidofExpr>(E)) { |
| 5305 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 5306 | break; |
| 5307 | } |
| 5308 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5309 | << Arg->getSourceRange(); |
| 5310 | return ExprError(); |
| 5311 | } |
| 5312 | auto *VD = const_cast<ValueDecl *>( |
| 5313 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 5314 | // -- a subobject |
| 5315 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 5316 | VD && VD->getType()->isArrayType() && |
| 5317 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 5318 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 5319 | // Per defect report (no number yet): |
| 5320 | // ... other than a pointer to the first element of a complete array |
| 5321 | // object. |
| 5322 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 5323 | Value.isLValueOnePastTheEnd()) { |
| 5324 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 5325 | << Value.getAsString(Context, ParamType); |
| 5326 | return ExprError(); |
| 5327 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5328 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5329 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5330 | assert((!VD || !ParamType->isNullPtrType()) && |
| 5331 | "non-null value of type nullptr_t?"); |
| 5332 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5333 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5334 | break; |
| 5335 | } |
| 5336 | case APValue::AddrLabelDiff: |
| 5337 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 5338 | case APValue::Float: |
| 5339 | case APValue::ComplexInt: |
| 5340 | case APValue::ComplexFloat: |
| 5341 | case APValue::Vector: |
| 5342 | case APValue::Array: |
| 5343 | case APValue::Struct: |
| 5344 | case APValue::Union: |
| 5345 | llvm_unreachable("invalid kind for template argument"); |
| 5346 | } |
| 5347 | |
| 5348 | return ArgResult.get(); |
| 5349 | } |
| 5350 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5351 | // C++ [temp.arg.nontype]p5: |
| 5352 | // The following conversions are performed on each expression used |
| 5353 | // as a non-type template-argument. If a non-type |
| 5354 | // template-argument cannot be converted to the type of the |
| 5355 | // corresponding template-parameter then the program is |
| 5356 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5357 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5358 | // C++11: |
| 5359 | // -- for a non-type template-parameter of integral or |
| 5360 | // enumeration type, conversions permitted in a converted |
| 5361 | // constant expression are applied. |
| 5362 | // |
| 5363 | // C++98: |
| 5364 | // -- for a non-type template-parameter of integral or |
| 5365 | // enumeration type, integral promotions (4.5) and integral |
| 5366 | // conversions (4.7) are applied. |
| 5367 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5368 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5369 | // C++ [temp.arg.nontype]p1: |
| 5370 | // A template-argument for a non-type, non-template template-parameter |
| 5371 | // shall be one of: |
| 5372 | // |
| 5373 | // -- for a non-type template-parameter of integral or enumeration |
| 5374 | // type, a converted constant expression of the type of the |
| 5375 | // template-parameter; or |
| 5376 | llvm::APSInt Value; |
| 5377 | ExprResult ArgResult = |
| 5378 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5379 | CCEK_TemplateArg); |
| 5380 | if (ArgResult.isInvalid()) |
| 5381 | return ExprError(); |
| 5382 | |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 5383 | // We can't check arbitrary value-dependent arguments. |
| 5384 | if (ArgResult.get()->isValueDependent()) { |
| 5385 | Converted = TemplateArgument(ArgResult.get()); |
| 5386 | return ArgResult; |
| 5387 | } |
| 5388 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5389 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5390 | // always a no-op, except when the parameter type is bool. In |
| 5391 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5392 | QualType IntegerType = ParamType; |
| 5393 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5394 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5395 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5396 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5397 | Converted = TemplateArgument(Context, Value, |
| 5398 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5399 | return ArgResult; |
| 5400 | } |
| 5401 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5402 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5403 | if (ArgResult.isInvalid()) |
| 5404 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5405 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5406 | |
| 5407 | QualType ArgType = Arg->getType(); |
| 5408 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5409 | // C++ [temp.arg.nontype]p1: |
| 5410 | // A template-argument for a non-type, non-template |
| 5411 | // template-parameter shall be one of: |
| 5412 | // |
| 5413 | // -- an integral constant-expression of integral or enumeration |
| 5414 | // type; or |
| 5415 | // -- the name of a non-type template-parameter; or |
| 5416 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5417 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5418 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5419 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5420 | diag::err_template_arg_not_integral_or_enumeral) |
| 5421 | << ArgType << Arg->getSourceRange(); |
| 5422 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5423 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5424 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5425 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5426 | QualType T; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5427 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5428 | public: |
| 5429 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5430 | |
| 5431 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5432 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5433 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5434 | } |
| 5435 | } Diagnoser(ArgType); |
| 5436 | |
| 5437 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5438 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5439 | if (!Arg) |
| 5440 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5443 | // From here on out, all we care about is the unqualified form |
| 5444 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5445 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5446 | |
| 5447 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5448 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5449 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5450 | } else if (ParamType->isBooleanType()) { |
| 5451 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5452 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5453 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5454 | !ParamType->isEnumeralType()) { |
| 5455 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5456 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5457 | } else { |
| 5458 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5459 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5460 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5461 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5462 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5463 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5466 | // Add the value of this argument to the list of converted |
| 5467 | // arguments. We use the bitwidth and signedness of the template |
| 5468 | // parameter. |
| 5469 | if (Arg->isValueDependent()) { |
| 5470 | // The argument is value-dependent. Create a new |
| 5471 | // TemplateArgument with the converted expression. |
| 5472 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5473 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5476 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5477 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5478 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5479 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5480 | if (ParamType->isBooleanType()) { |
| 5481 | // Value must be zero or one. |
| 5482 | Value = Value != 0; |
| 5483 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5484 | if (Value.getBitWidth() != AllowedBits) |
| 5485 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5486 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5487 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5488 | llvm::APSInt OldValue = Value; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5489 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5490 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5491 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5492 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5493 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5494 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5495 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5496 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5497 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5498 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5499 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5500 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5501 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5502 | << Arg->getSourceRange(); |
| 5503 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5504 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5505 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5506 | // Complain if we overflowed the template parameter's type. |
| 5507 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5508 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5509 | RequiredBits = OldValue.getActiveBits(); |
| 5510 | else if (OldValue.isUnsigned()) |
| 5511 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5512 | else |
| 5513 | RequiredBits = OldValue.getMinSignedBits(); |
| 5514 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5515 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5516 | diag::warn_template_arg_too_large) |
| 5517 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5518 | << Arg->getSourceRange(); |
| 5519 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5520 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5521 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5522 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5523 | Converted = TemplateArgument(Context, Value, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5524 | ParamType->isEnumeralType() |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5525 | ? Context.getCanonicalType(ParamType) |
| 5526 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5527 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5528 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5529 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5530 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5531 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5532 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5533 | // Handle pointer-to-function, reference-to-function, and |
| 5534 | // pointer-to-member-function all in (roughly) the same way. |
| 5535 | if (// -- For a non-type template-parameter of type pointer to |
| 5536 | // function, only the function-to-pointer conversion (4.3) is |
| 5537 | // applied. If the template-argument represents a set of |
| 5538 | // overloaded functions (or a pointer to such), the matching |
| 5539 | // function is selected from the set (13.4). |
| 5540 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5541 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5542 | // -- For a non-type template-parameter of type reference to |
| 5543 | // function, no conversions apply. If the template-argument |
| 5544 | // represents a set of overloaded functions, the matching |
| 5545 | // function is selected from the set (13.4). |
| 5546 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5547 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5548 | // -- For a non-type template-parameter of type pointer to |
| 5549 | // member function, no conversions apply. If the |
| 5550 | // template-argument represents a set of overloaded member |
| 5551 | // functions, the matching member function is selected from |
| 5552 | // the set (13.4). |
| 5553 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5554 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5555 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5556 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5557 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5558 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5559 | true, |
| 5560 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5561 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5562 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5563 | |
| 5564 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5565 | ArgType = Arg->getType(); |
| 5566 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5567 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5568 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5569 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5570 | if (!ParamType->isMemberPointerType()) { |
| 5571 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5572 | ParamType, |
| 5573 | Arg, Converted)) |
| 5574 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5575 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5576 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5577 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5578 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5579 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5580 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5581 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5584 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5585 | // -- for a non-type template-parameter of type pointer to |
| 5586 | // object, qualification conversions (4.4) and the |
| 5587 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5588 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5589 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5590 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5591 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5592 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5593 | ParamType, |
| 5594 | Arg, Converted)) |
| 5595 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5596 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5597 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5598 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5599 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5600 | // -- For a non-type template-parameter of type reference to |
| 5601 | // object, no conversions apply. The type referred to by the |
| 5602 | // reference may be more cv-qualified than the (otherwise |
| 5603 | // identical) type of the template-argument. The |
| 5604 | // template-parameter is bound directly to the |
| 5605 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5606 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5607 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5608 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5609 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5610 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5611 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5612 | true, |
| 5613 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5614 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5615 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5616 | |
| 5617 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5618 | ArgType = Arg->getType(); |
| 5619 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5620 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5621 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5622 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5623 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5624 | ParamType, |
| 5625 | Arg, Converted)) |
| 5626 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5627 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5628 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5629 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5630 | // Deal with parameters of type std::nullptr_t. |
| 5631 | if (ParamType->isNullPtrType()) { |
| 5632 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5633 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5634 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5635 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5636 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5637 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5638 | case NPV_NotNullPointer: |
| 5639 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5640 | << Arg->getType() << ParamType; |
| 5641 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5642 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5643 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5644 | case NPV_Error: |
| 5645 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5646 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5647 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5648 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5649 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5650 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5651 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5652 | } |
| 5653 | } |
| 5654 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5655 | // -- For a non-type template-parameter of type pointer to data |
| 5656 | // member, qualification conversions (4.4) are applied. |
| 5657 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5658 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5659 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5660 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5661 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5662 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5665 | static void DiagnoseTemplateParameterListArityMismatch( |
| 5666 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 5667 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 5668 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5669 | /// \brief Check a template argument against its corresponding |
| 5670 | /// template template parameter. |
| 5671 | /// |
| 5672 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5673 | /// It returns true if an error occurred, and false otherwise. |
| 5674 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5675 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5676 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5677 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5678 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5679 | if (!Template) { |
| 5680 | // Any dependent template name is fine. |
| 5681 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5682 | return false; |
| 5683 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5684 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5685 | if (Template->isInvalidDecl()) |
| 5686 | return true; |
| 5687 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5688 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5689 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5690 | // the name of a class template or an alias template, expressed as an |
| 5691 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5692 | // primary class templates are considered when matching the |
| 5693 | // template template argument with the corresponding parameter; |
| 5694 | // partial specializations are not considered even if their |
| 5695 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5696 | // |
| 5697 | // Note that we also allow template template parameters here, which |
| 5698 | // will happen when we are dealing with, e.g., class template |
| 5699 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5700 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5701 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5702 | !isa<TypeAliasTemplateDecl>(Template) && |
| 5703 | !isa<BuiltinTemplateDecl>(Template)) { |
| 5704 | assert(isa<FunctionTemplateDecl>(Template) && |
| 5705 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 5706 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5707 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 5708 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5711 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5712 | if (Param->isExpandedParameterPack()) |
| 5713 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5714 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5715 | // C++1z [temp.arg.template]p3: (DR 150) |
| 5716 | // A template-argument matches a template template-parameter P when P |
| 5717 | // is at least as specialized as the template-argument A. |
| 5718 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 5719 | // Quick check for the common case: |
| 5720 | // If P contains a parameter pack, then A [...] matches P if each of A's |
| 5721 | // template parameters matches the corresponding template parameter in |
| 5722 | // the template-parameter-list of P. |
| 5723 | if (TemplateParameterListsAreEqual( |
| 5724 | Template->getTemplateParameters(), Params, false, |
| 5725 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 5726 | return false; |
| 5727 | |
| 5728 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 5729 | Arg.getLocation())) |
| 5730 | return false; |
| 5731 | // FIXME: Produce better diagnostics for deduction failures. |
| 5732 | } |
| 5733 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5734 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5735 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5736 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5737 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5738 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5741 | /// \brief Given a non-type template argument that refers to a |
| 5742 | /// declaration and the type of its corresponding non-type template |
| 5743 | /// parameter, produce an expression that properly refers to that |
| 5744 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5745 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5746 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5747 | QualType ParamType, |
| 5748 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5749 | // C++ [temp.param]p8: |
| 5750 | // |
| 5751 | // A non-type template-parameter of type "array of T" or |
| 5752 | // "function returning T" is adjusted to be of type "pointer to |
| 5753 | // T" or "pointer to function returning T", respectively. |
| 5754 | if (ParamType->isArrayType()) |
| 5755 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5756 | else if (ParamType->isFunctionType()) |
| 5757 | ParamType = Context.getPointerType(ParamType); |
| 5758 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5759 | // For a NULL non-type template argument, return nullptr casted to the |
| 5760 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5761 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5762 | return ImpCastExprToType( |
| 5763 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5764 | ParamType, |
| 5765 | ParamType->getAs<MemberPointerType>() |
| 5766 | ? CK_NullToMemberPointer |
| 5767 | : CK_NullToPointer); |
| 5768 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5769 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5770 | "Only declaration template arguments permitted here"); |
| 5771 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5772 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5773 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5774 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5775 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5776 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5777 | // If the value is a class member, we might have a pointer-to-member. |
| 5778 | // Determine whether the non-type template template parameter is of |
| 5779 | // pointer-to-member type. If so, we need to build an appropriate |
| 5780 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5781 | // would refer to the member itself. |
| 5782 | if (ParamType->isMemberPointerType()) { |
| 5783 | QualType ClassType |
| 5784 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5785 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5786 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5787 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5788 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5789 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5790 | |
| 5791 | // The actual value-ness of this is unimportant, but for |
| 5792 | // internal consistency's sake, references to instance methods |
| 5793 | // are r-values. |
| 5794 | ExprValueKind VK = VK_LValue; |
| 5795 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5796 | VK = VK_RValue; |
| 5797 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5798 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5799 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5800 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5801 | Loc, |
| 5802 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5803 | if (RefExpr.isInvalid()) |
| 5804 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5805 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5806 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5807 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5808 | // We might need to perform a trailing qualification conversion, since |
| 5809 | // the element type on the parameter could be more qualified than the |
| 5810 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5811 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5812 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5813 | ParamType.getUnqualifiedType(), false, |
| 5814 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5815 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5816 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5817 | assert(!RefExpr.isInvalid() && |
| 5818 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5819 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5820 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5821 | } |
| 5822 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5823 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5824 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5825 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5826 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5827 | // When the non-type template parameter is a pointer, take the |
| 5828 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5829 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5830 | if (RefExpr.isInvalid()) |
| 5831 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5832 | |
Richard Smith | fc6fca1 | 2017-01-28 00:38:35 +0000 | [diff] [blame] | 5833 | if (!Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
| 5834 | (T->isFunctionType() || T->isArrayType())) { |
| 5835 | // Decay functions and arrays unless we're forming a pointer to array. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5836 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5837 | if (RefExpr.isInvalid()) |
| 5838 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5839 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5840 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5841 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5842 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5843 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5844 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5847 | ExprValueKind VK = VK_RValue; |
| 5848 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5849 | // If the non-type template parameter has reference type, qualify the |
| 5850 | // resulting declaration reference with the extra qualifiers on the |
| 5851 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5852 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5853 | VK = VK_LValue; |
| 5854 | T = Context.getQualifiedType(T, |
| 5855 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5856 | } else if (isa<FunctionDecl>(VD)) { |
| 5857 | // References to functions are always lvalues. |
| 5858 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5859 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5860 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5861 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5862 | } |
| 5863 | |
| 5864 | /// \brief Construct a new expression that refers to the given |
| 5865 | /// integral template argument with the given source-location |
| 5866 | /// information. |
| 5867 | /// |
| 5868 | /// This routine takes care of the mapping from an integral template |
| 5869 | /// argument (which may have any integral type) to the appropriate |
| 5870 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5871 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5872 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5873 | SourceLocation Loc) { |
| 5874 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5875 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5876 | QualType OrigT = Arg.getIntegralType(); |
| 5877 | |
| 5878 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5879 | // type the same size as the enumerator. We don't want to build an |
| 5880 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5881 | // any integral type with C++11 enum classes, make sure we create the right |
| 5882 | // type of literal for it. |
| 5883 | QualType T = OrigT; |
| 5884 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5885 | T = ET->getDecl()->getIntegerType(); |
| 5886 | |
| 5887 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5888 | if (T->isAnyCharacterType()) { |
Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 5889 | // This does not need to handle u8 character literals because those are |
| 5890 | // 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] | 5891 | CharacterLiteral::CharacterKind Kind; |
| 5892 | if (T->isWideCharType()) |
| 5893 | Kind = CharacterLiteral::Wide; |
| 5894 | else if (T->isChar16Type()) |
| 5895 | Kind = CharacterLiteral::UTF16; |
| 5896 | else if (T->isChar32Type()) |
| 5897 | Kind = CharacterLiteral::UTF32; |
| 5898 | else |
| 5899 | Kind = CharacterLiteral::Ascii; |
| 5900 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5901 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5902 | Kind, T, Loc); |
| 5903 | } else if (T->isBooleanType()) { |
| 5904 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5905 | T, Loc); |
| 5906 | } else if (T->isNullPtrType()) { |
| 5907 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5908 | } else { |
| 5909 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5912 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5913 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5914 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5915 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5916 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5917 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5918 | Loc, Loc); |
| 5919 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5920 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5921 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5922 | } |
| 5923 | |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 5924 | static bool isDependentOnOuter(NonTypeTemplateParmDecl *NTTP) { |
| 5925 | if (NTTP->getDepth() == 0 || !NTTP->getType()->isDependentType()) |
| 5926 | return false; |
| 5927 | DependencyChecker Checker(NTTP->getDepth(), /*IgnoreNonTypeDependent*/ false, |
| 5928 | /*FindLessThanDepth*/ true); |
| 5929 | Checker.TraverseType(NTTP->getType()); |
| 5930 | return Checker.Match; |
| 5931 | } |
| 5932 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5933 | /// \brief Match two template parameters within template parameter lists. |
| 5934 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5935 | bool Complain, |
| 5936 | Sema::TemplateParameterListEqualKind Kind, |
| 5937 | SourceLocation TemplateArgLoc) { |
| 5938 | // Check the actual kind (type, non-type, template). |
| 5939 | if (Old->getKind() != New->getKind()) { |
| 5940 | if (Complain) { |
| 5941 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5942 | if (TemplateArgLoc.isValid()) { |
| 5943 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5944 | NextDiag = diag::note_template_param_different_kind; |
| 5945 | } |
| 5946 | S.Diag(New->getLocation(), NextDiag) |
| 5947 | << (Kind != Sema::TPL_TemplateMatch); |
| 5948 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5949 | << (Kind != Sema::TPL_TemplateMatch); |
| 5950 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5951 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5952 | return false; |
| 5953 | } |
| 5954 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5955 | // Check that both are parameter packs or neither are parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5956 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5957 | // template template parameter, the template template parameter can have |
| 5958 | // a parameter pack where the template template argument does not. |
| 5959 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5960 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5961 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5962 | if (Complain) { |
| 5963 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5964 | if (TemplateArgLoc.isValid()) { |
| 5965 | S.Diag(TemplateArgLoc, |
| 5966 | diag::err_template_arg_template_params_mismatch); |
| 5967 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5968 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5969 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5970 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5971 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5972 | : 2; |
| 5973 | S.Diag(New->getLocation(), NextDiag) |
| 5974 | << ParamKind << New->isParameterPack(); |
| 5975 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5976 | << ParamKind << Old->isParameterPack(); |
| 5977 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5978 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5979 | return false; |
| 5980 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5981 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5982 | // For non-type template parameters, check the type of the parameter. |
| 5983 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5984 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5985 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5986 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5987 | // If we are matching a template template argument to a template |
| 5988 | // template parameter and one of the non-type template parameter types |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 5989 | // is dependent on an outer template's parameter, then we must wait until |
| 5990 | // template instantiation time to actually compare the arguments. |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5991 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
Richard Smith | 43a833b | 2017-01-09 23:54:33 +0000 | [diff] [blame] | 5992 | (isDependentOnOuter(OldNTTP) || isDependentOnOuter(NewNTTP))) |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5993 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5994 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5995 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5996 | if (Complain) { |
| 5997 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5998 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6000 | diag::err_template_arg_template_params_mismatch); |
| 6001 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 6002 | } |
| 6003 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 6004 | << NewNTTP->getType() |
| 6005 | << (Kind != Sema::TPL_TemplateMatch); |
| 6006 | S.Diag(OldNTTP->getLocation(), |
| 6007 | diag::note_template_nontype_parm_prev_declaration) |
| 6008 | << OldNTTP->getType(); |
| 6009 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6010 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6011 | return false; |
| 6012 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6013 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6014 | return true; |
| 6015 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6016 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6017 | // For template template parameters, check the template parameter types. |
| 6018 | // The template parameter lists of template template |
| 6019 | // parameters must agree. |
| 6020 | if (TemplateTemplateParmDecl *OldTTP |
| 6021 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6022 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6023 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 6024 | OldTTP->getTemplateParameters(), |
| 6025 | Complain, |
| 6026 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6027 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6028 | : Kind), |
| 6029 | TemplateArgLoc); |
| 6030 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6031 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6032 | return true; |
| 6033 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 6034 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6035 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 6036 | /// lists. |
| 6037 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6038 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6039 | TemplateParameterList *New, |
| 6040 | TemplateParameterList *Old, |
| 6041 | Sema::TemplateParameterListEqualKind Kind, |
| 6042 | SourceLocation TemplateArgLoc) { |
| 6043 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 6044 | if (TemplateArgLoc.isValid()) { |
| 6045 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6046 | NextDiag = diag::note_template_param_list_different_arity; |
| 6047 | } |
| 6048 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 6049 | << (New->size() > Old->size()) |
| 6050 | << (Kind != Sema::TPL_TemplateMatch) |
| 6051 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 6052 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 6053 | << (Kind != Sema::TPL_TemplateMatch) |
| 6054 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 6055 | } |
| 6056 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6057 | /// \brief Determine whether the given template parameter lists are |
| 6058 | /// equivalent. |
| 6059 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6060 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6061 | /// source code as part of a new template declaration. |
| 6062 | /// |
| 6063 | /// \param Old The old template parameter list, typically found via |
| 6064 | /// name lookup of the template declared with this template parameter |
| 6065 | /// list. |
| 6066 | /// |
| 6067 | /// \param Complain If true, this routine will produce a diagnostic if |
| 6068 | /// the template parameter lists are not equivalent. |
| 6069 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6070 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6071 | /// |
| 6072 | /// \param TemplateArgLoc If this source location is valid, then we |
| 6073 | /// are actually checking the template parameter list of a template |
| 6074 | /// argument (New) against the template parameter list of its |
| 6075 | /// corresponding template template parameter (Old). We produce |
| 6076 | /// slightly different diagnostics in this scenario. |
| 6077 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6078 | /// \returns True if the template parameter lists are equal, false |
| 6079 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6080 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6081 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 6082 | TemplateParameterList *Old, |
| 6083 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 6084 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 6085 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6086 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 6087 | if (Complain) |
| 6088 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6089 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6090 | |
| 6091 | return false; |
| 6092 | } |
| 6093 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 6094 | // C++0x [temp.arg.template]p3: |
| 6095 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6096 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6097 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6098 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6099 | // template-parameter-list of P. [...] |
| 6100 | TemplateParameterList::iterator NewParm = New->begin(); |
| 6101 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6102 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6103 | OldParmEnd = Old->end(); |
| 6104 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 6105 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 6106 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6107 | if (NewParm == NewParmEnd) { |
| 6108 | if (Complain) |
| 6109 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6110 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6111 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6112 | return false; |
| 6113 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6114 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6115 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 6116 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6117 | return false; |
| 6118 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6119 | ++NewParm; |
| 6120 | continue; |
| 6121 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6122 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6123 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6124 | // [...] When P's template- parameter-list contains a template parameter |
| 6125 | // pack (14.5.3), the template parameter pack will match zero or more |
| 6126 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6127 | // template-parameter-list of A with the same type and form as the |
| 6128 | // template parameter pack in P (ignoring whether those template |
| 6129 | // parameters are template parameter packs). |
| 6130 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 6131 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 6132 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6133 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6134 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6135 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6136 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6137 | // Make sure we exhausted all of the arguments. |
| 6138 | if (NewParm != NewParmEnd) { |
| 6139 | if (Complain) |
| 6140 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6141 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6142 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6143 | return false; |
| 6144 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6145 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6146 | return true; |
| 6147 | } |
| 6148 | |
| 6149 | /// \brief Check whether a template can be declared within this scope. |
| 6150 | /// |
| 6151 | /// If the template declaration is valid in this scope, returns |
| 6152 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6153 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6154 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 6155 | if (!S) |
| 6156 | return false; |
| 6157 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6158 | // Find the nearest enclosing declaration scope. |
| 6159 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6160 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6161 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6162 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 6163 | // C++ [temp]p4: |
| 6164 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 6165 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 6166 | if (Ctx && Ctx->isExternCContext()) { |
| 6167 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 6168 | << TemplateParams->getSourceRange(); |
| 6169 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 6170 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 6171 | return true; |
| 6172 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 6173 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6174 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 6175 | // C++ [temp]p2: |
| 6176 | // A template-declaration can appear only as a namespace scope or |
| 6177 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 6178 | if (Ctx) { |
| 6179 | if (Ctx->isFileContext()) |
| 6180 | return false; |
| 6181 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 6182 | // C++ [temp.mem]p2: |
| 6183 | // A local class shall not have member templates. |
| 6184 | if (RD->isLocalClass()) |
| 6185 | return Diag(TemplateParams->getTemplateLoc(), |
| 6186 | diag::err_template_inside_local_class) |
| 6187 | << TemplateParams->getSourceRange(); |
| 6188 | else |
| 6189 | return false; |
| 6190 | } |
| 6191 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6192 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6193 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6194 | diag::err_template_outside_namespace_or_class_scope) |
| 6195 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6196 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6197 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6198 | /// \brief Determine what kind of template specialization the given declaration |
| 6199 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6200 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6201 | if (!D) |
| 6202 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6203 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6204 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 6205 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6206 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 6207 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6208 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 6209 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6210 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6211 | return TSK_Undeclared; |
| 6212 | } |
| 6213 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6214 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6215 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6216 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6217 | /// This routine determines whether a template specialization can be declared |
| 6218 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6219 | /// |
| 6220 | /// \param S the semantic analysis object for which this check is being |
| 6221 | /// performed. |
| 6222 | /// |
| 6223 | /// \param Specialized the entity being specialized or instantiated, which |
| 6224 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6225 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6226 | /// member class). |
| 6227 | /// |
| 6228 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 6229 | /// |
| 6230 | /// \param Loc the location of the explicit specialization or instantiation of |
| 6231 | /// this entity. |
| 6232 | /// |
| 6233 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 6234 | /// a class template. |
| 6235 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6236 | /// \returns true if there was an error that we cannot recover from, false |
| 6237 | /// otherwise. |
| 6238 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 6239 | NamedDecl *Specialized, |
| 6240 | NamedDecl *PrevDecl, |
| 6241 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6242 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6243 | // Keep these "kind" numbers in sync with the %select statements in the |
| 6244 | // various diagnostics emitted by this routine. |
| 6245 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6246 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6247 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6248 | else if (isa<VarTemplateDecl>(Specialized)) |
| 6249 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6250 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6251 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6252 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6253 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6254 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6255 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6256 | else if (isa<RecordDecl>(Specialized)) |
| 6257 | EntityKind = 7; |
| 6258 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 6259 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6260 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6261 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6262 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6263 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6264 | return true; |
| 6265 | } |
| 6266 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6267 | // C++ [temp.expl.spec]p2: |
| 6268 | // An explicit specialization shall be declared in the namespace |
| 6269 | // of which the template is a member, or, for member templates, in |
| 6270 | // the namespace of which the enclosing class or enclosing class |
| 6271 | // template is a member. An explicit specialization of a member |
| 6272 | // function, member class or static data member of a class |
| 6273 | // template shall be declared in the namespace of which the class |
| 6274 | // template is a member. Such a declaration may also be a |
| 6275 | // definition. If the declaration is not a definition, the |
| 6276 | // specialization may be defined later in the name- space in which |
| 6277 | // the explicit specialization was declared, or in a namespace |
| 6278 | // that encloses the one in which the explicit specialization was |
| 6279 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6280 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6281 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6282 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6283 | return true; |
| 6284 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6285 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6286 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6287 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6288 | // Do not warn for class scope explicit specialization during |
| 6289 | // instantiation, warning was already emitted during pattern |
| 6290 | // semantic analysis. |
| 6291 | if (!S.ActiveTemplateInstantiations.size()) |
| 6292 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 6293 | << Specialized; |
| 6294 | } else { |
| 6295 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6296 | << Specialized; |
| 6297 | return true; |
| 6298 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6299 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6300 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 6301 | if (S.CurContext->isRecord() && |
| 6302 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 6303 | // Make sure that we're specializing in the right record context. |
| 6304 | // Otherwise, things can go horribly wrong. |
| 6305 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6306 | << Specialized; |
| 6307 | return true; |
| 6308 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6309 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6310 | // C++ [temp.class.spec]p6: |
| 6311 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6312 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 6313 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6314 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6315 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6316 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6317 | |
| 6318 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 6319 | // namespace. |
| 6320 | // Note that HandleDeclarator() performs this check for explicit |
| 6321 | // specializations of function templates, static data members, and member |
| 6322 | // functions, so we skip the check here for those kinds of entities. |
| 6323 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 6324 | // Should we refactor that check, so that it occurs later? |
| 6325 | if (!DC->Encloses(SpecializedContext) && |
| 6326 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 6327 | isa<FunctionDecl>(Specialized) || |
| 6328 | isa<VarTemplateDecl>(Specialized) || |
| 6329 | isa<VarDecl>(Specialized))) { |
| 6330 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 6331 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 6332 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 6333 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6334 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 6335 | if (S.getLangOpts().MicrosoftExt) |
| 6336 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 6337 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 6338 | << cast<NamedDecl>(SpecializedContext); |
| 6339 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6340 | llvm_unreachable("unexpected namespace context for specialization"); |
| 6341 | |
| 6342 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 6343 | } else if ((!PrevDecl || |
| 6344 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 6345 | getTemplateSpecializationKind(PrevDecl) == |
| 6346 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6347 | // C++ [temp.exp.spec]p2: |
| 6348 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6349 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6350 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6351 | // An explicit specialization of a member function, member class or |
| 6352 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6353 | // namespace of which the class template is a member. |
| 6354 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6355 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6356 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6357 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6358 | // C++11 [temp.explicit]p3: |
| 6359 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 6360 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6361 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6362 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6363 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6364 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6365 | "DC encloses TU but isn't in enclosing namespace set"); |
| 6366 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 6367 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6368 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6369 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6370 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6371 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6372 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6373 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 6374 | else |
| 6375 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 6376 | S.Diag(Loc, Diag) |
| 6377 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 6378 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6379 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6380 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6381 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6382 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6383 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6384 | return false; |
| 6385 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6386 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6387 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 6388 | if (!E->isTypeDependent()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6389 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6390 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6391 | Checker.TraverseStmt(E); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6392 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6393 | return E->getSourceRange(); |
| 6394 | return Checker.MatchLoc; |
| 6395 | } |
| 6396 | |
| 6397 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6398 | if (!TL.getType()->isDependentType()) |
| 6399 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6400 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6401 | Checker.TraverseTypeLoc(TL); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6402 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6403 | return TL.getSourceRange(); |
| 6404 | return Checker.MatchLoc; |
| 6405 | } |
| 6406 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6407 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6408 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6409 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6410 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6411 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6412 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6413 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6414 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6415 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6416 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6417 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6418 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6419 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6420 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6421 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6422 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6423 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6424 | |
| 6425 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6426 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6427 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6428 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6429 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6430 | |
| 6431 | // Strip off any implicit casts we added as part of type checking. |
| 6432 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6433 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6434 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6435 | // C++ [temp.class.spec]p8: |
| 6436 | // A non-type argument is non-specialized if it is the name of a |
| 6437 | // non-type parameter. All other non-type arguments are |
| 6438 | // specialized. |
| 6439 | // |
| 6440 | // Below, we check the two conditions that only apply to |
| 6441 | // specialized non-type arguments, so skip any non-specialized |
| 6442 | // arguments. |
| 6443 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6444 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6445 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6446 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6447 | // C++ [temp.class.spec]p9: |
| 6448 | // Within the argument list of a class template partial |
| 6449 | // specialization, the following restrictions apply: |
| 6450 | // -- A partially specialized non-type argument expression |
| 6451 | // shall not involve a template parameter of the partial |
| 6452 | // specialization except when the argument expression is a |
| 6453 | // simple identifier. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6454 | // -- The type of a template parameter corresponding to a |
| 6455 | // specialized non-type argument shall not be dependent on a |
| 6456 | // parameter of the specialization. |
| 6457 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
| 6458 | // We implement a compromise between the original rules and DR1315: |
| 6459 | // -- A specialized non-type template argument shall not be |
| 6460 | // type-dependent and the corresponding template parameter |
| 6461 | // shall have a non-dependent type. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6462 | SourceRange ParamUseRange = |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6463 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6464 | if (ParamUseRange.isValid()) { |
| 6465 | if (IsDefaultArgument) { |
| 6466 | S.Diag(TemplateNameLoc, |
| 6467 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6468 | S.Diag(ParamUseRange.getBegin(), |
| 6469 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6470 | << ParamUseRange; |
| 6471 | } else { |
| 6472 | S.Diag(ParamUseRange.getBegin(), |
| 6473 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6474 | << ParamUseRange; |
| 6475 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6476 | return true; |
| 6477 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6478 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6479 | ParamUseRange = findTemplateParameter( |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6480 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6481 | if (ParamUseRange.isValid()) { |
| 6482 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6483 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6484 | << Param->getType(); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6485 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6486 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 6487 | << ParamUseRange; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6488 | return true; |
| 6489 | } |
| 6490 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6491 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6492 | return false; |
| 6493 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6494 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6495 | /// \brief Check the non-type template arguments of a class template |
| 6496 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6497 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6498 | /// \param TemplateNameLoc the location of the template name. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6499 | /// \param PrimaryTemplate the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6500 | /// template. |
| 6501 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6502 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6503 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6504 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6505 | /// \returns \c true if there was an error, \c false otherwise. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6506 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 6507 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 6508 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 6509 | // We have to be conservative when checking a template in a dependent |
| 6510 | // context. |
| 6511 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 6512 | return false; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6513 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6514 | TemplateParameterList *TemplateParams = |
| 6515 | PrimaryTemplate->getTemplateParameters(); |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6516 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6517 | NonTypeTemplateParmDecl *Param |
| 6518 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6519 | if (!Param) |
| 6520 | continue; |
| 6521 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6522 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 6523 | Param, &TemplateArgs[I], |
| 6524 | 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6525 | return true; |
| 6526 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6527 | |
| 6528 | return false; |
| 6529 | } |
| 6530 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6531 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6532 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6533 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6534 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6535 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6536 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6537 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6538 | MultiTemplateParamsArg |
| 6539 | TemplateParameterLists, |
| 6540 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6541 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6542 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6543 | CXXScopeSpec &SS = TemplateId.SS; |
| 6544 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6545 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6546 | // store the location of the outermost template keyword in the declaration. |
| 6547 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6548 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6549 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6550 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6551 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6552 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6553 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6554 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6555 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6556 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6557 | |
| 6558 | if (!ClassTemplate) { |
| 6559 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6560 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6561 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6562 | return true; |
| 6563 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6564 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6565 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6566 | bool isPartialSpecialization = false; |
| 6567 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6568 | // Check the validity of the template headers that introduce this |
| 6569 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6570 | // FIXME: We probably shouldn't complain about these headers for |
| 6571 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6572 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6573 | TemplateParameterList *TemplateParams = |
| 6574 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6575 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6576 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6577 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6578 | if (Invalid) |
| 6579 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6580 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6581 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6582 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6583 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6584 | if (TUK == TUK_Friend) { |
| 6585 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6586 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6587 | return true; |
| 6588 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6589 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6590 | // C++ [temp.class.spec]p10: |
| 6591 | // The template parameter list of a specialization shall not |
| 6592 | // contain default template argument values. |
| 6593 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6594 | Decl *Param = TemplateParams->getParam(I); |
| 6595 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6596 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6597 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6598 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6599 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6600 | } |
| 6601 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6602 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6603 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6604 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6605 | diag::err_default_arg_in_partial_spec) |
| 6606 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6607 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6608 | } |
| 6609 | } else { |
| 6610 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6611 | if (TTP->hasDefaultArgument()) { |
| 6612 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6613 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6614 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6615 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6616 | } |
| 6617 | } |
| 6618 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6619 | } else if (TemplateParams) { |
| 6620 | if (TUK == TUK_Friend) |
| 6621 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6622 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6623 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6624 | TemplateParams->getRAngleLoc())) |
| 6625 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6626 | else |
| 6627 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6628 | } else { |
| 6629 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6630 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6631 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6632 | // Check that the specialization uses the same tag kind as the |
| 6633 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6634 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6635 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6636 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6637 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 6638 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6639 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6640 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6641 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6642 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6643 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6644 | diag::note_previous_use); |
| 6645 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6646 | } |
| 6647 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6648 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6649 | TemplateArgumentListInfo TemplateArgs = |
| 6650 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6651 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6652 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6653 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6654 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6655 | UPPC_PartialSpecialization)) |
| 6656 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6657 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6658 | // Check that the template argument list is well-formed for this |
| 6659 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6660 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6661 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6662 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6663 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6664 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6665 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6666 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6667 | if (isPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6668 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 6669 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6670 | return true; |
| 6671 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6672 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
| 6673 | // also do it during instantiation. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6674 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6675 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6676 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6677 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6678 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6679 | << ClassTemplate->getDeclName(); |
| 6680 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6681 | } |
| 6682 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6683 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6684 | void *InsertPos = nullptr; |
| 6685 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6686 | |
| 6687 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6688 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6689 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6690 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6691 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6692 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6693 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6694 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6695 | // Check whether we can declare a class template specialization in |
| 6696 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6697 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6698 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6699 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6700 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6701 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6702 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6703 | // The canonical type |
| 6704 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6705 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6706 | // Build the canonical type that describes the converted template |
| 6707 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6708 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6709 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6710 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6711 | |
| 6712 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6713 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6714 | // C++ [temp.class.spec]p9b3: |
| 6715 | // |
| 6716 | // -- The argument list of the specialization shall not be identical |
| 6717 | // to the implicit argument list of the primary template. |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6718 | // |
| 6719 | // This rule has since been removed, because it's redundant given DR1495, |
| 6720 | // but we keep it because it produces better diagnostics and recovery. |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6721 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6722 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6723 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6724 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6725 | ClassTemplate->getIdentifier(), |
| 6726 | TemplateNameLoc, |
| 6727 | Attr, |
| 6728 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6729 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6730 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6731 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6732 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6733 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6734 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6735 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6736 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6737 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6738 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6739 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6740 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6741 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6742 | TemplateParams, |
| 6743 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6744 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6745 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6746 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6747 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6748 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6749 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6750 | Partial->setTemplateParameterListsInfo( |
| 6751 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6752 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6753 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6754 | if (!PrevPartial) |
| 6755 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6756 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6757 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6758 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6759 | // template specialization, make a note of that. |
| 6760 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6761 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6762 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6763 | CheckTemplatePartialSpecialization(Partial); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6764 | } else { |
| 6765 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6766 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6767 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6768 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6769 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6770 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6771 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6772 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6773 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6774 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6775 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6776 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6777 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6778 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6779 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6780 | if (!PrevDecl) |
| 6781 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6782 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6783 | if (CurContext->isDependentContext()) { |
| 6784 | // -fms-extensions permits specialization of nested classes without |
| 6785 | // fully specializing the outer class(es). |
| 6786 | assert(getLangOpts().MicrosoftExt && |
| 6787 | "Only possible with -fms-extensions!"); |
| 6788 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6789 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6790 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6791 | } else { |
| 6792 | CanonType = Context.getTypeDeclType(Specialization); |
| 6793 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6794 | } |
| 6795 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6796 | // C++ [temp.expl.spec]p6: |
| 6797 | // 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] | 6798 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6799 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6800 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6801 | // use occurs; no diagnostic is required. |
| 6802 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6803 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6804 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6805 | // Is there any previous explicit specialization declaration? |
| 6806 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6807 | Okay = true; |
| 6808 | break; |
| 6809 | } |
| 6810 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6811 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6812 | if (!Okay) { |
| 6813 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6814 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6815 | << Context.getTypeDeclType(Specialization) << Range; |
| 6816 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6817 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6818 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6819 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6820 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6821 | return true; |
| 6822 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6823 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6824 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6825 | // If this is not a friend, note that this is an explicit specialization. |
| 6826 | if (TUK != TUK_Friend) |
| 6827 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6828 | |
| 6829 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6830 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6831 | RecordDecl *Def = Specialization->getDefinition(); |
| 6832 | NamedDecl *Hidden = nullptr; |
| 6833 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6834 | SkipBody->ShouldSkip = true; |
| 6835 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6836 | // From here on out, treat this as just a redeclaration. |
| 6837 | TUK = TUK_Declaration; |
| 6838 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6839 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 6840 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6841 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6842 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6843 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6844 | } |
| 6845 | } |
| 6846 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6847 | if (Attr) |
| 6848 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6849 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6850 | // Add alignment attributes if necessary; these attributes are checked when |
| 6851 | // the ASTContext lays out the structure. |
| 6852 | if (TUK == TUK_Definition) { |
| 6853 | AddAlignmentAttributesForRecord(Specialization); |
| 6854 | AddMsStructLayoutForRecord(Specialization); |
| 6855 | } |
| 6856 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6857 | if (ModulePrivateLoc.isValid()) |
| 6858 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6859 | << (isPartialSpecialization? 1 : 0) |
| 6860 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6861 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6862 | // Build the fully-sugared type for this class template |
| 6863 | // specialization as the user wrote in the specialization |
| 6864 | // itself. This means that we'll pretty-print the type retrieved |
| 6865 | // from the specialization's declaration the way that the user |
| 6866 | // actually wrote the specialization, rather than formatting the |
| 6867 | // name based on the "canonical" representation used to store the |
| 6868 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6869 | TypeSourceInfo *WrittenTy |
| 6870 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6871 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6872 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6873 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6874 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6875 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6876 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6877 | // C++ [temp.expl.spec]p9: |
| 6878 | // A template explicit specialization is in the scope of the |
| 6879 | // namespace in which the template was defined. |
| 6880 | // |
| 6881 | // We actually implement this paragraph where we set the semantic |
| 6882 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6883 | // but we also maintain the lexical context where the actual |
| 6884 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6885 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6886 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6887 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6888 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6889 | Specialization->startDefinition(); |
| 6890 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6891 | if (TUK == TUK_Friend) { |
| 6892 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6893 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6894 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6895 | /*FIXME:*/KWLoc); |
| 6896 | Friend->setAccess(AS_public); |
| 6897 | CurContext->addDecl(Friend); |
| 6898 | } else { |
| 6899 | // Add the specialization into its lexical context, so that it can |
| 6900 | // be seen when iterating through the list of declarations in that |
| 6901 | // context. However, specializations are not found by name lookup. |
| 6902 | CurContext->addDecl(Specialization); |
| 6903 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6904 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6905 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6906 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6907 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6908 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6909 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6910 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6911 | ActOnDocumentableDecl(NewDecl); |
| 6912 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6913 | } |
| 6914 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6915 | /// \brief Strips various properties off an implicit instantiation |
| 6916 | /// that has just been explicitly specialized. |
| 6917 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6918 | D->dropAttr<DLLImportAttr>(); |
| 6919 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6920 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6921 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6922 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6923 | } |
| 6924 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6925 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6926 | // declaration or definition. |
| 6927 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6928 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6929 | // Explicit instantiations following a specialization have no effect and |
| 6930 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6931 | // until a valid name loc is found. |
| 6932 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6933 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6934 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6935 | PrevDiagLoc = Prev->getLocation(); |
| 6936 | } |
| 6937 | assert(PrevDiagLoc.isValid() && |
| 6938 | "Explicit instantiation without point of instantiation?"); |
| 6939 | return PrevDiagLoc; |
| 6940 | } |
| 6941 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6942 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6943 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6944 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6945 | /// new specialization/instantiation will have any effect. |
| 6946 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6947 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6948 | /// instantiation. |
| 6949 | /// |
| 6950 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6951 | /// |
| 6952 | /// \param PrevDecl the previous declaration of the entity. |
| 6953 | /// |
| 6954 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6955 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6956 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6957 | /// declaration was instantiated (either implicitly or explicitly). |
| 6958 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6959 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6960 | /// specialization or instantiation has no effect and should be ignored. |
| 6961 | /// |
| 6962 | /// \returns true if there was an error that should prevent the introduction of |
| 6963 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6964 | bool |
| 6965 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6966 | TemplateSpecializationKind NewTSK, |
| 6967 | NamedDecl *PrevDecl, |
| 6968 | TemplateSpecializationKind PrevTSK, |
| 6969 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6970 | bool &HasNoEffect) { |
| 6971 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6972 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6973 | switch (NewTSK) { |
| 6974 | case TSK_Undeclared: |
| 6975 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6976 | assert( |
| 6977 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6978 | "previous declaration must be implicit!"); |
| 6979 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6980 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6981 | case TSK_ExplicitSpecialization: |
| 6982 | switch (PrevTSK) { |
| 6983 | case TSK_Undeclared: |
| 6984 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6985 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6986 | // explicitly specialized or has merely been mentioned without any |
| 6987 | // instantiation. |
| 6988 | return false; |
| 6989 | |
| 6990 | case TSK_ImplicitInstantiation: |
| 6991 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6992 | // The declaration itself has not actually been instantiated, so it is |
| 6993 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6994 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6995 | return false; |
| 6996 | } |
| 6997 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6998 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6999 | case TSK_ExplicitInstantiationDeclaration: |
| 7000 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7001 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 7002 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7003 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7004 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7005 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7006 | // 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] | 7007 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7008 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7009 | // implicit instantiation to take place, in every translation unit in |
| 7010 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7011 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 7012 | // Is there any previous explicit specialization declaration? |
| 7013 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 7014 | return false; |
| 7015 | } |
| 7016 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7017 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7018 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7019 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7020 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7021 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7022 | return true; |
| 7023 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7024 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7025 | case TSK_ExplicitInstantiationDeclaration: |
| 7026 | switch (PrevTSK) { |
| 7027 | case TSK_ExplicitInstantiationDeclaration: |
| 7028 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7029 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7030 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7031 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7032 | case TSK_Undeclared: |
| 7033 | case TSK_ImplicitInstantiation: |
| 7034 | // We're explicitly instantiating something that may have already been |
| 7035 | // implicitly instantiated; that's fine. |
| 7036 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7037 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7038 | case TSK_ExplicitSpecialization: |
| 7039 | // C++0x [temp.explicit]p4: |
| 7040 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7041 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7042 | // specialization for that template, the explicit instantiation has no |
| 7043 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7044 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7045 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7046 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7047 | case TSK_ExplicitInstantiationDefinition: |
| 7048 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7049 | // If an entity is the subject of both an explicit instantiation |
| 7050 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7051 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7052 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7053 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7054 | |
| 7055 | // Explicit instantiations following a specialization have no effect and |
| 7056 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 7057 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7058 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 7059 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7060 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7061 | return false; |
| 7062 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7063 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7064 | case TSK_ExplicitInstantiationDefinition: |
| 7065 | switch (PrevTSK) { |
| 7066 | case TSK_Undeclared: |
| 7067 | case TSK_ImplicitInstantiation: |
| 7068 | // We're explicitly instantiating something that may have already been |
| 7069 | // implicitly instantiated; that's fine. |
| 7070 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7071 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7072 | case TSK_ExplicitSpecialization: |
| 7073 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 7074 | // For a given set of template parameters, if an explicit |
| 7075 | // instantiation of a template appears after a declaration of |
| 7076 | // an explicit specialization for that template, the explicit |
| 7077 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 7078 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7079 | << PrevDecl; |
| 7080 | Diag(PrevDecl->getLocation(), |
| 7081 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7082 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7083 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7084 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7085 | case TSK_ExplicitInstantiationDeclaration: |
| 7086 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7087 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7088 | |
| 7089 | // C++0x [temp.explicit]p4: |
| 7090 | // For a given set of template parameters, if an explicit instantiation |
| 7091 | // of a template appears after a declaration of an explicit |
| 7092 | // specialization for that template, the explicit instantiation has no |
| 7093 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 7094 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 7095 | // Is there any previous explicit specialization declaration? |
| 7096 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7097 | HasNoEffect = true; |
| 7098 | break; |
| 7099 | } |
| 7100 | } |
| 7101 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7102 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7103 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7104 | case TSK_ExplicitInstantiationDefinition: |
| 7105 | // C++0x [temp.spec]p5: |
| 7106 | // For a given template and a given set of template-arguments, |
| 7107 | // - an explicit instantiation definition shall appear at most once |
| 7108 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7109 | |
| 7110 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 7111 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 7112 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7113 | : diag::err_explicit_instantiation_duplicate) |
| 7114 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7115 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7116 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7117 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7118 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7119 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7120 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7121 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7122 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7123 | } |
| 7124 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7125 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7126 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7127 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7128 | /// The only possible way to get a dependent function template specialization |
| 7129 | /// is with a friend declaration, like so: |
| 7130 | /// |
| 7131 | /// \code |
| 7132 | /// template \<class T> void foo(T); |
| 7133 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7134 | /// friend void foo<>(T); |
| 7135 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7136 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7137 | /// |
| 7138 | /// There really isn't any useful analysis we can do here, so we |
| 7139 | /// just store the information. |
| 7140 | bool |
| 7141 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 7142 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 7143 | LookupResult &Previous) { |
| 7144 | // Remove anything from Previous that isn't a function template in |
| 7145 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7146 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7147 | LookupResult::Filter F = Previous.makeFilter(); |
| 7148 | while (F.hasNext()) { |
| 7149 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 7150 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7151 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 7152 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7153 | F.erase(); |
| 7154 | } |
| 7155 | F.done(); |
| 7156 | |
| 7157 | // Should this be diagnosed here? |
| 7158 | if (Previous.empty()) return true; |
| 7159 | |
| 7160 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 7161 | ExplicitTemplateArgs); |
| 7162 | return false; |
| 7163 | } |
| 7164 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7165 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7166 | /// specialization. |
| 7167 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7168 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7169 | /// explicit function template specialization. On successful completion, |
| 7170 | /// the function declaration \p FD will become a function template |
| 7171 | /// specialization. |
| 7172 | /// |
| 7173 | /// \param FD the function declaration, which will be updated to become a |
| 7174 | /// function template specialization. |
| 7175 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7176 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 7177 | /// if any. Note that this may be valid info even when 0 arguments are |
| 7178 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 7179 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7180 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7181 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7182 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7183 | bool Sema::CheckFunctionTemplateSpecialization( |
| 7184 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 7185 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7186 | // The set of function template specializations that could match this |
| 7187 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7188 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 7189 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 7190 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7191 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7192 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 7193 | ConvertedTemplateArgs; |
| 7194 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7195 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7196 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7197 | I != E; ++I) { |
| 7198 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 7199 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7200 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7201 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7202 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 7203 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7204 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7205 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7206 | // When matching a constexpr member function template specialization |
| 7207 | // against the primary template, we don't yet know whether the |
| 7208 | // specialization has an implicit 'const' (because we don't know whether |
| 7209 | // it will be a static member function until we know which template it |
| 7210 | // specializes), so adjust it now assuming it specializes this template. |
| 7211 | QualType FT = FD->getType(); |
| 7212 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7213 | CXXMethodDecl *OldMD = |
| 7214 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7215 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7216 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7217 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7218 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7219 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7220 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7221 | } |
| 7222 | } |
| 7223 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7224 | TemplateArgumentListInfo Args; |
| 7225 | if (ExplicitTemplateArgs) |
| 7226 | Args = *ExplicitTemplateArgs; |
| 7227 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7228 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7229 | // A trailing template-argument can be left unspecified in the |
| 7230 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7231 | // provided it can be deduced from the function argument type. |
| 7232 | // Perform template argument deduction to determine whether we may be |
| 7233 | // specializing this template. |
| 7234 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7235 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7236 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 7237 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 7238 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7239 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 7240 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7241 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7242 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7243 | FailedCandidates.addCandidate().set( |
| 7244 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 7245 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7246 | (void)TDK; |
| 7247 | continue; |
| 7248 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7249 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7250 | // Target attributes are part of the cuda function signature, so |
| 7251 | // the deduced template's cuda target must match that of the |
| 7252 | // specialization. Given that C++ template deduction does not |
| 7253 | // take target attributes into account, we reject candidates |
| 7254 | // here that have a different target. |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 7255 | if (LangOpts.CUDA && |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7256 | IdentifyCUDATarget(Specialization, |
| 7257 | /* IgnoreImplicitHDAttributes = */ true) != |
| 7258 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 7259 | FailedCandidates.addCandidate().set( |
| 7260 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 7261 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 7262 | continue; |
| 7263 | } |
| 7264 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7265 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7266 | if (ExplicitTemplateArgs) |
| 7267 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7268 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7269 | } |
| 7270 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7271 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 7272 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7273 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7274 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7275 | FD->getLocation(), |
| 7276 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 7277 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7278 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7279 | PDiag(diag::note_function_template_spec_matched)); |
| 7280 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7281 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7282 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7283 | |
| 7284 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 7285 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7286 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7287 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare [...] |
| 7288 | // an explicit specialization (14.8.3) [...] of a concept definition. |
| 7289 | if (Specialization->getPrimaryTemplate()->isConcept()) { |
| 7290 | Diag(FD->getLocation(), diag::err_concept_specialized) |
| 7291 | << 0 /*function*/ << 1 /*explicitly specialized*/; |
| 7292 | Diag(Specialization->getLocation(), diag::note_previous_declaration); |
| 7293 | return true; |
| 7294 | } |
| 7295 | |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7296 | FunctionTemplateSpecializationInfo *SpecInfo |
| 7297 | = Specialization->getTemplateSpecializationInfo(); |
| 7298 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7299 | |
| 7300 | // Note: do not overwrite location info if previous template |
| 7301 | // specialization kind was explicit. |
| 7302 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7303 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7304 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7305 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 7306 | // function can differ from the template declaration with respect to |
| 7307 | // the constexpr specifier. |
| 7308 | Specialization->setConstexpr(FD->isConstexpr()); |
| 7309 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7310 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7311 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7312 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7313 | |
| 7314 | // If this is a friend declaration, then we're not really declaring |
| 7315 | // an explicit specialization. |
| 7316 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7317 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7318 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7319 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7320 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7321 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7322 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7323 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7324 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7325 | |
| 7326 | // C++ [temp.expl.spec]p6: |
| 7327 | // 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] | 7328 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7329 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7330 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7331 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7332 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7333 | if (!isFriend && |
| 7334 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7335 | TSK_ExplicitSpecialization, |
| 7336 | Specialization, |
| 7337 | SpecInfo->getTemplateSpecializationKind(), |
| 7338 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7339 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7340 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7341 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7342 | // Mark the prior declaration as an explicit specialization, so that later |
| 7343 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7344 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 7345 | // Since explicit specializations do not inherit '=delete' from their |
| 7346 | // primary function template - check if the 'specialization' that was |
| 7347 | // implicitly generated (during template argument deduction for partial |
| 7348 | // ordering) from the most specialized of all the function templates that |
| 7349 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 7350 | // first check that it was implicitly generated during template argument |
| 7351 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 7352 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 7353 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 7354 | !Specialization->getCanonicalDecl()->isReferenced()) { |
| 7355 | assert( |
| 7356 | Specialization->getCanonicalDecl() == Specialization && |
| 7357 | "This must be the only existing declaration of this specialization"); |
| 7358 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7359 | } |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7360 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7361 | MarkUnusedFileScopedDecl(Specialization); |
| 7362 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7363 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7364 | // Turn the given function declaration into a function template |
| 7365 | // specialization, with the template arguments from the previous |
| 7366 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7367 | // Take copies of (semantic and syntactic) template argument lists. |
| 7368 | const TemplateArgumentList* TemplArgs = new (Context) |
| 7369 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7370 | FD->setFunctionTemplateSpecialization( |
| 7371 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 7372 | SpecInfo->getTemplateSpecializationKind(), |
| 7373 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7374 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7375 | // A function template specialization inherits the target attributes |
| 7376 | // of its template. (We require the attributes explicitly in the |
| 7377 | // code to match, but a template may have implicit attributes by |
| 7378 | // virtue e.g. of being constexpr, and it passes these implicit |
| 7379 | // attributes on to its specializations.) |
| 7380 | if (LangOpts.CUDA) |
| 7381 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 7382 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7383 | // The "previous declaration" for this function template specialization is |
| 7384 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7385 | Previous.clear(); |
| 7386 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7387 | return false; |
| 7388 | } |
| 7389 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7390 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7391 | /// specialization. |
| 7392 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7393 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7394 | /// explicit member function specialization. On successful completion, |
| 7395 | /// the function declaration \p FD will become a member function |
| 7396 | /// specialization. |
| 7397 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7398 | /// \param Member the member declaration, which will be updated to become a |
| 7399 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7400 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7401 | /// \param Previous the set of declarations, one of which may be specialized |
| 7402 | /// by this function specialization; the set will be modified to contain the |
| 7403 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7404 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7405 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7406 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7407 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7408 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7409 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7410 | NamedDecl *Instantiation = nullptr; |
| 7411 | NamedDecl *InstantiatedFrom = nullptr; |
| 7412 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7413 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7414 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7415 | // Nowhere to look anyway. |
| 7416 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7417 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7418 | I != E; ++I) { |
| 7419 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7420 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7421 | QualType Adjusted = Function->getType(); |
| 7422 | if (!hasExplicitCallingConv(Adjusted)) |
| 7423 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7424 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7425 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7426 | Instantiation = Method; |
| 7427 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7428 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7429 | break; |
| 7430 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7431 | } |
| 7432 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7433 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7434 | VarDecl *PrevVar; |
| 7435 | if (Previous.isSingleResult() && |
| 7436 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7437 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7438 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7439 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7440 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7441 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7442 | } |
| 7443 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7444 | CXXRecordDecl *PrevRecord; |
| 7445 | if (Previous.isSingleResult() && |
| 7446 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7447 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7448 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7449 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7450 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7451 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7452 | } else if (isa<EnumDecl>(Member)) { |
| 7453 | EnumDecl *PrevEnum; |
| 7454 | if (Previous.isSingleResult() && |
| 7455 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7456 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7457 | Instantiation = PrevEnum; |
| 7458 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7459 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7460 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7461 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7462 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7463 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7464 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7465 | // specializations are always out-of-line, the caller will complain about |
| 7466 | // this mismatch later. |
| 7467 | return false; |
| 7468 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7469 | |
| 7470 | // If this is a friend, just bail out here before we start turning |
| 7471 | // things into explicit specializations. |
| 7472 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7473 | // Preserve instantiation information. |
| 7474 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7475 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7476 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7477 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7478 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7479 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7480 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7481 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7482 | } |
| 7483 | |
| 7484 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7485 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7486 | return false; |
| 7487 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7488 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7489 | // Make sure that this is a specialization of a member. |
| 7490 | if (!InstantiatedFrom) { |
| 7491 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 7492 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7493 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 7494 | return true; |
| 7495 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7496 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7497 | // C++ [temp.expl.spec]p6: |
| 7498 | // 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] | 7499 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7500 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7501 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7502 | // use occurs; no diagnostic is required. |
| 7503 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7504 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7505 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7506 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7507 | TSK_ExplicitSpecialization, |
| 7508 | Instantiation, |
| 7509 | MSInfo->getTemplateSpecializationKind(), |
| 7510 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7511 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7512 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7513 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7514 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7515 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7516 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7517 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7518 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7519 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7520 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7521 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7522 | // the original declaration to note that it is an explicit specialization |
| 7523 | // (if it was previously an implicit instantiation). This latter step |
| 7524 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7525 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7526 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7527 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7528 | TSK_ImplicitInstantiation) { |
| 7529 | InstantiationFunction->setTemplateSpecializationKind( |
| 7530 | TSK_ExplicitSpecialization); |
| 7531 | InstantiationFunction->setLocation(Member->getLocation()); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7532 | // Explicit specializations of member functions of class templates do not |
| 7533 | // inherit '=delete' from the member function they are specializing. |
| 7534 | if (InstantiationFunction->isDeleted()) { |
| 7535 | assert(InstantiationFunction->getCanonicalDecl() == |
| 7536 | InstantiationFunction); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 7537 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7538 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7539 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7540 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7541 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7542 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7543 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7544 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7545 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7546 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7547 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7548 | TSK_ImplicitInstantiation) { |
| 7549 | InstantiationVar->setTemplateSpecializationKind( |
| 7550 | TSK_ExplicitSpecialization); |
| 7551 | InstantiationVar->setLocation(Member->getLocation()); |
| 7552 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7553 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7554 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7555 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7556 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7557 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7558 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7559 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7560 | TSK_ImplicitInstantiation) { |
| 7561 | InstantiationClass->setTemplateSpecializationKind( |
| 7562 | TSK_ExplicitSpecialization); |
| 7563 | InstantiationClass->setLocation(Member->getLocation()); |
| 7564 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7565 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7566 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7567 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7568 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7569 | } else { |
| 7570 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7571 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7572 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7573 | TSK_ImplicitInstantiation) { |
| 7574 | InstantiationEnum->setTemplateSpecializationKind( |
| 7575 | TSK_ExplicitSpecialization); |
| 7576 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7577 | } |
| 7578 | |
| 7579 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7580 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7581 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7582 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7583 | // Save the caller the trouble of having to figure out which declaration |
| 7584 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7585 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7586 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7587 | return false; |
| 7588 | } |
| 7589 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7590 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7591 | /// |
| 7592 | /// \returns true if a serious error occurs, false otherwise. |
| 7593 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7594 | SourceLocation InstLoc, |
| 7595 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7596 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7597 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7598 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7599 | if (CurContext->isRecord()) { |
| 7600 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7601 | << D; |
| 7602 | return true; |
| 7603 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7604 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7605 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7606 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7607 | // template. If the name declared in the explicit instantiation is an |
| 7608 | // unqualified name, the explicit instantiation shall appear in the |
| 7609 | // namespace where its template is declared or, if that namespace is inline |
| 7610 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7611 | // |
| 7612 | // 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] | 7613 | if (WasQualifiedName) { |
| 7614 | if (CurContext->Encloses(OrigContext)) |
| 7615 | return false; |
| 7616 | } else { |
| 7617 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7618 | return false; |
| 7619 | } |
| 7620 | |
| 7621 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7622 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7623 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7624 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7625 | diag::err_explicit_instantiation_out_of_scope : |
| 7626 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7627 | << D << NS; |
| 7628 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7629 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7630 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7631 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7632 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7633 | << D << NS; |
| 7634 | } else |
| 7635 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7636 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7637 | diag::err_explicit_instantiation_must_be_global : |
| 7638 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7639 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7640 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7641 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7642 | } |
| 7643 | |
| 7644 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7645 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7646 | if (!SS.isSet()) |
| 7647 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7648 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7649 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7650 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7651 | // or a static data member of a class template specialization, the name of |
| 7652 | // the class template specialization in the qualified-id for the member |
| 7653 | // name shall be a simple-template-id. |
| 7654 | // |
| 7655 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7656 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7657 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7658 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7659 | if (isa<TemplateSpecializationType>(T)) |
| 7660 | return true; |
| 7661 | |
| 7662 | return false; |
| 7663 | } |
| 7664 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 7665 | /// Make a dllexport or dllimport attr on a class template specialization take |
| 7666 | /// effect. |
| 7667 | static void dllExportImportClassTemplateSpecialization( |
| 7668 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 7669 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 7670 | assert(A && "dllExportImportClassTemplateSpecialization called " |
| 7671 | "on Def without dllexport or dllimport"); |
| 7672 | |
| 7673 | // We reject explicit instantiations in class scope, so there should |
| 7674 | // never be any delayed exported classes to worry about. |
| 7675 | assert(S.DelayedDllExportClasses.empty() && |
| 7676 | "delayed exports present at explicit instantiation"); |
| 7677 | S.checkClassLevelDLLAttribute(Def); |
| 7678 | |
| 7679 | // Propagate attribute to base class templates. |
| 7680 | for (auto &B : Def->bases()) { |
| 7681 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 7682 | B.getType()->getAsCXXRecordDecl())) |
| 7683 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 7684 | } |
| 7685 | |
| 7686 | S.referenceDLLExportedClassMethods(); |
| 7687 | } |
| 7688 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7689 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7690 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7691 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7692 | SourceLocation ExternLoc, |
| 7693 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7694 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7695 | SourceLocation KWLoc, |
| 7696 | const CXXScopeSpec &SS, |
| 7697 | TemplateTy TemplateD, |
| 7698 | SourceLocation TemplateNameLoc, |
| 7699 | SourceLocation LAngleLoc, |
| 7700 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7701 | SourceLocation RAngleLoc, |
| 7702 | AttributeList *Attr) { |
| 7703 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7704 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7705 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7706 | // Check that the specialization uses the same tag kind as the |
| 7707 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7708 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7709 | assert(Kind != TTK_Enum && |
| 7710 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7711 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7712 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 7713 | |
| 7714 | if (!ClassTemplate) { |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 7715 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 7716 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7717 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7718 | return true; |
| 7719 | } |
| 7720 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7721 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7722 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7723 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7724 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7725 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7726 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7727 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7728 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7729 | diag::note_previous_use); |
| 7730 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7731 | } |
| 7732 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7733 | // C++0x [temp.explicit]p2: |
| 7734 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7735 | // definition and an explicit instantiation declaration. An explicit |
| 7736 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7737 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7738 | ? TSK_ExplicitInstantiationDefinition |
| 7739 | : TSK_ExplicitInstantiationDeclaration; |
| 7740 | |
| 7741 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7742 | // Check for dllexport class template instantiation declarations. |
| 7743 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7744 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7745 | Diag(ExternLoc, |
| 7746 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7747 | Diag(A->getLoc(), diag::note_attribute); |
| 7748 | break; |
| 7749 | } |
| 7750 | } |
| 7751 | |
| 7752 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7753 | Diag(ExternLoc, |
| 7754 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7755 | Diag(A->getLocation(), diag::note_attribute); |
| 7756 | } |
| 7757 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7758 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7759 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 7760 | // instantiation declarations for most purposes. |
| 7761 | bool DLLImportExplicitInstantiationDef = false; |
| 7762 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 7763 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7764 | // Check for dllimport class template instantiation definitions. |
| 7765 | bool DLLImport = |
| 7766 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 7767 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7768 | if (A->getKind() == AttributeList::AT_DLLImport) |
| 7769 | DLLImport = true; |
| 7770 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7771 | // dllexport trumps dllimport here. |
| 7772 | DLLImport = false; |
| 7773 | break; |
| 7774 | } |
| 7775 | } |
| 7776 | if (DLLImport) { |
| 7777 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 7778 | DLLImportExplicitInstantiationDef = true; |
| 7779 | } |
| 7780 | } |
| 7781 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7782 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7783 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7784 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7785 | |
| 7786 | // Check that the template argument list is well-formed for this |
| 7787 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7788 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7789 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7790 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7791 | return true; |
| 7792 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7793 | // Find the class template specialization declaration that |
| 7794 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7795 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7796 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7797 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7798 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7799 | TemplateSpecializationKind PrevDecl_TSK |
| 7800 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7801 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7802 | // C++0x [temp.explicit]p2: |
| 7803 | // [...] An explicit instantiation shall appear in an enclosing |
| 7804 | // namespace of its template. [...] |
| 7805 | // |
| 7806 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7807 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7808 | SS.isSet())) |
| 7809 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7810 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7811 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7812 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7813 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7814 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7815 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7816 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7817 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7818 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7819 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7820 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7821 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7822 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7823 | |
| 7824 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7825 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7826 | // Since the only prior class template specialization with these |
| 7827 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7828 | // declaration node as our own, updating the source location |
| 7829 | // for the template name to reflect our new declaration. |
| 7830 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7831 | Specialization = PrevDecl; |
| 7832 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7833 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7834 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7835 | |
| 7836 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7837 | DLLImportExplicitInstantiationDef) { |
| 7838 | // The new specialization might add a dllimport attribute. |
| 7839 | HasNoEffect = false; |
| 7840 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7841 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7842 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7843 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7844 | // Create a new class template specialization declaration node for |
| 7845 | // this explicit specialization. |
| 7846 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7847 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7848 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7849 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7850 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7851 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7852 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7853 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7854 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7855 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7856 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7857 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7858 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7859 | } |
| 7860 | |
| 7861 | // Build the fully-sugared type for this explicit instantiation as |
| 7862 | // the user wrote in the explicit instantiation itself. This means |
| 7863 | // that we'll pretty-print the type retrieved from the |
| 7864 | // specialization's declaration the way that the user actually wrote |
| 7865 | // the explicit instantiation, rather than formatting the name based |
| 7866 | // on the "canonical" representation used to store the template |
| 7867 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7868 | TypeSourceInfo *WrittenTy |
| 7869 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7870 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7871 | Context.getTypeDeclType(Specialization)); |
| 7872 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7873 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7874 | // Set source locations for keywords. |
| 7875 | Specialization->setExternLoc(ExternLoc); |
| 7876 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 7877 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7878 | |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 7879 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7880 | if (Attr) |
| 7881 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7882 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7883 | // Add the explicit instantiation into its lexical context. However, |
| 7884 | // since explicit instantiations are never found by name lookup, we |
| 7885 | // just put it into the declaration context directly. |
| 7886 | Specialization->setLexicalDeclContext(CurContext); |
| 7887 | CurContext->addDecl(Specialization); |
| 7888 | |
| 7889 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7890 | if (HasNoEffect) { |
| 7891 | // Set the template specialization kind. |
| 7892 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7893 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7894 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7895 | |
| 7896 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7897 | // A definition of a class template or class member template |
| 7898 | // shall be in scope at the point of the explicit instantiation of |
| 7899 | // the class template or class member template. |
| 7900 | // |
| 7901 | // This check comes when we actually try to perform the |
| 7902 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7903 | ClassTemplateSpecializationDecl *Def |
| 7904 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7905 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7906 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7907 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7908 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7909 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7910 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7911 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7912 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7913 | // Instantiate the members of this class template specialization. |
| 7914 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7915 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7916 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7917 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7918 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7919 | // TSK_ExplicitInstantiationDefinition |
| 7920 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7921 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 7922 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7923 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7924 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7925 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7926 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
Shoaib Meenai | ab3f96c | 2016-11-09 23:52:20 +0000 | [diff] [blame] | 7927 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 7928 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7929 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7930 | // attribute to a template with a previous instantiation declaration. |
| 7931 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7932 | auto *A = cast<InheritableAttr>( |
| 7933 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 7934 | A->setInherited(true); |
| 7935 | Def->addAttr(A); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 7936 | dllExportImportClassTemplateSpecialization(*this, Def); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7937 | } |
| 7938 | } |
| 7939 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 7940 | // Fix a TSK_ImplicitInstantiation followed by a |
| 7941 | // TSK_ExplicitInstantiationDefinition |
Shoaib Meenai | 5adfb5a | 2017-01-13 01:28:34 +0000 | [diff] [blame] | 7942 | bool NewlyDLLExported = |
| 7943 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
| 7944 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 7945 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 7946 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 7947 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7948 | // attribute to a template with a previous implicit instantiation. |
| 7949 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to |
| 7950 | // avoid potentially strange codegen behavior. For example, if we extend |
| 7951 | // this conditional to dllimport, and we have a source file calling a |
| 7952 | // method on an implicitly instantiated template class instance and then |
| 7953 | // declaring a dllimport explicit instantiation definition for the same |
| 7954 | // template class, the codegen for the method call will not respect the |
| 7955 | // dllimport, while it will with cl. The Def will already have the DLL |
| 7956 | // attribute, since the Def and Specialization will be the same in the |
| 7957 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the |
| 7958 | // attribute to the Specialization; we just need to make it take effect. |
| 7959 | assert(Def == Specialization && |
| 7960 | "Def and Specialization should match for implicit instantiation"); |
| 7961 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 7962 | } |
| 7963 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7964 | // Set the template specialization kind. Make sure it is set before |
| 7965 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 7966 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7967 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7968 | } else { |
| 7969 | |
| 7970 | // Set the template specialization kind. |
| 7971 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7972 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7973 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7974 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7975 | } |
| 7976 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7977 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7978 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7979 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7980 | SourceLocation ExternLoc, |
| 7981 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7982 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7983 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7984 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7985 | IdentifierInfo *Name, |
| 7986 | SourceLocation NameLoc, |
| 7987 | AttributeList *Attr) { |
| 7988 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7989 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7990 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7991 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7992 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7993 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7994 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7995 | SourceLocation(), false, TypeResult(), |
| 7996 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7997 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7998 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7999 | if (!TagD) |
| 8000 | return true; |
| 8001 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8002 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 8003 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8004 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 8005 | if (Tag->isInvalidDecl()) |
| 8006 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8007 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8008 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 8009 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 8010 | if (!Pattern) { |
| 8011 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 8012 | << Context.getTypeDeclType(Record); |
| 8013 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 8014 | return true; |
| 8015 | } |
| 8016 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8017 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8018 | // If the explicit instantiation is for a class or member class, the |
| 8019 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8020 | // simple-template-id. |
| 8021 | // |
| 8022 | // C++98 has the same restriction, just worded differently. |
| 8023 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8024 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8025 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8026 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8027 | // C++0x [temp.explicit]p2: |
| 8028 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8029 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8030 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 8031 | TemplateSpecializationKind TSK |
| 8032 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8033 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8034 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8035 | // C++0x [temp.explicit]p2: |
| 8036 | // [...] An explicit instantiation shall appear in an enclosing |
| 8037 | // namespace of its template. [...] |
| 8038 | // |
| 8039 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8040 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8041 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8042 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8043 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8044 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8045 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8046 | PrevDecl = Record; |
| 8047 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8048 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8049 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8050 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8051 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8052 | PrevDecl, |
| 8053 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8054 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8055 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8056 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8057 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8058 | return TagD; |
| 8059 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8060 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8061 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8062 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 8063 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8064 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8065 | // 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] | 8066 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8067 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8068 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8069 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 8070 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 8071 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 8072 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 8073 | << Pattern; |
| 8074 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8075 | } else { |
| 8076 | if (InstantiateClass(NameLoc, Record, Def, |
| 8077 | getTemplateInstantiationArgs(Record), |
| 8078 | TSK)) |
| 8079 | return true; |
| 8080 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 8081 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8082 | if (!RecordDef) |
| 8083 | return true; |
| 8084 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8085 | } |
| 8086 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8087 | // Instantiate all of the members of the class. |
| 8088 | InstantiateClassMembers(NameLoc, RecordDef, |
| 8089 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8090 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8091 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8092 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 8093 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8094 | // FIXME: We don't have any representation for explicit instantiations of |
| 8095 | // member classes. Such a representation is not needed for compilation, but it |
| 8096 | // should be available for clients that want to see all of the declarations in |
| 8097 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8098 | return TagD; |
| 8099 | } |
| 8100 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8101 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 8102 | SourceLocation ExternLoc, |
| 8103 | SourceLocation TemplateLoc, |
| 8104 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8105 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8106 | // TODO: check if/when DNInfo should replace Name. |
| 8107 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 8108 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8109 | if (!Name) { |
| 8110 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8111 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8112 | diag::err_explicit_instantiation_requires_name) |
| 8113 | << D.getDeclSpec().getSourceRange() |
| 8114 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8115 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8116 | return true; |
| 8117 | } |
| 8118 | |
| 8119 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 8120 | // we find one that is. |
| 8121 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 8122 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 8123 | S = S->getParent(); |
| 8124 | |
| 8125 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8126 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 8127 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8128 | if (R.isNull()) |
| 8129 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8130 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8131 | // C++ [dcl.stc]p1: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8132 | // A storage-class-specifier shall not be specified in [...] an explicit |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8133 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8134 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8135 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 8136 | << Name; |
| 8137 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8138 | } else if (D.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8139 | != DeclSpec::SCS_unspecified) { |
| 8140 | // Complain about then remove the storage class specifier. |
| 8141 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 8142 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8143 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8144 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8145 | } |
| 8146 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 8147 | // C++0x [temp.explicit]p1: |
| 8148 | // [...] An explicit instantiation of a function template shall not use the |
| 8149 | // inline or constexpr specifiers. |
| 8150 | // Presumably, this also applies to member functions of class templates as |
| 8151 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 8152 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8153 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8154 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 8155 | diag::err_explicit_instantiation_inline : |
| 8156 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 8157 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8158 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 8159 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 8160 | // not already specified. |
| 8161 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 8162 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8163 | |
Nathan Wilson | de49845 | 2016-02-08 05:34:00 +0000 | [diff] [blame] | 8164 | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
| 8165 | // applied only to the definition of a function template or variable template, |
| 8166 | // declared in namespace scope. |
| 8167 | if (D.getDeclSpec().isConceptSpecified()) { |
| 8168 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
| 8169 | diag::err_concept_specified_specialization) << 0; |
| 8170 | return true; |
| 8171 | } |
| 8172 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8173 | // C++0x [temp.explicit]p2: |
| 8174 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8175 | // definition and an explicit instantiation declaration. An explicit |
| 8176 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8177 | TemplateSpecializationKind TSK |
| 8178 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8179 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8180 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8181 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8182 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8183 | |
| 8184 | if (!R->isFunctionType()) { |
| 8185 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8186 | // A [...] static data member of a class template can be explicitly |
| 8187 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8188 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8189 | // C++1y [temp.explicit]p1: |
| 8190 | // A [...] variable [...] template specialization can be explicitly |
| 8191 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8192 | if (Previous.isAmbiguous()) |
| 8193 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8194 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 8195 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8196 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8197 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8198 | if (!PrevTemplate) { |
| 8199 | if (!Prev || !Prev->isStaticDataMember()) { |
| 8200 | // We expect to see a data data member here. |
| 8201 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 8202 | << Name; |
| 8203 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8204 | P != PEnd; ++P) |
| 8205 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 8206 | return true; |
| 8207 | } |
| 8208 | |
| 8209 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 8210 | // FIXME: Check for explicit specialization? |
| 8211 | Diag(D.getIdentifierLoc(), |
| 8212 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 8213 | << Prev; |
| 8214 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 8215 | // FIXME: Can we provide a note showing where this was declared? |
| 8216 | return true; |
| 8217 | } |
| 8218 | } else { |
| 8219 | // Explicitly instantiate a variable template. |
| 8220 | |
| 8221 | // C++1y [dcl.spec.auto]p6: |
| 8222 | // ... A program that uses auto or decltype(auto) in a context not |
| 8223 | // explicitly allowed in this section is ill-formed. |
| 8224 | // |
| 8225 | // This includes auto-typed variable template instantiations. |
| 8226 | if (R->isUndeducedType()) { |
| 8227 | Diag(T->getTypeLoc().getLocStart(), |
| 8228 | diag::err_auto_not_allowed_var_inst); |
| 8229 | return true; |
| 8230 | } |
| 8231 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8232 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 8233 | // C++1y [temp.explicit]p3: |
| 8234 | // If the explicit instantiation is for a variable, the unqualified-id |
| 8235 | // in the declaration shall be a template-id. |
| 8236 | Diag(D.getIdentifierLoc(), |
| 8237 | diag::err_explicit_instantiation_without_template_id) |
| 8238 | << PrevTemplate; |
| 8239 | Diag(PrevTemplate->getLocation(), |
| 8240 | diag::note_explicit_instantiation_here); |
| 8241 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8242 | } |
| 8243 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8244 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8245 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8246 | if (PrevTemplate->isConcept()) { |
| 8247 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8248 | << 1 /*variable*/ << 0 /*explicitly instantiated*/; |
| 8249 | Diag(PrevTemplate->getLocation(), diag::note_previous_declaration); |
| 8250 | return true; |
| 8251 | } |
| 8252 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8253 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8254 | TemplateArgumentListInfo TemplateArgs = |
| 8255 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8256 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8257 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 8258 | D.getIdentifierLoc(), TemplateArgs); |
| 8259 | if (Res.isInvalid()) |
| 8260 | return true; |
| 8261 | |
| 8262 | // Ignore access control bits, we don't need them for redeclaration |
| 8263 | // checking. |
| 8264 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8265 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8266 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8267 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8268 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8269 | // or a static data member of a class template specialization, the name of |
| 8270 | // the class template specialization in the qualified-id for the member |
| 8271 | // name shall be a simple-template-id. |
| 8272 | // |
| 8273 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8274 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 8275 | // This does not apply to variable template specializations, where the |
| 8276 | // template-id is in the unqualified-id instead. |
| 8277 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8278 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8279 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8280 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8281 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8282 | // Check the scope of this explicit instantiation. |
| 8283 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8284 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8285 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8286 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 8287 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8288 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8289 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8290 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8291 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8292 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8293 | if (!HasNoEffect) { |
| 8294 | // Instantiate static data member or variable template. |
| 8295 | |
| 8296 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 8297 | if (PrevTemplate) { |
| 8298 | // Merge attributes. |
| 8299 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 8300 | ProcessDeclAttributeList(S, Prev, Attr); |
| 8301 | } |
| 8302 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8303 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 8304 | } |
| 8305 | |
| 8306 | // Check the new variable specialization against the parsed input. |
| 8307 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 8308 | Diag(T->getTypeLoc().getLocStart(), |
| 8309 | diag::err_invalid_var_template_spec_type) |
| 8310 | << 0 << PrevTemplate << R << Prev->getType(); |
| 8311 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 8312 | << 2 << PrevTemplate->getDeclName(); |
| 8313 | return true; |
| 8314 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8315 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8316 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8317 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8318 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8319 | |
| 8320 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 8321 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8322 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8323 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8324 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8325 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8326 | HasExplicitTemplateArgs = true; |
| 8327 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8328 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8329 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8330 | // A [...] function [...] can be explicitly instantiated from its template. |
| 8331 | // A member function [...] of a class template can be explicitly |
| 8332 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8333 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8334 | UnresolvedSet<8> Matches; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8335 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8336 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8337 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8338 | P != PEnd; ++P) { |
| 8339 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8340 | if (!HasExplicitTemplateArgs) { |
| 8341 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 8342 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 8343 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 8344 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8345 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8346 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8347 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8348 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 8349 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8350 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8351 | } |
| 8352 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8353 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8354 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 8355 | if (!FunTmpl) |
| 8356 | continue; |
| 8357 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8358 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8359 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8360 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8361 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8362 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 8363 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8364 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8365 | // Keep track of almost-matches. |
| 8366 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8367 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8368 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8369 | (void)TDK; |
| 8370 | continue; |
| 8371 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8372 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8373 | // Target attributes are part of the cuda function signature, so |
| 8374 | // the cuda target of the instantiated function must match that of its |
| 8375 | // template. Given that C++ template deduction does not take |
| 8376 | // target attributes into account, we reject candidates here that |
| 8377 | // have a different target. |
| 8378 | if (LangOpts.CUDA && |
| 8379 | IdentifyCUDATarget(Specialization, |
| 8380 | /* IgnoreImplicitHDAttributes = */ true) != |
| 8381 | IdentifyCUDATarget(Attr)) { |
| 8382 | FailedCandidates.addCandidate().set( |
| 8383 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 8384 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8385 | continue; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8386 | } |
| 8387 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8388 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8389 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8390 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8391 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8392 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 8393 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8394 | D.getIdentifierLoc(), |
| 8395 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 8396 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 8397 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8398 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8399 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8400 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8401 | |
| 8402 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 8403 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8404 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 8405 | // C++11 [except.spec]p4 |
| 8406 | // In an explicit instantiation an exception-specification may be specified, |
| 8407 | // but is not required. |
| 8408 | // If an exception-specification is specified in an explicit instantiation |
| 8409 | // directive, it shall be compatible with the exception-specifications of |
| 8410 | // other declarations of that function. |
| 8411 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 8412 | if (FPT->hasExceptionSpec()) { |
| 8413 | unsigned DiagID = |
| 8414 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 8415 | if (getLangOpts().MicrosoftExt) |
| 8416 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 8417 | bool Result = CheckEquivalentExceptionSpec( |
| 8418 | PDiag(DiagID) << Specialization->getType(), |
| 8419 | PDiag(diag::note_explicit_instantiation_here), |
| 8420 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 8421 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 8422 | // In Microsoft mode, mismatching exception specifications just cause a |
| 8423 | // warning. |
| 8424 | if (!getLangOpts().MicrosoftExt && Result) |
| 8425 | return true; |
| 8426 | } |
| 8427 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8428 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8429 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8430 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 8431 | << Specialization |
| 8432 | << (Specialization->getTemplateSpecializationKind() == |
| 8433 | TSK_ExplicitSpecialization); |
| 8434 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 8435 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8436 | } |
| 8437 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8438 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8439 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 8440 | PrevDecl = Specialization; |
| 8441 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8442 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8443 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8444 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8445 | PrevDecl, |
| 8446 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8447 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8448 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8449 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8450 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8451 | // FIXME: We may still want to build some representation of this |
| 8452 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8453 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8454 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8455 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 8456 | |
| 8457 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 8458 | if (Attr) |
| 8459 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8460 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8461 | if (Specialization->isDefined()) { |
| 8462 | // Let the ASTConsumer know that this function has been explicitly |
| 8463 | // instantiated now, and its linkage might have changed. |
| 8464 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 8465 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 8466 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8467 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8468 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8469 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8470 | // or a static data member of a class template specialization, the name of |
| 8471 | // the class template specialization in the qualified-id for the member |
| 8472 | // name shall be a simple-template-id. |
| 8473 | // |
| 8474 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8475 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8476 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8477 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8478 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8479 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8480 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8481 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8482 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8483 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8484 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8485 | if (FunTmpl && FunTmpl->isConcept() && |
| 8486 | !D.getDeclSpec().isConceptSpecified()) { |
| 8487 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8488 | << 0 /*function*/ << 0 /*explicitly instantiated*/; |
| 8489 | Diag(FunTmpl->getLocation(), diag::note_previous_declaration); |
| 8490 | return true; |
| 8491 | } |
| 8492 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8493 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8494 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8495 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8496 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8497 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8498 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8499 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8500 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8501 | } |
| 8502 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8503 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8504 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 8505 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 8506 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 8507 | // This has to hold, because SS is expected to be defined. |
| 8508 | assert(Name && "Expected a name in a dependent tag"); |
| 8509 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8510 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8511 | if (!NNS) |
| 8512 | return true; |
| 8513 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8514 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 8515 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8516 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 8517 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8518 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8519 | return true; |
| 8520 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8521 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8522 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8523 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8524 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8525 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8526 | // Create type-source location information for this type. |
| 8527 | TypeLocBuilder TLB; |
| 8528 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8529 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8530 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8531 | TL.setNameLoc(NameLoc); |
| 8532 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8533 | } |
| 8534 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8535 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8536 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 8537 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 8538 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8539 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8540 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8541 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8542 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8543 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8544 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8545 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8546 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8547 | << FixItHint::CreateRemoval(TypenameLoc); |
| 8548 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8549 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8550 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 8551 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 8552 | if (T.isNull()) |
| 8553 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8554 | |
| 8555 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8556 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8557 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8558 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8559 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8560 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8561 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8562 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8563 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8564 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8565 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8566 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8567 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8568 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8569 | } |
| 8570 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8571 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8572 | Sema::ActOnTypenameType(Scope *S, |
| 8573 | SourceLocation TypenameLoc, |
| 8574 | const CXXScopeSpec &SS, |
| 8575 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8576 | TemplateTy TemplateIn, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8577 | IdentifierInfo *TemplateII, |
| 8578 | SourceLocation TemplateIILoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8579 | SourceLocation LAngleLoc, |
| 8580 | ASTTemplateArgsPtr TemplateArgsIn, |
| 8581 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8582 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8583 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8584 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8585 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8586 | diag::ext_typename_outside_of_template) |
| 8587 | << FixItHint::CreateRemoval(TypenameLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8588 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8589 | // Strangely, non-type results are not ignored by this lookup, so the |
| 8590 | // program is ill-formed if it finds an injected-class-name. |
| 8591 | auto *LookupRD = |
| 8592 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)); |
| 8593 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 8594 | Diag(TemplateIILoc, |
| 8595 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 8596 | << TemplateII << 0 /*injected-class-name used as template name*/ |
| 8597 | << (TemplateKWLoc.isValid() ? 1 : 0 /*'template'/'typename' keyword*/); |
| 8598 | } |
| 8599 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8600 | // Translate the parser's template argument list in our AST format. |
| 8601 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8602 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8603 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8604 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8605 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 8606 | // Construct a dependent template specialization type. |
| 8607 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8608 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8609 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 8610 | DTN->getQualifier(), |
| 8611 | DTN->getIdentifier(), |
| 8612 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8613 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8614 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8615 | TypeLocBuilder Builder; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8616 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8617 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8618 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 8619 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 8620 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8621 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8622 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8623 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8624 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8625 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8626 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 8627 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8628 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8629 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8630 | if (T.isNull()) |
| 8631 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8632 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8633 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8634 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8635 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8636 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8637 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8638 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8639 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8640 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8641 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8642 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8643 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8644 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 8645 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8646 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8647 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8648 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8649 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 8650 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 8651 | } |
| 8652 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8653 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8654 | /// Determine whether this failed name lookup should be treated as being |
| 8655 | /// disabled by a usage of std::enable_if. |
| 8656 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8657 | SourceRange &CondRange) { |
| 8658 | // We must be looking for a ::type... |
| 8659 | if (!II.isStr("type")) |
| 8660 | return false; |
| 8661 | |
| 8662 | // ... within an explicitly-written template specialization... |
| 8663 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8664 | return false; |
| 8665 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8666 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8667 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8668 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8669 | return false; |
| 8670 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8671 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8672 | |
| 8673 | // ... which names a complete class template declaration... |
| 8674 | const TemplateDecl *EnableIfDecl = |
| 8675 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8676 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8677 | return false; |
| 8678 | |
| 8679 | // ... called "enable_if". |
| 8680 | const IdentifierInfo *EnableIfII = |
| 8681 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8682 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8683 | return false; |
| 8684 | |
| 8685 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8686 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8687 | return true; |
| 8688 | } |
| 8689 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8690 | /// \brief Build the type that describes a C++ typename specifier, |
| 8691 | /// e.g., "typename T::type". |
| 8692 | QualType |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8693 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8694 | SourceLocation KeywordLoc, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8695 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8696 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8697 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8698 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8699 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8700 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8701 | DeclContext *Ctx = computeDeclContext(SS); |
| 8702 | if (!Ctx) { |
| 8703 | // If the nested-name-specifier is dependent and couldn't be |
| 8704 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8705 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8706 | return Context.getDependentNameType(Keyword, |
| 8707 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8708 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8709 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8710 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8711 | // If the nested-name-specifier refers to the current instantiation, |
| 8712 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8713 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8714 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8715 | // 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] | 8716 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8717 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8718 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8719 | |
| 8720 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8721 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8722 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8723 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8724 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8725 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8726 | case LookupResult::NotFound: { |
| 8727 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8728 | // a more specific diagnostic. |
| 8729 | SourceRange CondRange; |
| 8730 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8731 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8732 | << Ctx << CondRange; |
| 8733 | return QualType(); |
| 8734 | } |
| 8735 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8736 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8737 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8738 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8739 | |
| 8740 | case LookupResult::FoundUnresolvedValue: { |
| 8741 | // We found a using declaration that is a value. Most likely, the using |
| 8742 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8743 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8744 | IILoc); |
| 8745 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8746 | << Name << Ctx << FullRange; |
| 8747 | if (UnresolvedUsingValueDecl *Using |
| 8748 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8749 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8750 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8751 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8752 | } |
| 8753 | } |
| 8754 | // Fall through to create a dependent typename type, from which we can recover |
| 8755 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8756 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8757 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8758 | // Okay, it's a member of an unknown instantiation. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8759 | return Context.getDependentNameType(Keyword, |
| 8760 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8761 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8762 | |
| 8763 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8764 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8765 | // C++ [class.qual]p2: |
| 8766 | // In a lookup in which function names are not ignored and the |
| 8767 | // nested-name-specifier nominates a class C, if the name specified |
| 8768 | // after the nested-name-specifier, when looked up in C, is the |
| 8769 | // injected-class-name of C [...] then the name is instead considered |
| 8770 | // to name the constructor of class C. |
| 8771 | // |
| 8772 | // Unlike in an elaborated-type-specifier, function names are not ignored |
| 8773 | // in typename-specifier lookup. However, they are ignored in all the |
| 8774 | // contexts where we form a typename type with no keyword (that is, in |
| 8775 | // mem-initializer-ids, base-specifiers, and elaborated-type-specifiers). |
| 8776 | // |
| 8777 | // FIXME: That's not strictly true: mem-initializer-id lookup does not |
| 8778 | // ignore functions, but that appears to be an oversight. |
| 8779 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
| 8780 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
| 8781 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
| 8782 | FoundRD->isInjectedClassName() && |
| 8783 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
| 8784 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 8785 | << &II << 1 << 0 /*'typename' keyword used*/; |
| 8786 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8787 | // We found a type. Build an ElaboratedType, since the |
| 8788 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8789 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 8790 | return Context.getElaboratedType(Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8791 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8792 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8793 | } |
| 8794 | |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 8795 | // C++ [dcl.type.simple]p2: |
| 8796 | // A type-specifier of the form |
| 8797 | // typename[opt] nested-name-specifier[opt] template-name |
| 8798 | // is a placeholder for a deduced class type [...]. |
| 8799 | if (getLangOpts().CPlusPlus1z) { |
| 8800 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
| 8801 | return Context.getElaboratedType( |
| 8802 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
| 8803 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
| 8804 | QualType(), false)); |
| 8805 | } |
| 8806 | } |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 8807 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8808 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8809 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8810 | break; |
| 8811 | |
| 8812 | case LookupResult::FoundOverloaded: |
| 8813 | DiagID = diag::err_typename_nested_not_type; |
| 8814 | Referenced = *Result.begin(); |
| 8815 | break; |
| 8816 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8817 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8818 | return QualType(); |
| 8819 | } |
| 8820 | |
| 8821 | // If we get here, it's because name lookup did not find a |
| 8822 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8823 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8824 | IILoc); |
| 8825 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8826 | if (Referenced) |
| 8827 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8828 | << Name; |
| 8829 | return QualType(); |
| 8830 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8831 | |
| 8832 | namespace { |
| 8833 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8834 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8835 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8836 | SourceLocation Loc; |
| 8837 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8838 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8839 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8840 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8841 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8842 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8843 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8844 | DeclarationName Entity) |
| 8845 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8846 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8847 | |
| 8848 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8849 | /// transformed. |
| 8850 | /// |
| 8851 | /// For the purposes of type reconstruction, a type has already been |
| 8852 | /// transformed if it is NULL or if it is not dependent. |
| 8853 | bool AlreadyTransformed(QualType T) { |
| 8854 | return T.isNull() || !T->isDependentType(); |
| 8855 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8856 | |
| 8857 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8858 | /// rebuilt. |
| 8859 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8860 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8861 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8862 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8863 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8864 | /// \brief Sets the "base" location and entity when that |
| 8865 | /// information is known based on another transformation. |
| 8866 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8867 | this->Loc = Loc; |
| 8868 | this->Entity = Entity; |
| 8869 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8870 | |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8871 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8872 | // Lambdas never need to be transformed. |
| 8873 | return E; |
| 8874 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8875 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 8876 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8877 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8878 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8879 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8880 | /// 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] | 8881 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8882 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8883 | /// partial specialization thereof). This routine will rebuild that type now |
| 8884 | /// that we have entered the declarator's scope, which may produce different |
| 8885 | /// canonical types, e.g., |
| 8886 | /// |
| 8887 | /// \code |
| 8888 | /// template<typename T> |
| 8889 | /// struct X { |
| 8890 | /// typedef T* pointer; |
| 8891 | /// pointer data(); |
| 8892 | /// }; |
| 8893 | /// |
| 8894 | /// template<typename T> |
| 8895 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8896 | /// \endcode |
| 8897 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8898 | /// 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] | 8899 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8900 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8901 | /// 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] | 8902 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8903 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8904 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8905 | SourceLocation Loc, |
| 8906 | DeclarationName Name) { |
| 8907 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8908 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8909 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8910 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8911 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8912 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8913 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8914 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8915 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8916 | DeclarationName()); |
| 8917 | return Rebuilder.TransformExpr(E); |
| 8918 | } |
| 8919 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8920 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8921 | if (SS.isInvalid()) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8922 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8923 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8924 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8925 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8926 | DeclarationName()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8927 | NestedNameSpecifierLoc Rebuilt |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8928 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8929 | if (!Rebuilt) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8930 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8931 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8932 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8933 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8934 | } |
| 8935 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8936 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8937 | /// instantiation. |
| 8938 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8939 | TemplateParameterList *Params) { |
| 8940 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8941 | Decl *Param = Params->getParam(I); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8942 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8943 | // There is nothing to rebuild in a type parameter. |
| 8944 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8945 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8946 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8947 | // Rebuild the template parameter list of a template template parameter. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8948 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8949 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8950 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8951 | TTP->getTemplateParameters())) |
| 8952 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8953 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8954 | continue; |
| 8955 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8956 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8957 | // Rebuild the type of a non-type template parameter. |
| 8958 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8959 | TypeSourceInfo *NewTSI |
| 8960 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8961 | NTTP->getLocation(), |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8962 | NTTP->getDeclName()); |
| 8963 | if (!NewTSI) |
| 8964 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8965 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8966 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8967 | NTTP->setTypeSourceInfo(NewTSI); |
| 8968 | NTTP->setType(NewTSI->getType()); |
| 8969 | } |
| 8970 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8971 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8972 | return false; |
| 8973 | } |
| 8974 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8975 | /// \brief Produces a formatted string that describes the binding of |
| 8976 | /// template parameters to template arguments. |
| 8977 | std::string |
| 8978 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8979 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8980 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8981 | } |
| 8982 | |
| 8983 | std::string |
| 8984 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8985 | const TemplateArgument *Args, |
| 8986 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8987 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8988 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8989 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8990 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8991 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8992 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8993 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8994 | if (I >= NumArgs) |
| 8995 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8996 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8997 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8998 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8999 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9000 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9001 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9002 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9003 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9004 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9005 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9006 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9007 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9008 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 9009 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9010 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 9011 | |
| 9012 | Out << ']'; |
| 9013 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 9014 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9015 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9016 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 9017 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9018 | if (!FD) |
| 9019 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9020 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9021 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9022 | |
| 9023 | // Take tokens to avoid allocations |
| 9024 | LPT->Toks.swap(Toks); |
| 9025 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 9026 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 9027 | |
| 9028 | FD->setLateTemplateParsed(true); |
| 9029 | } |
| 9030 | |
| 9031 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 9032 | if (!FD) |
| 9033 | return; |
| 9034 | FD->setLateTemplateParsed(false); |
| 9035 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 9036 | |
| 9037 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 9038 | DeclContext *DC = CurContext; |
| 9039 | |
| 9040 | while (DC) { |
| 9041 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 9042 | const FunctionDecl *FD = RD->isLocalClass(); |
| 9043 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 9044 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 9045 | return false; |
| 9046 | |
| 9047 | DC = DC->getParent(); |
| 9048 | } |
| 9049 | return false; |
| 9050 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9051 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 9052 | namespace { |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9053 | /// \brief Walk the path from which a declaration was instantiated, and check |
| 9054 | /// that every explicit specialization along that path is visible. This enforces |
| 9055 | /// C++ [temp.expl.spec]/6: |
| 9056 | /// |
| 9057 | /// If a template, a member template or a member of a class template is |
| 9058 | /// explicitly specialized then that specialization shall be declared before |
| 9059 | /// the first use of that specialization that would cause an implicit |
| 9060 | /// instantiation to take place, in every translation unit in which such a |
| 9061 | /// use occurs; no diagnostic is required. |
| 9062 | /// |
| 9063 | /// and also C++ [temp.class.spec]/1: |
| 9064 | /// |
| 9065 | /// A partial specialization shall be declared before the first use of a |
| 9066 | /// class template specialization that would make use of the partial |
| 9067 | /// specialization as the result of an implicit or explicit instantiation |
| 9068 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 9069 | /// required. |
| 9070 | class ExplicitSpecializationVisibilityChecker { |
| 9071 | Sema &S; |
| 9072 | SourceLocation Loc; |
| 9073 | llvm::SmallVector<Module *, 8> Modules; |
| 9074 | |
| 9075 | public: |
| 9076 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 9077 | : S(S), Loc(Loc) {} |
| 9078 | |
| 9079 | void check(NamedDecl *ND) { |
| 9080 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 9081 | return checkImpl(FD); |
| 9082 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 9083 | return checkImpl(RD); |
| 9084 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 9085 | return checkImpl(VD); |
| 9086 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 9087 | return checkImpl(ED); |
| 9088 | } |
| 9089 | |
| 9090 | private: |
| 9091 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 9092 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 9093 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 9094 | const bool Recover = true; |
| 9095 | |
| 9096 | // If we got a custom set of modules (because only a subset of the |
| 9097 | // declarations are interesting), use them, otherwise let |
| 9098 | // diagnoseMissingImport intelligently pick some. |
| 9099 | if (Modules.empty()) |
| 9100 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 9101 | else |
| 9102 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 9103 | } |
| 9104 | |
| 9105 | // Check a specific declaration. There are three problematic cases: |
| 9106 | // |
| 9107 | // 1) The declaration is an explicit specialization of a template |
| 9108 | // specialization. |
| 9109 | // 2) The declaration is an explicit specialization of a member of an |
| 9110 | // templated class. |
| 9111 | // 3) The declaration is an instantiation of a template, and that template |
| 9112 | // is an explicit specialization of a member of a templated class. |
| 9113 | // |
| 9114 | // We don't need to go any deeper than that, as the instantiation of the |
| 9115 | // surrounding class / etc is not triggered by whatever triggered this |
| 9116 | // instantiation, and thus should be checked elsewhere. |
| 9117 | template<typename SpecDecl> |
| 9118 | void checkImpl(SpecDecl *Spec) { |
| 9119 | bool IsHiddenExplicitSpecialization = false; |
| 9120 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 9121 | IsHiddenExplicitSpecialization = |
| 9122 | Spec->getMemberSpecializationInfo() |
| 9123 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
| 9124 | : !S.hasVisibleDeclaration(Spec); |
| 9125 | } else { |
| 9126 | checkInstantiated(Spec); |
| 9127 | } |
| 9128 | |
| 9129 | if (IsHiddenExplicitSpecialization) |
| 9130 | diagnose(Spec->getMostRecentDecl(), false); |
| 9131 | } |
| 9132 | |
| 9133 | void checkInstantiated(FunctionDecl *FD) { |
| 9134 | if (auto *TD = FD->getPrimaryTemplate()) |
| 9135 | checkTemplate(TD); |
| 9136 | } |
| 9137 | |
| 9138 | void checkInstantiated(CXXRecordDecl *RD) { |
| 9139 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 9140 | if (!SD) |
| 9141 | return; |
| 9142 | |
| 9143 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 9144 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 9145 | checkTemplate(TD); |
| 9146 | else if (auto *TD = |
| 9147 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 9148 | if (!S.hasVisibleDeclaration(TD)) |
| 9149 | diagnose(TD, true); |
| 9150 | checkTemplate(TD); |
| 9151 | } |
| 9152 | } |
| 9153 | |
| 9154 | void checkInstantiated(VarDecl *RD) { |
| 9155 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 9156 | if (!SD) |
| 9157 | return; |
| 9158 | |
| 9159 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 9160 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 9161 | checkTemplate(TD); |
| 9162 | else if (auto *TD = |
| 9163 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 9164 | if (!S.hasVisibleDeclaration(TD)) |
| 9165 | diagnose(TD, true); |
| 9166 | checkTemplate(TD); |
| 9167 | } |
| 9168 | } |
| 9169 | |
| 9170 | void checkInstantiated(EnumDecl *FD) {} |
| 9171 | |
| 9172 | template<typename TemplDecl> |
| 9173 | void checkTemplate(TemplDecl *TD) { |
| 9174 | if (TD->isMemberSpecialization()) { |
| 9175 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 9176 | diagnose(TD->getMostRecentDecl(), false); |
| 9177 | } |
| 9178 | } |
| 9179 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 9180 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9181 | |
| 9182 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 9183 | if (!getLangOpts().Modules) |
| 9184 | return; |
| 9185 | |
| 9186 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 9187 | } |
| 9188 | |
| 9189 | /// \brief Check whether a template partial specialization that we've discovered |
| 9190 | /// is hidden, and produce suitable diagnostics if so. |
| 9191 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 9192 | NamedDecl *Spec) { |
| 9193 | llvm::SmallVector<Module *, 8> Modules; |
| 9194 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 9195 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 9196 | MissingImportKind::PartialSpecialization, |
| 9197 | /*Recover*/true); |
| 9198 | } |