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 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1228 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1230 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1231 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1232 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1233 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1234 | NewClass->setTemplateParameterListsInfo( |
| 1235 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1236 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1237 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1238 | // Add alignment attributes if necessary; these attributes are checked when |
| 1239 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1240 | if (TUK == TUK_Definition) { |
| 1241 | AddAlignmentAttributesForRecord(NewClass); |
| 1242 | AddMsStructLayoutForRecord(NewClass); |
| 1243 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1244 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1245 | ClassTemplateDecl *NewTemplate |
| 1246 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1247 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1248 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1249 | NewClass->setDescribedClassTemplate(NewTemplate); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1250 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1251 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1252 | NewTemplate->setModulePrivate(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1253 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1254 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1255 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1256 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1257 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1258 | (void)T; |
| 1259 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1260 | // 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] | 1261 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1262 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1263 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1264 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1265 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1266 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1267 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1268 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1270 | // Set the lexical context of these templates |
| 1271 | NewClass->setLexicalDeclContext(CurContext); |
| 1272 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1273 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1274 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1275 | NewClass->startDefinition(); |
| 1276 | |
| 1277 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1278 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1279 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1280 | if (PrevClassTemplate) |
| 1281 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1282 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1283 | AddPushedVisibilityAttribute(NewClass); |
| 1284 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1285 | if (TUK != TUK_Friend) { |
| 1286 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1287 | Scope *Outer = S; |
| 1288 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1289 | Outer = Outer->getParent(); |
| 1290 | PushOnScopeChains(NewTemplate, Outer); |
| 1291 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1292 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1293 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1294 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1295 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1296 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1297 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1298 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1299 | // Friend templates are visible in fairly strange ways. |
| 1300 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1301 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1302 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1303 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1304 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1305 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1306 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1307 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1308 | FriendDecl *Friend = FriendDecl::Create( |
| 1309 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1310 | Friend->setAccess(AS_public); |
| 1311 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1312 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1314 | if (Invalid) { |
| 1315 | NewTemplate->setInvalidDecl(); |
| 1316 | NewClass->setInvalidDecl(); |
| 1317 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1318 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1319 | ActOnDocumentableDecl(NewTemplate); |
| 1320 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1321 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1324 | /// \brief Diagnose the presence of a default template argument on a |
| 1325 | /// template parameter, which is ill-formed in certain contexts. |
| 1326 | /// |
| 1327 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1328 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1329 | Sema::TemplateParamListContext TPC, |
| 1330 | SourceLocation ParamLoc, |
| 1331 | SourceRange DefArgRange) { |
| 1332 | switch (TPC) { |
| 1333 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1334 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1335 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1336 | return false; |
| 1337 | |
| 1338 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1339 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1340 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1341 | // A default template-argument shall not be specified in a |
| 1342 | // function template declaration or a function template |
| 1343 | // definition [...] |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1344 | // If a friend function template declaration specifies a default |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1345 | // template-argument, that declaration shall be a definition and shall be |
| 1346 | // the only declaration of the function template in the translation unit. |
| 1347 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1348 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1349 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1350 | : diag::ext_template_parameter_default_in_function_template) |
| 1351 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1352 | return false; |
| 1353 | |
| 1354 | case Sema::TPC_ClassTemplateMember: |
| 1355 | // C++0x [temp.param]p9: |
| 1356 | // A default template-argument shall not be specified in the |
| 1357 | // template-parameter-lists of the definition of a member of a |
| 1358 | // class template that appears outside of the member's class. |
| 1359 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1360 | << DefArgRange; |
| 1361 | return true; |
| 1362 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1363 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1364 | case Sema::TPC_FriendFunctionTemplate: |
| 1365 | // C++ [temp.param]p9: |
| 1366 | // A default template-argument shall not be specified in a |
| 1367 | // friend template declaration. |
| 1368 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1369 | << DefArgRange; |
| 1370 | return true; |
| 1371 | |
| 1372 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1373 | // for friend function templates if there is only a single |
| 1374 | // declaration (and it is a definition). Strange! |
| 1375 | } |
| 1376 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1377 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1380 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1381 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1382 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1383 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1384 | // A template template parameter which is a parameter pack is also a pack |
| 1385 | // expansion. |
| 1386 | if (TTP->isParameterPack()) |
| 1387 | return false; |
| 1388 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1389 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1390 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1391 | NamedDecl *P = Params->getParam(I); |
| 1392 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1393 | if (!NTTP->isParameterPack() && |
| 1394 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1395 | NTTP->getTypeSourceInfo(), |
| 1396 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1397 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1398 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1399 | continue; |
| 1400 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1403 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1404 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1405 | return true; |
| 1406 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1407 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1408 | return false; |
| 1409 | } |
| 1410 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1411 | /// \brief Checks the validity of a template parameter list, possibly |
| 1412 | /// considering the template parameter list from a previous |
| 1413 | /// declaration. |
| 1414 | /// |
| 1415 | /// If an "old" template parameter list is provided, it must be |
| 1416 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1417 | /// template parameter list. |
| 1418 | /// |
| 1419 | /// \param NewParams Template parameter list for a new template |
| 1420 | /// declaration. This template parameter list will be updated with any |
| 1421 | /// default arguments that are carried through from the previous |
| 1422 | /// template parameter list. |
| 1423 | /// |
| 1424 | /// \param OldParams If provided, template parameter list from a |
| 1425 | /// previous declaration of the same template. Default template |
| 1426 | /// arguments will be merged from the old template parameter list to |
| 1427 | /// the new template parameter list. |
| 1428 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1429 | /// \param TPC Describes the context in which we are checking the given |
| 1430 | /// template parameter list. |
| 1431 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1432 | /// \returns true if an error occurred, false otherwise. |
| 1433 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1434 | TemplateParameterList *OldParams, |
| 1435 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1436 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1438 | // C++ [temp.param]p10: |
| 1439 | // The set of default template-arguments available for use with a |
| 1440 | // template declaration or definition is obtained by merging the |
| 1441 | // default arguments from the definition (if in scope) and all |
| 1442 | // declarations in scope in the same way default function |
| 1443 | // arguments are (8.3.6). |
| 1444 | bool SawDefaultArgument = false; |
| 1445 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1446 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1447 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1448 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1449 | if (OldParams) |
| 1450 | OldParam = OldParams->begin(); |
| 1451 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1452 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1453 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1454 | NewParamEnd = NewParams->end(); |
| 1455 | NewParam != NewParamEnd; ++NewParam) { |
| 1456 | // Variables used to diagnose redundant default arguments |
| 1457 | bool RedundantDefaultArg = false; |
| 1458 | SourceLocation OldDefaultLoc; |
| 1459 | SourceLocation NewDefaultLoc; |
| 1460 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1461 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1462 | bool MissingDefaultArg = false; |
| 1463 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1464 | // Variable used to diagnose non-final parameter packs |
| 1465 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1466 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1467 | if (TemplateTypeParmDecl *NewTypeParm |
| 1468 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1469 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1470 | if (NewTypeParm->hasDefaultArgument() && |
| 1471 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1472 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1473 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1474 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1475 | NewTypeParm->removeDefaultArgument(); |
| 1476 | |
| 1477 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1479 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1480 | if (NewTypeParm->isParameterPack()) { |
| 1481 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1482 | "Parameter packs can't have a default argument!"); |
| 1483 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1484 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1485 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1486 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1487 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1488 | SawDefaultArgument = true; |
| 1489 | RedundantDefaultArg = true; |
| 1490 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1491 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1492 | // Merge the default argument from the old declaration to the |
| 1493 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1494 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1495 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1496 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1497 | SawDefaultArgument = true; |
| 1498 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1499 | } else if (SawDefaultArgument) |
| 1500 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1501 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1502 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1503 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1504 | if (!NewNonTypeParm->isParameterPack() && |
| 1505 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1506 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1507 | UPPC_NonTypeTemplateParameterType)) { |
| 1508 | Invalid = true; |
| 1509 | continue; |
| 1510 | } |
| 1511 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1512 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1513 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1514 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1515 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1516 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1517 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1520 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1521 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1522 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1523 | if (NewNonTypeParm->isParameterPack()) { |
| 1524 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1525 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1526 | if (!NewNonTypeParm->isPackExpansion()) |
| 1527 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1528 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1529 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1530 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1531 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1532 | SawDefaultArgument = true; |
| 1533 | RedundantDefaultArg = true; |
| 1534 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1535 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1536 | // Merge the default argument from the old declaration to the |
| 1537 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1538 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1539 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1540 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1541 | SawDefaultArgument = true; |
| 1542 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1543 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1545 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1546 | TemplateTemplateParmDecl *NewTemplateParm |
| 1547 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1548 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1549 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1550 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1551 | Invalid = true; |
| 1552 | continue; |
| 1553 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1554 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1555 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1556 | if (NewTemplateParm->hasDefaultArgument() && |
| 1557 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1558 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1559 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1560 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1561 | |
| 1562 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1563 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1564 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1565 | if (NewTemplateParm->isParameterPack()) { |
| 1566 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1567 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1568 | if (!NewTemplateParm->isPackExpansion()) |
| 1569 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1570 | } else if (OldTemplateParm && |
| 1571 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 1572 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1573 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1574 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1575 | SawDefaultArgument = true; |
| 1576 | RedundantDefaultArg = true; |
| 1577 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1578 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1579 | // Merge the default argument from the old declaration to the |
| 1580 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1581 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1582 | PreviousDefaultArgLoc |
| 1583 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1584 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1585 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1586 | PreviousDefaultArgLoc |
| 1587 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1588 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1592 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1593 | // If a template parameter of a primary class template or alias template |
| 1594 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1595 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1596 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1597 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1598 | Diag((*NewParam)->getLocation(), |
| 1599 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1600 | Invalid = true; |
| 1601 | } |
| 1602 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1603 | if (RedundantDefaultArg) { |
| 1604 | // C++ [temp.param]p12: |
| 1605 | // A template-parameter shall not be given default arguments |
| 1606 | // by two different declarations in the same scope. |
| 1607 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1608 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1609 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1610 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1611 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1612 | // If a template-parameter of a class template has a default |
| 1613 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1614 | // have a default template-argument supplied or be a template parameter |
| 1615 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1617 | diag::err_template_param_default_arg_missing); |
| 1618 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1619 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1620 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | // If we have an old template parameter list that we're merging |
| 1624 | // in, move on to the next parameter. |
| 1625 | if (OldParams) |
| 1626 | ++OldParam; |
| 1627 | } |
| 1628 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1629 | // We were missing some default arguments at the end of the list, so remove |
| 1630 | // all of the default arguments. |
| 1631 | if (RemoveDefaultArguments) { |
| 1632 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1633 | NewParamEnd = NewParams->end(); |
| 1634 | NewParam != NewParamEnd; ++NewParam) { |
| 1635 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1636 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1637 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1638 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1639 | NTTP->removeDefaultArgument(); |
| 1640 | else |
| 1641 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1642 | } |
| 1643 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1644 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1645 | return Invalid; |
| 1646 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1647 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1648 | namespace { |
| 1649 | |
| 1650 | /// A class which looks for a use of a certain level of template |
| 1651 | /// parameter. |
| 1652 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1653 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1654 | |
| 1655 | unsigned Depth; |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1656 | |
| 1657 | // Whether we're looking for a use of a template parameter that makes the |
| 1658 | // overall construct type-dependent / a dependent type. This is strictly |
| 1659 | // best-effort for now; we may fail to match at all for a dependent type |
| 1660 | // in some cases if this is set. |
| 1661 | bool IgnoreNonTypeDependent; |
| 1662 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1663 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1664 | SourceLocation MatchLoc; |
| 1665 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1666 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
| 1667 | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
| 1668 | Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1669 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1670 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
| 1671 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1672 | NamedDecl *ND = Params->getParam(0); |
| 1673 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1674 | Depth = PD->getDepth(); |
| 1675 | } else if (NonTypeTemplateParmDecl *PD = |
| 1676 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1677 | Depth = PD->getDepth(); |
| 1678 | } else { |
| 1679 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1680 | } |
| 1681 | } |
| 1682 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1683 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1684 | if (ParmDepth >= Depth) { |
| 1685 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1686 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1687 | return true; |
| 1688 | } |
| 1689 | return false; |
| 1690 | } |
| 1691 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1692 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 1693 | // Prune out non-type-dependent expressions if requested. This can |
| 1694 | // sometimes result in us failing to find a template parameter reference |
| 1695 | // (if a value-dependent expression creates a dependent type), but this |
| 1696 | // mode is best-effort only. |
| 1697 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 1698 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 1699 | return true; |
| 1700 | return super::TraverseStmt(S, Q); |
| 1701 | } |
| 1702 | |
| 1703 | bool TraverseTypeLoc(TypeLoc TL) { |
| 1704 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 1705 | !TL.getType()->isDependentType()) |
| 1706 | return true; |
| 1707 | return super::TraverseTypeLoc(TL); |
| 1708 | } |
| 1709 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1710 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1711 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1712 | } |
| 1713 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1714 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1715 | // For a best-effort search, keep looking until we find a location. |
| 1716 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | bool TraverseTemplateName(TemplateName N) { |
| 1720 | if (TemplateTemplateParmDecl *PD = |
| 1721 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1722 | if (Matches(PD->getDepth())) |
| 1723 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1724 | return super::TraverseTemplateName(N); |
| 1725 | } |
| 1726 | |
| 1727 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1728 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1729 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1730 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1731 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1732 | return super::VisitDeclRefExpr(E); |
| 1733 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1734 | |
| 1735 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1736 | return TraverseType(T->getReplacementType()); |
| 1737 | } |
| 1738 | |
| 1739 | bool |
| 1740 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1741 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1742 | } |
| 1743 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1744 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1745 | return TraverseType(T->getInjectedSpecializationType()); |
| 1746 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1747 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1748 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1749 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1750 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1751 | /// list. |
| 1752 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1753 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 1754 | DependencyChecker Checker(Params, /*IgnoreNonTypeDependent*/false); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1755 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1756 | return Checker.Match; |
| 1757 | } |
| 1758 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1759 | // Find the source range corresponding to the named type in the given |
| 1760 | // nested-name-specifier, if any. |
| 1761 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1762 | QualType T, |
| 1763 | const CXXScopeSpec &SS) { |
| 1764 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1765 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1766 | if (const Type *CurType = NNS->getAsType()) { |
| 1767 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1768 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1769 | } else |
| 1770 | break; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1771 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1772 | NNSLoc = NNSLoc.getPrefix(); |
| 1773 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1774 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1775 | return SourceRange(); |
| 1776 | } |
| 1777 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1779 | /// specifier, returning the template parameter list that applies to the |
| 1780 | /// name. |
| 1781 | /// |
| 1782 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1783 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1785 | /// \param DeclLoc The location of the declaration itself. |
| 1786 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1787 | /// \param SS the scope specifier that will be matched to the given template |
| 1788 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1789 | /// being declared. |
| 1790 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1791 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1792 | /// is one. Used to check for a missing 'template<>'. |
| 1793 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1794 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1795 | /// innermost template parameter lists. |
| 1796 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1797 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1798 | /// matching template parameters to scope specifiers in friend |
| 1799 | /// declarations. |
| 1800 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1801 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1802 | /// declared is an explicit specialization, false otherwise. |
| 1803 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1805 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1806 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1808 | /// 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] | 1809 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1810 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1811 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1812 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1813 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1814 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1815 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1816 | Invalid = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1817 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1818 | // The sequence of nested types to which we will match up the template |
| 1819 | // parameter lists. We first build this list by starting with the type named |
| 1820 | // 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] | 1821 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1822 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1823 | if (SS.getScopeRep()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1824 | if (CXXRecordDecl *Record |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1825 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1826 | T = Context.getTypeDeclType(Record); |
| 1827 | else |
| 1828 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1829 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1830 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1831 | // If we found an explicit specialization that prevents us from needing |
| 1832 | // 'template<>' headers, this will be set to the location of that |
| 1833 | // explicit specialization. |
| 1834 | SourceLocation ExplicitSpecLoc; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1836 | while (!T.isNull()) { |
| 1837 | NestedTypes.push_back(T); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1839 | // Retrieve the parent of a record type. |
| 1840 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1841 | // If this type is an explicit specialization, we're done. |
| 1842 | if (ClassTemplateSpecializationDecl *Spec |
| 1843 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1844 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1845 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1846 | ExplicitSpecLoc = Spec->getLocation(); |
| 1847 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1848 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1849 | } else if (Record->getTemplateSpecializationKind() |
| 1850 | == TSK_ExplicitSpecialization) { |
| 1851 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1852 | break; |
| 1853 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1854 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1855 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1856 | T = Context.getTypeDeclType(Parent); |
| 1857 | else |
| 1858 | T = QualType(); |
| 1859 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1862 | if (const TemplateSpecializationType *TST |
| 1863 | = T->getAs<TemplateSpecializationType>()) { |
| 1864 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1865 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1866 | T = Context.getTypeDeclType(Parent); |
| 1867 | else |
| 1868 | T = QualType(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1869 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1870 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1871 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1872 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1873 | // Look one step prior in a dependent template specialization type. |
| 1874 | if (const DependentTemplateSpecializationType *DependentTST |
| 1875 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1876 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1877 | T = QualType(NNS->getAsType(), 0); |
| 1878 | else |
| 1879 | T = QualType(); |
| 1880 | continue; |
| 1881 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1882 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1883 | // Look one step prior in a dependent name type. |
| 1884 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1885 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1886 | T = QualType(NNS->getAsType(), 0); |
| 1887 | else |
| 1888 | T = QualType(); |
| 1889 | continue; |
| 1890 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1891 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1892 | // Retrieve the parent of an enumeration type. |
| 1893 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1894 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1895 | // check here. |
| 1896 | EnumDecl *Enum = EnumT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1897 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1898 | // Get to the parent type. |
| 1899 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1900 | T = Context.getTypeDeclType(Parent); |
| 1901 | else |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1902 | T = QualType(); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1903 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1904 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1906 | T = QualType(); |
| 1907 | } |
| 1908 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1909 | // to the innermost while checking template-parameter-lists. |
| 1910 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1912 | // C++0x [temp.expl.spec]p17: |
| 1913 | // A member or a member template may be nested within many |
| 1914 | // enclosing class templates. In an explicit specialization for |
| 1915 | // such a member, the member declaration shall be preceded by a |
| 1916 | // template<> for each enclosing class template that is |
| 1917 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1918 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1919 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1920 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1921 | if (SawNonEmptyTemplateParameterList) { |
| 1922 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1923 | << !Recovery << Range; |
| 1924 | Invalid = true; |
| 1925 | IsExplicitSpecialization = false; |
| 1926 | return true; |
| 1927 | } |
| 1928 | |
| 1929 | return false; |
| 1930 | }; |
| 1931 | |
| 1932 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1933 | // Check that we can have an explicit specialization here. |
| 1934 | if (CheckExplicitSpecialization(Range, true)) |
| 1935 | return true; |
| 1936 | |
| 1937 | // We don't have a template header, but we should. |
| 1938 | SourceLocation ExpectedTemplateLoc; |
| 1939 | if (!ParamLists.empty()) |
| 1940 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1941 | else |
| 1942 | ExpectedTemplateLoc = DeclStartLoc; |
| 1943 | |
| 1944 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1945 | << Range |
| 1946 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1947 | return false; |
| 1948 | }; |
| 1949 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1950 | unsigned ParamIdx = 0; |
| 1951 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1952 | ++TypeIdx) { |
| 1953 | T = NestedTypes[TypeIdx]; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1954 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1955 | // Whether we expect a 'template<>' header. |
| 1956 | bool NeedEmptyTemplateHeader = false; |
| 1957 | |
| 1958 | // Whether we expect a template header with parameters. |
| 1959 | bool NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1960 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1961 | // For a dependent type, the set of template parameters that we |
| 1962 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1963 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1965 | // C++0x [temp.expl.spec]p15: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1966 | // A member or a member template may be nested within many enclosing |
| 1967 | // class templates. In an explicit specialization for such a member, the |
| 1968 | // member declaration shall be preceded by a template<> for each |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1969 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1970 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1971 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1972 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1973 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1974 | NeedNonemptyTemplateHeader = true; |
| 1975 | } else if (Record->isDependentType()) { |
| 1976 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1977 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1978 | ->getTemplateParameters(); |
| 1979 | NeedNonemptyTemplateHeader = true; |
| 1980 | } |
| 1981 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1982 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1983 | // C++0x [temp.expl.spec]p4: |
| 1984 | // Members of an explicitly specialized class template are defined |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1985 | // in the same manner as members of normal classes, and not using |
| 1986 | // the template<> syntax. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1987 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1988 | NeedEmptyTemplateHeader = true; |
| 1989 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1990 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1991 | } else if (Record->getTemplateSpecializationKind()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1992 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1993 | != TSK_ExplicitSpecialization && |
| 1994 | TypeIdx == NumTypes - 1) |
| 1995 | IsExplicitSpecialization = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 1996 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1997 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1998 | } |
| 1999 | } else if (const TemplateSpecializationType *TST |
| 2000 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 2001 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2002 | ExpectedTemplateParams = Template->getTemplateParameters(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2003 | NeedNonemptyTemplateHeader = true; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2004 | } |
| 2005 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2006 | // FIXME: We actually could/should check the template arguments here |
| 2007 | // against the corresponding template parameter list. |
| 2008 | NeedNonemptyTemplateHeader = false; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2011 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2012 | // In an explicit specialization declaration for a member of a class |
| 2013 | // template or a member template that ap- pears in namespace scope, the |
| 2014 | // member template and some of its enclosing class templates may remain |
| 2015 | // unspecialized, except that the declaration shall not explicitly |
| 2016 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2017 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2018 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2019 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2020 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2021 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2022 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2023 | } else |
| 2024 | SawNonEmptyTemplateParameterList = true; |
| 2025 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2026 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2027 | if (NeedEmptyTemplateHeader) { |
| 2028 | // If we're on the last of the types, and we need a 'template<>' header |
| 2029 | // here, then it's an explicit specialization. |
| 2030 | if (TypeIdx == NumTypes - 1) |
| 2031 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2032 | |
| 2033 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2034 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2035 | // The header has template parameters when it shouldn't. Complain. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2036 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2037 | diag::err_template_param_list_matches_nontemplate) |
| 2038 | << T |
| 2039 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2040 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2041 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2042 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2043 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2044 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2045 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2046 | // Consume this template header. |
| 2047 | ++ParamIdx; |
| 2048 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2049 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2050 | |
| 2051 | if (!IsFriend) |
| 2052 | if (DiagnoseMissingExplicitSpecialization( |
| 2053 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2054 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2055 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2056 | continue; |
| 2057 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2058 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2059 | if (NeedNonemptyTemplateHeader) { |
| 2060 | // In friend declarations we can have template-ids which don't |
| 2061 | // depend on the corresponding template parameter lists. But |
| 2062 | // assume that empty parameter lists are supposed to match this |
| 2063 | // template-id. |
| 2064 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2065 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2066 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2067 | ExpectedTemplateParams = nullptr; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2068 | else |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2069 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2071 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2072 | if (ParamIdx < ParamLists.size()) { |
| 2073 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2074 | if (ExpectedTemplateParams && |
| 2075 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2076 | ExpectedTemplateParams, |
| 2077 | true, TPL_TemplateMatch)) |
| 2078 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2079 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2080 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2081 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2082 | TPC_ClassTemplateMember)) |
| 2083 | Invalid = true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2084 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2085 | ++ParamIdx; |
| 2086 | continue; |
| 2087 | } |
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 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2090 | << T |
| 2091 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2092 | Invalid = true; |
| 2093 | continue; |
| 2094 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2095 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2096 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2097 | // If there were at least as many template-ids as there were template |
| 2098 | // parameter lists, then there are no template parameter lists remaining for |
| 2099 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2100 | if (ParamIdx >= ParamLists.size()) { |
| 2101 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2102 | // We don't have a template header for the declaration itself, but we |
| 2103 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2104 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2105 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2106 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2107 | |
| 2108 | // Fabricate an empty template parameter list for the invented header. |
| 2109 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2110 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2111 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2114 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2115 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2117 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2118 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2119 | bool HasAnyExplicitSpecHeader = false; |
| 2120 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2121 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2122 | if (ParamLists[I]->size() == 0) |
| 2123 | HasAnyExplicitSpecHeader = true; |
| 2124 | else |
| 2125 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2126 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2127 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2128 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2129 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2130 | : diag::err_template_spec_extra_headers) |
| 2131 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2132 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2133 | |
| 2134 | // If there was a specialization somewhere, such that 'template<>' is |
| 2135 | // not required, and there were any 'template<>' headers, note where the |
| 2136 | // specialization occurred. |
| 2137 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2138 | Diag(ExplicitSpecLoc, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2139 | diag::note_explicit_template_spec_does_not_need_header) |
| 2140 | << NestedTypes.back(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2141 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2142 | // We have a template parameter list with no corresponding scope, which |
| 2143 | // means that the resulting template declaration can't be instantiated |
| 2144 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2145 | if (!AllExplicitSpecHeaders) |
| 2146 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2147 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2149 | // C++ [temp.expl.spec]p16: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2150 | // In an explicit specialization declaration for a member of a class |
| 2151 | // template or a member template that ap- pears in namespace scope, the |
| 2152 | // member template and some of its enclosing class templates may remain |
| 2153 | // unspecialized, except that the declaration shall not explicitly |
| 2154 | // specialize a class member template if its en- closing class templates |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2155 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2156 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2157 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2158 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2159 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2160 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2161 | // Return the last template parameter list, which corresponds to the |
| 2162 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2163 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2166 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2167 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2168 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2169 | << (isa<FunctionTemplateDecl>(Template) |
| 2170 | ? 0 |
| 2171 | : isa<ClassTemplateDecl>(Template) |
| 2172 | ? 1 |
| 2173 | : isa<VarTemplateDecl>(Template) |
| 2174 | ? 2 |
| 2175 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2176 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2177 | return; |
| 2178 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2179 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2180 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2181 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2182 | IEnd = OST->end(); |
| 2183 | I != IEnd; ++I) |
| 2184 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2185 | << 0 << (*I)->getDeclName(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2186 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2187 | return; |
| 2188 | } |
| 2189 | } |
| 2190 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2191 | static QualType |
| 2192 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2193 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2194 | SourceLocation TemplateLoc, |
| 2195 | TemplateArgumentListInfo &TemplateArgs) { |
| 2196 | ASTContext &Context = SemaRef.getASTContext(); |
| 2197 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2198 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2199 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2200 | // S<T, 0, ..., N-1>. |
| 2201 | |
| 2202 | // C++14 [inteseq.intseq]p1: |
| 2203 | // T shall be an integer type. |
| 2204 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2205 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2206 | diag::err_integer_sequence_integral_element_type); |
| 2207 | return QualType(); |
| 2208 | } |
| 2209 | |
| 2210 | // C++14 [inteseq.make]p1: |
| 2211 | // If N is negative the program is ill-formed. |
| 2212 | TemplateArgument NumArgsArg = Converted[2]; |
| 2213 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2214 | if (NumArgs < 0) { |
| 2215 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2216 | diag::err_integer_sequence_negative_length); |
| 2217 | return QualType(); |
| 2218 | } |
| 2219 | |
| 2220 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2221 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2222 | // The type argument gets reused as the first template argument in the |
| 2223 | // synthetic template argument list. |
| 2224 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2225 | // Expand N into 0 ... N-1. |
| 2226 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2227 | I < NumArgs; ++I) { |
| 2228 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2229 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2230 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2231 | } |
| 2232 | // The first template argument will be reused as the template decl that |
| 2233 | // our synthetic template arguments will be applied to. |
| 2234 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2235 | TemplateLoc, SyntheticTemplateArgs); |
| 2236 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2237 | |
| 2238 | case BTK__type_pack_element: |
| 2239 | // Specializations of |
| 2240 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2241 | // are treated like T_Index. |
| 2242 | assert(Converted.size() == 2 && |
| 2243 | "__type_pack_element should be given an index and a parameter pack"); |
| 2244 | |
| 2245 | // If the Index is out of bounds, the program is ill-formed. |
| 2246 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2247 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2248 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2249 | "type std::size_t, and hence be non-negative"); |
| 2250 | if (Index >= Ts.pack_size()) { |
| 2251 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2252 | diag::err_type_pack_element_out_of_bounds); |
| 2253 | return QualType(); |
| 2254 | } |
| 2255 | |
| 2256 | // We simply return the type at index `Index`. |
| 2257 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2258 | return Nth->getAsType(); |
| 2259 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2260 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2261 | } |
| 2262 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2263 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2264 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2265 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2266 | DependentTemplateName *DTN |
| 2267 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2268 | if (DTN && DTN->isIdentifier()) |
| 2269 | // When building a template-id where the template-name is dependent, |
| 2270 | // assume the template is a type template. Either our assumption is |
| 2271 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2272 | // dependent name is substituted. |
| 2273 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2274 | DTN->getQualifier(), |
| 2275 | DTN->getIdentifier(), |
| 2276 | TemplateArgs); |
| 2277 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2278 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2279 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2280 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2281 | // We might have a substituted template template parameter pack. If so, |
| 2282 | // build a template specialization type for it. |
| 2283 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2284 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2285 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2286 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2287 | << Name; |
| 2288 | NoteAllFoundTemplates(Name); |
| 2289 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2290 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2291 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2292 | // Check that the template argument list is well-formed for this |
| 2293 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2294 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2295 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2296 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2297 | return QualType(); |
| 2298 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2299 | QualType CanonType; |
| 2300 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2301 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2302 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2303 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2304 | // Find the canonical type for this type alias template specialization. |
| 2305 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2306 | if (Pattern->isInvalidDecl()) |
| 2307 | return QualType(); |
| 2308 | |
| 2309 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2310 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2311 | |
| 2312 | // Only substitute for the innermost template argument list. |
| 2313 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2314 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2315 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2316 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2317 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2318 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2319 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2320 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2321 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2322 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2323 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2324 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2325 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2326 | AliasTemplate->getDeclName()); |
| 2327 | if (CanonType.isNull()) |
| 2328 | return QualType(); |
| 2329 | } else if (Name.isDependent() || |
| 2330 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2331 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2332 | // This class template specialization is a dependent |
| 2333 | // type. Therefore, its canonical type is another class template |
| 2334 | // specialization type that contains all of the converted |
| 2335 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2336 | // A<T, T> have identical types when A is declared as: |
| 2337 | // |
| 2338 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2339 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | CanonType = Context.getTemplateSpecializationType(CanonName, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2341 | Converted); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2342 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2343 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2344 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2345 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2346 | // build the canonical type and return that to us. |
| 2347 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2348 | |
| 2349 | // This might work out to be a current instantiation, in which |
| 2350 | // case the canonical type needs to be the InjectedClassNameType. |
| 2351 | // |
| 2352 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2353 | // changes to CurContext don't change the set of current |
| 2354 | // instantiations. |
| 2355 | if (isa<ClassTemplateDecl>(Template)) { |
| 2356 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2357 | // If we get out to a namespace, we're done. |
| 2358 | if (Ctx->isFileContext()) break; |
| 2359 | |
| 2360 | // If this isn't a record, keep looking. |
| 2361 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2362 | if (!Record) continue; |
| 2363 | |
| 2364 | // Look for one of the two cases with InjectedClassNameTypes |
| 2365 | // and check whether it's the same template. |
| 2366 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2367 | !Record->getDescribedClassTemplate()) |
| 2368 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2369 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2370 | // Fetch the injected class name type and check whether its |
| 2371 | // injected type is equal to the type we just built. |
| 2372 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2373 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2374 | ->getInjectedSpecializationType(); |
| 2375 | |
| 2376 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2377 | continue; |
| 2378 | |
| 2379 | // If so, the canonical type of this TST is the injected |
| 2380 | // class name type of the record we just found. |
| 2381 | assert(ICNT.isCanonical()); |
| 2382 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2383 | break; |
| 2384 | } |
| 2385 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2387 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2388 | // Find the class template specialization declaration that |
| 2389 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2390 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2391 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2392 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2393 | if (!Decl) { |
| 2394 | // This is the first time we have referenced this class template |
| 2395 | // specialization. Create the canonical declaration and add it to |
| 2396 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2398 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2399 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2400 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2401 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2402 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2403 | Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2404 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2405 | if (ClassTemplate->isOutOfLine()) |
| 2406 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2409 | // Diagnose uses of this specialization. |
| 2410 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2411 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2412 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2413 | assert(isa<RecordType>(CanonType) && |
| 2414 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2415 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2416 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2417 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2418 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2419 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2420 | // Build the fully-sugared type for this class template |
| 2421 | // specialization, which refers back to the class template |
| 2422 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2423 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2426 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2427 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2428 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2429 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2430 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2431 | SourceLocation RAngleLoc, |
| 2432 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2433 | if (SS.isInvalid()) |
| 2434 | return true; |
| 2435 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2436 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2437 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2438 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2439 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2440 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2441 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2442 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2443 | QualType T |
| 2444 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2445 | DTN->getQualifier(), |
| 2446 | DTN->getIdentifier(), |
| 2447 | TemplateArgs); |
| 2448 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2449 | TypeLocBuilder TLB; |
| 2450 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2451 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2452 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2453 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2454 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2455 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2456 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2457 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2458 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2459 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2460 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2461 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2462 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2463 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2464 | |
| 2465 | if (Result.isNull()) |
| 2466 | return true; |
| 2467 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2468 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2469 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2470 | TemplateSpecializationTypeLoc SpecTL |
| 2471 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2472 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2473 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2474 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2475 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2476 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2477 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2478 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2479 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2480 | // constructor or destructor name (in such a case, the scope specifier |
| 2481 | // will be attached to the enclosing Decl or Expr node). |
| 2482 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2483 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2484 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2485 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2486 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2487 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2488 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2489 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2490 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2491 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2493 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2494 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2495 | SourceLocation TagLoc, |
| 2496 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2497 | SourceLocation TemplateKWLoc, |
| 2498 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2499 | SourceLocation TemplateLoc, |
| 2500 | SourceLocation LAngleLoc, |
| 2501 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2502 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2503 | TemplateName Template = TemplateD.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2504 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2505 | // Translate the parser's template argument list in our AST format. |
| 2506 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2507 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2508 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2509 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2510 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2511 | ElaboratedTypeKeyword Keyword |
| 2512 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2513 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2514 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2515 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2516 | DTN->getQualifier(), |
| 2517 | DTN->getIdentifier(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2518 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2519 | |
| 2520 | // Build type-source information. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2521 | TypeLocBuilder TLB; |
| 2522 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2523 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2524 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2525 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2526 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2527 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2528 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2529 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2530 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2531 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2532 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2533 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2534 | |
| 2535 | if (TypeAliasTemplateDecl *TAT = |
| 2536 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2537 | // C++0x [dcl.type.elab]p2: |
| 2538 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2539 | // resolves to an alias template specialization, the |
| 2540 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 2541 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 2542 | << TAT << NTK_TypeAliasTemplate << TagKind; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2543 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2544 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2545 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2546 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2547 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2548 | return TypeResult(true); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2550 | // Check the tag kind |
| 2551 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2552 | RecordDecl *D = RT->getDecl(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2553 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2554 | IdentifierInfo *Id = D->getIdentifier(); |
| 2555 | assert(Id && "templated class must have an identifier"); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 2556 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2557 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 2558 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2559 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2560 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2561 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2562 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2563 | } |
| 2564 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2565 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2566 | // Provide source-location information for the template specialization. |
| 2567 | TypeLocBuilder TLB; |
| 2568 | TemplateSpecializationTypeLoc SpecTL |
| 2569 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2570 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2571 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2572 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2573 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2574 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2575 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2576 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2577 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2578 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2579 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2580 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2581 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2582 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2583 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2586 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2587 | NamedDecl *PrevDecl, |
| 2588 | SourceLocation Loc, |
| 2589 | bool IsPartialSpecialization); |
| 2590 | |
| 2591 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2592 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2593 | static bool isTemplateArgumentTemplateParameter( |
| 2594 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2595 | switch (Arg.getKind()) { |
| 2596 | case TemplateArgument::Null: |
| 2597 | case TemplateArgument::NullPtr: |
| 2598 | case TemplateArgument::Integral: |
| 2599 | case TemplateArgument::Declaration: |
| 2600 | case TemplateArgument::Pack: |
| 2601 | case TemplateArgument::TemplateExpansion: |
| 2602 | return false; |
| 2603 | |
| 2604 | case TemplateArgument::Type: { |
| 2605 | QualType Type = Arg.getAsType(); |
| 2606 | const TemplateTypeParmType *TPT = |
| 2607 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2608 | return TPT && !Type.hasQualifiers() && |
| 2609 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2610 | } |
| 2611 | |
| 2612 | case TemplateArgument::Expression: { |
| 2613 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2614 | if (!DRE || !DRE->getDecl()) |
| 2615 | return false; |
| 2616 | const NonTypeTemplateParmDecl *NTTP = |
| 2617 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2618 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2619 | } |
| 2620 | |
| 2621 | case TemplateArgument::Template: |
| 2622 | const TemplateTemplateParmDecl *TTP = |
| 2623 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2624 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2625 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2626 | } |
| 2627 | llvm_unreachable("unexpected kind of template argument"); |
| 2628 | } |
| 2629 | |
| 2630 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2631 | ArrayRef<TemplateArgument> Args) { |
| 2632 | if (Params->size() != Args.size()) |
| 2633 | return false; |
| 2634 | |
| 2635 | unsigned Depth = Params->getDepth(); |
| 2636 | |
| 2637 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2638 | TemplateArgument Arg = Args[I]; |
| 2639 | |
| 2640 | // If the parameter is a pack expansion, the argument must be a pack |
| 2641 | // whose only element is a pack expansion. |
| 2642 | if (Params->getParam(I)->isParameterPack()) { |
| 2643 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2644 | !Arg.pack_begin()->isPackExpansion()) |
| 2645 | return false; |
| 2646 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2647 | } |
| 2648 | |
| 2649 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2650 | return false; |
| 2651 | } |
| 2652 | |
| 2653 | return true; |
| 2654 | } |
| 2655 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2656 | /// Convert the parser's template argument list representation into our form. |
| 2657 | static TemplateArgumentListInfo |
| 2658 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2659 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2660 | TemplateId.RAngleLoc); |
| 2661 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2662 | TemplateId.NumArgs); |
| 2663 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2664 | return TemplateArgs; |
| 2665 | } |
| 2666 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2667 | template<typename PartialSpecDecl> |
| 2668 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 2669 | if (Partial->getDeclContext()->isDependentContext()) |
| 2670 | return; |
| 2671 | |
| 2672 | // FIXME: Get the TDK from deduction in order to provide better diagnostics |
| 2673 | // for non-substitution-failure issues? |
| 2674 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 2675 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 2676 | return; |
| 2677 | |
| 2678 | auto *Template = Partial->getSpecializedTemplate(); |
| 2679 | S.Diag(Partial->getLocation(), |
Richard Smith | fa4a09d | 2016-12-27 20:03:09 +0000 | [diff] [blame] | 2680 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 2681 | << isa<VarTemplateDecl>(Template); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2682 | |
| 2683 | if (Info.hasSFINAEDiagnostic()) { |
| 2684 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 2685 | PartialDiagnostic::NullDiagnostic()}; |
| 2686 | Info.takeSFINAEDiagnostic(Diag); |
| 2687 | SmallString<128> SFINAEArgString; |
| 2688 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 2689 | S.Diag(Diag.first, |
| 2690 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 2691 | << SFINAEArgString; |
| 2692 | } |
| 2693 | |
| 2694 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2695 | } |
| 2696 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2697 | template<typename PartialSpecDecl> |
| 2698 | static void checkTemplatePartialSpecialization(Sema &S, |
| 2699 | PartialSpecDecl *Partial) { |
| 2700 | // C++1z [temp.class.spec]p8: (DR1495) |
| 2701 | // - The specialization shall be more specialized than the primary |
| 2702 | // template (14.5.5.2). |
| 2703 | checkMoreSpecializedThanPrimary(S, Partial); |
| 2704 | |
| 2705 | // C++ [temp.class.spec]p8: (DR1315) |
| 2706 | // - Each template-parameter shall appear at least once in the |
| 2707 | // template-id outside a non-deduced context. |
| 2708 | // C++1z [temp.class.spec.match]p3 (P0127R2) |
| 2709 | // If the template arguments of a partial specialization cannot be |
| 2710 | // deduced because of the structure of its template-parameter-list |
| 2711 | // and the template-id, the program is ill-formed. |
| 2712 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 2713 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2714 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2715 | TemplateParams->getDepth(), DeducibleParams); |
| 2716 | |
| 2717 | if (!DeducibleParams.all()) { |
| 2718 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 2719 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 2720 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 2721 | << (NumNonDeducible > 1) |
| 2722 | << SourceRange(Partial->getLocation(), |
| 2723 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
| 2724 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2725 | if (!DeducibleParams[I]) { |
| 2726 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2727 | if (Param->getDeclName()) |
| 2728 | S.Diag(Param->getLocation(), |
| 2729 | diag::note_partial_spec_unused_parameter) |
| 2730 | << Param->getDeclName(); |
| 2731 | else |
| 2732 | S.Diag(Param->getLocation(), |
| 2733 | diag::note_partial_spec_unused_parameter) |
| 2734 | << "(anonymous)"; |
| 2735 | } |
| 2736 | } |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | void Sema::CheckTemplatePartialSpecialization( |
| 2741 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 2742 | checkTemplatePartialSpecialization(*this, Partial); |
| 2743 | } |
| 2744 | |
| 2745 | void Sema::CheckTemplatePartialSpecialization( |
| 2746 | VarTemplatePartialSpecializationDecl *Partial) { |
| 2747 | checkTemplatePartialSpecialization(*this, Partial); |
| 2748 | } |
| 2749 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2750 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2751 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2752 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2753 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2754 | // D must be variable template id. |
| 2755 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2756 | "Variable template specialization is declared with a template it."); |
| 2757 | |
| 2758 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2759 | TemplateArgumentListInfo TemplateArgs = |
| 2760 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2761 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2762 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2763 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2764 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2765 | TemplateName Name = TemplateId->Template.get(); |
| 2766 | |
| 2767 | // The template-id must name a variable template. |
| 2768 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2769 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2770 | if (!VarTemplate) { |
| 2771 | NamedDecl *FnTemplate; |
| 2772 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2773 | FnTemplate = *OTS->begin(); |
| 2774 | else |
| 2775 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2776 | if (FnTemplate) |
| 2777 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2778 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2779 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2780 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2781 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2782 | |
| 2783 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2784 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2785 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2786 | UPPC_PartialSpecialization)) |
| 2787 | return true; |
| 2788 | |
| 2789 | // Check that the template argument list is well-formed for this |
| 2790 | // template. |
| 2791 | SmallVector<TemplateArgument, 4> Converted; |
| 2792 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2793 | false, Converted)) |
| 2794 | return true; |
| 2795 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2796 | // Find the variable template (partial) specialization declaration that |
| 2797 | // corresponds to these arguments. |
| 2798 | if (IsPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2799 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 2800 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2801 | return true; |
| 2802 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2803 | // FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we |
| 2804 | // also do them during instantiation. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2805 | bool InstantiationDependent; |
| 2806 | if (!Name.isDependent() && |
| 2807 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2808 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2809 | InstantiationDependent)) { |
| 2810 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2811 | << VarTemplate->getDeclName(); |
| 2812 | IsPartialSpecialization = false; |
| 2813 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2814 | |
| 2815 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2816 | Converted)) { |
| 2817 | // C++ [temp.class.spec]p9b3: |
| 2818 | // |
| 2819 | // -- The argument list of the specialization shall not be identical |
| 2820 | // to the implicit argument list of the primary template. |
| 2821 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2822 | << /*variable template*/ 1 |
| 2823 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2824 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2825 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2826 | // of the primary template. |
| 2827 | return true; |
| 2828 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2831 | void *InsertPos = nullptr; |
| 2832 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2833 | |
| 2834 | if (IsPartialSpecialization) |
| 2835 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2836 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2837 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2838 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2839 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2840 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2841 | |
| 2842 | // Check whether we can declare a variable template specialization in |
| 2843 | // the current scope. |
| 2844 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2845 | TemplateNameLoc, |
| 2846 | IsPartialSpecialization)) |
| 2847 | return true; |
| 2848 | |
| 2849 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2850 | // Since the only prior variable template specialization with these |
| 2851 | // arguments was referenced but not declared, reuse that |
| 2852 | // declaration node as our own, updating its source location and |
| 2853 | // the list of outer template parameters to reflect our new declaration. |
| 2854 | Specialization = PrevDecl; |
| 2855 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2856 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2857 | } else if (IsPartialSpecialization) { |
| 2858 | // Create a new class template partial specialization declaration node. |
| 2859 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2860 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2861 | VarTemplatePartialSpecializationDecl *Partial = |
| 2862 | VarTemplatePartialSpecializationDecl::Create( |
| 2863 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2864 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2865 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2866 | |
| 2867 | if (!PrevPartial) |
| 2868 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2869 | Specialization = Partial; |
| 2870 | |
| 2871 | // If we are providing an explicit specialization of a member variable |
| 2872 | // template specialization, make a note of that. |
| 2873 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2874 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2875 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 2876 | CheckTemplatePartialSpecialization(Partial); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2877 | } else { |
| 2878 | // Create a new class template specialization declaration node for |
| 2879 | // this explicit specialization or friend declaration. |
| 2880 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2881 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2882 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2883 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2884 | |
| 2885 | if (!PrevDecl) |
| 2886 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2887 | } |
| 2888 | |
| 2889 | // C++ [temp.expl.spec]p6: |
| 2890 | // If a template, a member template or the member of a class template is |
| 2891 | // explicitly specialized then that specialization shall be declared |
| 2892 | // before the first use of that specialization that would cause an implicit |
| 2893 | // instantiation to take place, in every translation unit in which such a |
| 2894 | // use occurs; no diagnostic is required. |
| 2895 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2896 | bool Okay = false; |
| 2897 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2898 | // Is there any previous explicit specialization declaration? |
| 2899 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2900 | Okay = true; |
| 2901 | break; |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | if (!Okay) { |
| 2906 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2907 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2908 | << Name << Range; |
| 2909 | |
| 2910 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2911 | diag::note_instantiation_required_here) |
| 2912 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2913 | TSK_ImplicitInstantiation); |
| 2914 | return true; |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2919 | Specialization->setLexicalDeclContext(CurContext); |
| 2920 | |
| 2921 | // Add the specialization into its lexical context, so that it can |
| 2922 | // be seen when iterating through the list of declarations in that |
| 2923 | // context. However, specializations are not found by name lookup. |
| 2924 | CurContext->addDecl(Specialization); |
| 2925 | |
| 2926 | // Note that this is an explicit specialization. |
| 2927 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2928 | |
| 2929 | if (PrevDecl) { |
| 2930 | // Check that this isn't a redefinition of this specialization, |
| 2931 | // merging with previous declarations. |
| 2932 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2933 | ForRedeclaration); |
| 2934 | PrevSpec.addDecl(PrevDecl); |
| 2935 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2936 | } else if (Specialization->isStaticDataMember() && |
| 2937 | Specialization->isOutOfLine()) { |
| 2938 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | // Link instantiations of static data members back to the template from |
| 2942 | // which they were instantiated. |
| 2943 | if (Specialization->isStaticDataMember()) |
| 2944 | Specialization->setInstantiationOfStaticDataMember( |
| 2945 | VarTemplate->getTemplatedDecl(), |
| 2946 | Specialization->getSpecializationKind()); |
| 2947 | |
| 2948 | return Specialization; |
| 2949 | } |
| 2950 | |
| 2951 | namespace { |
| 2952 | /// \brief A partial specialization whose template arguments have matched |
| 2953 | /// a given template-id. |
| 2954 | struct PartialSpecMatchResult { |
| 2955 | VarTemplatePartialSpecializationDecl *Partial; |
| 2956 | TemplateArgumentList *Args; |
| 2957 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2958 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2959 | |
| 2960 | DeclResult |
| 2961 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2962 | SourceLocation TemplateNameLoc, |
| 2963 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2964 | assert(Template && "A variable template id without template?"); |
| 2965 | |
| 2966 | // Check that the template argument list is well-formed for this template. |
| 2967 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2968 | if (CheckTemplateArgumentList( |
| 2969 | Template, TemplateNameLoc, |
| 2970 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2971 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2972 | return true; |
| 2973 | |
| 2974 | // Find the variable template specialization declaration that |
| 2975 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2976 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2977 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2978 | Converted, InsertPos)) { |
| 2979 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2980 | // If we already have a variable template specialization, return it. |
| 2981 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2982 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2983 | |
| 2984 | // This is the first time we have referenced this variable template |
| 2985 | // specialization. Create the canonical declaration and add it to |
| 2986 | // the set of specializations, based on the closest partial specialization |
| 2987 | // that it represents. That is, |
| 2988 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2989 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2990 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2991 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2992 | bool AmbiguousPartialSpec = false; |
| 2993 | typedef PartialSpecMatchResult MatchResult; |
| 2994 | SmallVector<MatchResult, 4> Matched; |
| 2995 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2996 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 2997 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2998 | |
| 2999 | // 1. Attempt to find the closest partial specialization that this |
| 3000 | // specializes, if any. |
| 3001 | // If any of the template arguments is dependent, then this is probably |
| 3002 | // a placeholder for an incomplete declarative context; which must be |
| 3003 | // complete by instantiation time. Thus, do not search through the partial |
| 3004 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3005 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 3006 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 3007 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3008 | bool InstantiationDependent = false; |
| 3009 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3010 | TemplateArgs, InstantiationDependent)) { |
| 3011 | |
| 3012 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 3013 | Template->getPartialSpecializations(PartialSpecs); |
| 3014 | |
| 3015 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 3016 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 3017 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 3018 | |
| 3019 | if (TemplateDeductionResult Result = |
| 3020 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 3021 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 3022 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3023 | FailedCandidates.addCandidate().set( |
| 3024 | DeclAccessPair::make(Template, AS_public), Partial, |
| 3025 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3026 | (void)Result; |
| 3027 | } else { |
| 3028 | Matched.push_back(PartialSpecMatchResult()); |
| 3029 | Matched.back().Partial = Partial; |
| 3030 | Matched.back().Args = Info.take(); |
| 3031 | } |
| 3032 | } |
| 3033 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3034 | if (Matched.size() >= 1) { |
| 3035 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 3036 | if (Matched.size() == 1) { |
| 3037 | // -- If exactly one matching specialization is found, the |
| 3038 | // instantiation is generated from that specialization. |
| 3039 | // We don't need to do anything for this. |
| 3040 | } else { |
| 3041 | // -- If more than one matching specialization is found, the |
| 3042 | // partial order rules (14.5.4.2) are used to determine |
| 3043 | // whether one of the specializations is more specialized |
| 3044 | // than the others. If none of the specializations is more |
| 3045 | // specialized than all of the other matching |
| 3046 | // specializations, then the use of the variable template is |
| 3047 | // ambiguous and the program is ill-formed. |
| 3048 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 3049 | PEnd = Matched.end(); |
| 3050 | P != PEnd; ++P) { |
| 3051 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 3052 | PointOfInstantiation) == |
| 3053 | P->Partial) |
| 3054 | Best = P; |
| 3055 | } |
| 3056 | |
| 3057 | // Determine if the best partial specialization is more specialized than |
| 3058 | // the others. |
| 3059 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 3060 | PEnd = Matched.end(); |
| 3061 | P != PEnd; ++P) { |
| 3062 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 3063 | P->Partial, Best->Partial, |
| 3064 | PointOfInstantiation) != Best->Partial) { |
| 3065 | AmbiguousPartialSpec = true; |
| 3066 | break; |
| 3067 | } |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | // Instantiate using the best variable template partial specialization. |
| 3072 | InstantiationPattern = Best->Partial; |
| 3073 | InstantiationArgs = Best->Args; |
| 3074 | } else { |
| 3075 | // -- If no match is found, the instantiation is generated |
| 3076 | // from the primary template. |
| 3077 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 3078 | } |
| 3079 | } |
| 3080 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3081 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3082 | // Note that we do not instantiate a definition until we see an odr-use |
| 3083 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3084 | // FIXME: LateAttrs et al.? |
| 3085 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 3086 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 3087 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 3088 | if (!Decl) |
| 3089 | return true; |
| 3090 | |
| 3091 | if (AmbiguousPartialSpec) { |
| 3092 | // Partial ordering did not produce a clear winner. Complain. |
| 3093 | Decl->setInvalidDecl(); |
| 3094 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 3095 | << Decl; |
| 3096 | |
| 3097 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 3098 | for (MatchResult P : Matched) |
| 3099 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 3100 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 3101 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3102 | return true; |
| 3103 | } |
| 3104 | |
| 3105 | if (VarTemplatePartialSpecializationDecl *D = |
| 3106 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 3107 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 3108 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3109 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 3110 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3111 | assert(Decl && "No variable template specialization?"); |
| 3112 | return Decl; |
| 3113 | } |
| 3114 | |
| 3115 | ExprResult |
| 3116 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 3117 | const DeclarationNameInfo &NameInfo, |
| 3118 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3119 | const TemplateArgumentListInfo *TemplateArgs) { |
| 3120 | |
| 3121 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 3122 | *TemplateArgs); |
| 3123 | if (Decl.isInvalid()) |
| 3124 | return ExprError(); |
| 3125 | |
| 3126 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 3127 | if (!Var->getTemplateSpecializationKind()) |
| 3128 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 3129 | NameInfo.getLoc()); |
| 3130 | |
| 3131 | // Build an ordinary singleton decl ref. |
| 3132 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3133 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3136 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3137 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3138 | LookupResult &R, |
| 3139 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3140 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3141 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 3142 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3143 | // 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] | 3144 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3145 | // foo<int> could identify a single function unambiguously |
| 3146 | // This approach does NOT work, since f<int>(1); |
| 3147 | // gets resolved prior to resorting to overload resolution |
| 3148 | // i.e., template<class T> void f(double); |
| 3149 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3150 | |
| 3151 | // These should be filtered out by our callers. |
| 3152 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 3153 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 3154 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3155 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 3156 | bool InstantiationDependent; |
| 3157 | if (R.getAsSingle<VarTemplateDecl>() && |
| 3158 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3159 | *TemplateArgs, InstantiationDependent)) { |
| 3160 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 3161 | R.getAsSingle<VarTemplateDecl>(), |
| 3162 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3165 | // We don't want lookup warnings at this point. |
| 3166 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3167 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3168 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3169 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3170 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3171 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3172 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3173 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3174 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3175 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3176 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3179 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3180 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3181 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3182 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3183 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3184 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3185 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3186 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3187 | DeclContext *DC; |
| 3188 | if (!(DC = computeDeclContext(SS, false)) || |
| 3189 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3190 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 3191 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3192 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3193 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3194 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3195 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3196 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3198 | if (R.isAmbiguous()) |
| 3199 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3200 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3201 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3202 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 3203 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3204 | return ExprError(); |
| 3205 | } |
| 3206 | |
| 3207 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3208 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3209 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 3210 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3211 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 3212 | return ExprError(); |
| 3213 | } |
| 3214 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3215 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3218 | /// \brief Form a dependent template name. |
| 3219 | /// |
| 3220 | /// This action forms a dependent template name given the template |
| 3221 | /// name and its (presumably dependent) scope specifier. For |
| 3222 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 3223 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 3224 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3225 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3226 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3227 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3228 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3229 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3230 | bool EnteringContext, |
| 3231 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3232 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 3233 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3234 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3235 | diag::warn_cxx98_compat_template_outside_of_template : |
| 3236 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3237 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 3238 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3239 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3240 | if (SS.isSet()) |
| 3241 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 3242 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3243 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3244 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3245 | // C++0x [temp.names]p5: |
| 3246 | // If a name prefixed by the keyword template is not the name of |
| 3247 | // a template, the program is ill-formed. [Note: the keyword |
| 3248 | // template may not be applied to non-template members of class |
| 3249 | // templates. -end note ] [ Note: as is the case with the |
| 3250 | // typename prefix, the template prefix is allowed in cases |
| 3251 | // where it is not strictly necessary; i.e., when the |
| 3252 | // nested-name-specifier or the expression on the left of the -> |
| 3253 | // or . is not dependent on a template-parameter, or the use |
| 3254 | // does not appear in the scope of a template. -end note] |
| 3255 | // |
| 3256 | // Note: C++03 was more strict here, because it banned the use of |
| 3257 | // the "template" keyword prior to a template-name that was not a |
| 3258 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3259 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3260 | // 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] | 3261 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3262 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3263 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3264 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3265 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3266 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3267 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3268 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3269 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3270 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3271 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3272 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3273 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3274 | << Name.getSourceRange() |
| 3275 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3276 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3277 | } else { |
| 3278 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3279 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3280 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3283 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3285 | switch (Name.getKind()) { |
| 3286 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3287 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3288 | Name.Identifier)); |
| 3289 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3291 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3292 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3293 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3294 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3295 | |
| 3296 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3297 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3298 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3299 | default: |
| 3300 | break; |
| 3301 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3302 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3303 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3304 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3305 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3306 | << Name.getSourceRange() |
| 3307 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3308 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3312 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3313 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3314 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3315 | QualType ArgType; |
| 3316 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3317 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3318 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3319 | switch(Arg.getKind()) { |
| 3320 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3321 | // C++ [temp.arg.type]p1: |
| 3322 | // A template-argument for a template-parameter which is a |
| 3323 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3324 | ArgType = Arg.getAsType(); |
| 3325 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3326 | break; |
| 3327 | case TemplateArgument::Template: { |
| 3328 | // We have a template type parameter but the template argument |
| 3329 | // is a template without any arguments. |
| 3330 | SourceRange SR = AL.getSourceRange(); |
| 3331 | TemplateName Name = Arg.getAsTemplate(); |
| 3332 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3333 | << Name << SR; |
| 3334 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3335 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3336 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3337 | return true; |
| 3338 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3339 | case TemplateArgument::Expression: { |
| 3340 | // We have a template type parameter but the template argument is an |
| 3341 | // expression; see if maybe it is missing the "typename" keyword. |
| 3342 | CXXScopeSpec SS; |
| 3343 | DeclarationNameInfo NameInfo; |
| 3344 | |
| 3345 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3346 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3347 | NameInfo = ArgExpr->getNameInfo(); |
| 3348 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3349 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3350 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3351 | NameInfo = ArgExpr->getNameInfo(); |
| 3352 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3353 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3354 | if (ArgExpr->isImplicitAccess()) { |
| 3355 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3356 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3357 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3360 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3361 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3362 | LookupParsedName(Result, CurScope, &SS); |
| 3363 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3364 | if (Result.getAsSingle<TypeDecl>() || |
| 3365 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3366 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3367 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3368 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3369 | Diag(Loc, getLangOpts().MSVCCompat |
| 3370 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3371 | : diag::err_template_arg_must_be_type_suggest) |
| 3372 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3373 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3374 | |
| 3375 | // Recover by synthesizing a type using the location information that we |
| 3376 | // already have. |
| 3377 | ArgType = |
| 3378 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3379 | TypeLocBuilder TLB; |
| 3380 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3381 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3382 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3383 | TL.setNameLoc(NameInfo.getLoc()); |
| 3384 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3385 | |
| 3386 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3387 | // properly. |
| 3388 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3389 | TemplateArgumentLocInfo(TSI)); |
| 3390 | |
| 3391 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3392 | } |
| 3393 | } |
| 3394 | // fallthrough |
| 3395 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3396 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3397 | // We have a template type parameter but the template argument |
| 3398 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3399 | SourceRange SR = AL.getSourceRange(); |
| 3400 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3401 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3402 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3403 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3405 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3406 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3407 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3408 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3409 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3410 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3411 | ArgType = Context.getCanonicalType(ArgType); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3412 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3413 | // Objective-C ARC: |
| 3414 | // If an explicitly-specified template argument type is a lifetime type |
| 3415 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3416 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3417 | ArgType->isObjCLifetimeType() && |
| 3418 | !ArgType.getObjCLifetime()) { |
| 3419 | Qualifiers Qs; |
| 3420 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3421 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3422 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3424 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3425 | return false; |
| 3426 | } |
| 3427 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3428 | /// \brief Substitute template arguments into the default template argument for |
| 3429 | /// the given template type parameter. |
| 3430 | /// |
| 3431 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3432 | /// the substitution. |
| 3433 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3434 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3435 | /// for. |
| 3436 | /// |
| 3437 | /// \param TemplateLoc the location of the template name that started the |
| 3438 | /// template-id we are checking. |
| 3439 | /// |
| 3440 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3441 | /// terminates the template-id. |
| 3442 | /// |
| 3443 | /// \param Param the template template parameter whose default we are |
| 3444 | /// substituting into. |
| 3445 | /// |
| 3446 | /// \param Converted the list of template arguments provided for template |
| 3447 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3448 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3449 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3450 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3451 | TemplateDecl *Template, |
| 3452 | SourceLocation TemplateLoc, |
| 3453 | SourceLocation RAngleLoc, |
| 3454 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3455 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3456 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3457 | |
| 3458 | // If the argument type is dependent, instantiate it now based |
| 3459 | // on the previously-computed template arguments. |
| 3460 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3461 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3462 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3463 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3464 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3465 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3466 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3467 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3468 | |
| 3469 | // Only substitute for the innermost template argument list. |
| 3470 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3471 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3472 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3473 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3474 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3475 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3476 | ArgType = |
| 3477 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3478 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | return ArgType; |
| 3482 | } |
| 3483 | |
| 3484 | /// \brief Substitute template arguments into the default template argument for |
| 3485 | /// the given non-type template parameter. |
| 3486 | /// |
| 3487 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3488 | /// the substitution. |
| 3489 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3490 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3491 | /// for. |
| 3492 | /// |
| 3493 | /// \param TemplateLoc the location of the template name that started the |
| 3494 | /// template-id we are checking. |
| 3495 | /// |
| 3496 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3497 | /// terminates the template-id. |
| 3498 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3499 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3500 | /// substituting into. |
| 3501 | /// |
| 3502 | /// \param Converted the list of template arguments provided for template |
| 3503 | /// parameters that precede \p Param in the template parameter list. |
| 3504 | /// |
| 3505 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3506 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3507 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3508 | TemplateDecl *Template, |
| 3509 | SourceLocation TemplateLoc, |
| 3510 | SourceLocation RAngleLoc, |
| 3511 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3512 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3513 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3514 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3515 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3516 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3517 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3518 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3519 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3520 | |
| 3521 | // Only substitute for the innermost template argument list. |
| 3522 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3523 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3524 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3525 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3526 | |
Faisal Vali | 48401eb | 2015-11-19 19:20:17 +0000 | [diff] [blame] | 3527 | EnterExpressionEvaluationContext ConstantEvaluated(SemaRef, |
| 3528 | Sema::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3529 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3532 | /// \brief Substitute template arguments into the default template argument for |
| 3533 | /// the given template template parameter. |
| 3534 | /// |
| 3535 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3536 | /// the substitution. |
| 3537 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3538 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3539 | /// for. |
| 3540 | /// |
| 3541 | /// \param TemplateLoc the location of the template name that started the |
| 3542 | /// template-id we are checking. |
| 3543 | /// |
| 3544 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3545 | /// terminates the template-id. |
| 3546 | /// |
| 3547 | /// \param Param the template template parameter whose default we are |
| 3548 | /// substituting into. |
| 3549 | /// |
| 3550 | /// \param Converted the list of template arguments provided for template |
| 3551 | /// parameters that precede \p Param in the template parameter list. |
| 3552 | /// |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3553 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3554 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3555 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3556 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3557 | static TemplateName |
| 3558 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3559 | TemplateDecl *Template, |
| 3560 | SourceLocation TemplateLoc, |
| 3561 | SourceLocation RAngleLoc, |
| 3562 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3563 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3564 | NestedNameSpecifierLoc &QualifierLoc) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3565 | Sema::InstantiatingTemplate Inst( |
| 3566 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 3567 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3568 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3569 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3570 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3571 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3572 | |
| 3573 | // Only substitute for the innermost template argument list. |
| 3574 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3575 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3576 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3577 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3578 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3579 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3580 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3581 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3582 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3583 | QualifierLoc = |
| 3584 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3585 | if (!QualifierLoc) |
| 3586 | return TemplateName(); |
| 3587 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3588 | |
| 3589 | return SemaRef.SubstTemplateName( |
| 3590 | QualifierLoc, |
| 3591 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3592 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3593 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3596 | /// \brief If the given template parameter has a default template |
| 3597 | /// argument, substitute into that default template argument and |
| 3598 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3599 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3600 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3601 | SourceLocation TemplateLoc, |
| 3602 | SourceLocation RAngleLoc, |
| 3603 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3604 | SmallVectorImpl<TemplateArgument> |
| 3605 | &Converted, |
| 3606 | bool &HasDefaultArg) { |
| 3607 | HasDefaultArg = false; |
| 3608 | |
| 3609 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3610 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3611 | return TemplateArgumentLoc(); |
| 3612 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3613 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3614 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3615 | TemplateLoc, |
| 3616 | RAngleLoc, |
| 3617 | TypeParm, |
| 3618 | Converted); |
| 3619 | if (DI) |
| 3620 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3621 | |
| 3622 | return TemplateArgumentLoc(); |
| 3623 | } |
| 3624 | |
| 3625 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3626 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3627 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3628 | return TemplateArgumentLoc(); |
| 3629 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3630 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3631 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3632 | TemplateLoc, |
| 3633 | RAngleLoc, |
| 3634 | NonTypeParm, |
| 3635 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3636 | if (Arg.isInvalid()) |
| 3637 | return TemplateArgumentLoc(); |
| 3638 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3639 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3640 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3641 | } |
| 3642 | |
| 3643 | TemplateTemplateParmDecl *TempTempParm |
| 3644 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3645 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3646 | return TemplateArgumentLoc(); |
| 3647 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3648 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3649 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3650 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3651 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3652 | RAngleLoc, |
| 3653 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3654 | Converted, |
| 3655 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3656 | if (TName.isNull()) |
| 3657 | return TemplateArgumentLoc(); |
| 3658 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3659 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3660 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3661 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3662 | } |
| 3663 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3664 | /// \brief Check that the given template argument corresponds to the given |
| 3665 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3666 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3667 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3668 | /// checked. |
| 3669 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3670 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3671 | /// |
| 3672 | /// \param Template The template in which the template argument resides. |
| 3673 | /// |
| 3674 | /// \param TemplateLoc The location of the template name for the template |
| 3675 | /// whose argument list we're matching. |
| 3676 | /// |
| 3677 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3678 | /// the template argument list. |
| 3679 | /// |
| 3680 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3681 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3682 | /// |
| 3683 | /// \param Converted The checked, converted argument will be added to the |
| 3684 | /// end of this small vector. |
| 3685 | /// |
| 3686 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3687 | /// explicitly written, deduced, etc. |
| 3688 | /// |
| 3689 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3690 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3691 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3692 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3693 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3694 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3695 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3696 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3697 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3698 | // Check template type parameters. |
| 3699 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3700 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3701 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3702 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3703 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3704 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3705 | // with the template arguments we've seen thus far. But if the |
| 3706 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3707 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3708 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3709 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3710 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3711 | if (NTTPType->isDependentType() && |
| 3712 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3713 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3714 | // Do substitution on the type of the non-type template parameter. |
| 3715 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3716 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3717 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3718 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3719 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3720 | |
| 3721 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3722 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3723 | NTTPType = SubstType(NTTPType, |
| 3724 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3725 | NTTP->getLocation(), |
| 3726 | NTTP->getDeclName()); |
| 3727 | // If that worked, check the non-type template parameter type |
| 3728 | // for validity. |
| 3729 | if (!NTTPType.isNull()) |
| 3730 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3731 | NTTP->getLocation()); |
| 3732 | if (NTTPType.isNull()) |
| 3733 | return true; |
| 3734 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3735 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3736 | switch (Arg.getArgument().getKind()) { |
| 3737 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3738 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3739 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3740 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3741 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3742 | ExprResult Res = |
| 3743 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3744 | Result, CTAK); |
| 3745 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3746 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3747 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3748 | // If the resulting expression is new, then use it in place of the |
| 3749 | // old expression in the template argument. |
| 3750 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3751 | TemplateArgument TA(Res.get()); |
| 3752 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3753 | } |
| 3754 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3755 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3756 | break; |
| 3757 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3758 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3759 | case TemplateArgument::Declaration: |
| 3760 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3761 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3762 | // We've already checked this template argument, so just copy |
| 3763 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3764 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3765 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3767 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3768 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3769 | // We were given a template template argument. It may not be ill-formed; |
| 3770 | // see below. |
| 3771 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3772 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3773 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3774 | // We have a template argument such as \c T::template X, which we |
| 3775 | // parsed as a template template argument. However, since we now |
| 3776 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3777 | // template name into an expression. |
| 3778 | |
| 3779 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3780 | Arg.getTemplateNameLoc()); |
| 3781 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3782 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3783 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3784 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3785 | // so it was provided with a template keyword. However, its source |
| 3786 | // location is not stored in the template argument structure. |
| 3787 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3788 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3789 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3790 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3792 | // If we parsed the template argument as a pack expansion, create a |
| 3793 | // pack expansion expression. |
| 3794 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3795 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3796 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3797 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3798 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3799 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3800 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3801 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3802 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3803 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3804 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3805 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3806 | break; |
| 3807 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3808 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3809 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3810 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3811 | // therefore cannot be a non-type template argument. |
| 3812 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3813 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3814 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3815 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3816 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3817 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3818 | case TemplateArgument::Type: { |
| 3819 | // We have a non-type template parameter but the template |
| 3820 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3821 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3822 | // C++ [temp.arg]p2: |
| 3823 | // In a template-argument, an ambiguity between a type-id and |
| 3824 | // an expression is resolved to a type-id, regardless of the |
| 3825 | // form of the corresponding template-parameter. |
| 3826 | // |
| 3827 | // We warn specifically about this case, since it can be rather |
| 3828 | // confusing for users. |
| 3829 | QualType T = Arg.getArgument().getAsType(); |
| 3830 | SourceRange SR = Arg.getSourceRange(); |
| 3831 | if (T->isFunctionType()) |
| 3832 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3833 | else |
| 3834 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3835 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3836 | return true; |
| 3837 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3838 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3839 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3840 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3841 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3842 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3843 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3847 | // Check template template parameters. |
| 3848 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3849 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3850 | // Substitute into the template parameter list of the template |
| 3851 | // template parameter, since previously-supplied template arguments |
| 3852 | // may appear within the template template parameter. |
| 3853 | { |
| 3854 | // Set up a template instantiation context. |
| 3855 | LocalInstantiationScope Scope(*this); |
| 3856 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3857 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3858 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3859 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3860 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3861 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3862 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3863 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3864 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3865 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3866 | if (!TempParm) |
| 3867 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3868 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3870 | switch (Arg.getArgument().getKind()) { |
| 3871 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3872 | llvm_unreachable("Should never see a NULL template argument here"); |
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 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3875 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3876 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3877 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3878 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3879 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3880 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3882 | case TemplateArgument::Expression: |
| 3883 | case TemplateArgument::Type: |
| 3884 | // We have a template template parameter but the template |
| 3885 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3886 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3887 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3888 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3889 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3890 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3891 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3892 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3893 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3894 | case TemplateArgument::NullPtr: |
| 3895 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3896 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3897 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3898 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3899 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3900 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3901 | return false; |
| 3902 | } |
| 3903 | |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3904 | /// \brief Diagnose an arity mismatch in the |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3905 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3906 | SourceLocation TemplateLoc, |
| 3907 | TemplateArgumentListInfo &TemplateArgs) { |
| 3908 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3909 | unsigned NumParams = Params->size(); |
| 3910 | unsigned NumArgs = TemplateArgs.size(); |
| 3911 | |
| 3912 | SourceRange Range; |
| 3913 | if (NumArgs > NumParams) |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 3914 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3915 | TemplateArgs.getRAngleLoc()); |
| 3916 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3917 | << (NumArgs > NumParams) |
| 3918 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3919 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3920 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3921 | << Template << Range; |
| 3922 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3923 | << Params->getSourceRange(); |
| 3924 | return true; |
| 3925 | } |
| 3926 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3927 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3928 | /// determine the number of parameters produced by that expansion. For instance: |
| 3929 | /// |
| 3930 | /// \code |
| 3931 | /// template<typename ...Ts> struct A { |
| 3932 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3933 | /// }; |
| 3934 | /// \endcode |
| 3935 | /// |
| 3936 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3937 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3938 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3939 | if (NonTypeTemplateParmDecl *NTTP |
| 3940 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3941 | if (NTTP->isExpandedParameterPack()) |
| 3942 | return NTTP->getNumExpansionTypes(); |
| 3943 | } |
| 3944 | |
| 3945 | if (TemplateTemplateParmDecl *TTP |
| 3946 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3947 | if (TTP->isExpandedParameterPack()) |
| 3948 | return TTP->getNumExpansionTemplateParameters(); |
| 3949 | } |
| 3950 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3951 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3954 | /// Diagnose a missing template argument. |
| 3955 | template<typename TemplateParmDecl> |
| 3956 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 3957 | TemplateDecl *TD, |
| 3958 | const TemplateParmDecl *D, |
| 3959 | TemplateArgumentListInfo &Args) { |
| 3960 | // Dig out the most recent declaration of the template parameter; there may be |
| 3961 | // declarations of the template that are more recent than TD. |
| 3962 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 3963 | ->getTemplateParameters() |
| 3964 | ->getParam(D->getIndex())); |
| 3965 | |
| 3966 | // If there's a default argument that's not visible, diagnose that we're |
| 3967 | // missing a module import. |
| 3968 | llvm::SmallVector<Module*, 8> Modules; |
| 3969 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 3970 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 3971 | D->getDefaultArgumentLoc(), Modules, |
| 3972 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3973 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3974 | return true; |
| 3975 | } |
| 3976 | |
| 3977 | // FIXME: If there's a more recent default argument that *is* visible, |
| 3978 | // diagnose that it was declared too late. |
| 3979 | |
| 3980 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 3981 | } |
| 3982 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3983 | /// \brief Check that the given template argument list is well-formed |
| 3984 | /// for specializing the given template. |
| 3985 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3986 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3987 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3988 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3989 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3990 | // Make a copy of the template arguments for processing. Only make the |
| 3991 | // changes at the end when successful in matching the arguments to the |
| 3992 | // template. |
| 3993 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3994 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3995 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3996 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3997 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3998 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4000 | // [...] The type and form of each template-argument specified in |
| 4001 | // a template-id shall match the type and form specified for the |
| 4002 | // corresponding parameter declared by the template in its |
| 4003 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4004 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4005 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4006 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4007 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4008 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4009 | ParamEnd = Params->end(); |
| 4010 | Param != ParamEnd; /* increment in loop */) { |
| 4011 | // If we have an expanded parameter pack, make sure we don't have too |
| 4012 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4013 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4014 | if (*Expansions == ArgumentPack.size()) { |
| 4015 | // We're done with this parameter pack. Pack up its arguments and add |
| 4016 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4017 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4018 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4019 | ArgumentPack.clear(); |
| 4020 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4021 | // This argument is assigned to the next parameter. |
| 4022 | ++Param; |
| 4023 | continue; |
| 4024 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 4025 | // Not enough arguments for this parameter pack. |
| 4026 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 4027 | << false |
| 4028 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 4029 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 4030 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 4031 | << Template; |
| 4032 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 4033 | << Params->getSourceRange(); |
| 4034 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4035 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4036 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4037 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4038 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4039 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4040 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4041 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4042 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4043 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4044 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4045 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4046 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4047 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 4048 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4049 | // 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] | 4050 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4051 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4052 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4053 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4054 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 4055 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 4056 | return true; |
| 4057 | } |
| 4058 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4059 | // We're now done with this argument. |
| 4060 | ++ArgIdx; |
| 4061 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4062 | if ((*Param)->isTemplateParameterPack()) { |
| 4063 | // The template parameter was a template parameter pack, so take the |
| 4064 | // deduced argument and place it on the argument pack. Note that we |
| 4065 | // stay on the same template parameter so that we can deduce more |
| 4066 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 4067 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4068 | } else { |
| 4069 | // Move to the next template parameter. |
| 4070 | ++Param; |
| 4071 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4072 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 4073 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 4074 | // the remaining arguments, because we don't know what parameters they'll |
| 4075 | // match up with. |
| 4076 | if (PackExpansionIntoNonPack) { |
| 4077 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4078 | // If we were part way through filling in an expanded parameter pack, |
| 4079 | // fall back to just producing individual arguments. |
| 4080 | Converted.insert(Converted.end(), |
| 4081 | ArgumentPack.begin(), ArgumentPack.end()); |
| 4082 | ArgumentPack.clear(); |
| 4083 | } |
| 4084 | |
| 4085 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4086 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4087 | ++ArgIdx; |
| 4088 | } |
| 4089 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4090 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4091 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4092 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4093 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4094 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4095 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 4096 | // If we're checking a partial template argument list, we're done. |
| 4097 | if (PartialTemplateArgs) { |
| 4098 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4099 | Converted.push_back( |
| 4100 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 4101 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4102 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4105 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4106 | // 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] | 4107 | if ((*Param)->isTemplateParameterPack()) { |
| 4108 | assert(!getExpandedPackSize(*Param) && |
| 4109 | "Should have dealt with this already"); |
| 4110 | |
| 4111 | // A non-expanded parameter pack before the end of the parameter list |
| 4112 | // only occurs for an ill-formed template parameter list, unless we've |
| 4113 | // got a partial argument list for a function template, so just bail out. |
| 4114 | if (Param + 1 != ParamEnd) |
| 4115 | return true; |
| 4116 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4117 | Converted.push_back( |
| 4118 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4119 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4120 | |
| 4121 | ++Param; |
| 4122 | continue; |
| 4123 | } |
| 4124 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4125 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4126 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4127 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4128 | // Retrieve the default template argument from the template |
| 4129 | // parameter. For each kind of template parameter, we substitute the |
| 4130 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4131 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4132 | // the default argument. |
| 4133 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4134 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4135 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 4136 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4137 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4138 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4139 | Template, |
| 4140 | TemplateLoc, |
| 4141 | RAngleLoc, |
| 4142 | TTP, |
| 4143 | Converted); |
| 4144 | if (!ArgType) |
| 4145 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4146 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4147 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 4148 | ArgType); |
| 4149 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4150 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4151 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4152 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 4153 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4154 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4155 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4156 | TemplateLoc, |
| 4157 | RAngleLoc, |
| 4158 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4159 | Converted); |
| 4160 | if (E.isInvalid()) |
| 4161 | return true; |
| 4162 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4163 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4164 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 4165 | } else { |
| 4166 | TemplateTemplateParmDecl *TempParm |
| 4167 | = cast<TemplateTemplateParmDecl>(*Param); |
| 4168 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4169 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4170 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 4171 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4172 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4173 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4174 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4175 | TemplateLoc, |
| 4176 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4177 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4178 | Converted, |
| 4179 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4180 | if (Name.isNull()) |
| 4181 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4182 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4183 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 4184 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4185 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4186 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4187 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4188 | // the default template argument. We're not actually instantiating a |
| 4189 | // template here, we just create this object to put a note into the |
| 4190 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4191 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 4192 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4193 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4194 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4195 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4196 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4197 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4198 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4199 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4200 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4201 | // Core issue 150 (assumed resolution): if this is a template template |
| 4202 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4203 | // template definition. |
| 4204 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4205 | NewArgs.addArgument(Arg); |
| 4206 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4207 | // Move to the next template parameter and argument. |
| 4208 | ++Param; |
| 4209 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4210 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4211 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4212 | // If we're performing a partial argument substitution, allow any trailing |
| 4213 | // pack expansions; they might be empty. This can happen even if |
| 4214 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 4215 | // still dependent). |
| 4216 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 4217 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4218 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 4219 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4222 | // If we have any leftover arguments, then there were too many arguments. |
| 4223 | // Complain and fail. |
| 4224 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4225 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 4226 | |
| 4227 | // No problems found with the new argument list, propagate changes back |
| 4228 | // to caller. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 4229 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4231 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4234 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4235 | class UnnamedLocalNoLinkageFinder |
| 4236 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4237 | { |
| 4238 | Sema &S; |
| 4239 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4240 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4241 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4242 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4243 | public: |
| 4244 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 4245 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4246 | bool Visit(QualType T) { |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame^] | 4247 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4248 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4250 | #define TYPE(Class, Parent) \ |
| 4251 | bool Visit##Class##Type(const Class##Type *); |
| 4252 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4253 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4254 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4255 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4256 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4257 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4258 | bool VisitTagDecl(const TagDecl *Tag); |
| 4259 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4260 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4261 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4262 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4264 | return false; |
| 4265 | } |
| 4266 | |
| 4267 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4268 | return Visit(T->getElementType()); |
| 4269 | } |
| 4270 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4271 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4272 | return Visit(T->getPointeeType()); |
| 4273 | } |
| 4274 | |
| 4275 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4277 | return Visit(T->getPointeeType()); |
| 4278 | } |
| 4279 | |
| 4280 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4282 | return Visit(T->getPointeeType()); |
| 4283 | } |
| 4284 | |
| 4285 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4286 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4287 | return Visit(T->getPointeeType()); |
| 4288 | } |
| 4289 | |
| 4290 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4291 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4292 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4293 | } |
| 4294 | |
| 4295 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4296 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4297 | return Visit(T->getElementType()); |
| 4298 | } |
| 4299 | |
| 4300 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4301 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4302 | return Visit(T->getElementType()); |
| 4303 | } |
| 4304 | |
| 4305 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4306 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4307 | return Visit(T->getElementType()); |
| 4308 | } |
| 4309 | |
| 4310 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4311 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4312 | return Visit(T->getElementType()); |
| 4313 | } |
| 4314 | |
| 4315 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4317 | return Visit(T->getElementType()); |
| 4318 | } |
| 4319 | |
| 4320 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4321 | return Visit(T->getElementType()); |
| 4322 | } |
| 4323 | |
| 4324 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4325 | return Visit(T->getElementType()); |
| 4326 | } |
| 4327 | |
| 4328 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4329 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4330 | for (const auto &A : T->param_types()) { |
| 4331 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4332 | return true; |
| 4333 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4334 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4335 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4339 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4340 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4344 | const UnresolvedUsingType*) { |
| 4345 | return false; |
| 4346 | } |
| 4347 | |
| 4348 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4349 | return false; |
| 4350 | } |
| 4351 | |
| 4352 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4353 | return Visit(T->getUnderlyingType()); |
| 4354 | } |
| 4355 | |
| 4356 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4357 | return false; |
| 4358 | } |
| 4359 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4360 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4361 | const UnaryTransformType*) { |
| 4362 | return false; |
| 4363 | } |
| 4364 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4365 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4366 | return Visit(T->getDeducedType()); |
| 4367 | } |
| 4368 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4369 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4370 | return VisitTagDecl(T->getDecl()); |
| 4371 | } |
| 4372 | |
| 4373 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4374 | return VisitTagDecl(T->getDecl()); |
| 4375 | } |
| 4376 | |
| 4377 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4378 | const TemplateTypeParmType*) { |
| 4379 | return false; |
| 4380 | } |
| 4381 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4382 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4383 | const SubstTemplateTypeParmPackType *) { |
| 4384 | return false; |
| 4385 | } |
| 4386 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4387 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4388 | const TemplateSpecializationType*) { |
| 4389 | return false; |
| 4390 | } |
| 4391 | |
| 4392 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4393 | const InjectedClassNameType* T) { |
| 4394 | return VisitTagDecl(T->getDecl()); |
| 4395 | } |
| 4396 | |
| 4397 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4398 | const DependentNameType* T) { |
| 4399 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4400 | } |
| 4401 | |
| 4402 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4403 | const DependentTemplateSpecializationType* T) { |
| 4404 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4405 | } |
| 4406 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4407 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4408 | const PackExpansionType* T) { |
| 4409 | return Visit(T->getPattern()); |
| 4410 | } |
| 4411 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4412 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4413 | return false; |
| 4414 | } |
| 4415 | |
| 4416 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4417 | const ObjCInterfaceType *) { |
| 4418 | return false; |
| 4419 | } |
| 4420 | |
| 4421 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4422 | const ObjCObjectPointerType *) { |
| 4423 | return false; |
| 4424 | } |
| 4425 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4426 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4427 | return Visit(T->getValueType()); |
| 4428 | } |
| 4429 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4430 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 4431 | return false; |
| 4432 | } |
| 4433 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4434 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4435 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4436 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4437 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4438 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4439 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4440 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4441 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4444 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4445 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4446 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4447 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4448 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4449 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4450 | return true; |
| 4451 | } |
| 4452 | |
| 4453 | return false; |
| 4454 | } |
| 4455 | |
| 4456 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4457 | NestedNameSpecifier *NNS) { |
| 4458 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4459 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4460 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4461 | switch (NNS->getKind()) { |
| 4462 | case NestedNameSpecifier::Identifier: |
| 4463 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4464 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4465 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4466 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4467 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4468 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4469 | case NestedNameSpecifier::TypeSpec: |
| 4470 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4471 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4472 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4473 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4476 | /// \brief Check a template argument against its corresponding |
| 4477 | /// template type parameter. |
| 4478 | /// |
| 4479 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4480 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4481 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4482 | TypeSourceInfo *ArgInfo) { |
| 4483 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4484 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4485 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4486 | |
| 4487 | if (Arg->isVariablyModifiedType()) { |
| 4488 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4489 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4490 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4493 | // C++03 [temp.arg.type]p2: |
| 4494 | // A local type, a type with no linkage, an unnamed type or a type |
| 4495 | // compounded from any of these types shall not be used as a |
| 4496 | // template-argument for a template type-parameter. |
| 4497 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4498 | // 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] | 4499 | // a warning. |
Daniel Jasper | 5cad685 | 2017-01-02 22:55:45 +0000 | [diff] [blame^] | 4500 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4501 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4502 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4503 | } |
| 4504 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4505 | return false; |
| 4506 | } |
| 4507 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4508 | enum NullPointerValueKind { |
| 4509 | NPV_NotNullPointer, |
| 4510 | NPV_NullPointer, |
| 4511 | NPV_Error |
| 4512 | }; |
| 4513 | |
| 4514 | /// \brief Determine whether the given template argument is a null pointer |
| 4515 | /// value of the appropriate type. |
| 4516 | static NullPointerValueKind |
| 4517 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4518 | QualType ParamType, Expr *Arg) { |
| 4519 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4520 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4521 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4522 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 4523 | llvm_unreachable( |
| 4524 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4525 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4526 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4527 | return NPV_NotNullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4528 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4529 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4530 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4531 | if (ArgRV.isInvalid()) |
| 4532 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4533 | Arg = ArgRV.get(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4534 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4535 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4536 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4537 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4538 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4539 | EvalResult.HasSideEffects) { |
| 4540 | SourceLocation DiagLoc = Arg->getExprLoc(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4541 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4542 | // If our only note is the usual "invalid subexpression" note, just point |
| 4543 | // the caret at its location rather than producing an essentially |
| 4544 | // redundant note. |
| 4545 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4546 | diag::note_invalid_subexpr_in_const_expr) { |
| 4547 | DiagLoc = Notes[0].first; |
| 4548 | Notes.clear(); |
| 4549 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4550 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4551 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4552 | << Arg->getType() << Arg->getSourceRange(); |
| 4553 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4554 | S.Diag(Notes[I].first, Notes[I].second); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4555 | |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4556 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4557 | return NPV_Error; |
| 4558 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4559 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4560 | // C++11 [temp.arg.nontype]p1: |
| 4561 | // - an address constant expression of type std::nullptr_t |
| 4562 | if (Arg->getType()->isNullPtrType()) |
| 4563 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4564 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4565 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4566 | // - a constant expression that evaluates to a null member pointer value |
| 4567 | // (4.11); or |
| 4568 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4569 | (EvalResult.Val.isMemberPointer() && |
| 4570 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4571 | // If our expression has an appropriate type, we've succeeded. |
| 4572 | bool ObjCLifetimeConversion; |
| 4573 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4574 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4575 | ObjCLifetimeConversion)) |
| 4576 | return NPV_NullPointer; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4577 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4578 | // The types didn't match, but we know we got a null pointer; complain, |
| 4579 | // then recover as if the types were correct. |
| 4580 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4581 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4582 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4583 | return NPV_NullPointer; |
| 4584 | } |
| 4585 | |
| 4586 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4587 | // constant, suggest a cast to the appropriate type. |
| 4588 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4589 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4590 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4591 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4592 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4593 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4594 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4595 | return NPV_NullPointer; |
| 4596 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 4597 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4598 | // FIXME: If we ever want to support general, address-constant expressions |
| 4599 | // as non-type template arguments, we should return the ExprResult here to |
| 4600 | // be interpreted by the caller. |
| 4601 | return NPV_NotNullPointer; |
| 4602 | } |
| 4603 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4604 | /// \brief Checks whether the given template argument is compatible with its |
| 4605 | /// template parameter. |
| 4606 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4607 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4608 | Expr *Arg, QualType ArgType) { |
| 4609 | bool ObjCLifetimeConversion; |
| 4610 | if (ParamType->isPointerType() && |
| 4611 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4612 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4613 | ObjCLifetimeConversion)) { |
| 4614 | // For pointer-to-object types, qualification conversions are |
| 4615 | // permitted. |
| 4616 | } else { |
| 4617 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4618 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4619 | // C++ [temp.arg.nontype]p5b3: |
| 4620 | // For a non-type template-parameter of type reference to |
| 4621 | // object, no conversions apply. The type referred to by the |
| 4622 | // reference may be more cv-qualified than the (otherwise |
| 4623 | // identical) type of the template- argument. The |
| 4624 | // template-parameter is bound directly to the |
| 4625 | // template-argument, which shall be an lvalue. |
| 4626 | |
| 4627 | // FIXME: Other qualifiers? |
| 4628 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4629 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4630 | |
| 4631 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4632 | S.Diag(Arg->getLocStart(), |
| 4633 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4634 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4635 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4636 | return true; |
| 4637 | } |
| 4638 | } |
| 4639 | } |
| 4640 | |
| 4641 | // At this point, the template argument refers to an object or |
| 4642 | // function with external linkage. We now need to check whether the |
| 4643 | // argument and parameter types are compatible. |
| 4644 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4645 | ParamType.getNonReferenceType())) { |
| 4646 | // We can't perform this conversion or binding. |
| 4647 | if (ParamType->isReferenceType()) |
| 4648 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4649 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4650 | else |
| 4651 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4652 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4653 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4654 | return true; |
| 4655 | } |
| 4656 | } |
| 4657 | |
| 4658 | return false; |
| 4659 | } |
| 4660 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4661 | /// \brief Checks whether the given template argument is the address |
| 4662 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4664 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4665 | NonTypeTemplateParmDecl *Param, |
| 4666 | QualType ParamType, |
| 4667 | Expr *ArgIn, |
| 4668 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4669 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4670 | Expr *Arg = ArgIn; |
| 4671 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4672 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4673 | bool AddressTaken = false; |
| 4674 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4675 | if (S.getLangOpts().MicrosoftExt) { |
| 4676 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4677 | // dereference and address-of operators. |
| 4678 | Arg = Arg->IgnoreParenCasts(); |
| 4679 | |
| 4680 | bool ExtWarnMSTemplateArg = false; |
| 4681 | UnaryOperatorKind FirstOpKind; |
| 4682 | SourceLocation FirstOpLoc; |
| 4683 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4684 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4685 | if (UnOpKind == UO_Deref) |
| 4686 | ExtWarnMSTemplateArg = true; |
| 4687 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4688 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4689 | if (!AddrOpLoc.isValid()) { |
| 4690 | FirstOpKind = UnOpKind; |
| 4691 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4692 | } |
| 4693 | } else |
| 4694 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4695 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4696 | if (FirstOpLoc.isValid()) { |
| 4697 | if (ExtWarnMSTemplateArg) |
| 4698 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4699 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4700 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4701 | if (FirstOpKind == UO_AddrOf) |
| 4702 | AddressTaken = true; |
| 4703 | else if (Arg->getType()->isPointerType()) { |
| 4704 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4705 | // constant expression. |
| 4706 | assert(FirstOpKind == UO_Deref); |
| 4707 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4708 | << Arg->getSourceRange(); |
| 4709 | } |
| 4710 | } |
| 4711 | } else { |
| 4712 | // See through any implicit casts we added to fix the type. |
| 4713 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4714 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4715 | // C++ [temp.arg.nontype]p1: |
| 4716 | // |
| 4717 | // A template-argument for a non-type, non-template |
| 4718 | // template-parameter shall be one of: [...] |
| 4719 | // |
| 4720 | // -- the address of an object or function with external |
| 4721 | // linkage, including function templates and function |
| 4722 | // template-ids but excluding non-static class members, |
| 4723 | // expressed as & id-expression where the & is optional if |
| 4724 | // the name refers to a function or array, or if the |
| 4725 | // corresponding template-parameter is a reference; or |
| 4726 | |
| 4727 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4728 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4729 | bool ExtraParens = false; |
| 4730 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4731 | if (!Invalid && !ExtraParens) { |
| 4732 | S.Diag(Arg->getLocStart(), |
| 4733 | S.getLangOpts().CPlusPlus11 |
| 4734 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4735 | : diag::ext_template_arg_extra_parens) |
| 4736 | << Arg->getSourceRange(); |
| 4737 | ExtraParens = true; |
| 4738 | } |
| 4739 | |
| 4740 | Arg = Parens->getSubExpr(); |
| 4741 | } |
| 4742 | |
| 4743 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4744 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4745 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4746 | |
| 4747 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4748 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4749 | Arg = UnOp->getSubExpr(); |
| 4750 | AddressTaken = true; |
| 4751 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4752 | } |
| 4753 | } |
| 4754 | |
| 4755 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4756 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4757 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4758 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4759 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4760 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4761 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4762 | |
| 4763 | // If our parameter has pointer type, check for a null template value. |
| 4764 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4765 | NullPointerValueKind NPV; |
| 4766 | // dllimport'd entities aren't constant but are available inside of template |
| 4767 | // arguments. |
| 4768 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4769 | NPV = NPV_NotNullPointer; |
| 4770 | else |
| 4771 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4772 | switch (NPV) { |
| 4773 | case NPV_NullPointer: |
| 4774 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4775 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4776 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4777 | return false; |
| 4778 | |
| 4779 | case NPV_Error: |
| 4780 | return true; |
| 4781 | |
| 4782 | case NPV_NotNullPointer: |
| 4783 | break; |
| 4784 | } |
| 4785 | } |
| 4786 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4787 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4788 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4789 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4790 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4791 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4792 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4793 | |
| 4794 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4795 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4796 | ArgIn, Arg, ArgType)) |
| 4797 | return true; |
| 4798 | |
| 4799 | Converted = TemplateArgument(ArgIn); |
| 4800 | return false; |
| 4801 | } |
| 4802 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4803 | if (!DRE) { |
| 4804 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4805 | << Arg->getSourceRange(); |
| 4806 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4807 | return true; |
| 4808 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4809 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4810 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4811 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4812 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4813 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4814 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4815 | return true; |
| 4816 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4817 | |
| 4818 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4819 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4820 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4821 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4822 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4823 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4824 | return true; |
| 4825 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4826 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4828 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4829 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4830 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4831 | // A non-type template argument must refer to an object or function. |
| 4832 | if (!Func && !Var) { |
| 4833 | // We found something, but we don't know specifically what it is. |
| 4834 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4835 | << Arg->getSourceRange(); |
| 4836 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4837 | return true; |
| 4838 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4839 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4840 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4841 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4842 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4843 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4844 | diag::ext_template_arg_object_internal) |
| 4845 | << !Func << Entity << Arg->getSourceRange(); |
| 4846 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4847 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4848 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4849 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4850 | << !Func << Entity << Arg->getSourceRange(); |
| 4851 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4852 | << !Func; |
| 4853 | return true; |
| 4854 | } |
| 4855 | |
| 4856 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4857 | // If the template parameter has pointer type, the function decays. |
| 4858 | if (ParamType->isPointerType() && !AddressTaken) |
| 4859 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4860 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4861 | // If we originally had an address-of operator, but the |
| 4862 | // parameter has reference type, complain and (if things look |
| 4863 | // like they will work) drop the address-of operator. |
| 4864 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4865 | ParamType.getNonReferenceType())) { |
| 4866 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4867 | << ParamType; |
| 4868 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4869 | return true; |
| 4870 | } |
| 4871 | |
| 4872 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4873 | << ParamType |
| 4874 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4875 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4876 | |
| 4877 | ArgType = Func->getType(); |
| 4878 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4879 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4880 | // A value of reference type is not an object. |
| 4881 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4882 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4883 | diag::err_template_arg_reference_var) |
| 4884 | << Var->getType() << Arg->getSourceRange(); |
| 4885 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4886 | return true; |
| 4887 | } |
| 4888 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4889 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4890 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4891 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4892 | << Arg->getSourceRange(); |
| 4893 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4894 | return true; |
| 4895 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4896 | |
| 4897 | // If the template parameter has pointer type, we must have taken |
| 4898 | // the address of this object. |
| 4899 | if (ParamType->isReferenceType()) { |
| 4900 | if (AddressTaken) { |
| 4901 | // If we originally had an address-of operator, but the |
| 4902 | // parameter has reference type, complain and (if things look |
| 4903 | // like they will work) drop the address-of operator. |
| 4904 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4905 | ParamType.getNonReferenceType())) { |
| 4906 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4907 | << ParamType; |
| 4908 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4909 | return true; |
| 4910 | } |
| 4911 | |
| 4912 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4913 | << ParamType |
| 4914 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4915 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4916 | |
| 4917 | ArgType = Var->getType(); |
| 4918 | } |
| 4919 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4920 | if (Var->getType()->isArrayType()) { |
| 4921 | // Array-to-pointer decay. |
| 4922 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4923 | } else { |
| 4924 | // If the template parameter has pointer type but the address of |
| 4925 | // this object was not taken, complain and (possibly) recover by |
| 4926 | // taking the address of the entity. |
| 4927 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4928 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4929 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4930 | << ParamType; |
| 4931 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4932 | return true; |
| 4933 | } |
| 4934 | |
| 4935 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4936 | << ParamType |
| 4937 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4938 | |
| 4939 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4940 | } |
| 4941 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4942 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4944 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4945 | Arg, ArgType)) |
| 4946 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4947 | |
| 4948 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4949 | Converted = |
| 4950 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4951 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4952 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | /// \brief Checks whether the given template argument is a pointer to |
| 4956 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4957 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4958 | NonTypeTemplateParmDecl *Param, |
| 4959 | QualType ParamType, |
| 4960 | Expr *&ResultArg, |
| 4961 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4962 | bool Invalid = false; |
| 4963 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4964 | // Check for a null pointer value. |
| 4965 | Expr *Arg = ResultArg; |
| 4966 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4967 | case NPV_Error: |
| 4968 | return true; |
| 4969 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4970 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4971 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4972 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4973 | return false; |
| 4974 | case NPV_NotNullPointer: |
| 4975 | break; |
| 4976 | } |
| 4977 | |
| 4978 | bool ObjCLifetimeConversion; |
| 4979 | if (S.IsQualificationConversion(Arg->getType(), |
| 4980 | ParamType.getNonReferenceType(), |
| 4981 | false, ObjCLifetimeConversion)) { |
| 4982 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4983 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4984 | ResultArg = Arg; |
| 4985 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4986 | ParamType.getNonReferenceType())) { |
| 4987 | // We can't perform this conversion. |
| 4988 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4989 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4990 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4991 | return true; |
| 4992 | } |
| 4993 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4994 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4995 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4996 | Arg = Cast->getSubExpr(); |
| 4997 | |
| 4998 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4999 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5000 | // A template-argument for a non-type, non-template |
| 5001 | // template-parameter shall be one of: [...] |
| 5002 | // |
| 5003 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5004 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5005 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 5006 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 5007 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 5008 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5009 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5010 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5011 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5012 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5013 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 5014 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5015 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 5016 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | Arg = Parens->getSubExpr(); |
| 5020 | } |
| 5021 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 5022 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5023 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5024 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5025 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5026 | // A pointer-to-member constant written &Class::member. |
| 5027 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5028 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5029 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 5030 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5031 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5032 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5033 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5034 | // A constant of pointer-to-member type. |
| 5035 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 5036 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 5037 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 5038 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5039 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5040 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5041 | } else { |
| 5042 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5043 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5044 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5045 | return Invalid; |
| 5046 | } |
| 5047 | } |
| 5048 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5049 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5050 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 5051 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5052 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5053 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5054 | return S.Diag(Arg->getLocStart(), |
| 5055 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5056 | << Arg->getSourceRange(); |
| 5057 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 5058 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 5059 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 5060 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5061 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 5062 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5063 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 5064 | "Only non-static member pointers can make it here"); |
| 5065 | |
| 5066 | // Okay: this is the address of a non-static member, and therefore |
| 5067 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5068 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5069 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5070 | } else { |
| 5071 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 5072 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5073 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5074 | return Invalid; |
| 5075 | } |
| 5076 | |
| 5077 | // 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] | 5078 | S.Diag(Arg->getLocStart(), |
| 5079 | diag::err_template_arg_not_pointer_to_member_form) |
| 5080 | << Arg->getSourceRange(); |
| 5081 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 5082 | return true; |
| 5083 | } |
| 5084 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5085 | /// \brief Check a template argument against its corresponding |
| 5086 | /// non-type template parameter. |
| 5087 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 5088 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5089 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5090 | /// returns the converted template argument. \p ParamType is the |
| 5091 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5092 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5093 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5094 | TemplateArgument &Converted, |
| 5095 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5096 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5097 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5098 | // If the parameter type somehow involves auto, deduce the type now. |
| 5099 | if (getLangOpts().CPlusPlus1z && ParamType->isUndeducedType()) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5100 | // When checking a deduced template argument, deduce from its type even if |
| 5101 | // the type is dependent, in order to check the types of non-type template |
| 5102 | // arguments line up properly in partial ordering. |
| 5103 | Optional<unsigned> Depth; |
| 5104 | if (CTAK != CTAK_Specified) |
| 5105 | Depth = Param->getDepth() + 1; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5106 | if (DeduceAutoType( |
| 5107 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5108 | Arg, ParamType, Depth) == DAR_Failed) { |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5109 | Diag(Arg->getExprLoc(), |
| 5110 | diag::err_non_type_template_parm_type_deduction_failure) |
| 5111 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 5112 | << Arg->getSourceRange(); |
| 5113 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5114 | return ExprError(); |
| 5115 | } |
| 5116 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 5117 | // an error. The error message normally references the parameter |
| 5118 | // declaration, but here we'll pass the argument location because that's |
| 5119 | // where the parameter type is deduced. |
| 5120 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 5121 | if (ParamType.isNull()) { |
| 5122 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5123 | return ExprError(); |
| 5124 | } |
| 5125 | } |
| 5126 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5127 | // We should have already dropped all cv-qualifiers by now. |
| 5128 | assert(!ParamType.hasQualifiers() && |
| 5129 | "non-type template parameter type cannot be qualified"); |
| 5130 | |
| 5131 | if (CTAK == CTAK_Deduced && |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 5132 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5133 | Arg->getType())) { |
Richard Smith | 4f9b3f4 | 2016-12-26 22:28:29 +0000 | [diff] [blame] | 5134 | // C++ [temp.deduct.type]p17: (DR1770) |
| 5135 | // If P has a form that contains <i>, and if the type of i differs from |
| 5136 | // the type of the corresponding template parameter of the template named |
| 5137 | // by the enclosing simple-template-id, deduction fails. |
| 5138 | // |
| 5139 | // Note that CTAK will be CTAK_DeducedFromArrayBound if the form was [i] |
| 5140 | // rather than <i>. |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 5141 | // |
| 5142 | // FIXME: We interpret the 'i' here as referring to the expression |
| 5143 | // denoting the non-type template parameter rather than the parameter |
| 5144 | // itself, and so strip off references before comparing types. It's |
| 5145 | // not clear how this is supposed to work for references. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5146 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5147 | << Arg->getType() |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5148 | << ParamType.getUnqualifiedType(); |
| 5149 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5150 | return ExprError(); |
| 5151 | } |
| 5152 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5153 | // If either the parameter has a dependent type or the argument is |
| 5154 | // type-dependent, there's nothing we can check now. |
| 5155 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 5156 | // FIXME: Produce a cloned, canonical expression? |
| 5157 | Converted = TemplateArgument(Arg); |
| 5158 | return Arg; |
| 5159 | } |
| 5160 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5161 | if (getLangOpts().CPlusPlus1z) { |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5162 | // C++1z [temp.arg.nontype]p1: |
| 5163 | // A template-argument for a non-type template parameter shall be |
| 5164 | // a converted constant expression of the type of the template-parameter. |
| 5165 | APValue Value; |
| 5166 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 5167 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 5168 | if (ArgResult.isInvalid()) |
| 5169 | return ExprError(); |
| 5170 | |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5171 | // For a value-dependent argument, CheckConvertedConstantExpression is |
| 5172 | // permitted (and expected) to be unable to determine a value. |
| 5173 | if (ArgResult.get()->isValueDependent()) { |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 5174 | Converted = TemplateArgument(ArgResult.get()); |
| 5175 | return ArgResult; |
Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5178 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 5179 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5180 | // Convert the APValue to a TemplateArgument. |
| 5181 | switch (Value.getKind()) { |
| 5182 | case APValue::Uninitialized: |
| 5183 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5184 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5185 | break; |
| 5186 | case APValue::Int: |
| 5187 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5188 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5189 | break; |
| 5190 | case APValue::MemberPointer: { |
| 5191 | assert(ParamType->isMemberPointerType()); |
| 5192 | |
| 5193 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 5194 | if (!Value.getMemberPointerPath().empty()) { |
| 5195 | Diag(Arg->getLocStart(), |
| 5196 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 5197 | << Value.getMemberPointerDecl() << ParamType |
| 5198 | << Arg->getSourceRange(); |
| 5199 | return ExprError(); |
| 5200 | } |
| 5201 | |
| 5202 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5203 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5204 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5205 | break; |
| 5206 | } |
| 5207 | case APValue::LValue: { |
| 5208 | // For a non-type template-parameter of pointer or reference type, |
| 5209 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5210 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 5211 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5212 | // -- a temporary object |
| 5213 | // -- a string literal |
| 5214 | // -- the result of a typeid expression, or |
| 5215 | // -- a predefind __func__ variable |
| 5216 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 5217 | if (isa<CXXUuidofExpr>(E)) { |
| 5218 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 5219 | break; |
| 5220 | } |
| 5221 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5222 | << Arg->getSourceRange(); |
| 5223 | return ExprError(); |
| 5224 | } |
| 5225 | auto *VD = const_cast<ValueDecl *>( |
| 5226 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 5227 | // -- a subobject |
| 5228 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 5229 | VD && VD->getType()->isArrayType() && |
| 5230 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 5231 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 5232 | // Per defect report (no number yet): |
| 5233 | // ... other than a pointer to the first element of a complete array |
| 5234 | // object. |
| 5235 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 5236 | Value.isLValueOnePastTheEnd()) { |
| 5237 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 5238 | << Value.getAsString(Context, ParamType); |
| 5239 | return ExprError(); |
| 5240 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5241 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5242 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5243 | assert((!VD || !ParamType->isNullPtrType()) && |
| 5244 | "non-null value of type nullptr_t?"); |
| 5245 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5246 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5247 | break; |
| 5248 | } |
| 5249 | case APValue::AddrLabelDiff: |
| 5250 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 5251 | case APValue::Float: |
| 5252 | case APValue::ComplexInt: |
| 5253 | case APValue::ComplexFloat: |
| 5254 | case APValue::Vector: |
| 5255 | case APValue::Array: |
| 5256 | case APValue::Struct: |
| 5257 | case APValue::Union: |
| 5258 | llvm_unreachable("invalid kind for template argument"); |
| 5259 | } |
| 5260 | |
| 5261 | return ArgResult.get(); |
| 5262 | } |
| 5263 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5264 | // C++ [temp.arg.nontype]p5: |
| 5265 | // The following conversions are performed on each expression used |
| 5266 | // as a non-type template-argument. If a non-type |
| 5267 | // template-argument cannot be converted to the type of the |
| 5268 | // corresponding template-parameter then the program is |
| 5269 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5270 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5271 | // C++11: |
| 5272 | // -- for a non-type template-parameter of integral or |
| 5273 | // enumeration type, conversions permitted in a converted |
| 5274 | // constant expression are applied. |
| 5275 | // |
| 5276 | // C++98: |
| 5277 | // -- for a non-type template-parameter of integral or |
| 5278 | // enumeration type, integral promotions (4.5) and integral |
| 5279 | // conversions (4.7) are applied. |
| 5280 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5281 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5282 | // C++ [temp.arg.nontype]p1: |
| 5283 | // A template-argument for a non-type, non-template template-parameter |
| 5284 | // shall be one of: |
| 5285 | // |
| 5286 | // -- for a non-type template-parameter of integral or enumeration |
| 5287 | // type, a converted constant expression of the type of the |
| 5288 | // template-parameter; or |
| 5289 | llvm::APSInt Value; |
| 5290 | ExprResult ArgResult = |
| 5291 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5292 | CCEK_TemplateArg); |
| 5293 | if (ArgResult.isInvalid()) |
| 5294 | return ExprError(); |
| 5295 | |
Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 5296 | // We can't check arbitrary value-dependent arguments. |
| 5297 | if (ArgResult.get()->isValueDependent()) { |
| 5298 | Converted = TemplateArgument(ArgResult.get()); |
| 5299 | return ArgResult; |
| 5300 | } |
| 5301 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5302 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5303 | // always a no-op, except when the parameter type is bool. In |
| 5304 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5305 | QualType IntegerType = ParamType; |
| 5306 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5307 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5308 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5309 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5310 | Converted = TemplateArgument(Context, Value, |
| 5311 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5312 | return ArgResult; |
| 5313 | } |
| 5314 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5315 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5316 | if (ArgResult.isInvalid()) |
| 5317 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5318 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5319 | |
| 5320 | QualType ArgType = Arg->getType(); |
| 5321 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5322 | // C++ [temp.arg.nontype]p1: |
| 5323 | // A template-argument for a non-type, non-template |
| 5324 | // template-parameter shall be one of: |
| 5325 | // |
| 5326 | // -- an integral constant-expression of integral or enumeration |
| 5327 | // type; or |
| 5328 | // -- the name of a non-type template-parameter; or |
| 5329 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5330 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5331 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5332 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5333 | diag::err_template_arg_not_integral_or_enumeral) |
| 5334 | << ArgType << Arg->getSourceRange(); |
| 5335 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5336 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5337 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5338 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5339 | QualType T; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5340 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5341 | public: |
| 5342 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5343 | |
| 5344 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5345 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5346 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5347 | } |
| 5348 | } Diagnoser(ArgType); |
| 5349 | |
| 5350 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5351 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5352 | if (!Arg) |
| 5353 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5356 | // From here on out, all we care about is the unqualified form |
| 5357 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5358 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5359 | |
| 5360 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5361 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5362 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5363 | } else if (ParamType->isBooleanType()) { |
| 5364 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5365 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5366 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5367 | !ParamType->isEnumeralType()) { |
| 5368 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5369 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5370 | } else { |
| 5371 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5372 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5373 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5374 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5375 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5376 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5379 | // Add the value of this argument to the list of converted |
| 5380 | // arguments. We use the bitwidth and signedness of the template |
| 5381 | // parameter. |
| 5382 | if (Arg->isValueDependent()) { |
| 5383 | // The argument is value-dependent. Create a new |
| 5384 | // TemplateArgument with the converted expression. |
| 5385 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5386 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5389 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5390 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5391 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5392 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5393 | if (ParamType->isBooleanType()) { |
| 5394 | // Value must be zero or one. |
| 5395 | Value = Value != 0; |
| 5396 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5397 | if (Value.getBitWidth() != AllowedBits) |
| 5398 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5399 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5400 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5401 | llvm::APSInt OldValue = Value; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5402 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5403 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5404 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5405 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5406 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5407 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5408 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5409 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5410 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5411 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5412 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5413 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5414 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5415 | << Arg->getSourceRange(); |
| 5416 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5417 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5418 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5419 | // Complain if we overflowed the template parameter's type. |
| 5420 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5421 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5422 | RequiredBits = OldValue.getActiveBits(); |
| 5423 | else if (OldValue.isUnsigned()) |
| 5424 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5425 | else |
| 5426 | RequiredBits = OldValue.getMinSignedBits(); |
| 5427 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5428 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5429 | diag::warn_template_arg_too_large) |
| 5430 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5431 | << Arg->getSourceRange(); |
| 5432 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5433 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5434 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5435 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5436 | Converted = TemplateArgument(Context, Value, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5437 | ParamType->isEnumeralType() |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5438 | ? Context.getCanonicalType(ParamType) |
| 5439 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5440 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5441 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5442 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5443 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5444 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5445 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5446 | // Handle pointer-to-function, reference-to-function, and |
| 5447 | // pointer-to-member-function all in (roughly) the same way. |
| 5448 | if (// -- For a non-type template-parameter of type pointer to |
| 5449 | // function, only the function-to-pointer conversion (4.3) is |
| 5450 | // applied. If the template-argument represents a set of |
| 5451 | // overloaded functions (or a pointer to such), the matching |
| 5452 | // function is selected from the set (13.4). |
| 5453 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5454 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5455 | // -- For a non-type template-parameter of type reference to |
| 5456 | // function, no conversions apply. If the template-argument |
| 5457 | // represents a set of overloaded functions, the matching |
| 5458 | // function is selected from the set (13.4). |
| 5459 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5460 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5461 | // -- For a non-type template-parameter of type pointer to |
| 5462 | // member function, no conversions apply. If the |
| 5463 | // template-argument represents a set of overloaded member |
| 5464 | // functions, the matching member function is selected from |
| 5465 | // the set (13.4). |
| 5466 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5467 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5468 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5469 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5470 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5471 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5472 | true, |
| 5473 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5474 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5475 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5476 | |
| 5477 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5478 | ArgType = Arg->getType(); |
| 5479 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5480 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5481 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5482 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5483 | if (!ParamType->isMemberPointerType()) { |
| 5484 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5485 | ParamType, |
| 5486 | Arg, Converted)) |
| 5487 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5488 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5489 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5490 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5491 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5492 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5493 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5494 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5497 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5498 | // -- for a non-type template-parameter of type pointer to |
| 5499 | // object, qualification conversions (4.4) and the |
| 5500 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5501 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5502 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5503 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5504 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5505 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5506 | ParamType, |
| 5507 | Arg, Converted)) |
| 5508 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5509 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5510 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5511 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5512 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5513 | // -- For a non-type template-parameter of type reference to |
| 5514 | // object, no conversions apply. The type referred to by the |
| 5515 | // reference may be more cv-qualified than the (otherwise |
| 5516 | // identical) type of the template-argument. The |
| 5517 | // template-parameter is bound directly to the |
| 5518 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5519 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5520 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5521 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5522 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5523 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5524 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5525 | true, |
| 5526 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5527 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5528 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5529 | |
| 5530 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5531 | ArgType = Arg->getType(); |
| 5532 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5533 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5534 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5535 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5536 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5537 | ParamType, |
| 5538 | Arg, Converted)) |
| 5539 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5540 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5541 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5542 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5543 | // Deal with parameters of type std::nullptr_t. |
| 5544 | if (ParamType->isNullPtrType()) { |
| 5545 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5546 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5547 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5548 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5549 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5550 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5551 | case NPV_NotNullPointer: |
| 5552 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5553 | << Arg->getType() << ParamType; |
| 5554 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5555 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5556 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5557 | case NPV_Error: |
| 5558 | return ExprError(); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5559 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5560 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5561 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5562 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5563 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5564 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5565 | } |
| 5566 | } |
| 5567 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5568 | // -- For a non-type template-parameter of type pointer to data |
| 5569 | // member, qualification conversions (4.4) are applied. |
| 5570 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5571 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5572 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5573 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5574 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5575 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5578 | static void DiagnoseTemplateParameterListArityMismatch( |
| 5579 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 5580 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 5581 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5582 | /// \brief Check a template argument against its corresponding |
| 5583 | /// template template parameter. |
| 5584 | /// |
| 5585 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5586 | /// It returns true if an error occurred, and false otherwise. |
| 5587 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5588 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5589 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5590 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5591 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5592 | if (!Template) { |
| 5593 | // Any dependent template name is fine. |
| 5594 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5595 | return false; |
| 5596 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5597 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5598 | if (Template->isInvalidDecl()) |
| 5599 | return true; |
| 5600 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5601 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5602 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5603 | // the name of a class template or an alias template, expressed as an |
| 5604 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5605 | // primary class templates are considered when matching the |
| 5606 | // template template argument with the corresponding parameter; |
| 5607 | // partial specializations are not considered even if their |
| 5608 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5609 | // |
| 5610 | // Note that we also allow template template parameters here, which |
| 5611 | // will happen when we are dealing with, e.g., class template |
| 5612 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5613 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5614 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5615 | !isa<TypeAliasTemplateDecl>(Template) && |
| 5616 | !isa<BuiltinTemplateDecl>(Template)) { |
| 5617 | assert(isa<FunctionTemplateDecl>(Template) && |
| 5618 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 5619 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5620 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 5621 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5624 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5625 | if (Param->isExpandedParameterPack()) |
| 5626 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5627 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5628 | // C++1z [temp.arg.template]p3: (DR 150) |
| 5629 | // A template-argument matches a template template-parameter P when P |
| 5630 | // is at least as specialized as the template-argument A. |
| 5631 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 5632 | // Quick check for the common case: |
| 5633 | // If P contains a parameter pack, then A [...] matches P if each of A's |
| 5634 | // template parameters matches the corresponding template parameter in |
| 5635 | // the template-parameter-list of P. |
| 5636 | if (TemplateParameterListsAreEqual( |
| 5637 | Template->getTemplateParameters(), Params, false, |
| 5638 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 5639 | return false; |
| 5640 | |
| 5641 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 5642 | Arg.getLocation())) |
| 5643 | return false; |
| 5644 | // FIXME: Produce better diagnostics for deduction failures. |
| 5645 | } |
| 5646 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5647 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5648 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5649 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5650 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5651 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5654 | /// \brief Given a non-type template argument that refers to a |
| 5655 | /// declaration and the type of its corresponding non-type template |
| 5656 | /// parameter, produce an expression that properly refers to that |
| 5657 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5658 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5659 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5660 | QualType ParamType, |
| 5661 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5662 | // C++ [temp.param]p8: |
| 5663 | // |
| 5664 | // A non-type template-parameter of type "array of T" or |
| 5665 | // "function returning T" is adjusted to be of type "pointer to |
| 5666 | // T" or "pointer to function returning T", respectively. |
| 5667 | if (ParamType->isArrayType()) |
| 5668 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5669 | else if (ParamType->isFunctionType()) |
| 5670 | ParamType = Context.getPointerType(ParamType); |
| 5671 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5672 | // For a NULL non-type template argument, return nullptr casted to the |
| 5673 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5674 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5675 | return ImpCastExprToType( |
| 5676 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5677 | ParamType, |
| 5678 | ParamType->getAs<MemberPointerType>() |
| 5679 | ? CK_NullToMemberPointer |
| 5680 | : CK_NullToPointer); |
| 5681 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5682 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5683 | "Only declaration template arguments permitted here"); |
| 5684 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5685 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5686 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5688 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5689 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5690 | // If the value is a class member, we might have a pointer-to-member. |
| 5691 | // Determine whether the non-type template template parameter is of |
| 5692 | // pointer-to-member type. If so, we need to build an appropriate |
| 5693 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5694 | // would refer to the member itself. |
| 5695 | if (ParamType->isMemberPointerType()) { |
| 5696 | QualType ClassType |
| 5697 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5698 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5699 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5700 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5701 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5702 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5703 | |
| 5704 | // The actual value-ness of this is unimportant, but for |
| 5705 | // internal consistency's sake, references to instance methods |
| 5706 | // are r-values. |
| 5707 | ExprValueKind VK = VK_LValue; |
| 5708 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5709 | VK = VK_RValue; |
| 5710 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5711 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5712 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5713 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5714 | Loc, |
| 5715 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5716 | if (RefExpr.isInvalid()) |
| 5717 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5718 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5719 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5720 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5721 | // We might need to perform a trailing qualification conversion, since |
| 5722 | // the element type on the parameter could be more qualified than the |
| 5723 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5724 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5725 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5726 | ParamType.getUnqualifiedType(), false, |
| 5727 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5728 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5729 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5730 | assert(!RefExpr.isInvalid() && |
| 5731 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5732 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5733 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5734 | } |
| 5735 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5736 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5737 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5738 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5739 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5740 | // When the non-type template parameter is a pointer, take the |
| 5741 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5742 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5743 | if (RefExpr.isInvalid()) |
| 5744 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5745 | |
| 5746 | if (T->isFunctionType() || T->isArrayType()) { |
| 5747 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5748 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5749 | if (RefExpr.isInvalid()) |
| 5750 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5751 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5752 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5753 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5754 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5755 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5756 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5759 | ExprValueKind VK = VK_RValue; |
| 5760 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5761 | // If the non-type template parameter has reference type, qualify the |
| 5762 | // resulting declaration reference with the extra qualifiers on the |
| 5763 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5764 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5765 | VK = VK_LValue; |
| 5766 | T = Context.getQualifiedType(T, |
| 5767 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5768 | } else if (isa<FunctionDecl>(VD)) { |
| 5769 | // References to functions are always lvalues. |
| 5770 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5771 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5772 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5773 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5774 | } |
| 5775 | |
| 5776 | /// \brief Construct a new expression that refers to the given |
| 5777 | /// integral template argument with the given source-location |
| 5778 | /// information. |
| 5779 | /// |
| 5780 | /// This routine takes care of the mapping from an integral template |
| 5781 | /// argument (which may have any integral type) to the appropriate |
| 5782 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5783 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5784 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5785 | SourceLocation Loc) { |
| 5786 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5787 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5788 | QualType OrigT = Arg.getIntegralType(); |
| 5789 | |
| 5790 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5791 | // type the same size as the enumerator. We don't want to build an |
| 5792 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5793 | // any integral type with C++11 enum classes, make sure we create the right |
| 5794 | // type of literal for it. |
| 5795 | QualType T = OrigT; |
| 5796 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5797 | T = ET->getDecl()->getIntegerType(); |
| 5798 | |
| 5799 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5800 | if (T->isAnyCharacterType()) { |
Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 5801 | // This does not need to handle u8 character literals because those are |
| 5802 | // 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] | 5803 | CharacterLiteral::CharacterKind Kind; |
| 5804 | if (T->isWideCharType()) |
| 5805 | Kind = CharacterLiteral::Wide; |
| 5806 | else if (T->isChar16Type()) |
| 5807 | Kind = CharacterLiteral::UTF16; |
| 5808 | else if (T->isChar32Type()) |
| 5809 | Kind = CharacterLiteral::UTF32; |
| 5810 | else |
| 5811 | Kind = CharacterLiteral::Ascii; |
| 5812 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5813 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5814 | Kind, T, Loc); |
| 5815 | } else if (T->isBooleanType()) { |
| 5816 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5817 | T, Loc); |
| 5818 | } else if (T->isNullPtrType()) { |
| 5819 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5820 | } else { |
| 5821 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5822 | } |
| 5823 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5824 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5825 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5826 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5827 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5828 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5829 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5830 | Loc, Loc); |
| 5831 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 5832 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5833 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5836 | /// \brief Match two template parameters within template parameter lists. |
| 5837 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5838 | bool Complain, |
| 5839 | Sema::TemplateParameterListEqualKind Kind, |
| 5840 | SourceLocation TemplateArgLoc) { |
| 5841 | // Check the actual kind (type, non-type, template). |
| 5842 | if (Old->getKind() != New->getKind()) { |
| 5843 | if (Complain) { |
| 5844 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5845 | if (TemplateArgLoc.isValid()) { |
| 5846 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5847 | NextDiag = diag::note_template_param_different_kind; |
| 5848 | } |
| 5849 | S.Diag(New->getLocation(), NextDiag) |
| 5850 | << (Kind != Sema::TPL_TemplateMatch); |
| 5851 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5852 | << (Kind != Sema::TPL_TemplateMatch); |
| 5853 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5854 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5855 | return false; |
| 5856 | } |
| 5857 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5858 | // Check that both are parameter packs or neither are parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5859 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5860 | // template template parameter, the template template parameter can have |
| 5861 | // a parameter pack where the template template argument does not. |
| 5862 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5863 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5864 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5865 | if (Complain) { |
| 5866 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5867 | if (TemplateArgLoc.isValid()) { |
| 5868 | S.Diag(TemplateArgLoc, |
| 5869 | diag::err_template_arg_template_params_mismatch); |
| 5870 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5871 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5872 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5873 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5874 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5875 | : 2; |
| 5876 | S.Diag(New->getLocation(), NextDiag) |
| 5877 | << ParamKind << New->isParameterPack(); |
| 5878 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5879 | << ParamKind << Old->isParameterPack(); |
| 5880 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5881 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5882 | return false; |
| 5883 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5884 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5885 | // For non-type template parameters, check the type of the parameter. |
| 5886 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5887 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5888 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5889 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5890 | // If we are matching a template template argument to a template |
| 5891 | // template parameter and one of the non-type template parameter types |
| 5892 | // is dependent, then we must wait until template instantiation time |
| 5893 | // to actually compare the arguments. |
| 5894 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5895 | (OldNTTP->getType()->isDependentType() || |
| 5896 | NewNTTP->getType()->isDependentType())) |
| 5897 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5898 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5899 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5900 | if (Complain) { |
| 5901 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5902 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5903 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5904 | diag::err_template_arg_template_params_mismatch); |
| 5905 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5906 | } |
| 5907 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5908 | << NewNTTP->getType() |
| 5909 | << (Kind != Sema::TPL_TemplateMatch); |
| 5910 | S.Diag(OldNTTP->getLocation(), |
| 5911 | diag::note_template_nontype_parm_prev_declaration) |
| 5912 | << OldNTTP->getType(); |
| 5913 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5914 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5915 | return false; |
| 5916 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5917 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5918 | return true; |
| 5919 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5920 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5921 | // For template template parameters, check the template parameter types. |
| 5922 | // The template parameter lists of template template |
| 5923 | // parameters must agree. |
| 5924 | if (TemplateTemplateParmDecl *OldTTP |
| 5925 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5926 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5927 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5928 | OldTTP->getTemplateParameters(), |
| 5929 | Complain, |
| 5930 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5931 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5932 | : Kind), |
| 5933 | TemplateArgLoc); |
| 5934 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5935 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5936 | return true; |
| 5937 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5938 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5939 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5940 | /// lists. |
| 5941 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5942 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5943 | TemplateParameterList *New, |
| 5944 | TemplateParameterList *Old, |
| 5945 | Sema::TemplateParameterListEqualKind Kind, |
| 5946 | SourceLocation TemplateArgLoc) { |
| 5947 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5948 | if (TemplateArgLoc.isValid()) { |
| 5949 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5950 | NextDiag = diag::note_template_param_list_different_arity; |
| 5951 | } |
| 5952 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5953 | << (New->size() > Old->size()) |
| 5954 | << (Kind != Sema::TPL_TemplateMatch) |
| 5955 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5956 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5957 | << (Kind != Sema::TPL_TemplateMatch) |
| 5958 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5959 | } |
| 5960 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5961 | /// \brief Determine whether the given template parameter lists are |
| 5962 | /// equivalent. |
| 5963 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5964 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5965 | /// source code as part of a new template declaration. |
| 5966 | /// |
| 5967 | /// \param Old The old template parameter list, typically found via |
| 5968 | /// name lookup of the template declared with this template parameter |
| 5969 | /// list. |
| 5970 | /// |
| 5971 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5972 | /// the template parameter lists are not equivalent. |
| 5973 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5974 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5975 | /// |
| 5976 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5977 | /// are actually checking the template parameter list of a template |
| 5978 | /// argument (New) against the template parameter list of its |
| 5979 | /// corresponding template template parameter (Old). We produce |
| 5980 | /// slightly different diagnostics in this scenario. |
| 5981 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5982 | /// \returns True if the template parameter lists are equal, false |
| 5983 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5984 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5985 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5986 | TemplateParameterList *Old, |
| 5987 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5988 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5989 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5990 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5991 | if (Complain) |
| 5992 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5993 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5994 | |
| 5995 | return false; |
| 5996 | } |
| 5997 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5998 | // C++0x [temp.arg.template]p3: |
| 5999 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6000 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6001 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6002 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6003 | // template-parameter-list of P. [...] |
| 6004 | TemplateParameterList::iterator NewParm = New->begin(); |
| 6005 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6006 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6007 | OldParmEnd = Old->end(); |
| 6008 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 6009 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 6010 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6011 | if (NewParm == NewParmEnd) { |
| 6012 | if (Complain) |
| 6013 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6014 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6015 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6016 | return false; |
| 6017 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6018 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6019 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 6020 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6021 | return false; |
| 6022 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6023 | ++NewParm; |
| 6024 | continue; |
| 6025 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6026 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6027 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 6028 | // [...] When P's template- parameter-list contains a template parameter |
| 6029 | // pack (14.5.3), the template parameter pack will match zero or more |
| 6030 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6031 | // template-parameter-list of A with the same type and form as the |
| 6032 | // template parameter pack in P (ignoring whether those template |
| 6033 | // parameters are template parameter packs). |
| 6034 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 6035 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 6036 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6037 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6038 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6039 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6040 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6041 | // Make sure we exhausted all of the arguments. |
| 6042 | if (NewParm != NewParmEnd) { |
| 6043 | if (Complain) |
| 6044 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 6045 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6046 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 6047 | return false; |
| 6048 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6049 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6050 | return true; |
| 6051 | } |
| 6052 | |
| 6053 | /// \brief Check whether a template can be declared within this scope. |
| 6054 | /// |
| 6055 | /// If the template declaration is valid in this scope, returns |
| 6056 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6057 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6058 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 6059 | if (!S) |
| 6060 | return false; |
| 6061 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6062 | // Find the nearest enclosing declaration scope. |
| 6063 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6064 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6065 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6066 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 6067 | // C++ [temp]p4: |
| 6068 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 6069 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 6070 | if (Ctx && Ctx->isExternCContext()) { |
| 6071 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 6072 | << TemplateParams->getSourceRange(); |
| 6073 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 6074 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 6075 | return true; |
| 6076 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 6077 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6078 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 6079 | // C++ [temp]p2: |
| 6080 | // A template-declaration can appear only as a namespace scope or |
| 6081 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 6082 | if (Ctx) { |
| 6083 | if (Ctx->isFileContext()) |
| 6084 | return false; |
| 6085 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 6086 | // C++ [temp.mem]p2: |
| 6087 | // A local class shall not have member templates. |
| 6088 | if (RD->isLocalClass()) |
| 6089 | return Diag(TemplateParams->getTemplateLoc(), |
| 6090 | diag::err_template_inside_local_class) |
| 6091 | << TemplateParams->getSourceRange(); |
| 6092 | else |
| 6093 | return false; |
| 6094 | } |
| 6095 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6096 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6097 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6098 | diag::err_template_outside_namespace_or_class_scope) |
| 6099 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 6100 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6101 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6102 | /// \brief Determine what kind of template specialization the given declaration |
| 6103 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6104 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6105 | if (!D) |
| 6106 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6107 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6108 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 6109 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6110 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 6111 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6112 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 6113 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6114 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6115 | return TSK_Undeclared; |
| 6116 | } |
| 6117 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6118 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6119 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6120 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6121 | /// This routine determines whether a template specialization can be declared |
| 6122 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6123 | /// |
| 6124 | /// \param S the semantic analysis object for which this check is being |
| 6125 | /// performed. |
| 6126 | /// |
| 6127 | /// \param Specialized the entity being specialized or instantiated, which |
| 6128 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6129 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6130 | /// member class). |
| 6131 | /// |
| 6132 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 6133 | /// |
| 6134 | /// \param Loc the location of the explicit specialization or instantiation of |
| 6135 | /// this entity. |
| 6136 | /// |
| 6137 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 6138 | /// a class template. |
| 6139 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6140 | /// \returns true if there was an error that we cannot recover from, false |
| 6141 | /// otherwise. |
| 6142 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 6143 | NamedDecl *Specialized, |
| 6144 | NamedDecl *PrevDecl, |
| 6145 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6146 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6147 | // Keep these "kind" numbers in sync with the %select statements in the |
| 6148 | // various diagnostics emitted by this routine. |
| 6149 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6150 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6151 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6152 | else if (isa<VarTemplateDecl>(Specialized)) |
| 6153 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6154 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6155 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6156 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6157 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6158 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6159 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6160 | else if (isa<RecordDecl>(Specialized)) |
| 6161 | EntityKind = 7; |
| 6162 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 6163 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6164 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6165 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6166 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6167 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6168 | return true; |
| 6169 | } |
| 6170 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6171 | // C++ [temp.expl.spec]p2: |
| 6172 | // An explicit specialization shall be declared in the namespace |
| 6173 | // of which the template is a member, or, for member templates, in |
| 6174 | // the namespace of which the enclosing class or enclosing class |
| 6175 | // template is a member. An explicit specialization of a member |
| 6176 | // function, member class or static data member of a class |
| 6177 | // template shall be declared in the namespace of which the class |
| 6178 | // template is a member. Such a declaration may also be a |
| 6179 | // definition. If the declaration is not a definition, the |
| 6180 | // specialization may be defined later in the name- space in which |
| 6181 | // the explicit specialization was declared, or in a namespace |
| 6182 | // that encloses the one in which the explicit specialization was |
| 6183 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6184 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6185 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6186 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6187 | return true; |
| 6188 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6189 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6190 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6191 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6192 | // Do not warn for class scope explicit specialization during |
| 6193 | // instantiation, warning was already emitted during pattern |
| 6194 | // semantic analysis. |
| 6195 | if (!S.ActiveTemplateInstantiations.size()) |
| 6196 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 6197 | << Specialized; |
| 6198 | } else { |
| 6199 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6200 | << Specialized; |
| 6201 | return true; |
| 6202 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6203 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6204 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 6205 | if (S.CurContext->isRecord() && |
| 6206 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 6207 | // Make sure that we're specializing in the right record context. |
| 6208 | // Otherwise, things can go horribly wrong. |
| 6209 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6210 | << Specialized; |
| 6211 | return true; |
| 6212 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6213 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6214 | // C++ [temp.class.spec]p6: |
| 6215 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6216 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 6217 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6218 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6219 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6220 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6221 | |
| 6222 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 6223 | // namespace. |
| 6224 | // Note that HandleDeclarator() performs this check for explicit |
| 6225 | // specializations of function templates, static data members, and member |
| 6226 | // functions, so we skip the check here for those kinds of entities. |
| 6227 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 6228 | // Should we refactor that check, so that it occurs later? |
| 6229 | if (!DC->Encloses(SpecializedContext) && |
| 6230 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 6231 | isa<FunctionDecl>(Specialized) || |
| 6232 | isa<VarTemplateDecl>(Specialized) || |
| 6233 | isa<VarDecl>(Specialized))) { |
| 6234 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 6235 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 6236 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 6237 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6238 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 6239 | if (S.getLangOpts().MicrosoftExt) |
| 6240 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 6241 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 6242 | << cast<NamedDecl>(SpecializedContext); |
| 6243 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6244 | llvm_unreachable("unexpected namespace context for specialization"); |
| 6245 | |
| 6246 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 6247 | } else if ((!PrevDecl || |
| 6248 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 6249 | getTemplateSpecializationKind(PrevDecl) == |
| 6250 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6251 | // C++ [temp.exp.spec]p2: |
| 6252 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6253 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6254 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6255 | // An explicit specialization of a member function, member class or |
| 6256 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6257 | // namespace of which the class template is a member. |
| 6258 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6259 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6260 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6261 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6262 | // C++11 [temp.explicit]p3: |
| 6263 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 6264 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6265 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6266 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6267 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6268 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6269 | "DC encloses TU but isn't in enclosing namespace set"); |
| 6270 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 6271 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6272 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6273 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6274 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6275 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6276 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6277 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 6278 | else |
| 6279 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 6280 | S.Diag(Loc, Diag) |
| 6281 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 6282 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6283 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6284 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6285 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6286 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6287 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6288 | return false; |
| 6289 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6290 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6291 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 6292 | if (!E->isTypeDependent()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6293 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6294 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6295 | Checker.TraverseStmt(E); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6296 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6297 | return E->getSourceRange(); |
| 6298 | return Checker.MatchLoc; |
| 6299 | } |
| 6300 | |
| 6301 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6302 | if (!TL.getType()->isDependentType()) |
| 6303 | return SourceLocation(); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6304 | DependencyChecker Checker(Depth, /*IgnoreNonTypeDependent*/true); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6305 | Checker.TraverseTypeLoc(TL); |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6306 | if (Checker.MatchLoc.isInvalid()) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6307 | return TL.getSourceRange(); |
| 6308 | return Checker.MatchLoc; |
| 6309 | } |
| 6310 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6311 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6312 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6313 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6314 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6315 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6316 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6317 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6318 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6319 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6320 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6321 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6322 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6323 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6324 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6325 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6326 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6327 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6328 | |
| 6329 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6330 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6331 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6332 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6333 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6334 | |
| 6335 | // Strip off any implicit casts we added as part of type checking. |
| 6336 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6337 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6338 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6339 | // C++ [temp.class.spec]p8: |
| 6340 | // A non-type argument is non-specialized if it is the name of a |
| 6341 | // non-type parameter. All other non-type arguments are |
| 6342 | // specialized. |
| 6343 | // |
| 6344 | // Below, we check the two conditions that only apply to |
| 6345 | // specialized non-type arguments, so skip any non-specialized |
| 6346 | // arguments. |
| 6347 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6348 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6349 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6350 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6351 | // C++ [temp.class.spec]p9: |
| 6352 | // Within the argument list of a class template partial |
| 6353 | // specialization, the following restrictions apply: |
| 6354 | // -- A partially specialized non-type argument expression |
| 6355 | // shall not involve a template parameter of the partial |
| 6356 | // specialization except when the argument expression is a |
| 6357 | // simple identifier. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6358 | // -- The type of a template parameter corresponding to a |
| 6359 | // specialized non-type argument shall not be dependent on a |
| 6360 | // parameter of the specialization. |
| 6361 | // DR1315 removes the first bullet, leaving an incoherent set of rules. |
| 6362 | // We implement a compromise between the original rules and DR1315: |
| 6363 | // -- A specialized non-type template argument shall not be |
| 6364 | // type-dependent and the corresponding template parameter |
| 6365 | // shall have a non-dependent type. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6366 | SourceRange ParamUseRange = |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6367 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6368 | if (ParamUseRange.isValid()) { |
| 6369 | if (IsDefaultArgument) { |
| 6370 | S.Diag(TemplateNameLoc, |
| 6371 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6372 | S.Diag(ParamUseRange.getBegin(), |
| 6373 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6374 | << ParamUseRange; |
| 6375 | } else { |
| 6376 | S.Diag(ParamUseRange.getBegin(), |
| 6377 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6378 | << ParamUseRange; |
| 6379 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6380 | return true; |
| 6381 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6382 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6383 | ParamUseRange = findTemplateParameter( |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6384 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6385 | if (ParamUseRange.isValid()) { |
| 6386 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6387 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6388 | << Param->getType(); |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6389 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6390 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 6391 | << ParamUseRange; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6392 | return true; |
| 6393 | } |
| 6394 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6395 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6396 | return false; |
| 6397 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6398 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6399 | /// \brief Check the non-type template arguments of a class template |
| 6400 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6401 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6402 | /// \param TemplateNameLoc the location of the template name. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6403 | /// \param PrimaryTemplate the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6404 | /// template. |
| 6405 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6406 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6407 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6408 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6409 | /// \returns \c true if there was an error, \c false otherwise. |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6410 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 6411 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 6412 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 6413 | // We have to be conservative when checking a template in a dependent |
| 6414 | // context. |
| 6415 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 6416 | return false; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6417 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6418 | TemplateParameterList *TemplateParams = |
| 6419 | PrimaryTemplate->getTemplateParameters(); |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6420 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6421 | NonTypeTemplateParmDecl *Param |
| 6422 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6423 | if (!Param) |
| 6424 | continue; |
| 6425 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6426 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 6427 | Param, &TemplateArgs[I], |
| 6428 | 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6429 | return true; |
| 6430 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6431 | |
| 6432 | return false; |
| 6433 | } |
| 6434 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6435 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6436 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6437 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6438 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6439 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6440 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6441 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6442 | MultiTemplateParamsArg |
| 6443 | TemplateParameterLists, |
| 6444 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6445 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6446 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6447 | CXXScopeSpec &SS = TemplateId.SS; |
| 6448 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6449 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6450 | // store the location of the outermost template keyword in the declaration. |
| 6451 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6452 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6453 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6454 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6455 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6456 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6457 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6458 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6459 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6460 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6461 | |
| 6462 | if (!ClassTemplate) { |
| 6463 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6464 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6465 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6466 | return true; |
| 6467 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6468 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6469 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6470 | bool isPartialSpecialization = false; |
| 6471 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6472 | // Check the validity of the template headers that introduce this |
| 6473 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6474 | // FIXME: We probably shouldn't complain about these headers for |
| 6475 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6476 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6477 | TemplateParameterList *TemplateParams = |
| 6478 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6479 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6480 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6481 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6482 | if (Invalid) |
| 6483 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6484 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6485 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6486 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6487 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6488 | if (TUK == TUK_Friend) { |
| 6489 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6490 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6491 | return true; |
| 6492 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6493 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6494 | // C++ [temp.class.spec]p10: |
| 6495 | // The template parameter list of a specialization shall not |
| 6496 | // contain default template argument values. |
| 6497 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6498 | Decl *Param = TemplateParams->getParam(I); |
| 6499 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6500 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6501 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6502 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6503 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6504 | } |
| 6505 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6506 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6507 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6508 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6509 | diag::err_default_arg_in_partial_spec) |
| 6510 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6511 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6512 | } |
| 6513 | } else { |
| 6514 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6515 | if (TTP->hasDefaultArgument()) { |
| 6516 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6517 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6518 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6519 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6520 | } |
| 6521 | } |
| 6522 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6523 | } else if (TemplateParams) { |
| 6524 | if (TUK == TUK_Friend) |
| 6525 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6526 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6527 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6528 | TemplateParams->getRAngleLoc())) |
| 6529 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6530 | else |
| 6531 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6532 | } else { |
| 6533 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6534 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6535 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6536 | // Check that the specialization uses the same tag kind as the |
| 6537 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6538 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6539 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6540 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6541 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 6542 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6543 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6544 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6545 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6546 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6547 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6548 | diag::note_previous_use); |
| 6549 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6550 | } |
| 6551 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6552 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6553 | TemplateArgumentListInfo TemplateArgs = |
| 6554 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6555 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6556 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6557 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6558 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6559 | UPPC_PartialSpecialization)) |
| 6560 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6561 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6562 | // Check that the template argument list is well-formed for this |
| 6563 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6564 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6565 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6566 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6567 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6568 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6569 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6570 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6571 | if (isPartialSpecialization) { |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6572 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 6573 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6574 | return true; |
| 6575 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6576 | // FIXME: Move this to CheckTemplatePartialSpecializationArgs so we |
| 6577 | // also do it during instantiation. |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6578 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6579 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6580 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6581 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6582 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6583 | << ClassTemplate->getDeclName(); |
| 6584 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6585 | } |
| 6586 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6587 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6588 | void *InsertPos = nullptr; |
| 6589 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6590 | |
| 6591 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6592 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6593 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6594 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6595 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6596 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6597 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6598 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6599 | // Check whether we can declare a class template specialization in |
| 6600 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6601 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6602 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6603 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6604 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6605 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6606 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6607 | // The canonical type |
| 6608 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6609 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6610 | // Build the canonical type that describes the converted template |
| 6611 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6612 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6613 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6614 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6615 | |
| 6616 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6617 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6618 | // C++ [temp.class.spec]p9b3: |
| 6619 | // |
| 6620 | // -- The argument list of the specialization shall not be identical |
| 6621 | // to the implicit argument list of the primary template. |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 6622 | // |
| 6623 | // This rule has since been removed, because it's redundant given DR1495, |
| 6624 | // but we keep it because it produces better diagnostics and recovery. |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6625 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6626 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6627 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6628 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6629 | ClassTemplate->getIdentifier(), |
| 6630 | TemplateNameLoc, |
| 6631 | Attr, |
| 6632 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6633 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6634 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6635 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6636 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6637 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6638 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6639 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6640 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6641 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6642 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6643 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6644 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6645 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6646 | TemplateParams, |
| 6647 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6648 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6649 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6650 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6651 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6652 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6653 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6654 | Partial->setTemplateParameterListsInfo( |
| 6655 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6656 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6657 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6658 | if (!PrevPartial) |
| 6659 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6660 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6661 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6662 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6663 | // template specialization, make a note of that. |
| 6664 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6665 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6666 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 6667 | CheckTemplatePartialSpecialization(Partial); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6668 | } else { |
| 6669 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6670 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6671 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6672 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6673 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6674 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6675 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6676 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6677 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6678 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6679 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6680 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6681 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6682 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6683 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6684 | if (!PrevDecl) |
| 6685 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6686 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6687 | if (CurContext->isDependentContext()) { |
| 6688 | // -fms-extensions permits specialization of nested classes without |
| 6689 | // fully specializing the outer class(es). |
| 6690 | assert(getLangOpts().MicrosoftExt && |
| 6691 | "Only possible with -fms-extensions!"); |
| 6692 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6693 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6694 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6695 | } else { |
| 6696 | CanonType = Context.getTypeDeclType(Specialization); |
| 6697 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6700 | // C++ [temp.expl.spec]p6: |
| 6701 | // 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] | 6702 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6703 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6704 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6705 | // use occurs; no diagnostic is required. |
| 6706 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6707 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6708 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6709 | // Is there any previous explicit specialization declaration? |
| 6710 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6711 | Okay = true; |
| 6712 | break; |
| 6713 | } |
| 6714 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6715 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6716 | if (!Okay) { |
| 6717 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6718 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6719 | << Context.getTypeDeclType(Specialization) << Range; |
| 6720 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6721 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6722 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6723 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6724 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6725 | return true; |
| 6726 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6727 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6728 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6729 | // If this is not a friend, note that this is an explicit specialization. |
| 6730 | if (TUK != TUK_Friend) |
| 6731 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6732 | |
| 6733 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6734 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6735 | RecordDecl *Def = Specialization->getDefinition(); |
| 6736 | NamedDecl *Hidden = nullptr; |
| 6737 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6738 | SkipBody->ShouldSkip = true; |
| 6739 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6740 | // From here on out, treat this as just a redeclaration. |
| 6741 | TUK = TUK_Declaration; |
| 6742 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6743 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Richard Smith | 792c22d | 2016-12-24 04:09:05 +0000 | [diff] [blame] | 6744 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6745 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6746 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6747 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6748 | } |
| 6749 | } |
| 6750 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6751 | if (Attr) |
| 6752 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6753 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6754 | // Add alignment attributes if necessary; these attributes are checked when |
| 6755 | // the ASTContext lays out the structure. |
| 6756 | if (TUK == TUK_Definition) { |
| 6757 | AddAlignmentAttributesForRecord(Specialization); |
| 6758 | AddMsStructLayoutForRecord(Specialization); |
| 6759 | } |
| 6760 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6761 | if (ModulePrivateLoc.isValid()) |
| 6762 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6763 | << (isPartialSpecialization? 1 : 0) |
| 6764 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 6765 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6766 | // Build the fully-sugared type for this class template |
| 6767 | // specialization as the user wrote in the specialization |
| 6768 | // itself. This means that we'll pretty-print the type retrieved |
| 6769 | // from the specialization's declaration the way that the user |
| 6770 | // actually wrote the specialization, rather than formatting the |
| 6771 | // name based on the "canonical" representation used to store the |
| 6772 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6773 | TypeSourceInfo *WrittenTy |
| 6774 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6775 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6776 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6777 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6778 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6779 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6780 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6781 | // C++ [temp.expl.spec]p9: |
| 6782 | // A template explicit specialization is in the scope of the |
| 6783 | // namespace in which the template was defined. |
| 6784 | // |
| 6785 | // We actually implement this paragraph where we set the semantic |
| 6786 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6787 | // but we also maintain the lexical context where the actual |
| 6788 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6789 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6790 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6791 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6792 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6793 | Specialization->startDefinition(); |
| 6794 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6795 | if (TUK == TUK_Friend) { |
| 6796 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6797 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6798 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6799 | /*FIXME:*/KWLoc); |
| 6800 | Friend->setAccess(AS_public); |
| 6801 | CurContext->addDecl(Friend); |
| 6802 | } else { |
| 6803 | // Add the specialization into its lexical context, so that it can |
| 6804 | // be seen when iterating through the list of declarations in that |
| 6805 | // context. However, specializations are not found by name lookup. |
| 6806 | CurContext->addDecl(Specialization); |
| 6807 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6808 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6809 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6810 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6811 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6812 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6813 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6814 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6815 | ActOnDocumentableDecl(NewDecl); |
| 6816 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6817 | } |
| 6818 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6819 | /// \brief Strips various properties off an implicit instantiation |
| 6820 | /// that has just been explicitly specialized. |
| 6821 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6822 | D->dropAttr<DLLImportAttr>(); |
| 6823 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6824 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6825 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6826 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6827 | } |
| 6828 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6829 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6830 | // declaration or definition. |
| 6831 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6832 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6833 | // Explicit instantiations following a specialization have no effect and |
| 6834 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6835 | // until a valid name loc is found. |
| 6836 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6837 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6838 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6839 | PrevDiagLoc = Prev->getLocation(); |
| 6840 | } |
| 6841 | assert(PrevDiagLoc.isValid() && |
| 6842 | "Explicit instantiation without point of instantiation?"); |
| 6843 | return PrevDiagLoc; |
| 6844 | } |
| 6845 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6846 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6847 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6848 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6849 | /// new specialization/instantiation will have any effect. |
| 6850 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6851 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6852 | /// instantiation. |
| 6853 | /// |
| 6854 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6855 | /// |
| 6856 | /// \param PrevDecl the previous declaration of the entity. |
| 6857 | /// |
| 6858 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6859 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6860 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6861 | /// declaration was instantiated (either implicitly or explicitly). |
| 6862 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6863 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6864 | /// specialization or instantiation has no effect and should be ignored. |
| 6865 | /// |
| 6866 | /// \returns true if there was an error that should prevent the introduction of |
| 6867 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6868 | bool |
| 6869 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6870 | TemplateSpecializationKind NewTSK, |
| 6871 | NamedDecl *PrevDecl, |
| 6872 | TemplateSpecializationKind PrevTSK, |
| 6873 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6874 | bool &HasNoEffect) { |
| 6875 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6876 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6877 | switch (NewTSK) { |
| 6878 | case TSK_Undeclared: |
| 6879 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6880 | assert( |
| 6881 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6882 | "previous declaration must be implicit!"); |
| 6883 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6884 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6885 | case TSK_ExplicitSpecialization: |
| 6886 | switch (PrevTSK) { |
| 6887 | case TSK_Undeclared: |
| 6888 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6889 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6890 | // explicitly specialized or has merely been mentioned without any |
| 6891 | // instantiation. |
| 6892 | return false; |
| 6893 | |
| 6894 | case TSK_ImplicitInstantiation: |
| 6895 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6896 | // The declaration itself has not actually been instantiated, so it is |
| 6897 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6898 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6899 | return false; |
| 6900 | } |
| 6901 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6902 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6903 | case TSK_ExplicitInstantiationDeclaration: |
| 6904 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6905 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6906 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6907 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6908 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6909 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6910 | // 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] | 6911 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6912 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6913 | // implicit instantiation to take place, in every translation unit in |
| 6914 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6915 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6916 | // Is there any previous explicit specialization declaration? |
| 6917 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6918 | return false; |
| 6919 | } |
| 6920 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6921 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6922 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6923 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6924 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6925 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6926 | return true; |
| 6927 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6928 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6929 | case TSK_ExplicitInstantiationDeclaration: |
| 6930 | switch (PrevTSK) { |
| 6931 | case TSK_ExplicitInstantiationDeclaration: |
| 6932 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6933 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6934 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6935 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6936 | case TSK_Undeclared: |
| 6937 | case TSK_ImplicitInstantiation: |
| 6938 | // We're explicitly instantiating something that may have already been |
| 6939 | // implicitly instantiated; that's fine. |
| 6940 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6941 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6942 | case TSK_ExplicitSpecialization: |
| 6943 | // C++0x [temp.explicit]p4: |
| 6944 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6945 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6946 | // specialization for that template, the explicit instantiation has no |
| 6947 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6948 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6949 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6950 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6951 | case TSK_ExplicitInstantiationDefinition: |
| 6952 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6953 | // If an entity is the subject of both an explicit instantiation |
| 6954 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6955 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6956 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6957 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6958 | |
| 6959 | // Explicit instantiations following a specialization have no effect and |
| 6960 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6961 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6962 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6963 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6964 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6965 | return false; |
| 6966 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6967 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6968 | case TSK_ExplicitInstantiationDefinition: |
| 6969 | switch (PrevTSK) { |
| 6970 | case TSK_Undeclared: |
| 6971 | case TSK_ImplicitInstantiation: |
| 6972 | // We're explicitly instantiating something that may have already been |
| 6973 | // implicitly instantiated; that's fine. |
| 6974 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6975 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6976 | case TSK_ExplicitSpecialization: |
| 6977 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6978 | // For a given set of template parameters, if an explicit |
| 6979 | // instantiation of a template appears after a declaration of |
| 6980 | // an explicit specialization for that template, the explicit |
| 6981 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 6982 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6983 | << PrevDecl; |
| 6984 | Diag(PrevDecl->getLocation(), |
| 6985 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6986 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6987 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6988 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6989 | case TSK_ExplicitInstantiationDeclaration: |
| 6990 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6991 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6992 | |
| 6993 | // C++0x [temp.explicit]p4: |
| 6994 | // For a given set of template parameters, if an explicit instantiation |
| 6995 | // of a template appears after a declaration of an explicit |
| 6996 | // specialization for that template, the explicit instantiation has no |
| 6997 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6998 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6999 | // Is there any previous explicit specialization declaration? |
| 7000 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7001 | HasNoEffect = true; |
| 7002 | break; |
| 7003 | } |
| 7004 | } |
| 7005 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7006 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7007 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7008 | case TSK_ExplicitInstantiationDefinition: |
| 7009 | // C++0x [temp.spec]p5: |
| 7010 | // For a given template and a given set of template-arguments, |
| 7011 | // - an explicit instantiation definition shall appear at most once |
| 7012 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7013 | |
| 7014 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 7015 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 7016 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 7017 | : diag::err_explicit_instantiation_duplicate) |
| 7018 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 7019 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7020 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7021 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7022 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7023 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7024 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7025 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7026 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7027 | } |
| 7028 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7029 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7030 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7031 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7032 | /// The only possible way to get a dependent function template specialization |
| 7033 | /// is with a friend declaration, like so: |
| 7034 | /// |
| 7035 | /// \code |
| 7036 | /// template \<class T> void foo(T); |
| 7037 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7038 | /// friend void foo<>(T); |
| 7039 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 7040 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7041 | /// |
| 7042 | /// There really isn't any useful analysis we can do here, so we |
| 7043 | /// just store the information. |
| 7044 | bool |
| 7045 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 7046 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 7047 | LookupResult &Previous) { |
| 7048 | // Remove anything from Previous that isn't a function template in |
| 7049 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7050 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7051 | LookupResult::Filter F = Previous.makeFilter(); |
| 7052 | while (F.hasNext()) { |
| 7053 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 7054 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7055 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 7056 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 7057 | F.erase(); |
| 7058 | } |
| 7059 | F.done(); |
| 7060 | |
| 7061 | // Should this be diagnosed here? |
| 7062 | if (Previous.empty()) return true; |
| 7063 | |
| 7064 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 7065 | ExplicitTemplateArgs); |
| 7066 | return false; |
| 7067 | } |
| 7068 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7069 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7070 | /// specialization. |
| 7071 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7072 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7073 | /// explicit function template specialization. On successful completion, |
| 7074 | /// the function declaration \p FD will become a function template |
| 7075 | /// specialization. |
| 7076 | /// |
| 7077 | /// \param FD the function declaration, which will be updated to become a |
| 7078 | /// function template specialization. |
| 7079 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7080 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 7081 | /// if any. Note that this may be valid info even when 0 arguments are |
| 7082 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 7083 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7084 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7085 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7086 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7087 | bool Sema::CheckFunctionTemplateSpecialization( |
| 7088 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 7089 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7090 | // The set of function template specializations that could match this |
| 7091 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7092 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 7093 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 7094 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7095 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7096 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 7097 | ConvertedTemplateArgs; |
| 7098 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7099 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7100 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7101 | I != E; ++I) { |
| 7102 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 7103 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7104 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7105 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7106 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 7107 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7108 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7109 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7110 | // When matching a constexpr member function template specialization |
| 7111 | // against the primary template, we don't yet know whether the |
| 7112 | // specialization has an implicit 'const' (because we don't know whether |
| 7113 | // it will be a static member function until we know which template it |
| 7114 | // specializes), so adjust it now assuming it specializes this template. |
| 7115 | QualType FT = FD->getType(); |
| 7116 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7117 | CXXMethodDecl *OldMD = |
| 7118 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7119 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7120 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7121 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7122 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7123 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7124 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7125 | } |
| 7126 | } |
| 7127 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7128 | TemplateArgumentListInfo Args; |
| 7129 | if (ExplicitTemplateArgs) |
| 7130 | Args = *ExplicitTemplateArgs; |
| 7131 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7132 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7133 | // A trailing template-argument can be left unspecified in the |
| 7134 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7135 | // provided it can be deduced from the function argument type. |
| 7136 | // Perform template argument deduction to determine whether we may be |
| 7137 | // specializing this template. |
| 7138 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7139 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7140 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 7141 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 7142 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7143 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 7144 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7145 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7146 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7147 | FailedCandidates.addCandidate().set( |
| 7148 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 7149 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7150 | (void)TDK; |
| 7151 | continue; |
| 7152 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7153 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7154 | // Target attributes are part of the cuda function signature, so |
| 7155 | // the deduced template's cuda target must match that of the |
| 7156 | // specialization. Given that C++ template deduction does not |
| 7157 | // take target attributes into account, we reject candidates |
| 7158 | // here that have a different target. |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 7159 | if (LangOpts.CUDA && |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7160 | IdentifyCUDATarget(Specialization, |
| 7161 | /* IgnoreImplicitHDAttributes = */ true) != |
| 7162 | IdentifyCUDATarget(FD, /* IgnoreImplicitHDAttributes = */ true)) { |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 7163 | FailedCandidates.addCandidate().set( |
| 7164 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 7165 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 7166 | continue; |
| 7167 | } |
| 7168 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7169 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7170 | if (ExplicitTemplateArgs) |
| 7171 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7172 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7173 | } |
| 7174 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7175 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 7176 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7177 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7178 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7179 | FD->getLocation(), |
| 7180 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 7181 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7182 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7183 | PDiag(diag::note_function_template_spec_matched)); |
| 7184 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7185 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7186 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7187 | |
| 7188 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 7189 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7190 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7191 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare [...] |
| 7192 | // an explicit specialization (14.8.3) [...] of a concept definition. |
| 7193 | if (Specialization->getPrimaryTemplate()->isConcept()) { |
| 7194 | Diag(FD->getLocation(), diag::err_concept_specialized) |
| 7195 | << 0 /*function*/ << 1 /*explicitly specialized*/; |
| 7196 | Diag(Specialization->getLocation(), diag::note_previous_declaration); |
| 7197 | return true; |
| 7198 | } |
| 7199 | |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7200 | FunctionTemplateSpecializationInfo *SpecInfo |
| 7201 | = Specialization->getTemplateSpecializationInfo(); |
| 7202 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7203 | |
| 7204 | // Note: do not overwrite location info if previous template |
| 7205 | // specialization kind was explicit. |
| 7206 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7207 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7208 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7209 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 7210 | // function can differ from the template declaration with respect to |
| 7211 | // the constexpr specifier. |
| 7212 | Specialization->setConstexpr(FD->isConstexpr()); |
| 7213 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7214 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7215 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7216 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7217 | |
| 7218 | // If this is a friend declaration, then we're not really declaring |
| 7219 | // an explicit specialization. |
| 7220 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7221 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7222 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7223 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7224 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7225 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7226 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7227 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7228 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7229 | |
| 7230 | // C++ [temp.expl.spec]p6: |
| 7231 | // 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] | 7232 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7233 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7234 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7235 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7236 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7237 | if (!isFriend && |
| 7238 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7239 | TSK_ExplicitSpecialization, |
| 7240 | Specialization, |
| 7241 | SpecInfo->getTemplateSpecializationKind(), |
| 7242 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7243 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7244 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 7245 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7246 | // Mark the prior declaration as an explicit specialization, so that later |
| 7247 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7248 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 7249 | // Since explicit specializations do not inherit '=delete' from their |
| 7250 | // primary function template - check if the 'specialization' that was |
| 7251 | // implicitly generated (during template argument deduction for partial |
| 7252 | // ordering) from the most specialized of all the function templates that |
| 7253 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 7254 | // first check that it was implicitly generated during template argument |
| 7255 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 7256 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 7257 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 7258 | !Specialization->getCanonicalDecl()->isReferenced()) { |
| 7259 | assert( |
| 7260 | Specialization->getCanonicalDecl() == Specialization && |
| 7261 | "This must be the only existing declaration of this specialization"); |
| 7262 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7263 | } |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7264 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7265 | MarkUnusedFileScopedDecl(Specialization); |
| 7266 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7267 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7268 | // Turn the given function declaration into a function template |
| 7269 | // specialization, with the template arguments from the previous |
| 7270 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7271 | // Take copies of (semantic and syntactic) template argument lists. |
| 7272 | const TemplateArgumentList* TemplArgs = new (Context) |
| 7273 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7274 | FD->setFunctionTemplateSpecialization( |
| 7275 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 7276 | SpecInfo->getTemplateSpecializationKind(), |
| 7277 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7278 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 7279 | // A function template specialization inherits the target attributes |
| 7280 | // of its template. (We require the attributes explicitly in the |
| 7281 | // code to match, but a template may have implicit attributes by |
| 7282 | // virtue e.g. of being constexpr, and it passes these implicit |
| 7283 | // attributes on to its specializations.) |
| 7284 | if (LangOpts.CUDA) |
| 7285 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 7286 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7287 | // The "previous declaration" for this function template specialization is |
| 7288 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7289 | Previous.clear(); |
| 7290 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7291 | return false; |
| 7292 | } |
| 7293 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7294 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7295 | /// specialization. |
| 7296 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7297 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7298 | /// explicit member function specialization. On successful completion, |
| 7299 | /// the function declaration \p FD will become a member function |
| 7300 | /// specialization. |
| 7301 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7302 | /// \param Member the member declaration, which will be updated to become a |
| 7303 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7304 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7305 | /// \param Previous the set of declarations, one of which may be specialized |
| 7306 | /// by this function specialization; the set will be modified to contain the |
| 7307 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7308 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7309 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7310 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7311 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7312 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7313 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7314 | NamedDecl *Instantiation = nullptr; |
| 7315 | NamedDecl *InstantiatedFrom = nullptr; |
| 7316 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7317 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7318 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7319 | // Nowhere to look anyway. |
| 7320 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7321 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7322 | I != E; ++I) { |
| 7323 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7324 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7325 | QualType Adjusted = Function->getType(); |
| 7326 | if (!hasExplicitCallingConv(Adjusted)) |
| 7327 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7328 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7329 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7330 | Instantiation = Method; |
| 7331 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7332 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7333 | break; |
| 7334 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7335 | } |
| 7336 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7337 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7338 | VarDecl *PrevVar; |
| 7339 | if (Previous.isSingleResult() && |
| 7340 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7341 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7342 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7343 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7344 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7345 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7346 | } |
| 7347 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7348 | CXXRecordDecl *PrevRecord; |
| 7349 | if (Previous.isSingleResult() && |
| 7350 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7351 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7352 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7353 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7354 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7355 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7356 | } else if (isa<EnumDecl>(Member)) { |
| 7357 | EnumDecl *PrevEnum; |
| 7358 | if (Previous.isSingleResult() && |
| 7359 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7360 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7361 | Instantiation = PrevEnum; |
| 7362 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7363 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7364 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7365 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7366 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7367 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7368 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7369 | // specializations are always out-of-line, the caller will complain about |
| 7370 | // this mismatch later. |
| 7371 | return false; |
| 7372 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7373 | |
| 7374 | // If this is a friend, just bail out here before we start turning |
| 7375 | // things into explicit specializations. |
| 7376 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7377 | // Preserve instantiation information. |
| 7378 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7379 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7380 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7381 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7382 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7383 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7384 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7385 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7386 | } |
| 7387 | |
| 7388 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7389 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7390 | return false; |
| 7391 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7392 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7393 | // Make sure that this is a specialization of a member. |
| 7394 | if (!InstantiatedFrom) { |
| 7395 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 7396 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7397 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 7398 | return true; |
| 7399 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7400 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7401 | // C++ [temp.expl.spec]p6: |
| 7402 | // 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] | 7403 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7404 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7405 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7406 | // use occurs; no diagnostic is required. |
| 7407 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7408 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7409 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7410 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7411 | TSK_ExplicitSpecialization, |
| 7412 | Instantiation, |
| 7413 | MSInfo->getTemplateSpecializationKind(), |
| 7414 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7415 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7416 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7417 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7418 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7419 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7420 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7421 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7422 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7423 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7424 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7425 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7426 | // the original declaration to note that it is an explicit specialization |
| 7427 | // (if it was previously an implicit instantiation). This latter step |
| 7428 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7429 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7430 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7431 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7432 | TSK_ImplicitInstantiation) { |
| 7433 | InstantiationFunction->setTemplateSpecializationKind( |
| 7434 | TSK_ExplicitSpecialization); |
| 7435 | InstantiationFunction->setLocation(Member->getLocation()); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7436 | // Explicit specializations of member functions of class templates do not |
| 7437 | // inherit '=delete' from the member function they are specializing. |
| 7438 | if (InstantiationFunction->isDeleted()) { |
| 7439 | assert(InstantiationFunction->getCanonicalDecl() == |
| 7440 | InstantiationFunction); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 7441 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7442 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7443 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7444 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7445 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7446 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7447 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7448 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7449 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7450 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7451 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7452 | TSK_ImplicitInstantiation) { |
| 7453 | InstantiationVar->setTemplateSpecializationKind( |
| 7454 | TSK_ExplicitSpecialization); |
| 7455 | InstantiationVar->setLocation(Member->getLocation()); |
| 7456 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7457 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7458 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7459 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7460 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7461 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7462 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7463 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7464 | TSK_ImplicitInstantiation) { |
| 7465 | InstantiationClass->setTemplateSpecializationKind( |
| 7466 | TSK_ExplicitSpecialization); |
| 7467 | InstantiationClass->setLocation(Member->getLocation()); |
| 7468 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7469 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7470 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7471 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7472 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7473 | } else { |
| 7474 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7475 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7476 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7477 | TSK_ImplicitInstantiation) { |
| 7478 | InstantiationEnum->setTemplateSpecializationKind( |
| 7479 | TSK_ExplicitSpecialization); |
| 7480 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7481 | } |
| 7482 | |
| 7483 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7484 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7485 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7486 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7487 | // Save the caller the trouble of having to figure out which declaration |
| 7488 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7489 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7490 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7491 | return false; |
| 7492 | } |
| 7493 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7494 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7495 | /// |
| 7496 | /// \returns true if a serious error occurs, false otherwise. |
| 7497 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7498 | SourceLocation InstLoc, |
| 7499 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7500 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7501 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7502 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7503 | if (CurContext->isRecord()) { |
| 7504 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7505 | << D; |
| 7506 | return true; |
| 7507 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7508 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7509 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7510 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7511 | // template. If the name declared in the explicit instantiation is an |
| 7512 | // unqualified name, the explicit instantiation shall appear in the |
| 7513 | // namespace where its template is declared or, if that namespace is inline |
| 7514 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7515 | // |
| 7516 | // 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] | 7517 | if (WasQualifiedName) { |
| 7518 | if (CurContext->Encloses(OrigContext)) |
| 7519 | return false; |
| 7520 | } else { |
| 7521 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7522 | return false; |
| 7523 | } |
| 7524 | |
| 7525 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7526 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7527 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7528 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7529 | diag::err_explicit_instantiation_out_of_scope : |
| 7530 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7531 | << D << NS; |
| 7532 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7533 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7534 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7535 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7536 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7537 | << D << NS; |
| 7538 | } else |
| 7539 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7540 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7541 | diag::err_explicit_instantiation_must_be_global : |
| 7542 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7543 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7544 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7545 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7546 | } |
| 7547 | |
| 7548 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7549 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7550 | if (!SS.isSet()) |
| 7551 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7552 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7553 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7554 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7555 | // or a static data member of a class template specialization, the name of |
| 7556 | // the class template specialization in the qualified-id for the member |
| 7557 | // name shall be a simple-template-id. |
| 7558 | // |
| 7559 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7560 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7561 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7562 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7563 | if (isa<TemplateSpecializationType>(T)) |
| 7564 | return true; |
| 7565 | |
| 7566 | return false; |
| 7567 | } |
| 7568 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 7569 | /// Make a dllexport or dllimport attr on a class template specialization take |
| 7570 | /// effect. |
| 7571 | static void dllExportImportClassTemplateSpecialization( |
| 7572 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 7573 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 7574 | assert(A && "dllExportImportClassTemplateSpecialization called " |
| 7575 | "on Def without dllexport or dllimport"); |
| 7576 | |
| 7577 | // We reject explicit instantiations in class scope, so there should |
| 7578 | // never be any delayed exported classes to worry about. |
| 7579 | assert(S.DelayedDllExportClasses.empty() && |
| 7580 | "delayed exports present at explicit instantiation"); |
| 7581 | S.checkClassLevelDLLAttribute(Def); |
| 7582 | |
| 7583 | // Propagate attribute to base class templates. |
| 7584 | for (auto &B : Def->bases()) { |
| 7585 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 7586 | B.getType()->getAsCXXRecordDecl())) |
| 7587 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 7588 | } |
| 7589 | |
| 7590 | S.referenceDLLExportedClassMethods(); |
| 7591 | } |
| 7592 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7593 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7594 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7595 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7596 | SourceLocation ExternLoc, |
| 7597 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7598 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7599 | SourceLocation KWLoc, |
| 7600 | const CXXScopeSpec &SS, |
| 7601 | TemplateTy TemplateD, |
| 7602 | SourceLocation TemplateNameLoc, |
| 7603 | SourceLocation LAngleLoc, |
| 7604 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7605 | SourceLocation RAngleLoc, |
| 7606 | AttributeList *Attr) { |
| 7607 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7608 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7609 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7610 | // Check that the specialization uses the same tag kind as the |
| 7611 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7612 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7613 | assert(Kind != TTK_Enum && |
| 7614 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7615 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7616 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 7617 | |
| 7618 | if (!ClassTemplate) { |
Reid Kleckner | 1a4ab7e | 2016-12-09 19:47:58 +0000 | [diff] [blame] | 7619 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 7620 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7621 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7622 | return true; |
| 7623 | } |
| 7624 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7625 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7626 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7627 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7628 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7629 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7630 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7631 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7632 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7633 | diag::note_previous_use); |
| 7634 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7635 | } |
| 7636 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7637 | // C++0x [temp.explicit]p2: |
| 7638 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7639 | // definition and an explicit instantiation declaration. An explicit |
| 7640 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7641 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7642 | ? TSK_ExplicitInstantiationDefinition |
| 7643 | : TSK_ExplicitInstantiationDeclaration; |
| 7644 | |
| 7645 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7646 | // Check for dllexport class template instantiation declarations. |
| 7647 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7648 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7649 | Diag(ExternLoc, |
| 7650 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7651 | Diag(A->getLoc(), diag::note_attribute); |
| 7652 | break; |
| 7653 | } |
| 7654 | } |
| 7655 | |
| 7656 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7657 | Diag(ExternLoc, |
| 7658 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7659 | Diag(A->getLocation(), diag::note_attribute); |
| 7660 | } |
| 7661 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7662 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7663 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 7664 | // instantiation declarations for most purposes. |
| 7665 | bool DLLImportExplicitInstantiationDef = false; |
| 7666 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 7667 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7668 | // Check for dllimport class template instantiation definitions. |
| 7669 | bool DLLImport = |
| 7670 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 7671 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7672 | if (A->getKind() == AttributeList::AT_DLLImport) |
| 7673 | DLLImport = true; |
| 7674 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7675 | // dllexport trumps dllimport here. |
| 7676 | DLLImport = false; |
| 7677 | break; |
| 7678 | } |
| 7679 | } |
| 7680 | if (DLLImport) { |
| 7681 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 7682 | DLLImportExplicitInstantiationDef = true; |
| 7683 | } |
| 7684 | } |
| 7685 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7686 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7687 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7688 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7689 | |
| 7690 | // Check that the template argument list is well-formed for this |
| 7691 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7692 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7693 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7694 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7695 | return true; |
| 7696 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7697 | // Find the class template specialization declaration that |
| 7698 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7699 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7700 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7701 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7702 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7703 | TemplateSpecializationKind PrevDecl_TSK |
| 7704 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7705 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7706 | // C++0x [temp.explicit]p2: |
| 7707 | // [...] An explicit instantiation shall appear in an enclosing |
| 7708 | // namespace of its template. [...] |
| 7709 | // |
| 7710 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7711 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7712 | SS.isSet())) |
| 7713 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7714 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7715 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7716 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7717 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7718 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7719 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7720 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7721 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7722 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7723 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7724 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7725 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7726 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7727 | |
| 7728 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7729 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7730 | // Since the only prior class template specialization with these |
| 7731 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7732 | // declaration node as our own, updating the source location |
| 7733 | // for the template name to reflect our new declaration. |
| 7734 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7735 | Specialization = PrevDecl; |
| 7736 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7737 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7738 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7739 | |
| 7740 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7741 | DLLImportExplicitInstantiationDef) { |
| 7742 | // The new specialization might add a dllimport attribute. |
| 7743 | HasNoEffect = false; |
| 7744 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7745 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7746 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7747 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7748 | // Create a new class template specialization declaration node for |
| 7749 | // this explicit specialization. |
| 7750 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7751 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7752 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7753 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7754 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7755 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7756 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7757 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7758 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7759 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7760 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7761 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7762 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7763 | } |
| 7764 | |
| 7765 | // Build the fully-sugared type for this explicit instantiation as |
| 7766 | // the user wrote in the explicit instantiation itself. This means |
| 7767 | // that we'll pretty-print the type retrieved from the |
| 7768 | // specialization's declaration the way that the user actually wrote |
| 7769 | // the explicit instantiation, rather than formatting the name based |
| 7770 | // on the "canonical" representation used to store the template |
| 7771 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7772 | TypeSourceInfo *WrittenTy |
| 7773 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7774 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7775 | Context.getTypeDeclType(Specialization)); |
| 7776 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7777 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7778 | // Set source locations for keywords. |
| 7779 | Specialization->setExternLoc(ExternLoc); |
| 7780 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 7781 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7782 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7783 | if (Attr) |
| 7784 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7785 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7786 | // Add the explicit instantiation into its lexical context. However, |
| 7787 | // since explicit instantiations are never found by name lookup, we |
| 7788 | // just put it into the declaration context directly. |
| 7789 | Specialization->setLexicalDeclContext(CurContext); |
| 7790 | CurContext->addDecl(Specialization); |
| 7791 | |
| 7792 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7793 | if (HasNoEffect) { |
| 7794 | // Set the template specialization kind. |
| 7795 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7796 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7797 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7798 | |
| 7799 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7800 | // A definition of a class template or class member template |
| 7801 | // shall be in scope at the point of the explicit instantiation of |
| 7802 | // the class template or class member template. |
| 7803 | // |
| 7804 | // This check comes when we actually try to perform the |
| 7805 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7806 | ClassTemplateSpecializationDecl *Def |
| 7807 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7808 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7809 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7810 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7811 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7812 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7813 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7814 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7815 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7816 | // Instantiate the members of this class template specialization. |
| 7817 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7818 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7819 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7820 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7821 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7822 | // TSK_ExplicitInstantiationDefinition |
| 7823 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7824 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 7825 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7826 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7827 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7828 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7829 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
Shoaib Meenai | ab3f96c | 2016-11-09 23:52:20 +0000 | [diff] [blame] | 7830 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 7831 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7832 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7833 | // attribute to a template with a previous instantiation declaration. |
| 7834 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7835 | auto *A = cast<InheritableAttr>( |
| 7836 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 7837 | A->setInherited(true); |
| 7838 | Def->addAttr(A); |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 7839 | dllExportImportClassTemplateSpecialization(*this, Def); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7840 | } |
| 7841 | } |
| 7842 | |
Shoaib Meenai | fc78d7c | 2016-12-05 18:01:35 +0000 | [diff] [blame] | 7843 | // Fix a TSK_ImplicitInstantiation followed by a |
| 7844 | // TSK_ExplicitInstantiationDefinition |
| 7845 | if (Old_TSK == TSK_ImplicitInstantiation && |
| 7846 | Specialization->hasAttr<DLLExportAttr>() && |
| 7847 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 7848 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 7849 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7850 | // attribute to a template with a previous implicit instantiation. |
| 7851 | // MinGW doesn't allow this. We limit clang to only adding dllexport, to |
| 7852 | // avoid potentially strange codegen behavior. For example, if we extend |
| 7853 | // this conditional to dllimport, and we have a source file calling a |
| 7854 | // method on an implicitly instantiated template class instance and then |
| 7855 | // declaring a dllimport explicit instantiation definition for the same |
| 7856 | // template class, the codegen for the method call will not respect the |
| 7857 | // dllimport, while it will with cl. The Def will already have the DLL |
| 7858 | // attribute, since the Def and Specialization will be the same in the |
| 7859 | // case of Old_TSK == TSK_ImplicitInstantiation, and we already added the |
| 7860 | // attribute to the Specialization; we just need to make it take effect. |
| 7861 | assert(Def == Specialization && |
| 7862 | "Def and Specialization should match for implicit instantiation"); |
| 7863 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 7864 | } |
| 7865 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7866 | // Set the template specialization kind. Make sure it is set before |
| 7867 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 7868 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7869 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7870 | } else { |
| 7871 | |
| 7872 | // Set the template specialization kind. |
| 7873 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7874 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7875 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7876 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7877 | } |
| 7878 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7879 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7880 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7881 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7882 | SourceLocation ExternLoc, |
| 7883 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7884 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7885 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7886 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7887 | IdentifierInfo *Name, |
| 7888 | SourceLocation NameLoc, |
| 7889 | AttributeList *Attr) { |
| 7890 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7891 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7892 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7893 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7894 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7895 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7896 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7897 | SourceLocation(), false, TypeResult(), |
| 7898 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7899 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7900 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7901 | if (!TagD) |
| 7902 | return true; |
| 7903 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7904 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7905 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7906 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7907 | if (Tag->isInvalidDecl()) |
| 7908 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7909 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7910 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7911 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7912 | if (!Pattern) { |
| 7913 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7914 | << Context.getTypeDeclType(Record); |
| 7915 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7916 | return true; |
| 7917 | } |
| 7918 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7919 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7920 | // If the explicit instantiation is for a class or member class, the |
| 7921 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7922 | // simple-template-id. |
| 7923 | // |
| 7924 | // C++98 has the same restriction, just worded differently. |
| 7925 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7926 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7927 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7928 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7929 | // C++0x [temp.explicit]p2: |
| 7930 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7931 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7932 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7933 | TemplateSpecializationKind TSK |
| 7934 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7935 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7936 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7937 | // C++0x [temp.explicit]p2: |
| 7938 | // [...] An explicit instantiation shall appear in an enclosing |
| 7939 | // namespace of its template. [...] |
| 7940 | // |
| 7941 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7942 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7943 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7944 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7945 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7946 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7947 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7948 | PrevDecl = Record; |
| 7949 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7950 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7951 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7952 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7953 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7954 | PrevDecl, |
| 7955 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7956 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7957 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7958 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7959 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7960 | return TagD; |
| 7961 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7962 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7963 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7964 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7965 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7966 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7967 | // 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] | 7968 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7969 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7970 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7971 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7972 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7973 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7974 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7975 | << Pattern; |
| 7976 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7977 | } else { |
| 7978 | if (InstantiateClass(NameLoc, Record, Def, |
| 7979 | getTemplateInstantiationArgs(Record), |
| 7980 | TSK)) |
| 7981 | return true; |
| 7982 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7983 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7984 | if (!RecordDef) |
| 7985 | return true; |
| 7986 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7987 | } |
| 7988 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7989 | // Instantiate all of the members of the class. |
| 7990 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7991 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7992 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7993 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7994 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7995 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7996 | // FIXME: We don't have any representation for explicit instantiations of |
| 7997 | // member classes. Such a representation is not needed for compilation, but it |
| 7998 | // should be available for clients that want to see all of the declarations in |
| 7999 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 8000 | return TagD; |
| 8001 | } |
| 8002 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8003 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 8004 | SourceLocation ExternLoc, |
| 8005 | SourceLocation TemplateLoc, |
| 8006 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8007 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8008 | // TODO: check if/when DNInfo should replace Name. |
| 8009 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 8010 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8011 | if (!Name) { |
| 8012 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 8013 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8014 | diag::err_explicit_instantiation_requires_name) |
| 8015 | << D.getDeclSpec().getSourceRange() |
| 8016 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8017 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8018 | return true; |
| 8019 | } |
| 8020 | |
| 8021 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 8022 | // we find one that is. |
| 8023 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 8024 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 8025 | S = S->getParent(); |
| 8026 | |
| 8027 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8028 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 8029 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8030 | if (R.isNull()) |
| 8031 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8032 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8033 | // C++ [dcl.stc]p1: |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8034 | // A storage-class-specifier shall not be specified in [...] an explicit |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8035 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8036 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8037 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 8038 | << Name; |
| 8039 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8040 | } else if (D.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8041 | != DeclSpec::SCS_unspecified) { |
| 8042 | // Complain about then remove the storage class specifier. |
| 8043 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 8044 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8045 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 8046 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8047 | } |
| 8048 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 8049 | // C++0x [temp.explicit]p1: |
| 8050 | // [...] An explicit instantiation of a function template shall not use the |
| 8051 | // inline or constexpr specifiers. |
| 8052 | // Presumably, this also applies to member functions of class templates as |
| 8053 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 8054 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8055 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8056 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 8057 | diag::err_explicit_instantiation_inline : |
| 8058 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 8059 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8060 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 8061 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 8062 | // not already specified. |
| 8063 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 8064 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8065 | |
Nathan Wilson | de49845 | 2016-02-08 05:34:00 +0000 | [diff] [blame] | 8066 | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
| 8067 | // applied only to the definition of a function template or variable template, |
| 8068 | // declared in namespace scope. |
| 8069 | if (D.getDeclSpec().isConceptSpecified()) { |
| 8070 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
| 8071 | diag::err_concept_specified_specialization) << 0; |
| 8072 | return true; |
| 8073 | } |
| 8074 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8075 | // C++0x [temp.explicit]p2: |
| 8076 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8077 | // definition and an explicit instantiation declaration. An explicit |
| 8078 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8079 | TemplateSpecializationKind TSK |
| 8080 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 8081 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8082 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8083 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8084 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8085 | |
| 8086 | if (!R->isFunctionType()) { |
| 8087 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8088 | // A [...] static data member of a class template can be explicitly |
| 8089 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8090 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8091 | // C++1y [temp.explicit]p1: |
| 8092 | // A [...] variable [...] template specialization can be explicitly |
| 8093 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8094 | if (Previous.isAmbiguous()) |
| 8095 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8096 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 8097 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8098 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8099 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8100 | if (!PrevTemplate) { |
| 8101 | if (!Prev || !Prev->isStaticDataMember()) { |
| 8102 | // We expect to see a data data member here. |
| 8103 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 8104 | << Name; |
| 8105 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8106 | P != PEnd; ++P) |
| 8107 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 8108 | return true; |
| 8109 | } |
| 8110 | |
| 8111 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 8112 | // FIXME: Check for explicit specialization? |
| 8113 | Diag(D.getIdentifierLoc(), |
| 8114 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 8115 | << Prev; |
| 8116 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 8117 | // FIXME: Can we provide a note showing where this was declared? |
| 8118 | return true; |
| 8119 | } |
| 8120 | } else { |
| 8121 | // Explicitly instantiate a variable template. |
| 8122 | |
| 8123 | // C++1y [dcl.spec.auto]p6: |
| 8124 | // ... A program that uses auto or decltype(auto) in a context not |
| 8125 | // explicitly allowed in this section is ill-formed. |
| 8126 | // |
| 8127 | // This includes auto-typed variable template instantiations. |
| 8128 | if (R->isUndeducedType()) { |
| 8129 | Diag(T->getTypeLoc().getLocStart(), |
| 8130 | diag::err_auto_not_allowed_var_inst); |
| 8131 | return true; |
| 8132 | } |
| 8133 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8134 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 8135 | // C++1y [temp.explicit]p3: |
| 8136 | // If the explicit instantiation is for a variable, the unqualified-id |
| 8137 | // in the declaration shall be a template-id. |
| 8138 | Diag(D.getIdentifierLoc(), |
| 8139 | diag::err_explicit_instantiation_without_template_id) |
| 8140 | << PrevTemplate; |
| 8141 | Diag(PrevTemplate->getLocation(), |
| 8142 | diag::note_explicit_instantiation_here); |
| 8143 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8144 | } |
| 8145 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8146 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8147 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8148 | if (PrevTemplate->isConcept()) { |
| 8149 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8150 | << 1 /*variable*/ << 0 /*explicitly instantiated*/; |
| 8151 | Diag(PrevTemplate->getLocation(), diag::note_previous_declaration); |
| 8152 | return true; |
| 8153 | } |
| 8154 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8155 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8156 | TemplateArgumentListInfo TemplateArgs = |
| 8157 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 8158 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8159 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 8160 | D.getIdentifierLoc(), TemplateArgs); |
| 8161 | if (Res.isInvalid()) |
| 8162 | return true; |
| 8163 | |
| 8164 | // Ignore access control bits, we don't need them for redeclaration |
| 8165 | // checking. |
| 8166 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8167 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8168 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8169 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8170 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8171 | // or a static data member of a class template specialization, the name of |
| 8172 | // the class template specialization in the qualified-id for the member |
| 8173 | // name shall be a simple-template-id. |
| 8174 | // |
| 8175 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8176 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 8177 | // This does not apply to variable template specializations, where the |
| 8178 | // template-id is in the unqualified-id instead. |
| 8179 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8180 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8181 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8182 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8183 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8184 | // Check the scope of this explicit instantiation. |
| 8185 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8186 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8187 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8188 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 8189 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8190 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8191 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8192 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8193 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8194 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8195 | if (!HasNoEffect) { |
| 8196 | // Instantiate static data member or variable template. |
| 8197 | |
| 8198 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 8199 | if (PrevTemplate) { |
| 8200 | // Merge attributes. |
| 8201 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 8202 | ProcessDeclAttributeList(S, Prev, Attr); |
| 8203 | } |
| 8204 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8205 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 8206 | } |
| 8207 | |
| 8208 | // Check the new variable specialization against the parsed input. |
| 8209 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 8210 | Diag(T->getTypeLoc().getLocStart(), |
| 8211 | diag::err_invalid_var_template_spec_type) |
| 8212 | << 0 << PrevTemplate << R << Prev->getType(); |
| 8213 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 8214 | << 2 << PrevTemplate->getDeclName(); |
| 8215 | return true; |
| 8216 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8217 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8218 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8219 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8220 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8221 | |
| 8222 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 8223 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8224 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8225 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8226 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8227 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8228 | HasExplicitTemplateArgs = true; |
| 8229 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8230 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8231 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8232 | // A [...] function [...] can be explicitly instantiated from its template. |
| 8233 | // A member function [...] of a class template can be explicitly |
| 8234 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8235 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8236 | UnresolvedSet<8> Matches; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8237 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8238 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8239 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8240 | P != PEnd; ++P) { |
| 8241 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8242 | if (!HasExplicitTemplateArgs) { |
| 8243 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 8244 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 8245 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 8246 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8247 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8248 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8249 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8250 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 8251 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8252 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8253 | } |
| 8254 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8255 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8256 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 8257 | if (!FunTmpl) |
| 8258 | continue; |
| 8259 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8260 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8261 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8262 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8263 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8264 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 8265 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8266 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8267 | // Keep track of almost-matches. |
| 8268 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8269 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8270 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8271 | (void)TDK; |
| 8272 | continue; |
| 8273 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8274 | |
Artem Belevich | 64135c3 | 2016-12-08 19:38:13 +0000 | [diff] [blame] | 8275 | // Target attributes are part of the cuda function signature, so |
| 8276 | // the cuda target of the instantiated function must match that of its |
| 8277 | // template. Given that C++ template deduction does not take |
| 8278 | // target attributes into account, we reject candidates here that |
| 8279 | // have a different target. |
| 8280 | if (LangOpts.CUDA && |
| 8281 | IdentifyCUDATarget(Specialization, |
| 8282 | /* IgnoreImplicitHDAttributes = */ true) != |
| 8283 | IdentifyCUDATarget(Attr)) { |
| 8284 | FailedCandidates.addCandidate().set( |
| 8285 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 8286 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8287 | continue; |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 8288 | } |
| 8289 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8290 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8291 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8292 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8293 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8294 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 8295 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8296 | D.getIdentifierLoc(), |
| 8297 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 8298 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 8299 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8300 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8301 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8302 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8303 | |
| 8304 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 8305 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8306 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 8307 | // C++11 [except.spec]p4 |
| 8308 | // In an explicit instantiation an exception-specification may be specified, |
| 8309 | // but is not required. |
| 8310 | // If an exception-specification is specified in an explicit instantiation |
| 8311 | // directive, it shall be compatible with the exception-specifications of |
| 8312 | // other declarations of that function. |
| 8313 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 8314 | if (FPT->hasExceptionSpec()) { |
| 8315 | unsigned DiagID = |
| 8316 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 8317 | if (getLangOpts().MicrosoftExt) |
| 8318 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 8319 | bool Result = CheckEquivalentExceptionSpec( |
| 8320 | PDiag(DiagID) << Specialization->getType(), |
| 8321 | PDiag(diag::note_explicit_instantiation_here), |
| 8322 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 8323 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 8324 | // In Microsoft mode, mismatching exception specifications just cause a |
| 8325 | // warning. |
| 8326 | if (!getLangOpts().MicrosoftExt && Result) |
| 8327 | return true; |
| 8328 | } |
| 8329 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8330 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8331 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8332 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 8333 | << Specialization |
| 8334 | << (Specialization->getTemplateSpecializationKind() == |
| 8335 | TSK_ExplicitSpecialization); |
| 8336 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 8337 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8338 | } |
| 8339 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8340 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8341 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 8342 | PrevDecl = Specialization; |
| 8343 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8344 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8345 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8346 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8347 | PrevDecl, |
| 8348 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8349 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8350 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8351 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8352 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8353 | // FIXME: We may still want to build some representation of this |
| 8354 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8355 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8356 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8357 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 8358 | |
| 8359 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 8360 | if (Attr) |
| 8361 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8362 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8363 | if (Specialization->isDefined()) { |
| 8364 | // Let the ASTConsumer know that this function has been explicitly |
| 8365 | // instantiated now, and its linkage might have changed. |
| 8366 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 8367 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 8368 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8369 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8370 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8371 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8372 | // or a static data member of a class template specialization, the name of |
| 8373 | // the class template specialization in the qualified-id for the member |
| 8374 | // name shall be a simple-template-id. |
| 8375 | // |
| 8376 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8377 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8378 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8379 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8380 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8381 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8382 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8383 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8384 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8385 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8386 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8387 | if (FunTmpl && FunTmpl->isConcept() && |
| 8388 | !D.getDeclSpec().isConceptSpecified()) { |
| 8389 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8390 | << 0 /*function*/ << 0 /*explicitly instantiated*/; |
| 8391 | Diag(FunTmpl->getLocation(), diag::note_previous_declaration); |
| 8392 | return true; |
| 8393 | } |
| 8394 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8395 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8396 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8397 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8398 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8399 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8400 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8401 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8402 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8403 | } |
| 8404 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8405 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8406 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 8407 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 8408 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 8409 | // This has to hold, because SS is expected to be defined. |
| 8410 | assert(Name && "Expected a name in a dependent tag"); |
| 8411 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8412 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8413 | if (!NNS) |
| 8414 | return true; |
| 8415 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8416 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 8417 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8418 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 8419 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8420 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8421 | return true; |
| 8422 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8423 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8424 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8425 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8426 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8427 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8428 | // Create type-source location information for this type. |
| 8429 | TypeLocBuilder TLB; |
| 8430 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8431 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8432 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8433 | TL.setNameLoc(NameLoc); |
| 8434 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8435 | } |
| 8436 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8437 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8438 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 8439 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 8440 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8441 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8442 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8443 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8444 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8445 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8446 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8447 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8448 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8449 | << FixItHint::CreateRemoval(TypenameLoc); |
| 8450 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8451 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8452 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 8453 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 8454 | if (T.isNull()) |
| 8455 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8456 | |
| 8457 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8458 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8459 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8460 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8461 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8462 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8463 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8464 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8465 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8466 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8467 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8468 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8469 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8470 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8471 | } |
| 8472 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8473 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8474 | Sema::ActOnTypenameType(Scope *S, |
| 8475 | SourceLocation TypenameLoc, |
| 8476 | const CXXScopeSpec &SS, |
| 8477 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8478 | TemplateTy TemplateIn, |
| 8479 | SourceLocation TemplateNameLoc, |
| 8480 | SourceLocation LAngleLoc, |
| 8481 | ASTTemplateArgsPtr TemplateArgsIn, |
| 8482 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8483 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8484 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8485 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8486 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8487 | diag::ext_typename_outside_of_template) |
| 8488 | << FixItHint::CreateRemoval(TypenameLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8489 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8490 | // Translate the parser's template argument list in our AST format. |
| 8491 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8492 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8493 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8494 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8495 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 8496 | // Construct a dependent template specialization type. |
| 8497 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8498 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8499 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 8500 | DTN->getQualifier(), |
| 8501 | DTN->getIdentifier(), |
| 8502 | TemplateArgs); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8503 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8504 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8505 | TypeLocBuilder Builder; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8506 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8507 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8508 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 8509 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 8510 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8511 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8512 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8513 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8514 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8515 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8516 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 8517 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8518 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8519 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 8520 | if (T.isNull()) |
| 8521 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8522 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8523 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8524 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8525 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8526 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8527 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 8528 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8529 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8530 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8531 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8532 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8533 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8534 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 8535 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8536 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8537 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8538 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8539 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 8540 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 8541 | } |
| 8542 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8543 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8544 | /// Determine whether this failed name lookup should be treated as being |
| 8545 | /// disabled by a usage of std::enable_if. |
| 8546 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8547 | SourceRange &CondRange) { |
| 8548 | // We must be looking for a ::type... |
| 8549 | if (!II.isStr("type")) |
| 8550 | return false; |
| 8551 | |
| 8552 | // ... within an explicitly-written template specialization... |
| 8553 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8554 | return false; |
| 8555 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8556 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8557 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8558 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8559 | return false; |
| 8560 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8561 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8562 | |
| 8563 | // ... which names a complete class template declaration... |
| 8564 | const TemplateDecl *EnableIfDecl = |
| 8565 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8566 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8567 | return false; |
| 8568 | |
| 8569 | // ... called "enable_if". |
| 8570 | const IdentifierInfo *EnableIfII = |
| 8571 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8572 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8573 | return false; |
| 8574 | |
| 8575 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8576 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8577 | return true; |
| 8578 | } |
| 8579 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8580 | /// \brief Build the type that describes a C++ typename specifier, |
| 8581 | /// e.g., "typename T::type". |
| 8582 | QualType |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8583 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8584 | SourceLocation KeywordLoc, |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8585 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8586 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8587 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8588 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8589 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8590 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8591 | DeclContext *Ctx = computeDeclContext(SS); |
| 8592 | if (!Ctx) { |
| 8593 | // If the nested-name-specifier is dependent and couldn't be |
| 8594 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8595 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8596 | return Context.getDependentNameType(Keyword, |
| 8597 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8598 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8599 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8600 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8601 | // If the nested-name-specifier refers to the current instantiation, |
| 8602 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8603 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8604 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8605 | // 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] | 8606 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8607 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8608 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8609 | |
| 8610 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8611 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8612 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8613 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8614 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8615 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8616 | case LookupResult::NotFound: { |
| 8617 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8618 | // a more specific diagnostic. |
| 8619 | SourceRange CondRange; |
| 8620 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8621 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8622 | << Ctx << CondRange; |
| 8623 | return QualType(); |
| 8624 | } |
| 8625 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8626 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8627 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8628 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8629 | |
| 8630 | case LookupResult::FoundUnresolvedValue: { |
| 8631 | // We found a using declaration that is a value. Most likely, the using |
| 8632 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8633 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8634 | IILoc); |
| 8635 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8636 | << Name << Ctx << FullRange; |
| 8637 | if (UnresolvedUsingValueDecl *Using |
| 8638 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8639 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8640 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8641 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8642 | } |
| 8643 | } |
| 8644 | // Fall through to create a dependent typename type, from which we can recover |
| 8645 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8646 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8647 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8648 | // Okay, it's a member of an unknown instantiation. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8649 | return Context.getDependentNameType(Keyword, |
| 8650 | QualifierLoc.getNestedNameSpecifier(), |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8651 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8652 | |
| 8653 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8654 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8655 | // We found a type. Build an ElaboratedType, since the |
| 8656 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8657 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8658 | return Context.getElaboratedType(ETK_Typename, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8659 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8660 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8661 | } |
| 8662 | |
| 8663 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8664 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8665 | break; |
| 8666 | |
| 8667 | case LookupResult::FoundOverloaded: |
| 8668 | DiagID = diag::err_typename_nested_not_type; |
| 8669 | Referenced = *Result.begin(); |
| 8670 | break; |
| 8671 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8672 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8673 | return QualType(); |
| 8674 | } |
| 8675 | |
| 8676 | // If we get here, it's because name lookup did not find a |
| 8677 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8678 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8679 | IILoc); |
| 8680 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8681 | if (Referenced) |
| 8682 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8683 | << Name; |
| 8684 | return QualType(); |
| 8685 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8686 | |
| 8687 | namespace { |
| 8688 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8689 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8690 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8691 | SourceLocation Loc; |
| 8692 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8693 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8694 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8695 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8696 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8697 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8698 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8699 | DeclarationName Entity) |
| 8700 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8701 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8702 | |
| 8703 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8704 | /// transformed. |
| 8705 | /// |
| 8706 | /// For the purposes of type reconstruction, a type has already been |
| 8707 | /// transformed if it is NULL or if it is not dependent. |
| 8708 | bool AlreadyTransformed(QualType T) { |
| 8709 | return T.isNull() || !T->isDependentType(); |
| 8710 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8711 | |
| 8712 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8713 | /// rebuilt. |
| 8714 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8715 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8716 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8717 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8718 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8719 | /// \brief Sets the "base" location and entity when that |
| 8720 | /// information is known based on another transformation. |
| 8721 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8722 | this->Loc = Loc; |
| 8723 | this->Entity = Entity; |
| 8724 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8725 | |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8726 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8727 | // Lambdas never need to be transformed. |
| 8728 | return E; |
| 8729 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8730 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 8731 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8732 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8733 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8734 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8735 | /// 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] | 8736 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8737 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8738 | /// partial specialization thereof). This routine will rebuild that type now |
| 8739 | /// that we have entered the declarator's scope, which may produce different |
| 8740 | /// canonical types, e.g., |
| 8741 | /// |
| 8742 | /// \code |
| 8743 | /// template<typename T> |
| 8744 | /// struct X { |
| 8745 | /// typedef T* pointer; |
| 8746 | /// pointer data(); |
| 8747 | /// }; |
| 8748 | /// |
| 8749 | /// template<typename T> |
| 8750 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8751 | /// \endcode |
| 8752 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8753 | /// 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] | 8754 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8755 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8756 | /// 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] | 8757 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8758 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8759 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8760 | SourceLocation Loc, |
| 8761 | DeclarationName Name) { |
| 8762 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8763 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8764 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8765 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8766 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8767 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8768 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8769 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8770 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8771 | DeclarationName()); |
| 8772 | return Rebuilder.TransformExpr(E); |
| 8773 | } |
| 8774 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8775 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8776 | if (SS.isInvalid()) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8777 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8778 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8779 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8780 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8781 | DeclarationName()); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8782 | NestedNameSpecifierLoc Rebuilt |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8783 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8784 | if (!Rebuilt) |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8785 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8786 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8787 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8788 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8789 | } |
| 8790 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8791 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8792 | /// instantiation. |
| 8793 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8794 | TemplateParameterList *Params) { |
| 8795 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8796 | Decl *Param = Params->getParam(I); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8797 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8798 | // There is nothing to rebuild in a type parameter. |
| 8799 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8800 | continue; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8801 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8802 | // Rebuild the template parameter list of a template template parameter. |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8803 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8804 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8805 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8806 | TTP->getTemplateParameters())) |
| 8807 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8808 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8809 | continue; |
| 8810 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8811 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8812 | // Rebuild the type of a non-type template parameter. |
| 8813 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8814 | TypeSourceInfo *NewTSI |
| 8815 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8816 | NTTP->getLocation(), |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8817 | NTTP->getDeclName()); |
| 8818 | if (!NewTSI) |
| 8819 | return true; |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8820 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8821 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8822 | NTTP->setTypeSourceInfo(NewTSI); |
| 8823 | NTTP->setType(NewTSI->getType()); |
| 8824 | } |
| 8825 | } |
Simon Pilgrim | 6905d22 | 2016-12-30 22:55:33 +0000 | [diff] [blame] | 8826 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8827 | return false; |
| 8828 | } |
| 8829 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8830 | /// \brief Produces a formatted string that describes the binding of |
| 8831 | /// template parameters to template arguments. |
| 8832 | std::string |
| 8833 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8834 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8835 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8836 | } |
| 8837 | |
| 8838 | std::string |
| 8839 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8840 | const TemplateArgument *Args, |
| 8841 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8842 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8843 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8844 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8845 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8846 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8847 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8848 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8849 | if (I >= NumArgs) |
| 8850 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8851 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8852 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8853 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8854 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8855 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8856 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8857 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8858 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8859 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8860 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8861 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8862 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8863 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8864 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8865 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8866 | |
| 8867 | Out << ']'; |
| 8868 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8869 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8870 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8871 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8872 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8873 | if (!FD) |
| 8874 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8875 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 8876 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8877 | |
| 8878 | // Take tokens to avoid allocations |
| 8879 | LPT->Toks.swap(Toks); |
| 8880 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 8881 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8882 | |
| 8883 | FD->setLateTemplateParsed(true); |
| 8884 | } |
| 8885 | |
| 8886 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8887 | if (!FD) |
| 8888 | return; |
| 8889 | FD->setLateTemplateParsed(false); |
| 8890 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8891 | |
| 8892 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8893 | DeclContext *DC = CurContext; |
| 8894 | |
| 8895 | while (DC) { |
| 8896 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8897 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8898 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8899 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8900 | return false; |
| 8901 | |
| 8902 | DC = DC->getParent(); |
| 8903 | } |
| 8904 | return false; |
| 8905 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8906 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 8907 | namespace { |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8908 | /// \brief Walk the path from which a declaration was instantiated, and check |
| 8909 | /// that every explicit specialization along that path is visible. This enforces |
| 8910 | /// C++ [temp.expl.spec]/6: |
| 8911 | /// |
| 8912 | /// If a template, a member template or a member of a class template is |
| 8913 | /// explicitly specialized then that specialization shall be declared before |
| 8914 | /// the first use of that specialization that would cause an implicit |
| 8915 | /// instantiation to take place, in every translation unit in which such a |
| 8916 | /// use occurs; no diagnostic is required. |
| 8917 | /// |
| 8918 | /// and also C++ [temp.class.spec]/1: |
| 8919 | /// |
| 8920 | /// A partial specialization shall be declared before the first use of a |
| 8921 | /// class template specialization that would make use of the partial |
| 8922 | /// specialization as the result of an implicit or explicit instantiation |
| 8923 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 8924 | /// required. |
| 8925 | class ExplicitSpecializationVisibilityChecker { |
| 8926 | Sema &S; |
| 8927 | SourceLocation Loc; |
| 8928 | llvm::SmallVector<Module *, 8> Modules; |
| 8929 | |
| 8930 | public: |
| 8931 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 8932 | : S(S), Loc(Loc) {} |
| 8933 | |
| 8934 | void check(NamedDecl *ND) { |
| 8935 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 8936 | return checkImpl(FD); |
| 8937 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 8938 | return checkImpl(RD); |
| 8939 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 8940 | return checkImpl(VD); |
| 8941 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 8942 | return checkImpl(ED); |
| 8943 | } |
| 8944 | |
| 8945 | private: |
| 8946 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 8947 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 8948 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 8949 | const bool Recover = true; |
| 8950 | |
| 8951 | // If we got a custom set of modules (because only a subset of the |
| 8952 | // declarations are interesting), use them, otherwise let |
| 8953 | // diagnoseMissingImport intelligently pick some. |
| 8954 | if (Modules.empty()) |
| 8955 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 8956 | else |
| 8957 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 8958 | } |
| 8959 | |
| 8960 | // Check a specific declaration. There are three problematic cases: |
| 8961 | // |
| 8962 | // 1) The declaration is an explicit specialization of a template |
| 8963 | // specialization. |
| 8964 | // 2) The declaration is an explicit specialization of a member of an |
| 8965 | // templated class. |
| 8966 | // 3) The declaration is an instantiation of a template, and that template |
| 8967 | // is an explicit specialization of a member of a templated class. |
| 8968 | // |
| 8969 | // We don't need to go any deeper than that, as the instantiation of the |
| 8970 | // surrounding class / etc is not triggered by whatever triggered this |
| 8971 | // instantiation, and thus should be checked elsewhere. |
| 8972 | template<typename SpecDecl> |
| 8973 | void checkImpl(SpecDecl *Spec) { |
| 8974 | bool IsHiddenExplicitSpecialization = false; |
| 8975 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 8976 | IsHiddenExplicitSpecialization = |
| 8977 | Spec->getMemberSpecializationInfo() |
| 8978 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
| 8979 | : !S.hasVisibleDeclaration(Spec); |
| 8980 | } else { |
| 8981 | checkInstantiated(Spec); |
| 8982 | } |
| 8983 | |
| 8984 | if (IsHiddenExplicitSpecialization) |
| 8985 | diagnose(Spec->getMostRecentDecl(), false); |
| 8986 | } |
| 8987 | |
| 8988 | void checkInstantiated(FunctionDecl *FD) { |
| 8989 | if (auto *TD = FD->getPrimaryTemplate()) |
| 8990 | checkTemplate(TD); |
| 8991 | } |
| 8992 | |
| 8993 | void checkInstantiated(CXXRecordDecl *RD) { |
| 8994 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 8995 | if (!SD) |
| 8996 | return; |
| 8997 | |
| 8998 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 8999 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 9000 | checkTemplate(TD); |
| 9001 | else if (auto *TD = |
| 9002 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 9003 | if (!S.hasVisibleDeclaration(TD)) |
| 9004 | diagnose(TD, true); |
| 9005 | checkTemplate(TD); |
| 9006 | } |
| 9007 | } |
| 9008 | |
| 9009 | void checkInstantiated(VarDecl *RD) { |
| 9010 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 9011 | if (!SD) |
| 9012 | return; |
| 9013 | |
| 9014 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 9015 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 9016 | checkTemplate(TD); |
| 9017 | else if (auto *TD = |
| 9018 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 9019 | if (!S.hasVisibleDeclaration(TD)) |
| 9020 | diagnose(TD, true); |
| 9021 | checkTemplate(TD); |
| 9022 | } |
| 9023 | } |
| 9024 | |
| 9025 | void checkInstantiated(EnumDecl *FD) {} |
| 9026 | |
| 9027 | template<typename TemplDecl> |
| 9028 | void checkTemplate(TemplDecl *TD) { |
| 9029 | if (TD->isMemberSpecialization()) { |
| 9030 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 9031 | diagnose(TD->getMostRecentDecl(), false); |
| 9032 | } |
| 9033 | } |
| 9034 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 9035 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 9036 | |
| 9037 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 9038 | if (!getLangOpts().Modules) |
| 9039 | return; |
| 9040 | |
| 9041 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 9042 | } |
| 9043 | |
| 9044 | /// \brief Check whether a template partial specialization that we've discovered |
| 9045 | /// is hidden, and produce suitable diagnostics if so. |
| 9046 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 9047 | NamedDecl *Spec) { |
| 9048 | llvm::SmallVector<Module *, 8> Modules; |
| 9049 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 9050 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 9051 | MissingImportKind::PartialSpecialization, |
| 9052 | /*Recover*/true); |
| 9053 | } |