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 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 91 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
| 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(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 98 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
| 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; |
| 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"); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 268 | |
| 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); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 315 | |
| 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 | |
| 432 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && |
| 433 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 434 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 435 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 436 | // Since the 'this' expression is synthesized, we don't need to |
| 437 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 438 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 439 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 440 | return CXXDependentScopeMemberExpr::Create( |
| 441 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 442 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 443 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 446 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 447 | } |
| 448 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 449 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 450 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 451 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 452 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 453 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 454 | return DependentScopeDeclRefExpr::Create( |
| 455 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 456 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 459 | |
| 460 | /// Determine whether we would be unable to instantiate this template (because |
| 461 | /// it either has no definition, or is in the process of being instantiated). |
| 462 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 463 | NamedDecl *Instantiation, |
| 464 | bool InstantiatedFromMember, |
| 465 | const NamedDecl *Pattern, |
| 466 | const NamedDecl *PatternDef, |
| 467 | TemplateSpecializationKind TSK, |
| 468 | bool Complain /*= true*/) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 469 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 470 | isa<VarDecl>(Instantiation)); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 471 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 472 | bool IsEntityBeingDefined = false; |
| 473 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 474 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 475 | |
| 476 | if (PatternDef && !IsEntityBeingDefined) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 477 | NamedDecl *SuggestedDef = nullptr; |
| 478 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 479 | /*OnlyNeedComplete*/false)) { |
| 480 | // If we're allowed to diagnose this and recover, do so. |
| 481 | bool Recover = Complain && !isSFINAEContext(); |
| 482 | if (Complain) |
| 483 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 484 | Sema::MissingImportKind::Definition, Recover); |
| 485 | return !Recover; |
| 486 | } |
| 487 | return false; |
| 488 | } |
| 489 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 490 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 491 | return true; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 492 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 493 | llvm::Optional<unsigned> Note; |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 494 | QualType InstantiationTy; |
| 495 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 496 | InstantiationTy = Context.getTypeDeclType(TD); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 497 | if (PatternDef) { |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 498 | Diag(PointOfInstantiation, |
| 499 | diag::err_template_instantiate_within_definition) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 500 | << /*implicit|explicit*/(TSK != TSK_ImplicitInstantiation) |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 501 | << InstantiationTy; |
| 502 | // Not much point in noting the template declaration here, since |
| 503 | // we're lexically inside it. |
| 504 | Instantiation->setInvalidDecl(); |
| 505 | } else if (InstantiatedFromMember) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 506 | if (isa<FunctionDecl>(Instantiation)) { |
| 507 | Diag(PointOfInstantiation, |
| 508 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 509 | << /*member function*/ 1 << Instantiation->getDeclName() |
| 510 | << Instantiation->getDeclContext(); |
| 511 | Note = diag::note_explicit_instantiation_here; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 512 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 513 | assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 514 | Diag(PointOfInstantiation, |
| 515 | diag::err_implicit_instantiate_member_undefined) |
| 516 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 517 | Note = diag::note_member_declared_at; |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 518 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 519 | } else { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 520 | if (isa<FunctionDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 521 | Diag(PointOfInstantiation, |
| 522 | diag::err_explicit_instantiation_undefined_func_template) |
| 523 | << Pattern; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 524 | Note = diag::note_explicit_instantiation_here; |
| 525 | } else if (isa<TagDecl>(Instantiation)) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 526 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 527 | << (TSK != TSK_ImplicitInstantiation) |
| 528 | << InstantiationTy; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 529 | Note = diag::note_template_decl_here; |
| 530 | } else { |
| 531 | assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 532 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 533 | Diag(PointOfInstantiation, |
| 534 | diag::err_explicit_instantiation_undefined_var_template) |
| 535 | << Instantiation; |
| 536 | Instantiation->setInvalidDecl(); |
| 537 | } else |
| 538 | Diag(PointOfInstantiation, |
| 539 | diag::err_explicit_instantiation_undefined_member) |
| 540 | << /*static data member*/ 2 << Instantiation->getDeclName() |
| 541 | << Instantiation->getDeclContext(); |
| 542 | Note = diag::note_explicit_instantiation_here; |
| 543 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 544 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 545 | if (Note) // Diagnostics were emitted. |
| 546 | Diag(Pattern->getLocation(), Note.getValue()); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 547 | |
| 548 | // In general, Instantiation isn't marked invalid to get more than one |
| 549 | // error for multiple undefined instantiations. But the code that does |
| 550 | // explicit declaration -> explicit definition conversion can't handle |
| 551 | // invalid declarations, so mark as invalid in that case. |
| 552 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 553 | Instantiation->setInvalidDecl(); |
| 554 | return true; |
| 555 | } |
| 556 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 557 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 558 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 559 | /// declaration at location Loc. Returns true to indicate that this is |
| 560 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 561 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 562 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 563 | |
| 564 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 565 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 566 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 567 | |
| 568 | // C++ [temp.local]p4: |
| 569 | // A template-parameter shall not be redeclared within its |
| 570 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 572 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 573 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 576 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 577 | /// the parameter D to reference the templated declaration and return a pointer |
| 578 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 579 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 580 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 581 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 582 | return Temp; |
| 583 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 584 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 587 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 588 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 589 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 590 | "Only template template arguments can be pack expansions here"); |
| 591 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 592 | "Template template argument pack expansion without packs"); |
| 593 | ParsedTemplateArgument Result(*this); |
| 594 | Result.EllipsisLoc = EllipsisLoc; |
| 595 | return Result; |
| 596 | } |
| 597 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 598 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 599 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 601 | switch (Arg.getKind()) { |
| 602 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 603 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 604 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 605 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 606 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 607 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 608 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 610 | case ParsedTemplateArgument::NonType: { |
| 611 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 612 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 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::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 616 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 617 | TemplateArgument TArg; |
| 618 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 619 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 620 | else |
| 621 | TArg = Template; |
| 622 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 623 | Arg.getScopeSpec().getWithLocInContext( |
| 624 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 625 | Arg.getLocation(), |
| 626 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 629 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 630 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 631 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 632 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 633 | /// \brief Translates template arguments as provided by the parser |
| 634 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 635 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 636 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 637 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 638 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 639 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 640 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 641 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 642 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 643 | SourceLocation Loc, |
| 644 | IdentifierInfo *Name) { |
| 645 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 646 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 647 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 648 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 649 | } |
| 650 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 651 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 652 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 653 | /// the keyword "typename" was used to declare the type parameter |
| 654 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 655 | /// "class" or "typename" keyword. ParamName is the name of the |
| 656 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 657 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 658 | /// If the type parameter has a default argument, it will be added |
| 659 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 660 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 661 | SourceLocation EllipsisLoc, |
| 662 | SourceLocation KeyLoc, |
| 663 | IdentifierInfo *ParamName, |
| 664 | SourceLocation ParamNameLoc, |
| 665 | unsigned Depth, unsigned Position, |
| 666 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 667 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | assert(S->isTemplateParamScope() && |
| 669 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 671 | SourceLocation Loc = ParamNameLoc; |
| 672 | if (!ParamName) |
| 673 | Loc = KeyLoc; |
| 674 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 675 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 676 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 677 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 678 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 679 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 680 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 681 | |
| 682 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 683 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 684 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 685 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 686 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 687 | IdResolver.AddDecl(Param); |
| 688 | } |
| 689 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 690 | // C++0x [temp.param]p9: |
| 691 | // A default template-argument may be specified for any kind of |
| 692 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 693 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 694 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 695 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 698 | // Handle the default argument, if provided. |
| 699 | if (DefaultArg) { |
| 700 | TypeSourceInfo *DefaultTInfo; |
| 701 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 703 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 704 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 705 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 706 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 707 | UPPC_DefaultArgument)) |
| 708 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 709 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 710 | // Check the template argument itself. |
| 711 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 712 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 713 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 714 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 715 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 716 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 717 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 718 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 719 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 722 | /// \brief Check that the type of a non-type template parameter is |
| 723 | /// well-formed. |
| 724 | /// |
| 725 | /// \returns the (possibly-promoted) parameter type if valid; |
| 726 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | QualType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 728 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 729 | // We don't allow variably-modified types as the type of non-type template |
| 730 | // parameters. |
| 731 | if (T->isVariablyModifiedType()) { |
| 732 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 733 | << T; |
| 734 | return QualType(); |
| 735 | } |
| 736 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 737 | // C++ [temp.param]p4: |
| 738 | // |
| 739 | // A non-type template-parameter shall have one of the following |
| 740 | // (optionally cv-qualified) types: |
| 741 | // |
| 742 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 743 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 745 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 747 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 748 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 749 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 750 | // -- std::nullptr_t. |
| 751 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 752 | // If T is a dependent type, we can't do the check now, so we |
| 753 | // assume that it is well-formed. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 754 | T->isDependentType() || |
| 755 | // Allow use of auto in template parameter declarations. |
| 756 | T->isUndeducedType()) { |
| 757 | if (T->isUndeducedType()) { |
| 758 | Diag(Loc, diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 759 | << QualType(T->getContainedAutoType(), 0); |
| 760 | } |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 761 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 762 | // are ignored when determining its type. |
| 763 | return T.getUnqualifiedType(); |
| 764 | } |
| 765 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 766 | // C++ [temp.param]p8: |
| 767 | // |
| 768 | // A non-type template-parameter of type "array of T" or |
| 769 | // "function returning T" is adjusted to be of type "pointer to |
| 770 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 771 | else if (T->isArrayType() || T->isFunctionType()) |
| 772 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 774 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 775 | << T; |
| 776 | |
| 777 | return QualType(); |
| 778 | } |
| 779 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 780 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 781 | unsigned Depth, |
| 782 | unsigned Position, |
| 783 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 784 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 785 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 786 | QualType T = TInfo->getType(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 787 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 788 | assert(S->isTemplateParamScope() && |
| 789 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 790 | bool Invalid = false; |
| 791 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 792 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 793 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 794 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 795 | Invalid = true; |
| 796 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 798 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 799 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 800 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 801 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 802 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 803 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 804 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 805 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 806 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 808 | if (Invalid) |
| 809 | Param->setInvalidDecl(); |
| 810 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 811 | if (ParamName) { |
| 812 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 813 | ParamName); |
| 814 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 815 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 816 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 817 | IdResolver.AddDecl(Param); |
| 818 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 820 | // C++0x [temp.param]p9: |
| 821 | // A default template-argument may be specified for any kind of |
| 822 | // template-parameter that is not a template parameter pack. |
| 823 | if (Default && IsParameterPack) { |
| 824 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 825 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 828 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 829 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 830 | // Check for unexpanded parameter packs. |
| 831 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 832 | return Param; |
| 833 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 834 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 835 | ExprResult DefaultRes = |
| 836 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 837 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 838 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 839 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 840 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 841 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 842 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 843 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 844 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 845 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 846 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 847 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 849 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 850 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 851 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 852 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 853 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 854 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 855 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 856 | IdentifierInfo *Name, |
| 857 | SourceLocation NameLoc, |
| 858 | unsigned Depth, |
| 859 | unsigned Position, |
| 860 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 861 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 862 | assert(S->isTemplateParamScope() && |
| 863 | "Template template parameter not in template parameter scope!"); |
| 864 | |
| 865 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 866 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 867 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 868 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 869 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 870 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 871 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 872 | Param->setAccess(AS_public); |
| 873 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 874 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 875 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 876 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 877 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 878 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 879 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 880 | IdResolver.AddDecl(Param); |
| 881 | } |
| 882 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 883 | if (Params->size() == 0) { |
| 884 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 885 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 886 | Param->setInvalidDecl(); |
| 887 | } |
| 888 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 889 | // C++0x [temp.param]p9: |
| 890 | // A default template-argument may be specified for any kind of |
| 891 | // template-parameter that is not a template parameter pack. |
| 892 | if (IsParameterPack && !Default.isInvalid()) { |
| 893 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 894 | Default = ParsedTemplateArgument(); |
| 895 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 896 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 897 | if (!Default.isInvalid()) { |
| 898 | // Check only that we have a template template argument. We don't want to |
| 899 | // try to check well-formedness now, because our template template parameter |
| 900 | // might have dependent types in its template parameters, which we wouldn't |
| 901 | // be able to match now. |
| 902 | // |
| 903 | // If none of the template template parameter's template arguments mention |
| 904 | // other template parameters, we could actually perform more checking here. |
| 905 | // However, it isn't worth doing. |
| 906 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 907 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 908 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 909 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 910 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 911 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 912 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 913 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 914 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 915 | DefaultArg.getArgument().getAsTemplate(), |
| 916 | UPPC_DefaultArgument)) |
| 917 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 918 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 919 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 920 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 921 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 922 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 925 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 926 | /// constrained by RequiresClause, that contains the template parameters in |
| 927 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 928 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 929 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 930 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 932 | SourceLocation LAngleLoc, |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 933 | ArrayRef<Decl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 934 | SourceLocation RAngleLoc, |
| 935 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 936 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 937 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 938 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 939 | return TemplateParameterList::Create( |
| 940 | Context, TemplateLoc, LAngleLoc, |
| 941 | llvm::makeArrayRef((NamedDecl *const *)Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 942 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 943 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 944 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 945 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 946 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 947 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 948 | } |
| 949 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 950 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 951 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 952 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 953 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 954 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 955 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 956 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 957 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 958 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 959 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 960 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 962 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 963 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 964 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 965 | |
| 966 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 967 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 968 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 969 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 970 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 971 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 972 | |
| 973 | // There is no such thing as an unnamed class template. |
| 974 | if (!Name) { |
| 975 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 976 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 979 | // Find any previous declaration with this name. For a friend with no |
| 980 | // scope explicitly specified, we only look for tag declarations (per |
| 981 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 982 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 983 | LookupResult Previous(*this, Name, NameLoc, |
| 984 | (SS.isEmpty() && TUK == TUK_Friend) |
| 985 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 986 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 987 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 988 | SemanticContext = computeDeclContext(SS, true); |
| 989 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 990 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 991 | // in the AST, and historically we have just ignored such friend |
| 992 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 993 | Diag(NameLoc, TUK == TUK_Friend |
| 994 | ? diag::warn_template_qualified_friend_ignored |
| 995 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 996 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 997 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 998 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1000 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 1001 | return true; |
| 1002 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 1003 | // If we're adding a template to a dependent context, we may need to |
| 1004 | // rebuilding some of the types used within the template parameter list, |
| 1005 | // now that we know what the current instantiation is. |
| 1006 | if (SemanticContext->isDependentContext()) { |
| 1007 | ContextRAII SavedContext(*this, SemanticContext); |
| 1008 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 1009 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1010 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 1011 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1012 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1013 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1014 | } else { |
| 1015 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 1016 | |
| 1017 | // C++14 [class.mem]p14: |
| 1018 | // If T is the name of a class, then each of the following shall have a |
| 1019 | // name different from T: |
| 1020 | // -- every member template of class T |
| 1021 | if (TUK != TUK_Friend && |
| 1022 | DiagnoseClassNameShadow(SemanticContext, |
| 1023 | DeclarationNameInfo(Name, NameLoc))) |
| 1024 | return true; |
| 1025 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1026 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 1027 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1029 | if (Previous.isAmbiguous()) |
| 1030 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1031 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1032 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1033 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1034 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1035 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1036 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1037 | // Maybe we will complain about the shadowed template parameter. |
| 1038 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1039 | // Just pretend that we didn't see the previous declaration. |
| 1040 | PrevDecl = nullptr; |
| 1041 | } |
| 1042 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1043 | // If there is a previous declaration with the same name, check |
| 1044 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1046 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1047 | |
| 1048 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1049 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1050 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1051 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1052 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1053 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1054 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1055 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1056 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1057 | PrevClassTemplate |
| 1058 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1059 | ->getSpecializedTemplate(); |
| 1060 | } |
| 1061 | } |
| 1062 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1063 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1064 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1065 | // [...] When looking for a prior declaration of a class or a function |
| 1066 | // 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] | 1067 | // function is neither a qualified name nor a template-id, scopes outside |
| 1068 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1069 | if (!SS.isSet()) { |
| 1070 | DeclContext *OutermostContext = CurContext; |
| 1071 | while (!OutermostContext->isFileContext()) |
| 1072 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1073 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 1074 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1075 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1076 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1077 | SemanticContext = PrevDecl->getDeclContext(); |
| 1078 | } else { |
| 1079 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1080 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1081 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1082 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1083 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1084 | |
| 1085 | // Check that the chosen semantic context doesn't already contain a |
| 1086 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1087 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1088 | DeclContext *LookupContext = SemanticContext; |
| 1089 | while (LookupContext->isTransparentContext()) |
| 1090 | LookupContext = LookupContext->getLookupParent(); |
| 1091 | LookupQualifiedName(Previous, LookupContext); |
| 1092 | |
| 1093 | if (Previous.isAmbiguous()) |
| 1094 | return true; |
| 1095 | |
| 1096 | if (Previous.begin() != Previous.end()) |
| 1097 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1098 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1099 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1100 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1101 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1102 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1103 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1104 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1105 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1106 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1107 | if (SS.isEmpty() && |
| 1108 | !(PrevClassTemplate && |
| 1109 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1110 | SemanticContext->getRedeclContext()))) { |
| 1111 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1112 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1113 | diag::note_using_decl_target); |
| 1114 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1115 | // Recover by ignoring the old declaration. |
| 1116 | PrevDecl = PrevClassTemplate = nullptr; |
| 1117 | } |
| 1118 | } |
| 1119 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1120 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1121 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1122 | // for a friend in a dependent context: the template parameter list itself |
| 1123 | // could be dependent. |
| 1124 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1125 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1126 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1127 | /*Complain=*/true, |
| 1128 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1129 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1130 | |
| 1131 | // C++ [temp.class]p4: |
| 1132 | // In a redeclaration, partial specialization, explicit |
| 1133 | // specialization or explicit instantiation of a class template, |
| 1134 | // the class-key shall agree in kind with the original class |
| 1135 | // template declaration (7.1.5.3). |
| 1136 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1137 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1138 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1140 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1141 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1142 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1143 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1146 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1147 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1148 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1149 | // If we have a prior definition that is not visible, treat this as |
| 1150 | // simply making that previous definition visible. |
| 1151 | NamedDecl *Hidden = nullptr; |
| 1152 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1153 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1154 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1155 | assert(Tmpl && "original definition of a class template is not a " |
| 1156 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1157 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1158 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1159 | return Def; |
| 1160 | } |
| 1161 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1162 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1163 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1164 | // FIXME: Would it make sense to try to "forget" the previous |
| 1165 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1166 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1167 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1168 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1169 | } else if (PrevDecl) { |
| 1170 | // C++ [temp]p5: |
| 1171 | // A class template shall not have the same name as any other |
| 1172 | // template, class, function, object, enumeration, enumerator, |
| 1173 | // namespace, or type in the same scope (3.3), except as specified |
| 1174 | // in (14.5.4). |
| 1175 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1176 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1177 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1180 | // Check the template parameter list of this declaration, possibly |
| 1181 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1182 | // template declaration. Skip this check for a friend in a dependent |
| 1183 | // context, because the template parameter list might be dependent. |
| 1184 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1185 | CheckTemplateParameterList( |
| 1186 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1187 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1188 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1189 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1190 | SemanticContext->isDependentContext()) |
| 1191 | ? TPC_ClassTemplateMember |
| 1192 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1193 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1194 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1196 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1197 | // 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] | 1198 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1199 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1200 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1201 | : diag::err_member_decl_does_not_match) |
| 1202 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1203 | Invalid = true; |
| 1204 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1208 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1210 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1211 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1212 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1213 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1214 | NewClass->setTemplateParameterListsInfo( |
| 1215 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1216 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1217 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1218 | // Add alignment attributes if necessary; these attributes are checked when |
| 1219 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1220 | if (TUK == TUK_Definition) { |
| 1221 | AddAlignmentAttributesForRecord(NewClass); |
| 1222 | AddMsStructLayoutForRecord(NewClass); |
| 1223 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1225 | ClassTemplateDecl *NewTemplate |
| 1226 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1227 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1228 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1229 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1230 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1231 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1232 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1233 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1234 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1235 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1236 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1237 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1238 | (void)T; |
| 1239 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1240 | // 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] | 1241 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1242 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1243 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1244 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1245 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1246 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1247 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1248 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1250 | // Set the lexical context of these templates |
| 1251 | NewClass->setLexicalDeclContext(CurContext); |
| 1252 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1253 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1254 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1255 | NewClass->startDefinition(); |
| 1256 | |
| 1257 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1258 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1259 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1260 | if (PrevClassTemplate) |
| 1261 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1262 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1263 | AddPushedVisibilityAttribute(NewClass); |
| 1264 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1265 | if (TUK != TUK_Friend) { |
| 1266 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1267 | Scope *Outer = S; |
| 1268 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1269 | Outer = Outer->getParent(); |
| 1270 | PushOnScopeChains(NewTemplate, Outer); |
| 1271 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1272 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1273 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1274 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1275 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1276 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1277 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1278 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1279 | // Friend templates are visible in fairly strange ways. |
| 1280 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1281 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1282 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1283 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1284 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1285 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1286 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1287 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1288 | FriendDecl *Friend = FriendDecl::Create( |
| 1289 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1290 | Friend->setAccess(AS_public); |
| 1291 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1292 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1293 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1294 | if (Invalid) { |
| 1295 | NewTemplate->setInvalidDecl(); |
| 1296 | NewClass->setInvalidDecl(); |
| 1297 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1298 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1299 | ActOnDocumentableDecl(NewTemplate); |
| 1300 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1301 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1304 | /// \brief Diagnose the presence of a default template argument on a |
| 1305 | /// template parameter, which is ill-formed in certain contexts. |
| 1306 | /// |
| 1307 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1308 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1309 | Sema::TemplateParamListContext TPC, |
| 1310 | SourceLocation ParamLoc, |
| 1311 | SourceRange DefArgRange) { |
| 1312 | switch (TPC) { |
| 1313 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1314 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1315 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1316 | return false; |
| 1317 | |
| 1318 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1319 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1320 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1321 | // A default template-argument shall not be specified in a |
| 1322 | // function template declaration or a function template |
| 1323 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1324 | // If a friend function template declaration specifies a default |
| 1325 | // template-argument, that declaration shall be a definition and shall be |
| 1326 | // the only declaration of the function template in the translation unit. |
| 1327 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1328 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1329 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1330 | : diag::ext_template_parameter_default_in_function_template) |
| 1331 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1332 | return false; |
| 1333 | |
| 1334 | case Sema::TPC_ClassTemplateMember: |
| 1335 | // C++0x [temp.param]p9: |
| 1336 | // A default template-argument shall not be specified in the |
| 1337 | // template-parameter-lists of the definition of a member of a |
| 1338 | // class template that appears outside of the member's class. |
| 1339 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1340 | << DefArgRange; |
| 1341 | return true; |
| 1342 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1343 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1344 | case Sema::TPC_FriendFunctionTemplate: |
| 1345 | // C++ [temp.param]p9: |
| 1346 | // A default template-argument shall not be specified in a |
| 1347 | // friend template declaration. |
| 1348 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1349 | << DefArgRange; |
| 1350 | return true; |
| 1351 | |
| 1352 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1353 | // for friend function templates if there is only a single |
| 1354 | // declaration (and it is a definition). Strange! |
| 1355 | } |
| 1356 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1357 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1360 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1361 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1362 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1363 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1364 | // A template template parameter which is a parameter pack is also a pack |
| 1365 | // expansion. |
| 1366 | if (TTP->isParameterPack()) |
| 1367 | return false; |
| 1368 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1369 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1370 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1371 | NamedDecl *P = Params->getParam(I); |
| 1372 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1373 | if (!NTTP->isParameterPack() && |
| 1374 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1375 | NTTP->getTypeSourceInfo(), |
| 1376 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1377 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1378 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1379 | continue; |
| 1380 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1383 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1384 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1385 | return true; |
| 1386 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1387 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1388 | return false; |
| 1389 | } |
| 1390 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1391 | /// \brief Checks the validity of a template parameter list, possibly |
| 1392 | /// considering the template parameter list from a previous |
| 1393 | /// declaration. |
| 1394 | /// |
| 1395 | /// If an "old" template parameter list is provided, it must be |
| 1396 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1397 | /// template parameter list. |
| 1398 | /// |
| 1399 | /// \param NewParams Template parameter list for a new template |
| 1400 | /// declaration. This template parameter list will be updated with any |
| 1401 | /// default arguments that are carried through from the previous |
| 1402 | /// template parameter list. |
| 1403 | /// |
| 1404 | /// \param OldParams If provided, template parameter list from a |
| 1405 | /// previous declaration of the same template. Default template |
| 1406 | /// arguments will be merged from the old template parameter list to |
| 1407 | /// the new template parameter list. |
| 1408 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1409 | /// \param TPC Describes the context in which we are checking the given |
| 1410 | /// template parameter list. |
| 1411 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1412 | /// \returns true if an error occurred, false otherwise. |
| 1413 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1414 | TemplateParameterList *OldParams, |
| 1415 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1416 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1418 | // C++ [temp.param]p10: |
| 1419 | // The set of default template-arguments available for use with a |
| 1420 | // template declaration or definition is obtained by merging the |
| 1421 | // default arguments from the definition (if in scope) and all |
| 1422 | // declarations in scope in the same way default function |
| 1423 | // arguments are (8.3.6). |
| 1424 | bool SawDefaultArgument = false; |
| 1425 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1426 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1427 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1428 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1429 | if (OldParams) |
| 1430 | OldParam = OldParams->begin(); |
| 1431 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1432 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1433 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1434 | NewParamEnd = NewParams->end(); |
| 1435 | NewParam != NewParamEnd; ++NewParam) { |
| 1436 | // Variables used to diagnose redundant default arguments |
| 1437 | bool RedundantDefaultArg = false; |
| 1438 | SourceLocation OldDefaultLoc; |
| 1439 | SourceLocation NewDefaultLoc; |
| 1440 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1441 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1442 | bool MissingDefaultArg = false; |
| 1443 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1444 | // Variable used to diagnose non-final parameter packs |
| 1445 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1446 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1447 | if (TemplateTypeParmDecl *NewTypeParm |
| 1448 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1449 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1450 | if (NewTypeParm->hasDefaultArgument() && |
| 1451 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1452 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1453 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1454 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1455 | NewTypeParm->removeDefaultArgument(); |
| 1456 | |
| 1457 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1459 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1460 | if (NewTypeParm->isParameterPack()) { |
| 1461 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1462 | "Parameter packs can't have a default argument!"); |
| 1463 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1464 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1465 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1466 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1467 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1468 | SawDefaultArgument = true; |
| 1469 | RedundantDefaultArg = true; |
| 1470 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1471 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1472 | // Merge the default argument from the old declaration to the |
| 1473 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1474 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1475 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1476 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1477 | SawDefaultArgument = true; |
| 1478 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1479 | } else if (SawDefaultArgument) |
| 1480 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1481 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1482 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1483 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1484 | if (!NewNonTypeParm->isParameterPack() && |
| 1485 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1486 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1487 | UPPC_NonTypeTemplateParameterType)) { |
| 1488 | Invalid = true; |
| 1489 | continue; |
| 1490 | } |
| 1491 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1492 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1493 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1494 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1495 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1496 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1497 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1500 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1501 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1502 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1503 | if (NewNonTypeParm->isParameterPack()) { |
| 1504 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1505 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1506 | if (!NewNonTypeParm->isPackExpansion()) |
| 1507 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1508 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1509 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1510 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1511 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1512 | SawDefaultArgument = true; |
| 1513 | RedundantDefaultArg = true; |
| 1514 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1515 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1516 | // Merge the default argument from the old declaration to the |
| 1517 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1518 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1519 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1520 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1521 | SawDefaultArgument = true; |
| 1522 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1523 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1525 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1526 | TemplateTemplateParmDecl *NewTemplateParm |
| 1527 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1528 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1529 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1530 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1531 | Invalid = true; |
| 1532 | continue; |
| 1533 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1534 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1535 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1536 | if (NewTemplateParm->hasDefaultArgument() && |
| 1537 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1538 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1539 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1540 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1541 | |
| 1542 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1543 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1544 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1545 | if (NewTemplateParm->isParameterPack()) { |
| 1546 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1547 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1548 | if (!NewTemplateParm->isPackExpansion()) |
| 1549 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1550 | } else if (OldTemplateParm && |
| 1551 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 1552 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1553 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1554 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1555 | SawDefaultArgument = true; |
| 1556 | RedundantDefaultArg = true; |
| 1557 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1558 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1559 | // Merge the default argument from the old declaration to the |
| 1560 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1561 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1562 | PreviousDefaultArgLoc |
| 1563 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1564 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1565 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1566 | PreviousDefaultArgLoc |
| 1567 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1568 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1572 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1573 | // If a template parameter of a primary class template or alias template |
| 1574 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1575 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1576 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1577 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1578 | Diag((*NewParam)->getLocation(), |
| 1579 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1580 | Invalid = true; |
| 1581 | } |
| 1582 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1583 | if (RedundantDefaultArg) { |
| 1584 | // C++ [temp.param]p12: |
| 1585 | // A template-parameter shall not be given default arguments |
| 1586 | // by two different declarations in the same scope. |
| 1587 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1588 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1589 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1590 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1591 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1592 | // If a template-parameter of a class template has a default |
| 1593 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1594 | // have a default template-argument supplied or be a template parameter |
| 1595 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1597 | diag::err_template_param_default_arg_missing); |
| 1598 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1599 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1600 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | // If we have an old template parameter list that we're merging |
| 1604 | // in, move on to the next parameter. |
| 1605 | if (OldParams) |
| 1606 | ++OldParam; |
| 1607 | } |
| 1608 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1609 | // We were missing some default arguments at the end of the list, so remove |
| 1610 | // all of the default arguments. |
| 1611 | if (RemoveDefaultArguments) { |
| 1612 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1613 | NewParamEnd = NewParams->end(); |
| 1614 | NewParam != NewParamEnd; ++NewParam) { |
| 1615 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1616 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1617 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1618 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1619 | NTTP->removeDefaultArgument(); |
| 1620 | else |
| 1621 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1622 | } |
| 1623 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1624 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1625 | return Invalid; |
| 1626 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1627 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1628 | namespace { |
| 1629 | |
| 1630 | /// A class which looks for a use of a certain level of template |
| 1631 | /// parameter. |
| 1632 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1633 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1634 | |
| 1635 | unsigned Depth; |
| 1636 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1637 | SourceLocation MatchLoc; |
| 1638 | |
| 1639 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1640 | |
| 1641 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1642 | NamedDecl *ND = Params->getParam(0); |
| 1643 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1644 | Depth = PD->getDepth(); |
| 1645 | } else if (NonTypeTemplateParmDecl *PD = |
| 1646 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1647 | Depth = PD->getDepth(); |
| 1648 | } else { |
| 1649 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1650 | } |
| 1651 | } |
| 1652 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1653 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1654 | if (ParmDepth >= Depth) { |
| 1655 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1656 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1657 | return true; |
| 1658 | } |
| 1659 | return false; |
| 1660 | } |
| 1661 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1662 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1663 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1664 | } |
| 1665 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1666 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1667 | return !Matches(T->getDepth()); |
| 1668 | } |
| 1669 | |
| 1670 | bool TraverseTemplateName(TemplateName N) { |
| 1671 | if (TemplateTemplateParmDecl *PD = |
| 1672 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1673 | if (Matches(PD->getDepth())) |
| 1674 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1675 | return super::TraverseTemplateName(N); |
| 1676 | } |
| 1677 | |
| 1678 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1679 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1680 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1681 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1682 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1683 | return super::VisitDeclRefExpr(E); |
| 1684 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1685 | |
| 1686 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1687 | return TraverseType(T->getReplacementType()); |
| 1688 | } |
| 1689 | |
| 1690 | bool |
| 1691 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1692 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1693 | } |
| 1694 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1695 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1696 | return TraverseType(T->getInjectedSpecializationType()); |
| 1697 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1698 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1699 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1700 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1701 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1702 | /// list. |
| 1703 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1704 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1705 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1706 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1707 | return Checker.Match; |
| 1708 | } |
| 1709 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1710 | // Find the source range corresponding to the named type in the given |
| 1711 | // nested-name-specifier, if any. |
| 1712 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1713 | QualType T, |
| 1714 | const CXXScopeSpec &SS) { |
| 1715 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1716 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1717 | if (const Type *CurType = NNS->getAsType()) { |
| 1718 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1719 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1720 | } else |
| 1721 | break; |
| 1722 | |
| 1723 | NNSLoc = NNSLoc.getPrefix(); |
| 1724 | } |
| 1725 | |
| 1726 | return SourceRange(); |
| 1727 | } |
| 1728 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1730 | /// specifier, returning the template parameter list that applies to the |
| 1731 | /// name. |
| 1732 | /// |
| 1733 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1734 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1736 | /// \param DeclLoc The location of the declaration itself. |
| 1737 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1738 | /// \param SS the scope specifier that will be matched to the given template |
| 1739 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1740 | /// being declared. |
| 1741 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1742 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1743 | /// is one. Used to check for a missing 'template<>'. |
| 1744 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1745 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1746 | /// innermost template parameter lists. |
| 1747 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1748 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1749 | /// matching template parameters to scope specifiers in friend |
| 1750 | /// declarations. |
| 1751 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1752 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1753 | /// declared is an explicit specialization, false otherwise. |
| 1754 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1756 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1757 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1759 | /// 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] | 1760 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1761 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1762 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1763 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1764 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1765 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1766 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1767 | Invalid = false; |
| 1768 | |
| 1769 | // The sequence of nested types to which we will match up the template |
| 1770 | // parameter lists. We first build this list by starting with the type named |
| 1771 | // 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] | 1772 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1773 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1774 | if (SS.getScopeRep()) { |
| 1775 | if (CXXRecordDecl *Record |
| 1776 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1777 | T = Context.getTypeDeclType(Record); |
| 1778 | else |
| 1779 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1780 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1781 | |
| 1782 | // If we found an explicit specialization that prevents us from needing |
| 1783 | // 'template<>' headers, this will be set to the location of that |
| 1784 | // explicit specialization. |
| 1785 | SourceLocation ExplicitSpecLoc; |
| 1786 | |
| 1787 | while (!T.isNull()) { |
| 1788 | NestedTypes.push_back(T); |
| 1789 | |
| 1790 | // Retrieve the parent of a record type. |
| 1791 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1792 | // If this type is an explicit specialization, we're done. |
| 1793 | if (ClassTemplateSpecializationDecl *Spec |
| 1794 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1795 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1796 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1797 | ExplicitSpecLoc = Spec->getLocation(); |
| 1798 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1799 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1800 | } else if (Record->getTemplateSpecializationKind() |
| 1801 | == TSK_ExplicitSpecialization) { |
| 1802 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1803 | break; |
| 1804 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1805 | |
| 1806 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1807 | T = Context.getTypeDeclType(Parent); |
| 1808 | else |
| 1809 | T = QualType(); |
| 1810 | continue; |
| 1811 | } |
| 1812 | |
| 1813 | if (const TemplateSpecializationType *TST |
| 1814 | = T->getAs<TemplateSpecializationType>()) { |
| 1815 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1816 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1817 | T = Context.getTypeDeclType(Parent); |
| 1818 | else |
| 1819 | T = QualType(); |
| 1820 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1821 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | // Look one step prior in a dependent template specialization type. |
| 1825 | if (const DependentTemplateSpecializationType *DependentTST |
| 1826 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1827 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1828 | T = QualType(NNS->getAsType(), 0); |
| 1829 | else |
| 1830 | T = QualType(); |
| 1831 | continue; |
| 1832 | } |
| 1833 | |
| 1834 | // Look one step prior in a dependent name type. |
| 1835 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1836 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1837 | T = QualType(NNS->getAsType(), 0); |
| 1838 | else |
| 1839 | T = QualType(); |
| 1840 | continue; |
| 1841 | } |
| 1842 | |
| 1843 | // Retrieve the parent of an enumeration type. |
| 1844 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1845 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1846 | // check here. |
| 1847 | EnumDecl *Enum = EnumT->getDecl(); |
| 1848 | |
| 1849 | // Get to the parent type. |
| 1850 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1851 | T = Context.getTypeDeclType(Parent); |
| 1852 | else |
| 1853 | T = QualType(); |
| 1854 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1855 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1857 | T = QualType(); |
| 1858 | } |
| 1859 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1860 | // to the innermost while checking template-parameter-lists. |
| 1861 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1862 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1863 | // C++0x [temp.expl.spec]p17: |
| 1864 | // A member or a member template may be nested within many |
| 1865 | // enclosing class templates. In an explicit specialization for |
| 1866 | // such a member, the member declaration shall be preceded by a |
| 1867 | // template<> for each enclosing class template that is |
| 1868 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1869 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1870 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1871 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1872 | if (SawNonEmptyTemplateParameterList) { |
| 1873 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1874 | << !Recovery << Range; |
| 1875 | Invalid = true; |
| 1876 | IsExplicitSpecialization = false; |
| 1877 | return true; |
| 1878 | } |
| 1879 | |
| 1880 | return false; |
| 1881 | }; |
| 1882 | |
| 1883 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1884 | // Check that we can have an explicit specialization here. |
| 1885 | if (CheckExplicitSpecialization(Range, true)) |
| 1886 | return true; |
| 1887 | |
| 1888 | // We don't have a template header, but we should. |
| 1889 | SourceLocation ExpectedTemplateLoc; |
| 1890 | if (!ParamLists.empty()) |
| 1891 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1892 | else |
| 1893 | ExpectedTemplateLoc = DeclStartLoc; |
| 1894 | |
| 1895 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1896 | << Range |
| 1897 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1898 | return false; |
| 1899 | }; |
| 1900 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1901 | unsigned ParamIdx = 0; |
| 1902 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1903 | ++TypeIdx) { |
| 1904 | T = NestedTypes[TypeIdx]; |
| 1905 | |
| 1906 | // Whether we expect a 'template<>' header. |
| 1907 | bool NeedEmptyTemplateHeader = false; |
| 1908 | |
| 1909 | // Whether we expect a template header with parameters. |
| 1910 | bool NeedNonemptyTemplateHeader = false; |
| 1911 | |
| 1912 | // For a dependent type, the set of template parameters that we |
| 1913 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1914 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1915 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1916 | // C++0x [temp.expl.spec]p15: |
| 1917 | // A member or a member template may be nested within many enclosing |
| 1918 | // class templates. In an explicit specialization for such a member, the |
| 1919 | // member declaration shall be preceded by a template<> for each |
| 1920 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1921 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1922 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1923 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1924 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1925 | NeedNonemptyTemplateHeader = true; |
| 1926 | } else if (Record->isDependentType()) { |
| 1927 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1928 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1929 | ->getTemplateParameters(); |
| 1930 | NeedNonemptyTemplateHeader = true; |
| 1931 | } |
| 1932 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1933 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1934 | // C++0x [temp.expl.spec]p4: |
| 1935 | // Members of an explicitly specialized class template are defined |
| 1936 | // in the same manner as members of normal classes, and not using |
| 1937 | // the template<> syntax. |
| 1938 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1939 | NeedEmptyTemplateHeader = true; |
| 1940 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1941 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1942 | } else if (Record->getTemplateSpecializationKind()) { |
| 1943 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1944 | != TSK_ExplicitSpecialization && |
| 1945 | TypeIdx == NumTypes - 1) |
| 1946 | IsExplicitSpecialization = true; |
| 1947 | |
| 1948 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1949 | } |
| 1950 | } else if (const TemplateSpecializationType *TST |
| 1951 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 1952 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1953 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1954 | NeedNonemptyTemplateHeader = true; |
| 1955 | } |
| 1956 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1957 | // FIXME: We actually could/should check the template arguments here |
| 1958 | // against the corresponding template parameter list. |
| 1959 | NeedNonemptyTemplateHeader = false; |
| 1960 | } |
| 1961 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1962 | // C++ [temp.expl.spec]p16: |
| 1963 | // In an explicit specialization declaration for a member of a class |
| 1964 | // template or a member template that ap- pears in namespace scope, the |
| 1965 | // member template and some of its enclosing class templates may remain |
| 1966 | // unspecialized, except that the declaration shall not explicitly |
| 1967 | // specialize a class member template if its en- closing class templates |
| 1968 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1969 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1970 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1971 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1972 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1973 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1974 | } else |
| 1975 | SawNonEmptyTemplateParameterList = true; |
| 1976 | } |
| 1977 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1978 | if (NeedEmptyTemplateHeader) { |
| 1979 | // If we're on the last of the types, and we need a 'template<>' header |
| 1980 | // here, then it's an explicit specialization. |
| 1981 | if (TypeIdx == NumTypes - 1) |
| 1982 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1983 | |
| 1984 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1985 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1986 | // The header has template parameters when it shouldn't. Complain. |
| 1987 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1988 | diag::err_template_param_list_matches_nontemplate) |
| 1989 | << T |
| 1990 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1991 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1992 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1993 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1994 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1995 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1996 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1997 | // Consume this template header. |
| 1998 | ++ParamIdx; |
| 1999 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2000 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2001 | |
| 2002 | if (!IsFriend) |
| 2003 | if (DiagnoseMissingExplicitSpecialization( |
| 2004 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2005 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2006 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2007 | continue; |
| 2008 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2009 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2010 | if (NeedNonemptyTemplateHeader) { |
| 2011 | // In friend declarations we can have template-ids which don't |
| 2012 | // depend on the corresponding template parameter lists. But |
| 2013 | // assume that empty parameter lists are supposed to match this |
| 2014 | // template-id. |
| 2015 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2016 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2017 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2018 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2019 | else |
| 2020 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 2022 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2023 | if (ParamIdx < ParamLists.size()) { |
| 2024 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2025 | if (ExpectedTemplateParams && |
| 2026 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2027 | ExpectedTemplateParams, |
| 2028 | true, TPL_TemplateMatch)) |
| 2029 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2030 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2031 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2032 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2033 | TPC_ClassTemplateMember)) |
| 2034 | Invalid = true; |
| 2035 | |
| 2036 | ++ParamIdx; |
| 2037 | continue; |
| 2038 | } |
| 2039 | |
| 2040 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2041 | << T |
| 2042 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2043 | Invalid = true; |
| 2044 | continue; |
| 2045 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2046 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2047 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2048 | // If there were at least as many template-ids as there were template |
| 2049 | // parameter lists, then there are no template parameter lists remaining for |
| 2050 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2051 | if (ParamIdx >= ParamLists.size()) { |
| 2052 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2053 | // We don't have a template header for the declaration itself, but we |
| 2054 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2055 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2056 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2057 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2058 | |
| 2059 | // Fabricate an empty template parameter list for the invented header. |
| 2060 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2061 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2062 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2065 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2066 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2068 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2069 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2070 | bool HasAnyExplicitSpecHeader = false; |
| 2071 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2072 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2073 | if (ParamLists[I]->size() == 0) |
| 2074 | HasAnyExplicitSpecHeader = true; |
| 2075 | else |
| 2076 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2077 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2078 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2079 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2080 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2081 | : diag::err_template_spec_extra_headers) |
| 2082 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2083 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2084 | |
| 2085 | // If there was a specialization somewhere, such that 'template<>' is |
| 2086 | // not required, and there were any 'template<>' headers, note where the |
| 2087 | // specialization occurred. |
| 2088 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 2089 | Diag(ExplicitSpecLoc, |
| 2090 | diag::note_explicit_template_spec_does_not_need_header) |
| 2091 | << NestedTypes.back(); |
| 2092 | |
| 2093 | // We have a template parameter list with no corresponding scope, which |
| 2094 | // means that the resulting template declaration can't be instantiated |
| 2095 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2096 | if (!AllExplicitSpecHeaders) |
| 2097 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2098 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2099 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2100 | // C++ [temp.expl.spec]p16: |
| 2101 | // In an explicit specialization declaration for a member of a class |
| 2102 | // template or a member template that ap- pears in namespace scope, the |
| 2103 | // member template and some of its enclosing class templates may remain |
| 2104 | // unspecialized, except that the declaration shall not explicitly |
| 2105 | // specialize a class member template if its en- closing class templates |
| 2106 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2107 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2108 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2109 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2110 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2111 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2112 | // Return the last template parameter list, which corresponds to the |
| 2113 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2114 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2117 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2118 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2119 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2120 | << (isa<FunctionTemplateDecl>(Template) |
| 2121 | ? 0 |
| 2122 | : isa<ClassTemplateDecl>(Template) |
| 2123 | ? 1 |
| 2124 | : isa<VarTemplateDecl>(Template) |
| 2125 | ? 2 |
| 2126 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2127 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2128 | return; |
| 2129 | } |
| 2130 | |
| 2131 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 2132 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 2133 | IEnd = OST->end(); |
| 2134 | I != IEnd; ++I) |
| 2135 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2136 | << 0 << (*I)->getDeclName(); |
| 2137 | |
| 2138 | return; |
| 2139 | } |
| 2140 | } |
| 2141 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2142 | static QualType |
| 2143 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2144 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2145 | SourceLocation TemplateLoc, |
| 2146 | TemplateArgumentListInfo &TemplateArgs) { |
| 2147 | ASTContext &Context = SemaRef.getASTContext(); |
| 2148 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2149 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2150 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2151 | // S<T, 0, ..., N-1>. |
| 2152 | |
| 2153 | // C++14 [inteseq.intseq]p1: |
| 2154 | // T shall be an integer type. |
| 2155 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2156 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2157 | diag::err_integer_sequence_integral_element_type); |
| 2158 | return QualType(); |
| 2159 | } |
| 2160 | |
| 2161 | // C++14 [inteseq.make]p1: |
| 2162 | // If N is negative the program is ill-formed. |
| 2163 | TemplateArgument NumArgsArg = Converted[2]; |
| 2164 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2165 | if (NumArgs < 0) { |
| 2166 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2167 | diag::err_integer_sequence_negative_length); |
| 2168 | return QualType(); |
| 2169 | } |
| 2170 | |
| 2171 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2172 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2173 | // The type argument gets reused as the first template argument in the |
| 2174 | // synthetic template argument list. |
| 2175 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2176 | // Expand N into 0 ... N-1. |
| 2177 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2178 | I < NumArgs; ++I) { |
| 2179 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2180 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2181 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2182 | } |
| 2183 | // The first template argument will be reused as the template decl that |
| 2184 | // our synthetic template arguments will be applied to. |
| 2185 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2186 | TemplateLoc, SyntheticTemplateArgs); |
| 2187 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2188 | |
| 2189 | case BTK__type_pack_element: |
| 2190 | // Specializations of |
| 2191 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2192 | // are treated like T_Index. |
| 2193 | assert(Converted.size() == 2 && |
| 2194 | "__type_pack_element should be given an index and a parameter pack"); |
| 2195 | |
| 2196 | // If the Index is out of bounds, the program is ill-formed. |
| 2197 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2198 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2199 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2200 | "type std::size_t, and hence be non-negative"); |
| 2201 | if (Index >= Ts.pack_size()) { |
| 2202 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2203 | diag::err_type_pack_element_out_of_bounds); |
| 2204 | return QualType(); |
| 2205 | } |
| 2206 | |
| 2207 | // We simply return the type at index `Index`. |
| 2208 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2209 | return Nth->getAsType(); |
| 2210 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2211 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2212 | } |
| 2213 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2214 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2215 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2216 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2217 | DependentTemplateName *DTN |
| 2218 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2219 | if (DTN && DTN->isIdentifier()) |
| 2220 | // When building a template-id where the template-name is dependent, |
| 2221 | // assume the template is a type template. Either our assumption is |
| 2222 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2223 | // dependent name is substituted. |
| 2224 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2225 | DTN->getQualifier(), |
| 2226 | DTN->getIdentifier(), |
| 2227 | TemplateArgs); |
| 2228 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2229 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2230 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2231 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2232 | // We might have a substituted template template parameter pack. If so, |
| 2233 | // build a template specialization type for it. |
| 2234 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2235 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2237 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2238 | << Name; |
| 2239 | NoteAllFoundTemplates(Name); |
| 2240 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2241 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2242 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2243 | // Check that the template argument list is well-formed for this |
| 2244 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2245 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2246 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2247 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2248 | return QualType(); |
| 2249 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2250 | QualType CanonType; |
| 2251 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2252 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2253 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2254 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2255 | // Find the canonical type for this type alias template specialization. |
| 2256 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2257 | if (Pattern->isInvalidDecl()) |
| 2258 | return QualType(); |
| 2259 | |
| 2260 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2261 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2262 | |
| 2263 | // Only substitute for the innermost template argument list. |
| 2264 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2265 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2266 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2267 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2268 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2269 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2270 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2271 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2272 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2273 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2274 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2275 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2276 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2277 | AliasTemplate->getDeclName()); |
| 2278 | if (CanonType.isNull()) |
| 2279 | return QualType(); |
| 2280 | } else if (Name.isDependent() || |
| 2281 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2282 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2283 | // This class template specialization is a dependent |
| 2284 | // type. Therefore, its canonical type is another class template |
| 2285 | // specialization type that contains all of the converted |
| 2286 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2287 | // A<T, T> have identical types when A is declared as: |
| 2288 | // |
| 2289 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2290 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2291 | CanonType = Context.getTemplateSpecializationType(CanonName, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2292 | Converted); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2294 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2295 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2296 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2297 | // build the canonical type and return that to us. |
| 2298 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2299 | |
| 2300 | // This might work out to be a current instantiation, in which |
| 2301 | // case the canonical type needs to be the InjectedClassNameType. |
| 2302 | // |
| 2303 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2304 | // changes to CurContext don't change the set of current |
| 2305 | // instantiations. |
| 2306 | if (isa<ClassTemplateDecl>(Template)) { |
| 2307 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2308 | // If we get out to a namespace, we're done. |
| 2309 | if (Ctx->isFileContext()) break; |
| 2310 | |
| 2311 | // If this isn't a record, keep looking. |
| 2312 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2313 | if (!Record) continue; |
| 2314 | |
| 2315 | // Look for one of the two cases with InjectedClassNameTypes |
| 2316 | // and check whether it's the same template. |
| 2317 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2318 | !Record->getDescribedClassTemplate()) |
| 2319 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2320 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2321 | // Fetch the injected class name type and check whether its |
| 2322 | // injected type is equal to the type we just built. |
| 2323 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2324 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2325 | ->getInjectedSpecializationType(); |
| 2326 | |
| 2327 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2328 | continue; |
| 2329 | |
| 2330 | // If so, the canonical type of this TST is the injected |
| 2331 | // class name type of the record we just found. |
| 2332 | assert(ICNT.isCanonical()); |
| 2333 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | } |
| 2336 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2337 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2338 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2339 | // Find the class template specialization declaration that |
| 2340 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2341 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2342 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2343 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2344 | if (!Decl) { |
| 2345 | // This is the first time we have referenced this class template |
| 2346 | // specialization. Create the canonical declaration and add it to |
| 2347 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2349 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2350 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2351 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2352 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2353 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2354 | Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2355 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2356 | if (ClassTemplate->isOutOfLine()) |
| 2357 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2360 | // Diagnose uses of this specialization. |
| 2361 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2362 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2363 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2364 | assert(isa<RecordType>(CanonType) && |
| 2365 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2366 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2367 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2368 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2369 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2370 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2371 | // Build the fully-sugared type for this class template |
| 2372 | // specialization, which refers back to the class template |
| 2373 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2374 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2377 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2378 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2379 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2380 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2381 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2382 | SourceLocation RAngleLoc, |
| 2383 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2384 | if (SS.isInvalid()) |
| 2385 | return true; |
| 2386 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2387 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2389 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2390 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2391 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2392 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2393 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2394 | QualType T |
| 2395 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2396 | DTN->getQualifier(), |
| 2397 | DTN->getIdentifier(), |
| 2398 | TemplateArgs); |
| 2399 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2400 | TypeLocBuilder TLB; |
| 2401 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2402 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2403 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2404 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2405 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2406 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2407 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2408 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2409 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2410 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2411 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2412 | } |
| 2413 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2414 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2415 | |
| 2416 | if (Result.isNull()) |
| 2417 | return true; |
| 2418 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2419 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2420 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2421 | TemplateSpecializationTypeLoc SpecTL |
| 2422 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2423 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2424 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2425 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2426 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2427 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2428 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2429 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2430 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2431 | // constructor or destructor name (in such a case, the scope specifier |
| 2432 | // will be attached to the enclosing Decl or Expr node). |
| 2433 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2434 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2435 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2436 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2437 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2438 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2439 | } |
| 2440 | |
| 2441 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2442 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2443 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2444 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2445 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2446 | SourceLocation TagLoc, |
| 2447 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2448 | SourceLocation TemplateKWLoc, |
| 2449 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2450 | SourceLocation TemplateLoc, |
| 2451 | SourceLocation LAngleLoc, |
| 2452 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2453 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2454 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2455 | |
| 2456 | // Translate the parser's template argument list in our AST format. |
| 2457 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2458 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2459 | |
| 2460 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2461 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2462 | ElaboratedTypeKeyword Keyword |
| 2463 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2464 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2465 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2466 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2467 | DTN->getQualifier(), |
| 2468 | DTN->getIdentifier(), |
| 2469 | TemplateArgs); |
| 2470 | |
| 2471 | // Build type-source information. |
| 2472 | TypeLocBuilder TLB; |
| 2473 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2474 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2475 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2476 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2477 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2478 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2479 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2480 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2481 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2482 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2483 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2484 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2485 | |
| 2486 | if (TypeAliasTemplateDecl *TAT = |
| 2487 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2488 | // C++0x [dcl.type.elab]p2: |
| 2489 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2490 | // resolves to an alias template specialization, the |
| 2491 | // elaborated-type-specifier is ill-formed. |
Reid Kleckner | f33bfcb0 | 2016-10-03 18:34:23 +0000 | [diff] [blame] | 2492 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << NTK_TypeAliasTemplate; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2493 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2494 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2495 | |
| 2496 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2497 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2498 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2499 | |
| 2500 | // Check the tag kind |
| 2501 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2502 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2503 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2504 | IdentifierInfo *Id = D->getIdentifier(); |
| 2505 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2506 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2507 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 2508 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2509 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2510 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2511 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2512 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2513 | } |
| 2514 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2515 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2516 | // Provide source-location information for the template specialization. |
| 2517 | TypeLocBuilder TLB; |
| 2518 | TemplateSpecializationTypeLoc SpecTL |
| 2519 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2520 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2521 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2522 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2523 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2524 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2525 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2526 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2527 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2528 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2529 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2530 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2531 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2532 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2533 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2536 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2537 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2538 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2539 | |
| 2540 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2541 | NamedDecl *PrevDecl, |
| 2542 | SourceLocation Loc, |
| 2543 | bool IsPartialSpecialization); |
| 2544 | |
| 2545 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2546 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2547 | static bool isTemplateArgumentTemplateParameter( |
| 2548 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2549 | switch (Arg.getKind()) { |
| 2550 | case TemplateArgument::Null: |
| 2551 | case TemplateArgument::NullPtr: |
| 2552 | case TemplateArgument::Integral: |
| 2553 | case TemplateArgument::Declaration: |
| 2554 | case TemplateArgument::Pack: |
| 2555 | case TemplateArgument::TemplateExpansion: |
| 2556 | return false; |
| 2557 | |
| 2558 | case TemplateArgument::Type: { |
| 2559 | QualType Type = Arg.getAsType(); |
| 2560 | const TemplateTypeParmType *TPT = |
| 2561 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2562 | return TPT && !Type.hasQualifiers() && |
| 2563 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2564 | } |
| 2565 | |
| 2566 | case TemplateArgument::Expression: { |
| 2567 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2568 | if (!DRE || !DRE->getDecl()) |
| 2569 | return false; |
| 2570 | const NonTypeTemplateParmDecl *NTTP = |
| 2571 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2572 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2573 | } |
| 2574 | |
| 2575 | case TemplateArgument::Template: |
| 2576 | const TemplateTemplateParmDecl *TTP = |
| 2577 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2578 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2579 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2580 | } |
| 2581 | llvm_unreachable("unexpected kind of template argument"); |
| 2582 | } |
| 2583 | |
| 2584 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2585 | ArrayRef<TemplateArgument> Args) { |
| 2586 | if (Params->size() != Args.size()) |
| 2587 | return false; |
| 2588 | |
| 2589 | unsigned Depth = Params->getDepth(); |
| 2590 | |
| 2591 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2592 | TemplateArgument Arg = Args[I]; |
| 2593 | |
| 2594 | // If the parameter is a pack expansion, the argument must be a pack |
| 2595 | // whose only element is a pack expansion. |
| 2596 | if (Params->getParam(I)->isParameterPack()) { |
| 2597 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2598 | !Arg.pack_begin()->isPackExpansion()) |
| 2599 | return false; |
| 2600 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2601 | } |
| 2602 | |
| 2603 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2604 | return false; |
| 2605 | } |
| 2606 | |
| 2607 | return true; |
| 2608 | } |
| 2609 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2610 | /// Convert the parser's template argument list representation into our form. |
| 2611 | static TemplateArgumentListInfo |
| 2612 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2613 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2614 | TemplateId.RAngleLoc); |
| 2615 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2616 | TemplateId.NumArgs); |
| 2617 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2618 | return TemplateArgs; |
| 2619 | } |
| 2620 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2621 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2622 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2623 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2624 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2625 | // D must be variable template id. |
| 2626 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2627 | "Variable template specialization is declared with a template it."); |
| 2628 | |
| 2629 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2630 | TemplateArgumentListInfo TemplateArgs = |
| 2631 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2632 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2633 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2634 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2635 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2636 | TemplateName Name = TemplateId->Template.get(); |
| 2637 | |
| 2638 | // The template-id must name a variable template. |
| 2639 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2640 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2641 | if (!VarTemplate) { |
| 2642 | NamedDecl *FnTemplate; |
| 2643 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2644 | FnTemplate = *OTS->begin(); |
| 2645 | else |
| 2646 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2647 | if (FnTemplate) |
| 2648 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2649 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2650 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2651 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2652 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2653 | |
| 2654 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2655 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2656 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2657 | UPPC_PartialSpecialization)) |
| 2658 | return true; |
| 2659 | |
| 2660 | // Check that the template argument list is well-formed for this |
| 2661 | // template. |
| 2662 | SmallVector<TemplateArgument, 4> Converted; |
| 2663 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2664 | false, Converted)) |
| 2665 | return true; |
| 2666 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2667 | // Find the variable template (partial) specialization declaration that |
| 2668 | // corresponds to these arguments. |
| 2669 | if (IsPartialSpecialization) { |
| 2670 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2671 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2672 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2673 | return true; |
| 2674 | |
| 2675 | bool InstantiationDependent; |
| 2676 | if (!Name.isDependent() && |
| 2677 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2678 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2679 | InstantiationDependent)) { |
| 2680 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2681 | << VarTemplate->getDeclName(); |
| 2682 | IsPartialSpecialization = false; |
| 2683 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2684 | |
| 2685 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2686 | Converted)) { |
| 2687 | // C++ [temp.class.spec]p9b3: |
| 2688 | // |
| 2689 | // -- The argument list of the specialization shall not be identical |
| 2690 | // to the implicit argument list of the primary template. |
| 2691 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2692 | << /*variable template*/ 1 |
| 2693 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2694 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2695 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2696 | // of the primary template. |
| 2697 | return true; |
| 2698 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2701 | void *InsertPos = nullptr; |
| 2702 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2703 | |
| 2704 | if (IsPartialSpecialization) |
| 2705 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2706 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2707 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2708 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2709 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2710 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2711 | |
| 2712 | // Check whether we can declare a variable template specialization in |
| 2713 | // the current scope. |
| 2714 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2715 | TemplateNameLoc, |
| 2716 | IsPartialSpecialization)) |
| 2717 | return true; |
| 2718 | |
| 2719 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2720 | // Since the only prior variable template specialization with these |
| 2721 | // arguments was referenced but not declared, reuse that |
| 2722 | // declaration node as our own, updating its source location and |
| 2723 | // the list of outer template parameters to reflect our new declaration. |
| 2724 | Specialization = PrevDecl; |
| 2725 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2726 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2727 | } else if (IsPartialSpecialization) { |
| 2728 | // Create a new class template partial specialization declaration node. |
| 2729 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2730 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2731 | VarTemplatePartialSpecializationDecl *Partial = |
| 2732 | VarTemplatePartialSpecializationDecl::Create( |
| 2733 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2734 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2735 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2736 | |
| 2737 | if (!PrevPartial) |
| 2738 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2739 | Specialization = Partial; |
| 2740 | |
| 2741 | // If we are providing an explicit specialization of a member variable |
| 2742 | // template specialization, make a note of that. |
| 2743 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2744 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2745 | |
| 2746 | // Check that all of the template parameters of the variable template |
| 2747 | // partial specialization are deducible from the template |
| 2748 | // arguments. If not, this variable template partial specialization |
| 2749 | // will never be used. |
| 2750 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2751 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2752 | TemplateParams->getDepth(), DeducibleParams); |
| 2753 | |
| 2754 | if (!DeducibleParams.all()) { |
| 2755 | unsigned NumNonDeducible = |
| 2756 | DeducibleParams.size() - DeducibleParams.count(); |
| 2757 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2758 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2759 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2760 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2761 | if (!DeducibleParams[I]) { |
| 2762 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2763 | if (Param->getDeclName()) |
| 2764 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2765 | << Param->getDeclName(); |
| 2766 | else |
| 2767 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2768 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | } |
| 2772 | } else { |
| 2773 | // Create a new class template specialization declaration node for |
| 2774 | // this explicit specialization or friend declaration. |
| 2775 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2776 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2777 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2778 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2779 | |
| 2780 | if (!PrevDecl) |
| 2781 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2782 | } |
| 2783 | |
| 2784 | // C++ [temp.expl.spec]p6: |
| 2785 | // If a template, a member template or the member of a class template is |
| 2786 | // explicitly specialized then that specialization shall be declared |
| 2787 | // before the first use of that specialization that would cause an implicit |
| 2788 | // instantiation to take place, in every translation unit in which such a |
| 2789 | // use occurs; no diagnostic is required. |
| 2790 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2791 | bool Okay = false; |
| 2792 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2793 | // Is there any previous explicit specialization declaration? |
| 2794 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2795 | Okay = true; |
| 2796 | break; |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | if (!Okay) { |
| 2801 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2802 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2803 | << Name << Range; |
| 2804 | |
| 2805 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2806 | diag::note_instantiation_required_here) |
| 2807 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2808 | TSK_ImplicitInstantiation); |
| 2809 | return true; |
| 2810 | } |
| 2811 | } |
| 2812 | |
| 2813 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2814 | Specialization->setLexicalDeclContext(CurContext); |
| 2815 | |
| 2816 | // Add the specialization into its lexical context, so that it can |
| 2817 | // be seen when iterating through the list of declarations in that |
| 2818 | // context. However, specializations are not found by name lookup. |
| 2819 | CurContext->addDecl(Specialization); |
| 2820 | |
| 2821 | // Note that this is an explicit specialization. |
| 2822 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2823 | |
| 2824 | if (PrevDecl) { |
| 2825 | // Check that this isn't a redefinition of this specialization, |
| 2826 | // merging with previous declarations. |
| 2827 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2828 | ForRedeclaration); |
| 2829 | PrevSpec.addDecl(PrevDecl); |
| 2830 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2831 | } else if (Specialization->isStaticDataMember() && |
| 2832 | Specialization->isOutOfLine()) { |
| 2833 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | // Link instantiations of static data members back to the template from |
| 2837 | // which they were instantiated. |
| 2838 | if (Specialization->isStaticDataMember()) |
| 2839 | Specialization->setInstantiationOfStaticDataMember( |
| 2840 | VarTemplate->getTemplatedDecl(), |
| 2841 | Specialization->getSpecializationKind()); |
| 2842 | |
| 2843 | return Specialization; |
| 2844 | } |
| 2845 | |
| 2846 | namespace { |
| 2847 | /// \brief A partial specialization whose template arguments have matched |
| 2848 | /// a given template-id. |
| 2849 | struct PartialSpecMatchResult { |
| 2850 | VarTemplatePartialSpecializationDecl *Partial; |
| 2851 | TemplateArgumentList *Args; |
| 2852 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2853 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2854 | |
| 2855 | DeclResult |
| 2856 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2857 | SourceLocation TemplateNameLoc, |
| 2858 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2859 | assert(Template && "A variable template id without template?"); |
| 2860 | |
| 2861 | // Check that the template argument list is well-formed for this template. |
| 2862 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2863 | if (CheckTemplateArgumentList( |
| 2864 | Template, TemplateNameLoc, |
| 2865 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2866 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2867 | return true; |
| 2868 | |
| 2869 | // Find the variable template specialization declaration that |
| 2870 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2871 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2872 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2873 | Converted, InsertPos)) { |
| 2874 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2875 | // If we already have a variable template specialization, return it. |
| 2876 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2877 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2878 | |
| 2879 | // This is the first time we have referenced this variable template |
| 2880 | // specialization. Create the canonical declaration and add it to |
| 2881 | // the set of specializations, based on the closest partial specialization |
| 2882 | // that it represents. That is, |
| 2883 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2884 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2885 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2886 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2887 | bool AmbiguousPartialSpec = false; |
| 2888 | typedef PartialSpecMatchResult MatchResult; |
| 2889 | SmallVector<MatchResult, 4> Matched; |
| 2890 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2891 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 2892 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2893 | |
| 2894 | // 1. Attempt to find the closest partial specialization that this |
| 2895 | // specializes, if any. |
| 2896 | // If any of the template arguments is dependent, then this is probably |
| 2897 | // a placeholder for an incomplete declarative context; which must be |
| 2898 | // complete by instantiation time. Thus, do not search through the partial |
| 2899 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2900 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2901 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2902 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2903 | bool InstantiationDependent = false; |
| 2904 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2905 | TemplateArgs, InstantiationDependent)) { |
| 2906 | |
| 2907 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2908 | Template->getPartialSpecializations(PartialSpecs); |
| 2909 | |
| 2910 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2911 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2912 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2913 | |
| 2914 | if (TemplateDeductionResult Result = |
| 2915 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2916 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2917 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 2918 | FailedCandidates.addCandidate().set( |
| 2919 | DeclAccessPair::make(Template, AS_public), Partial, |
| 2920 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2921 | (void)Result; |
| 2922 | } else { |
| 2923 | Matched.push_back(PartialSpecMatchResult()); |
| 2924 | Matched.back().Partial = Partial; |
| 2925 | Matched.back().Args = Info.take(); |
| 2926 | } |
| 2927 | } |
| 2928 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2929 | if (Matched.size() >= 1) { |
| 2930 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2931 | if (Matched.size() == 1) { |
| 2932 | // -- If exactly one matching specialization is found, the |
| 2933 | // instantiation is generated from that specialization. |
| 2934 | // We don't need to do anything for this. |
| 2935 | } else { |
| 2936 | // -- If more than one matching specialization is found, the |
| 2937 | // partial order rules (14.5.4.2) are used to determine |
| 2938 | // whether one of the specializations is more specialized |
| 2939 | // than the others. If none of the specializations is more |
| 2940 | // specialized than all of the other matching |
| 2941 | // specializations, then the use of the variable template is |
| 2942 | // ambiguous and the program is ill-formed. |
| 2943 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2944 | PEnd = Matched.end(); |
| 2945 | P != PEnd; ++P) { |
| 2946 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2947 | PointOfInstantiation) == |
| 2948 | P->Partial) |
| 2949 | Best = P; |
| 2950 | } |
| 2951 | |
| 2952 | // Determine if the best partial specialization is more specialized than |
| 2953 | // the others. |
| 2954 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2955 | PEnd = Matched.end(); |
| 2956 | P != PEnd; ++P) { |
| 2957 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2958 | P->Partial, Best->Partial, |
| 2959 | PointOfInstantiation) != Best->Partial) { |
| 2960 | AmbiguousPartialSpec = true; |
| 2961 | break; |
| 2962 | } |
| 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | // Instantiate using the best variable template partial specialization. |
| 2967 | InstantiationPattern = Best->Partial; |
| 2968 | InstantiationArgs = Best->Args; |
| 2969 | } else { |
| 2970 | // -- If no match is found, the instantiation is generated |
| 2971 | // from the primary template. |
| 2972 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2973 | } |
| 2974 | } |
| 2975 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2976 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2977 | // Note that we do not instantiate a definition until we see an odr-use |
| 2978 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2979 | // FIXME: LateAttrs et al.? |
| 2980 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2981 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2982 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2983 | if (!Decl) |
| 2984 | return true; |
| 2985 | |
| 2986 | if (AmbiguousPartialSpec) { |
| 2987 | // Partial ordering did not produce a clear winner. Complain. |
| 2988 | Decl->setInvalidDecl(); |
| 2989 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2990 | << Decl; |
| 2991 | |
| 2992 | // Print the matching partial specializations. |
Yaron Keren | 1cb8146 | 2016-11-16 13:45:34 +0000 | [diff] [blame] | 2993 | for (MatchResult P : Matched) |
| 2994 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 2995 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 2996 | *P.Args); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2997 | return true; |
| 2998 | } |
| 2999 | |
| 3000 | if (VarTemplatePartialSpecializationDecl *D = |
| 3001 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 3002 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 3003 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3004 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 3005 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3006 | assert(Decl && "No variable template specialization?"); |
| 3007 | return Decl; |
| 3008 | } |
| 3009 | |
| 3010 | ExprResult |
| 3011 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 3012 | const DeclarationNameInfo &NameInfo, |
| 3013 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3014 | const TemplateArgumentListInfo *TemplateArgs) { |
| 3015 | |
| 3016 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 3017 | *TemplateArgs); |
| 3018 | if (Decl.isInvalid()) |
| 3019 | return ExprError(); |
| 3020 | |
| 3021 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 3022 | if (!Var->getTemplateSpecializationKind()) |
| 3023 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 3024 | NameInfo.getLoc()); |
| 3025 | |
| 3026 | // Build an ordinary singleton decl ref. |
| 3027 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3028 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3031 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3032 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3033 | LookupResult &R, |
| 3034 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3035 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3036 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 3037 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | // 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] | 3039 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3040 | // foo<int> could identify a single function unambiguously |
| 3041 | // This approach does NOT work, since f<int>(1); |
| 3042 | // gets resolved prior to resorting to overload resolution |
| 3043 | // i.e., template<class T> void f(double); |
| 3044 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3045 | |
| 3046 | // These should be filtered out by our callers. |
| 3047 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 3048 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 3049 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3050 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 3051 | bool InstantiationDependent; |
| 3052 | if (R.getAsSingle<VarTemplateDecl>() && |
| 3053 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3054 | *TemplateArgs, InstantiationDependent)) { |
| 3055 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 3056 | R.getAsSingle<VarTemplateDecl>(), |
| 3057 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3060 | // We don't want lookup warnings at this point. |
| 3061 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3062 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3063 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3064 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3065 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3066 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3067 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3068 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3069 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3070 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3071 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3074 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3075 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3076 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3077 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3078 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3079 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3080 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3081 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3082 | DeclContext *DC; |
| 3083 | if (!(DC = computeDeclContext(SS, false)) || |
| 3084 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3085 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 3086 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3088 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3089 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3090 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3091 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3093 | if (R.isAmbiguous()) |
| 3094 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3095 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3096 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3097 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 3098 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3099 | return ExprError(); |
| 3100 | } |
| 3101 | |
| 3102 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3103 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3104 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 3105 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3106 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 3107 | return ExprError(); |
| 3108 | } |
| 3109 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3110 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3113 | /// \brief Form a dependent template name. |
| 3114 | /// |
| 3115 | /// This action forms a dependent template name given the template |
| 3116 | /// name and its (presumably dependent) scope specifier. For |
| 3117 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 3118 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 3119 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3120 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3121 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3122 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3123 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3124 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3125 | bool EnteringContext, |
| 3126 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3127 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 3128 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3129 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3130 | diag::warn_cxx98_compat_template_outside_of_template : |
| 3131 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3132 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 3133 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3134 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3135 | if (SS.isSet()) |
| 3136 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 3137 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3138 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3139 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3140 | // C++0x [temp.names]p5: |
| 3141 | // If a name prefixed by the keyword template is not the name of |
| 3142 | // a template, the program is ill-formed. [Note: the keyword |
| 3143 | // template may not be applied to non-template members of class |
| 3144 | // templates. -end note ] [ Note: as is the case with the |
| 3145 | // typename prefix, the template prefix is allowed in cases |
| 3146 | // where it is not strictly necessary; i.e., when the |
| 3147 | // nested-name-specifier or the expression on the left of the -> |
| 3148 | // or . is not dependent on a template-parameter, or the use |
| 3149 | // does not appear in the scope of a template. -end note] |
| 3150 | // |
| 3151 | // Note: C++03 was more strict here, because it banned the use of |
| 3152 | // the "template" keyword prior to a template-name that was not a |
| 3153 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3154 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3155 | // 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] | 3156 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3157 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3158 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3159 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3160 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3161 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3162 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3163 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3164 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3165 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3166 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3167 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3168 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3169 | << Name.getSourceRange() |
| 3170 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3171 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3172 | } else { |
| 3173 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3174 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3175 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3178 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3179 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3180 | switch (Name.getKind()) { |
| 3181 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3182 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3183 | Name.Identifier)); |
| 3184 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3186 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3187 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3188 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3189 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3190 | |
| 3191 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3192 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3193 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3194 | default: |
| 3195 | break; |
| 3196 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3197 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3198 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3199 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3200 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3201 | << Name.getSourceRange() |
| 3202 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3203 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3207 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3208 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3209 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3210 | QualType ArgType; |
| 3211 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3212 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3213 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3214 | switch(Arg.getKind()) { |
| 3215 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3216 | // C++ [temp.arg.type]p1: |
| 3217 | // A template-argument for a template-parameter which is a |
| 3218 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3219 | ArgType = Arg.getAsType(); |
| 3220 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3221 | break; |
| 3222 | case TemplateArgument::Template: { |
| 3223 | // We have a template type parameter but the template argument |
| 3224 | // is a template without any arguments. |
| 3225 | SourceRange SR = AL.getSourceRange(); |
| 3226 | TemplateName Name = Arg.getAsTemplate(); |
| 3227 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3228 | << Name << SR; |
| 3229 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3230 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3231 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3232 | return true; |
| 3233 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3234 | case TemplateArgument::Expression: { |
| 3235 | // We have a template type parameter but the template argument is an |
| 3236 | // expression; see if maybe it is missing the "typename" keyword. |
| 3237 | CXXScopeSpec SS; |
| 3238 | DeclarationNameInfo NameInfo; |
| 3239 | |
| 3240 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3241 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3242 | NameInfo = ArgExpr->getNameInfo(); |
| 3243 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3244 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3245 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3246 | NameInfo = ArgExpr->getNameInfo(); |
| 3247 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3248 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3249 | if (ArgExpr->isImplicitAccess()) { |
| 3250 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3251 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3252 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3255 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3256 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3257 | LookupParsedName(Result, CurScope, &SS); |
| 3258 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3259 | if (Result.getAsSingle<TypeDecl>() || |
| 3260 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3261 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3262 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3263 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3264 | Diag(Loc, getLangOpts().MSVCCompat |
| 3265 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3266 | : diag::err_template_arg_must_be_type_suggest) |
| 3267 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3268 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3269 | |
| 3270 | // Recover by synthesizing a type using the location information that we |
| 3271 | // already have. |
| 3272 | ArgType = |
| 3273 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3274 | TypeLocBuilder TLB; |
| 3275 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3276 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3277 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3278 | TL.setNameLoc(NameInfo.getLoc()); |
| 3279 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3280 | |
| 3281 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3282 | // properly. |
| 3283 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3284 | TemplateArgumentLocInfo(TSI)); |
| 3285 | |
| 3286 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3287 | } |
| 3288 | } |
| 3289 | // fallthrough |
| 3290 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3291 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3292 | // We have a template type parameter but the template argument |
| 3293 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3294 | SourceRange SR = AL.getSourceRange(); |
| 3295 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3296 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3297 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3298 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3299 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3300 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3301 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3302 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3303 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3304 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3305 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3306 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3307 | |
| 3308 | // Objective-C ARC: |
| 3309 | // If an explicitly-specified template argument type is a lifetime type |
| 3310 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3311 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3312 | ArgType->isObjCLifetimeType() && |
| 3313 | !ArgType.getObjCLifetime()) { |
| 3314 | Qualifiers Qs; |
| 3315 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3316 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3317 | } |
| 3318 | |
| 3319 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3320 | return false; |
| 3321 | } |
| 3322 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3323 | /// \brief Substitute template arguments into the default template argument for |
| 3324 | /// the given template type parameter. |
| 3325 | /// |
| 3326 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3327 | /// the substitution. |
| 3328 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3329 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3330 | /// for. |
| 3331 | /// |
| 3332 | /// \param TemplateLoc the location of the template name that started the |
| 3333 | /// template-id we are checking. |
| 3334 | /// |
| 3335 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3336 | /// terminates the template-id. |
| 3337 | /// |
| 3338 | /// \param Param the template template parameter whose default we are |
| 3339 | /// substituting into. |
| 3340 | /// |
| 3341 | /// \param Converted the list of template arguments provided for template |
| 3342 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3343 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3344 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3345 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3346 | TemplateDecl *Template, |
| 3347 | SourceLocation TemplateLoc, |
| 3348 | SourceLocation RAngleLoc, |
| 3349 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3350 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3351 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3352 | |
| 3353 | // If the argument type is dependent, instantiate it now based |
| 3354 | // on the previously-computed template arguments. |
| 3355 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3356 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3357 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3358 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3359 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3360 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3361 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3362 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3363 | |
| 3364 | // Only substitute for the innermost template argument list. |
| 3365 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3366 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3367 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3368 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3369 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3370 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3371 | ArgType = |
| 3372 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3373 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | return ArgType; |
| 3377 | } |
| 3378 | |
| 3379 | /// \brief Substitute template arguments into the default template argument for |
| 3380 | /// the given non-type template parameter. |
| 3381 | /// |
| 3382 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3383 | /// the substitution. |
| 3384 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3385 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3386 | /// for. |
| 3387 | /// |
| 3388 | /// \param TemplateLoc the location of the template name that started the |
| 3389 | /// template-id we are checking. |
| 3390 | /// |
| 3391 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3392 | /// terminates the template-id. |
| 3393 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3394 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3395 | /// substituting into. |
| 3396 | /// |
| 3397 | /// \param Converted the list of template arguments provided for template |
| 3398 | /// parameters that precede \p Param in the template parameter list. |
| 3399 | /// |
| 3400 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3401 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3402 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3403 | TemplateDecl *Template, |
| 3404 | SourceLocation TemplateLoc, |
| 3405 | SourceLocation RAngleLoc, |
| 3406 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3407 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3408 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3409 | Param, Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3410 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3411 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3412 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3413 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3414 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3415 | |
| 3416 | // Only substitute for the innermost template argument list. |
| 3417 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3418 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3419 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3420 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3421 | |
Faisal Vali | 48401eb | 2015-11-19 19:20:17 +0000 | [diff] [blame] | 3422 | EnterExpressionEvaluationContext ConstantEvaluated(SemaRef, |
| 3423 | Sema::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3424 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3427 | /// \brief Substitute template arguments into the default template argument for |
| 3428 | /// the given template template parameter. |
| 3429 | /// |
| 3430 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3431 | /// the substitution. |
| 3432 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3433 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3434 | /// for. |
| 3435 | /// |
| 3436 | /// \param TemplateLoc the location of the template name that started the |
| 3437 | /// template-id we are checking. |
| 3438 | /// |
| 3439 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3440 | /// terminates the template-id. |
| 3441 | /// |
| 3442 | /// \param Param the template template parameter whose default we are |
| 3443 | /// substituting into. |
| 3444 | /// |
| 3445 | /// \param Converted the list of template arguments provided for template |
| 3446 | /// parameters that precede \p Param in the template parameter list. |
| 3447 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3448 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3449 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3450 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3451 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3452 | static TemplateName |
| 3453 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3454 | TemplateDecl *Template, |
| 3455 | SourceLocation TemplateLoc, |
| 3456 | SourceLocation RAngleLoc, |
| 3457 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3458 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3459 | NestedNameSpecifierLoc &QualifierLoc) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3460 | Sema::InstantiatingTemplate Inst( |
| 3461 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 3462 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3463 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3464 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3465 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3466 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3467 | |
| 3468 | // Only substitute for the innermost template argument list. |
| 3469 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3470 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3471 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3472 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3473 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3474 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3475 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3476 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3477 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3478 | QualifierLoc = |
| 3479 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3480 | if (!QualifierLoc) |
| 3481 | return TemplateName(); |
| 3482 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3483 | |
| 3484 | return SemaRef.SubstTemplateName( |
| 3485 | QualifierLoc, |
| 3486 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3487 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3488 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3491 | /// \brief If the given template parameter has a default template |
| 3492 | /// argument, substitute into that default template argument and |
| 3493 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3494 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3495 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3496 | SourceLocation TemplateLoc, |
| 3497 | SourceLocation RAngleLoc, |
| 3498 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3499 | SmallVectorImpl<TemplateArgument> |
| 3500 | &Converted, |
| 3501 | bool &HasDefaultArg) { |
| 3502 | HasDefaultArg = false; |
| 3503 | |
| 3504 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3505 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3506 | return TemplateArgumentLoc(); |
| 3507 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3508 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3509 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3510 | TemplateLoc, |
| 3511 | RAngleLoc, |
| 3512 | TypeParm, |
| 3513 | Converted); |
| 3514 | if (DI) |
| 3515 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3516 | |
| 3517 | return TemplateArgumentLoc(); |
| 3518 | } |
| 3519 | |
| 3520 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3521 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3522 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3523 | return TemplateArgumentLoc(); |
| 3524 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3525 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3526 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3527 | TemplateLoc, |
| 3528 | RAngleLoc, |
| 3529 | NonTypeParm, |
| 3530 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3531 | if (Arg.isInvalid()) |
| 3532 | return TemplateArgumentLoc(); |
| 3533 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3534 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3535 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3536 | } |
| 3537 | |
| 3538 | TemplateTemplateParmDecl *TempTempParm |
| 3539 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3540 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3541 | return TemplateArgumentLoc(); |
| 3542 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3543 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3544 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3545 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3546 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3547 | RAngleLoc, |
| 3548 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3549 | Converted, |
| 3550 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3551 | if (TName.isNull()) |
| 3552 | return TemplateArgumentLoc(); |
| 3553 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3554 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3555 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3556 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3557 | } |
| 3558 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3559 | /// \brief Check that the given template argument corresponds to the given |
| 3560 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3561 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3562 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3563 | /// checked. |
| 3564 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3565 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3566 | /// |
| 3567 | /// \param Template The template in which the template argument resides. |
| 3568 | /// |
| 3569 | /// \param TemplateLoc The location of the template name for the template |
| 3570 | /// whose argument list we're matching. |
| 3571 | /// |
| 3572 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3573 | /// the template argument list. |
| 3574 | /// |
| 3575 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3576 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3577 | /// |
| 3578 | /// \param Converted The checked, converted argument will be added to the |
| 3579 | /// end of this small vector. |
| 3580 | /// |
| 3581 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3582 | /// explicitly written, deduced, etc. |
| 3583 | /// |
| 3584 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3585 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3586 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3587 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3588 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3589 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3590 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3591 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3592 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3593 | // Check template type parameters. |
| 3594 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3595 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3596 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3597 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3598 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3599 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3600 | // with the template arguments we've seen thus far. But if the |
| 3601 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3602 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3603 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3604 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3605 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3606 | if (NTTPType->isDependentType() && |
| 3607 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3608 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3609 | // Do substitution on the type of the non-type template parameter. |
| 3610 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3611 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3612 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3613 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3614 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3615 | |
| 3616 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3617 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3618 | NTTPType = SubstType(NTTPType, |
| 3619 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3620 | NTTP->getLocation(), |
| 3621 | NTTP->getDeclName()); |
| 3622 | // If that worked, check the non-type template parameter type |
| 3623 | // for validity. |
| 3624 | if (!NTTPType.isNull()) |
| 3625 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3626 | NTTP->getLocation()); |
| 3627 | if (NTTPType.isNull()) |
| 3628 | return true; |
| 3629 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3630 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3631 | switch (Arg.getArgument().getKind()) { |
| 3632 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3633 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3634 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3635 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3636 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3637 | ExprResult Res = |
| 3638 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3639 | Result, CTAK); |
| 3640 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3641 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3642 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3643 | // If the resulting expression is new, then use it in place of the |
| 3644 | // old expression in the template argument. |
| 3645 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3646 | TemplateArgument TA(Res.get()); |
| 3647 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3648 | } |
| 3649 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3650 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3651 | break; |
| 3652 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3653 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3654 | case TemplateArgument::Declaration: |
| 3655 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3656 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3657 | // We've already checked this template argument, so just copy |
| 3658 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3659 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3660 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3662 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3663 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3664 | // We were given a template template argument. It may not be ill-formed; |
| 3665 | // see below. |
| 3666 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3667 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3668 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3669 | // We have a template argument such as \c T::template X, which we |
| 3670 | // parsed as a template template argument. However, since we now |
| 3671 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3672 | // template name into an expression. |
| 3673 | |
| 3674 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3675 | Arg.getTemplateNameLoc()); |
| 3676 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3677 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3678 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3679 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3680 | // so it was provided with a template keyword. However, its source |
| 3681 | // location is not stored in the template argument structure. |
| 3682 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3683 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3684 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3685 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3687 | // If we parsed the template argument as a pack expansion, create a |
| 3688 | // pack expansion expression. |
| 3689 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3690 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3691 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3692 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3693 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3694 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3695 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3696 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3697 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3698 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3699 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3700 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3703 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3704 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3705 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3706 | // therefore cannot be a non-type template argument. |
| 3707 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3708 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3709 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3710 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3711 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3712 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3713 | case TemplateArgument::Type: { |
| 3714 | // We have a non-type template parameter but the template |
| 3715 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3717 | // C++ [temp.arg]p2: |
| 3718 | // In a template-argument, an ambiguity between a type-id and |
| 3719 | // an expression is resolved to a type-id, regardless of the |
| 3720 | // form of the corresponding template-parameter. |
| 3721 | // |
| 3722 | // We warn specifically about this case, since it can be rather |
| 3723 | // confusing for users. |
| 3724 | QualType T = Arg.getArgument().getAsType(); |
| 3725 | SourceRange SR = Arg.getSourceRange(); |
| 3726 | if (T->isFunctionType()) |
| 3727 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3728 | else |
| 3729 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3730 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3731 | return true; |
| 3732 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3733 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3734 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3735 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3736 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3737 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3738 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3742 | // Check template template parameters. |
| 3743 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3744 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3745 | // Substitute into the template parameter list of the template |
| 3746 | // template parameter, since previously-supplied template arguments |
| 3747 | // may appear within the template template parameter. |
| 3748 | { |
| 3749 | // Set up a template instantiation context. |
| 3750 | LocalInstantiationScope Scope(*this); |
| 3751 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3752 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3753 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3754 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3755 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3756 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3757 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3758 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3759 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3760 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3761 | if (!TempParm) |
| 3762 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3763 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3764 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3765 | switch (Arg.getArgument().getKind()) { |
| 3766 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3767 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3768 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3769 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3770 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3771 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3772 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3774 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3775 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3777 | case TemplateArgument::Expression: |
| 3778 | case TemplateArgument::Type: |
| 3779 | // We have a template template parameter but the template |
| 3780 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3781 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3782 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3783 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3784 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3785 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3786 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3787 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3788 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3789 | case TemplateArgument::NullPtr: |
| 3790 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3792 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3793 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3794 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3795 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3796 | return false; |
| 3797 | } |
| 3798 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3799 | /// \brief Diagnose an arity mismatch in the |
| 3800 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3801 | SourceLocation TemplateLoc, |
| 3802 | TemplateArgumentListInfo &TemplateArgs) { |
| 3803 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3804 | unsigned NumParams = Params->size(); |
| 3805 | unsigned NumArgs = TemplateArgs.size(); |
| 3806 | |
| 3807 | SourceRange Range; |
| 3808 | if (NumArgs > NumParams) |
| 3809 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3810 | TemplateArgs.getRAngleLoc()); |
| 3811 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3812 | << (NumArgs > NumParams) |
| 3813 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3814 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3815 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3816 | << Template << Range; |
| 3817 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3818 | << Params->getSourceRange(); |
| 3819 | return true; |
| 3820 | } |
| 3821 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3822 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3823 | /// determine the number of parameters produced by that expansion. For instance: |
| 3824 | /// |
| 3825 | /// \code |
| 3826 | /// template<typename ...Ts> struct A { |
| 3827 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3828 | /// }; |
| 3829 | /// \endcode |
| 3830 | /// |
| 3831 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3832 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3833 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3834 | if (NonTypeTemplateParmDecl *NTTP |
| 3835 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3836 | if (NTTP->isExpandedParameterPack()) |
| 3837 | return NTTP->getNumExpansionTypes(); |
| 3838 | } |
| 3839 | |
| 3840 | if (TemplateTemplateParmDecl *TTP |
| 3841 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3842 | if (TTP->isExpandedParameterPack()) |
| 3843 | return TTP->getNumExpansionTemplateParameters(); |
| 3844 | } |
| 3845 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3846 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3849 | /// Diagnose a missing template argument. |
| 3850 | template<typename TemplateParmDecl> |
| 3851 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 3852 | TemplateDecl *TD, |
| 3853 | const TemplateParmDecl *D, |
| 3854 | TemplateArgumentListInfo &Args) { |
| 3855 | // Dig out the most recent declaration of the template parameter; there may be |
| 3856 | // declarations of the template that are more recent than TD. |
| 3857 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 3858 | ->getTemplateParameters() |
| 3859 | ->getParam(D->getIndex())); |
| 3860 | |
| 3861 | // If there's a default argument that's not visible, diagnose that we're |
| 3862 | // missing a module import. |
| 3863 | llvm::SmallVector<Module*, 8> Modules; |
| 3864 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 3865 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 3866 | D->getDefaultArgumentLoc(), Modules, |
| 3867 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3868 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3869 | return true; |
| 3870 | } |
| 3871 | |
| 3872 | // FIXME: If there's a more recent default argument that *is* visible, |
| 3873 | // diagnose that it was declared too late. |
| 3874 | |
| 3875 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 3876 | } |
| 3877 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3878 | /// \brief Check that the given template argument list is well-formed |
| 3879 | /// for specializing the given template. |
| 3880 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3881 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3882 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3883 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3884 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3885 | // Make a copy of the template arguments for processing. Only make the |
| 3886 | // changes at the end when successful in matching the arguments to the |
| 3887 | // template. |
| 3888 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3889 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3890 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3891 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3892 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3893 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3894 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3895 | // [...] The type and form of each template-argument specified in |
| 3896 | // a template-id shall match the type and form specified for the |
| 3897 | // corresponding parameter declared by the template in its |
| 3898 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3899 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3900 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3901 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3902 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3903 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3904 | ParamEnd = Params->end(); |
| 3905 | Param != ParamEnd; /* increment in loop */) { |
| 3906 | // If we have an expanded parameter pack, make sure we don't have too |
| 3907 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3908 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3909 | if (*Expansions == ArgumentPack.size()) { |
| 3910 | // We're done with this parameter pack. Pack up its arguments and add |
| 3911 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3912 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3913 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3914 | ArgumentPack.clear(); |
| 3915 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3916 | // This argument is assigned to the next parameter. |
| 3917 | ++Param; |
| 3918 | continue; |
| 3919 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3920 | // Not enough arguments for this parameter pack. |
| 3921 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3922 | << false |
| 3923 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3924 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3925 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3926 | << Template; |
| 3927 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3928 | << Params->getSourceRange(); |
| 3929 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3930 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3931 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3932 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3933 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3934 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3935 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3936 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3937 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3938 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3939 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3940 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3941 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3942 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3943 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3944 | // 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] | 3945 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3946 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3947 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3948 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3949 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3950 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3951 | return true; |
| 3952 | } |
| 3953 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3954 | // We're now done with this argument. |
| 3955 | ++ArgIdx; |
| 3956 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3957 | if ((*Param)->isTemplateParameterPack()) { |
| 3958 | // The template parameter was a template parameter pack, so take the |
| 3959 | // deduced argument and place it on the argument pack. Note that we |
| 3960 | // stay on the same template parameter so that we can deduce more |
| 3961 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3962 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3963 | } else { |
| 3964 | // Move to the next template parameter. |
| 3965 | ++Param; |
| 3966 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3967 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3968 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3969 | // the remaining arguments, because we don't know what parameters they'll |
| 3970 | // match up with. |
| 3971 | if (PackExpansionIntoNonPack) { |
| 3972 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3973 | // If we were part way through filling in an expanded parameter pack, |
| 3974 | // fall back to just producing individual arguments. |
| 3975 | Converted.insert(Converted.end(), |
| 3976 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3977 | ArgumentPack.clear(); |
| 3978 | } |
| 3979 | |
| 3980 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3981 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3982 | ++ArgIdx; |
| 3983 | } |
| 3984 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3985 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3986 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3987 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3988 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3989 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3991 | // If we're checking a partial template argument list, we're done. |
| 3992 | if (PartialTemplateArgs) { |
| 3993 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3994 | Converted.push_back( |
| 3995 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 3996 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3997 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4000 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4001 | // 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] | 4002 | if ((*Param)->isTemplateParameterPack()) { |
| 4003 | assert(!getExpandedPackSize(*Param) && |
| 4004 | "Should have dealt with this already"); |
| 4005 | |
| 4006 | // A non-expanded parameter pack before the end of the parameter list |
| 4007 | // only occurs for an ill-formed template parameter list, unless we've |
| 4008 | // got a partial argument list for a function template, so just bail out. |
| 4009 | if (Param + 1 != ParamEnd) |
| 4010 | return true; |
| 4011 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4012 | Converted.push_back( |
| 4013 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4014 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4015 | |
| 4016 | ++Param; |
| 4017 | continue; |
| 4018 | } |
| 4019 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4020 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4021 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4022 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4023 | // Retrieve the default template argument from the template |
| 4024 | // parameter. For each kind of template parameter, we substitute the |
| 4025 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4026 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4027 | // the default argument. |
| 4028 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4029 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4030 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 4031 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4032 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4033 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4034 | Template, |
| 4035 | TemplateLoc, |
| 4036 | RAngleLoc, |
| 4037 | TTP, |
| 4038 | Converted); |
| 4039 | if (!ArgType) |
| 4040 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4041 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4042 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 4043 | ArgType); |
| 4044 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4045 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4046 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4047 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 4048 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4049 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4050 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4051 | TemplateLoc, |
| 4052 | RAngleLoc, |
| 4053 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4054 | Converted); |
| 4055 | if (E.isInvalid()) |
| 4056 | return true; |
| 4057 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4058 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4059 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 4060 | } else { |
| 4061 | TemplateTemplateParmDecl *TempParm |
| 4062 | = cast<TemplateTemplateParmDecl>(*Param); |
| 4063 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4064 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4065 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 4066 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4067 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4068 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4069 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4070 | TemplateLoc, |
| 4071 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4072 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4073 | Converted, |
| 4074 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4075 | if (Name.isNull()) |
| 4076 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4077 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4078 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 4079 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4080 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4081 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4082 | // Introduce an instantiation record that describes where we are using |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4083 | // the default template argument. We're not actually instantiating a |
| 4084 | // template here, we just create this object to put a note into the |
| 4085 | // context stack. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4086 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 4087 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4088 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4089 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4090 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4091 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4092 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4093 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4094 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4095 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4096 | // Core issue 150 (assumed resolution): if this is a template template |
| 4097 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4098 | // template definition. |
| 4099 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4100 | NewArgs.addArgument(Arg); |
| 4101 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4102 | // Move to the next template parameter and argument. |
| 4103 | ++Param; |
| 4104 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4105 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4106 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4107 | // If we're performing a partial argument substitution, allow any trailing |
| 4108 | // pack expansions; they might be empty. This can happen even if |
| 4109 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 4110 | // still dependent). |
| 4111 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 4112 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4113 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 4114 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4117 | // If we have any leftover arguments, then there were too many arguments. |
| 4118 | // Complain and fail. |
| 4119 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4120 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 4121 | |
| 4122 | // No problems found with the new argument list, propagate changes back |
| 4123 | // to caller. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 4124 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4125 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4126 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4129 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4130 | class UnnamedLocalNoLinkageFinder |
| 4131 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4132 | { |
| 4133 | Sema &S; |
| 4134 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4135 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4136 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4137 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4138 | public: |
| 4139 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 4140 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4141 | bool Visit(QualType T) { |
| 4142 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4143 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4144 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4145 | #define TYPE(Class, Parent) \ |
| 4146 | bool Visit##Class##Type(const Class##Type *); |
| 4147 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4148 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4149 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4150 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4151 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4152 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4153 | bool VisitTagDecl(const TagDecl *Tag); |
| 4154 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4155 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4156 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4157 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4158 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4159 | return false; |
| 4160 | } |
| 4161 | |
| 4162 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4163 | return Visit(T->getElementType()); |
| 4164 | } |
| 4165 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4166 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4167 | return Visit(T->getPointeeType()); |
| 4168 | } |
| 4169 | |
| 4170 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4171 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4172 | return Visit(T->getPointeeType()); |
| 4173 | } |
| 4174 | |
| 4175 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4176 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4177 | return Visit(T->getPointeeType()); |
| 4178 | } |
| 4179 | |
| 4180 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4182 | return Visit(T->getPointeeType()); |
| 4183 | } |
| 4184 | |
| 4185 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4186 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4187 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4188 | } |
| 4189 | |
| 4190 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4191 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4192 | return Visit(T->getElementType()); |
| 4193 | } |
| 4194 | |
| 4195 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4196 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4197 | return Visit(T->getElementType()); |
| 4198 | } |
| 4199 | |
| 4200 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4201 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4202 | return Visit(T->getElementType()); |
| 4203 | } |
| 4204 | |
| 4205 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4206 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4207 | return Visit(T->getElementType()); |
| 4208 | } |
| 4209 | |
| 4210 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4211 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4212 | return Visit(T->getElementType()); |
| 4213 | } |
| 4214 | |
| 4215 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4216 | return Visit(T->getElementType()); |
| 4217 | } |
| 4218 | |
| 4219 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4220 | return Visit(T->getElementType()); |
| 4221 | } |
| 4222 | |
| 4223 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4224 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4225 | for (const auto &A : T->param_types()) { |
| 4226 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4227 | return true; |
| 4228 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4229 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4230 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4234 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4235 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4239 | const UnresolvedUsingType*) { |
| 4240 | return false; |
| 4241 | } |
| 4242 | |
| 4243 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4244 | return false; |
| 4245 | } |
| 4246 | |
| 4247 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4248 | return Visit(T->getUnderlyingType()); |
| 4249 | } |
| 4250 | |
| 4251 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4252 | return false; |
| 4253 | } |
| 4254 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4255 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4256 | const UnaryTransformType*) { |
| 4257 | return false; |
| 4258 | } |
| 4259 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4260 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4261 | return Visit(T->getDeducedType()); |
| 4262 | } |
| 4263 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4264 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4265 | return VisitTagDecl(T->getDecl()); |
| 4266 | } |
| 4267 | |
| 4268 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4269 | return VisitTagDecl(T->getDecl()); |
| 4270 | } |
| 4271 | |
| 4272 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4273 | const TemplateTypeParmType*) { |
| 4274 | return false; |
| 4275 | } |
| 4276 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4277 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4278 | const SubstTemplateTypeParmPackType *) { |
| 4279 | return false; |
| 4280 | } |
| 4281 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4282 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4283 | const TemplateSpecializationType*) { |
| 4284 | return false; |
| 4285 | } |
| 4286 | |
| 4287 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4288 | const InjectedClassNameType* T) { |
| 4289 | return VisitTagDecl(T->getDecl()); |
| 4290 | } |
| 4291 | |
| 4292 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4293 | const DependentNameType* T) { |
| 4294 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4295 | } |
| 4296 | |
| 4297 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4298 | const DependentTemplateSpecializationType* T) { |
| 4299 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4300 | } |
| 4301 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4302 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4303 | const PackExpansionType* T) { |
| 4304 | return Visit(T->getPattern()); |
| 4305 | } |
| 4306 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4307 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4308 | return false; |
| 4309 | } |
| 4310 | |
| 4311 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4312 | const ObjCInterfaceType *) { |
| 4313 | return false; |
| 4314 | } |
| 4315 | |
| 4316 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4317 | const ObjCObjectPointerType *) { |
| 4318 | return false; |
| 4319 | } |
| 4320 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4321 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4322 | return Visit(T->getValueType()); |
| 4323 | } |
| 4324 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4325 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 4326 | return false; |
| 4327 | } |
| 4328 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4329 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4330 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4331 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4332 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4333 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4334 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4335 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4336 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4339 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4340 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4341 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4342 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4343 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4344 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4345 | return true; |
| 4346 | } |
| 4347 | |
| 4348 | return false; |
| 4349 | } |
| 4350 | |
| 4351 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4352 | NestedNameSpecifier *NNS) { |
| 4353 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4354 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4355 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4356 | switch (NNS->getKind()) { |
| 4357 | case NestedNameSpecifier::Identifier: |
| 4358 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4359 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4360 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4361 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4362 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4363 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4364 | case NestedNameSpecifier::TypeSpec: |
| 4365 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4366 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4367 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4368 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4369 | } |
| 4370 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4371 | /// \brief Check a template argument against its corresponding |
| 4372 | /// template type parameter. |
| 4373 | /// |
| 4374 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4375 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4376 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4377 | TypeSourceInfo *ArgInfo) { |
| 4378 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4379 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4380 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4381 | |
| 4382 | if (Arg->isVariablyModifiedType()) { |
| 4383 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4384 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4385 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4388 | // C++03 [temp.arg.type]p2: |
| 4389 | // A local type, a type with no linkage, an unnamed type or a type |
| 4390 | // compounded from any of these types shall not be used as a |
| 4391 | // template-argument for a template type-parameter. |
| 4392 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4393 | // 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] | 4394 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4395 | bool NeedsCheck; |
| 4396 | if (LangOpts.CPlusPlus11) |
| 4397 | NeedsCheck = |
| 4398 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4399 | SR.getBegin()) || |
| 4400 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4401 | SR.getBegin()); |
| 4402 | else |
| 4403 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4404 | |
| 4405 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4406 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4407 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4408 | } |
| 4409 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4410 | return false; |
| 4411 | } |
| 4412 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4413 | enum NullPointerValueKind { |
| 4414 | NPV_NotNullPointer, |
| 4415 | NPV_NullPointer, |
| 4416 | NPV_Error |
| 4417 | }; |
| 4418 | |
| 4419 | /// \brief Determine whether the given template argument is a null pointer |
| 4420 | /// value of the appropriate type. |
| 4421 | static NullPointerValueKind |
| 4422 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4423 | QualType ParamType, Expr *Arg) { |
| 4424 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4425 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4426 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4427 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 4428 | llvm_unreachable( |
| 4429 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4430 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4431 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4432 | return NPV_NotNullPointer; |
| 4433 | |
| 4434 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4435 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4436 | if (ArgRV.isInvalid()) |
| 4437 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4438 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4439 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4440 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4441 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4442 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4443 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4444 | EvalResult.HasSideEffects) { |
| 4445 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4446 | |
| 4447 | // If our only note is the usual "invalid subexpression" note, just point |
| 4448 | // the caret at its location rather than producing an essentially |
| 4449 | // redundant note. |
| 4450 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4451 | diag::note_invalid_subexpr_in_const_expr) { |
| 4452 | DiagLoc = Notes[0].first; |
| 4453 | Notes.clear(); |
| 4454 | } |
| 4455 | |
| 4456 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4457 | << Arg->getType() << Arg->getSourceRange(); |
| 4458 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4459 | S.Diag(Notes[I].first, Notes[I].second); |
| 4460 | |
| 4461 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4462 | return NPV_Error; |
| 4463 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4464 | |
| 4465 | // C++11 [temp.arg.nontype]p1: |
| 4466 | // - an address constant expression of type std::nullptr_t |
| 4467 | if (Arg->getType()->isNullPtrType()) |
| 4468 | return NPV_NullPointer; |
| 4469 | |
| 4470 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4471 | // - a constant expression that evaluates to a null member pointer value |
| 4472 | // (4.11); or |
| 4473 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4474 | (EvalResult.Val.isMemberPointer() && |
| 4475 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4476 | // If our expression has an appropriate type, we've succeeded. |
| 4477 | bool ObjCLifetimeConversion; |
| 4478 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4479 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4480 | ObjCLifetimeConversion)) |
| 4481 | return NPV_NullPointer; |
| 4482 | |
| 4483 | // The types didn't match, but we know we got a null pointer; complain, |
| 4484 | // then recover as if the types were correct. |
| 4485 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4486 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4487 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4488 | return NPV_NullPointer; |
| 4489 | } |
| 4490 | |
| 4491 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4492 | // constant, suggest a cast to the appropriate type. |
| 4493 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4494 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4495 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4496 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4497 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4498 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4499 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4500 | return NPV_NullPointer; |
| 4501 | } |
| 4502 | |
| 4503 | // FIXME: If we ever want to support general, address-constant expressions |
| 4504 | // as non-type template arguments, we should return the ExprResult here to |
| 4505 | // be interpreted by the caller. |
| 4506 | return NPV_NotNullPointer; |
| 4507 | } |
| 4508 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4509 | /// \brief Checks whether the given template argument is compatible with its |
| 4510 | /// template parameter. |
| 4511 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4512 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4513 | Expr *Arg, QualType ArgType) { |
| 4514 | bool ObjCLifetimeConversion; |
| 4515 | if (ParamType->isPointerType() && |
| 4516 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4517 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4518 | ObjCLifetimeConversion)) { |
| 4519 | // For pointer-to-object types, qualification conversions are |
| 4520 | // permitted. |
| 4521 | } else { |
| 4522 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4523 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4524 | // C++ [temp.arg.nontype]p5b3: |
| 4525 | // For a non-type template-parameter of type reference to |
| 4526 | // object, no conversions apply. The type referred to by the |
| 4527 | // reference may be more cv-qualified than the (otherwise |
| 4528 | // identical) type of the template- argument. The |
| 4529 | // template-parameter is bound directly to the |
| 4530 | // template-argument, which shall be an lvalue. |
| 4531 | |
| 4532 | // FIXME: Other qualifiers? |
| 4533 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4534 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4535 | |
| 4536 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4537 | S.Diag(Arg->getLocStart(), |
| 4538 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4539 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4540 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4541 | return true; |
| 4542 | } |
| 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | // At this point, the template argument refers to an object or |
| 4547 | // function with external linkage. We now need to check whether the |
| 4548 | // argument and parameter types are compatible. |
| 4549 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4550 | ParamType.getNonReferenceType())) { |
| 4551 | // We can't perform this conversion or binding. |
| 4552 | if (ParamType->isReferenceType()) |
| 4553 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4554 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4555 | else |
| 4556 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4557 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4558 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4559 | return true; |
| 4560 | } |
| 4561 | } |
| 4562 | |
| 4563 | return false; |
| 4564 | } |
| 4565 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4566 | /// \brief Checks whether the given template argument is the address |
| 4567 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4568 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4569 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4570 | NonTypeTemplateParmDecl *Param, |
| 4571 | QualType ParamType, |
| 4572 | Expr *ArgIn, |
| 4573 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4574 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4575 | Expr *Arg = ArgIn; |
| 4576 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4577 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4578 | bool AddressTaken = false; |
| 4579 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4580 | if (S.getLangOpts().MicrosoftExt) { |
| 4581 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4582 | // dereference and address-of operators. |
| 4583 | Arg = Arg->IgnoreParenCasts(); |
| 4584 | |
| 4585 | bool ExtWarnMSTemplateArg = false; |
| 4586 | UnaryOperatorKind FirstOpKind; |
| 4587 | SourceLocation FirstOpLoc; |
| 4588 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4589 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4590 | if (UnOpKind == UO_Deref) |
| 4591 | ExtWarnMSTemplateArg = true; |
| 4592 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4593 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4594 | if (!AddrOpLoc.isValid()) { |
| 4595 | FirstOpKind = UnOpKind; |
| 4596 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4597 | } |
| 4598 | } else |
| 4599 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4600 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4601 | if (FirstOpLoc.isValid()) { |
| 4602 | if (ExtWarnMSTemplateArg) |
| 4603 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4604 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4605 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4606 | if (FirstOpKind == UO_AddrOf) |
| 4607 | AddressTaken = true; |
| 4608 | else if (Arg->getType()->isPointerType()) { |
| 4609 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4610 | // constant expression. |
| 4611 | assert(FirstOpKind == UO_Deref); |
| 4612 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4613 | << Arg->getSourceRange(); |
| 4614 | } |
| 4615 | } |
| 4616 | } else { |
| 4617 | // See through any implicit casts we added to fix the type. |
| 4618 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4619 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4620 | // C++ [temp.arg.nontype]p1: |
| 4621 | // |
| 4622 | // A template-argument for a non-type, non-template |
| 4623 | // template-parameter shall be one of: [...] |
| 4624 | // |
| 4625 | // -- the address of an object or function with external |
| 4626 | // linkage, including function templates and function |
| 4627 | // template-ids but excluding non-static class members, |
| 4628 | // expressed as & id-expression where the & is optional if |
| 4629 | // the name refers to a function or array, or if the |
| 4630 | // corresponding template-parameter is a reference; or |
| 4631 | |
| 4632 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4633 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4634 | bool ExtraParens = false; |
| 4635 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4636 | if (!Invalid && !ExtraParens) { |
| 4637 | S.Diag(Arg->getLocStart(), |
| 4638 | S.getLangOpts().CPlusPlus11 |
| 4639 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4640 | : diag::ext_template_arg_extra_parens) |
| 4641 | << Arg->getSourceRange(); |
| 4642 | ExtraParens = true; |
| 4643 | } |
| 4644 | |
| 4645 | Arg = Parens->getSubExpr(); |
| 4646 | } |
| 4647 | |
| 4648 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4649 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4650 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4651 | |
| 4652 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4653 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4654 | Arg = UnOp->getSubExpr(); |
| 4655 | AddressTaken = true; |
| 4656 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4657 | } |
| 4658 | } |
| 4659 | |
| 4660 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4661 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4662 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4663 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4664 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4665 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4666 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4667 | |
| 4668 | // If our parameter has pointer type, check for a null template value. |
| 4669 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4670 | NullPointerValueKind NPV; |
| 4671 | // dllimport'd entities aren't constant but are available inside of template |
| 4672 | // arguments. |
| 4673 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4674 | NPV = NPV_NotNullPointer; |
| 4675 | else |
| 4676 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4677 | switch (NPV) { |
| 4678 | case NPV_NullPointer: |
| 4679 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4680 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4681 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4682 | return false; |
| 4683 | |
| 4684 | case NPV_Error: |
| 4685 | return true; |
| 4686 | |
| 4687 | case NPV_NotNullPointer: |
| 4688 | break; |
| 4689 | } |
| 4690 | } |
| 4691 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4692 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4693 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4694 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4695 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4696 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4697 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4698 | |
| 4699 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4700 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4701 | ArgIn, Arg, ArgType)) |
| 4702 | return true; |
| 4703 | |
| 4704 | Converted = TemplateArgument(ArgIn); |
| 4705 | return false; |
| 4706 | } |
| 4707 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4708 | if (!DRE) { |
| 4709 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4710 | << Arg->getSourceRange(); |
| 4711 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4712 | return true; |
| 4713 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4714 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4715 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4716 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4717 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4718 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4719 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4720 | return true; |
| 4721 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4722 | |
| 4723 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4724 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4725 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4726 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4727 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4728 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4729 | return true; |
| 4730 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4731 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4732 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4733 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4734 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4735 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4736 | // A non-type template argument must refer to an object or function. |
| 4737 | if (!Func && !Var) { |
| 4738 | // We found something, but we don't know specifically what it is. |
| 4739 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4740 | << Arg->getSourceRange(); |
| 4741 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4742 | return true; |
| 4743 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4744 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4745 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4746 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4747 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4748 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4749 | diag::ext_template_arg_object_internal) |
| 4750 | << !Func << Entity << Arg->getSourceRange(); |
| 4751 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4752 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4753 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4754 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4755 | << !Func << Entity << Arg->getSourceRange(); |
| 4756 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4757 | << !Func; |
| 4758 | return true; |
| 4759 | } |
| 4760 | |
| 4761 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4762 | // If the template parameter has pointer type, the function decays. |
| 4763 | if (ParamType->isPointerType() && !AddressTaken) |
| 4764 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4765 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4766 | // If we originally had an address-of operator, but the |
| 4767 | // parameter has reference type, complain and (if things look |
| 4768 | // like they will work) drop the address-of operator. |
| 4769 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4770 | ParamType.getNonReferenceType())) { |
| 4771 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4772 | << ParamType; |
| 4773 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4774 | return true; |
| 4775 | } |
| 4776 | |
| 4777 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4778 | << ParamType |
| 4779 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4780 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4781 | |
| 4782 | ArgType = Func->getType(); |
| 4783 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4784 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4785 | // A value of reference type is not an object. |
| 4786 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4787 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4788 | diag::err_template_arg_reference_var) |
| 4789 | << Var->getType() << Arg->getSourceRange(); |
| 4790 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4791 | return true; |
| 4792 | } |
| 4793 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4794 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4795 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4796 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4797 | << Arg->getSourceRange(); |
| 4798 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4799 | return true; |
| 4800 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4801 | |
| 4802 | // If the template parameter has pointer type, we must have taken |
| 4803 | // the address of this object. |
| 4804 | if (ParamType->isReferenceType()) { |
| 4805 | if (AddressTaken) { |
| 4806 | // If we originally had an address-of operator, but the |
| 4807 | // parameter has reference type, complain and (if things look |
| 4808 | // like they will work) drop the address-of operator. |
| 4809 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4810 | ParamType.getNonReferenceType())) { |
| 4811 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4812 | << ParamType; |
| 4813 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4814 | return true; |
| 4815 | } |
| 4816 | |
| 4817 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4818 | << ParamType |
| 4819 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4820 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4821 | |
| 4822 | ArgType = Var->getType(); |
| 4823 | } |
| 4824 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4825 | if (Var->getType()->isArrayType()) { |
| 4826 | // Array-to-pointer decay. |
| 4827 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4828 | } else { |
| 4829 | // If the template parameter has pointer type but the address of |
| 4830 | // this object was not taken, complain and (possibly) recover by |
| 4831 | // taking the address of the entity. |
| 4832 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4833 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4834 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4835 | << ParamType; |
| 4836 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4837 | return true; |
| 4838 | } |
| 4839 | |
| 4840 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4841 | << ParamType |
| 4842 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4843 | |
| 4844 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4845 | } |
| 4846 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4847 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4849 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4850 | Arg, ArgType)) |
| 4851 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4852 | |
| 4853 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4854 | Converted = |
| 4855 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4856 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4857 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | /// \brief Checks whether the given template argument is a pointer to |
| 4861 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4862 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4863 | NonTypeTemplateParmDecl *Param, |
| 4864 | QualType ParamType, |
| 4865 | Expr *&ResultArg, |
| 4866 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4867 | bool Invalid = false; |
| 4868 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4869 | // Check for a null pointer value. |
| 4870 | Expr *Arg = ResultArg; |
| 4871 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4872 | case NPV_Error: |
| 4873 | return true; |
| 4874 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4875 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4876 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4877 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4878 | return false; |
| 4879 | case NPV_NotNullPointer: |
| 4880 | break; |
| 4881 | } |
| 4882 | |
| 4883 | bool ObjCLifetimeConversion; |
| 4884 | if (S.IsQualificationConversion(Arg->getType(), |
| 4885 | ParamType.getNonReferenceType(), |
| 4886 | false, ObjCLifetimeConversion)) { |
| 4887 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4888 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4889 | ResultArg = Arg; |
| 4890 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4891 | ParamType.getNonReferenceType())) { |
| 4892 | // We can't perform this conversion. |
| 4893 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4894 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4895 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4896 | return true; |
| 4897 | } |
| 4898 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4899 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4900 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4901 | Arg = Cast->getSubExpr(); |
| 4902 | |
| 4903 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4904 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4905 | // A template-argument for a non-type, non-template |
| 4906 | // template-parameter shall be one of: [...] |
| 4907 | // |
| 4908 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4909 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4910 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4911 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4912 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4913 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4914 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4915 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4916 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4917 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4918 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4919 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4920 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4921 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | Arg = Parens->getSubExpr(); |
| 4925 | } |
| 4926 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4927 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4928 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4929 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4930 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4931 | // A pointer-to-member constant written &Class::member. |
| 4932 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4933 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4934 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4935 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4936 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4937 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4938 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4939 | // A constant of pointer-to-member type. |
| 4940 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4941 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4942 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4943 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4944 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4945 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4946 | } else { |
| 4947 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4948 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4949 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4950 | return Invalid; |
| 4951 | } |
| 4952 | } |
| 4953 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4954 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4955 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4956 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4958 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4959 | return S.Diag(Arg->getLocStart(), |
| 4960 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4961 | << Arg->getSourceRange(); |
| 4962 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4963 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4964 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4965 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4966 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4967 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4968 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4969 | "Only non-static member pointers can make it here"); |
| 4970 | |
| 4971 | // Okay: this is the address of a non-static member, and therefore |
| 4972 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4973 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4974 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4975 | } else { |
| 4976 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4977 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4978 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4979 | return Invalid; |
| 4980 | } |
| 4981 | |
| 4982 | // 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] | 4983 | S.Diag(Arg->getLocStart(), |
| 4984 | diag::err_template_arg_not_pointer_to_member_form) |
| 4985 | << Arg->getSourceRange(); |
| 4986 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4987 | return true; |
| 4988 | } |
| 4989 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4990 | /// \brief Check a template argument against its corresponding |
| 4991 | /// non-type template parameter. |
| 4992 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4993 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4994 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4995 | /// returns the converted template argument. \p ParamType is the |
| 4996 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4997 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4998 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4999 | TemplateArgument &Converted, |
| 5000 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5001 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5002 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5003 | // If the parameter type somehow involves auto, deduce the type now. |
| 5004 | if (getLangOpts().CPlusPlus1z && ParamType->isUndeducedType()) { |
| 5005 | if (DeduceAutoType( |
| 5006 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
| 5007 | Arg, ParamType) == DAR_Failed) { |
| 5008 | Diag(Arg->getExprLoc(), |
| 5009 | diag::err_non_type_template_parm_type_deduction_failure) |
| 5010 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 5011 | << Arg->getSourceRange(); |
| 5012 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5013 | return ExprError(); |
| 5014 | } |
| 5015 | // CheckNonTypeTemplateParameterType will produce a diagnostic if there's |
| 5016 | // an error. The error message normally references the parameter |
| 5017 | // declaration, but here we'll pass the argument location because that's |
| 5018 | // where the parameter type is deduced. |
| 5019 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 5020 | if (ParamType.isNull()) { |
| 5021 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5022 | return ExprError(); |
| 5023 | } |
| 5024 | } |
| 5025 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5026 | // If either the parameter has a dependent type or the argument is |
| 5027 | // type-dependent, there's nothing we can check now. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5028 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5029 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5030 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5031 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5032 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5033 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5034 | // We should have already dropped all cv-qualifiers by now. |
| 5035 | assert(!ParamType.hasQualifiers() && |
| 5036 | "non-type template parameter type cannot be qualified"); |
| 5037 | |
| 5038 | if (CTAK == CTAK_Deduced && |
| 5039 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 5040 | // C++ [temp.deduct.type]p17: |
| 5041 | // If, in the declaration of a function template with a non-type |
| 5042 | // template-parameter, the non-type template-parameter is used |
| 5043 | // in an expression in the function parameter-list and, if the |
| 5044 | // corresponding template-argument is deduced, the |
| 5045 | // template-argument type shall match the type of the |
| 5046 | // template-parameter exactly, except that a template-argument |
| 5047 | // deduced from an array bound may be of any integral type. |
| 5048 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 5049 | << Arg->getType().getUnqualifiedType() |
| 5050 | << ParamType.getUnqualifiedType(); |
| 5051 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5052 | return ExprError(); |
| 5053 | } |
| 5054 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5055 | if (getLangOpts().CPlusPlus1z) { |
| 5056 | // FIXME: We can do some limited checking for a value-dependent but not |
| 5057 | // type-dependent argument. |
| 5058 | if (Arg->isValueDependent()) { |
| 5059 | Converted = TemplateArgument(Arg); |
| 5060 | return Arg; |
| 5061 | } |
| 5062 | |
| 5063 | // C++1z [temp.arg.nontype]p1: |
| 5064 | // A template-argument for a non-type template parameter shall be |
| 5065 | // a converted constant expression of the type of the template-parameter. |
| 5066 | APValue Value; |
| 5067 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 5068 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 5069 | if (ArgResult.isInvalid()) |
| 5070 | return ExprError(); |
| 5071 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5072 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 5073 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5074 | // Convert the APValue to a TemplateArgument. |
| 5075 | switch (Value.getKind()) { |
| 5076 | case APValue::Uninitialized: |
| 5077 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5078 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5079 | break; |
| 5080 | case APValue::Int: |
| 5081 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5082 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5083 | break; |
| 5084 | case APValue::MemberPointer: { |
| 5085 | assert(ParamType->isMemberPointerType()); |
| 5086 | |
| 5087 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 5088 | if (!Value.getMemberPointerPath().empty()) { |
| 5089 | Diag(Arg->getLocStart(), |
| 5090 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 5091 | << Value.getMemberPointerDecl() << ParamType |
| 5092 | << Arg->getSourceRange(); |
| 5093 | return ExprError(); |
| 5094 | } |
| 5095 | |
| 5096 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5097 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5098 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5099 | break; |
| 5100 | } |
| 5101 | case APValue::LValue: { |
| 5102 | // For a non-type template-parameter of pointer or reference type, |
| 5103 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5104 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 5105 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5106 | // -- a temporary object |
| 5107 | // -- a string literal |
| 5108 | // -- the result of a typeid expression, or |
| 5109 | // -- a predefind __func__ variable |
| 5110 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 5111 | if (isa<CXXUuidofExpr>(E)) { |
| 5112 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 5113 | break; |
| 5114 | } |
| 5115 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5116 | << Arg->getSourceRange(); |
| 5117 | return ExprError(); |
| 5118 | } |
| 5119 | auto *VD = const_cast<ValueDecl *>( |
| 5120 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 5121 | // -- a subobject |
| 5122 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 5123 | VD && VD->getType()->isArrayType() && |
| 5124 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 5125 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 5126 | // Per defect report (no number yet): |
| 5127 | // ... other than a pointer to the first element of a complete array |
| 5128 | // object. |
| 5129 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 5130 | Value.isLValueOnePastTheEnd()) { |
| 5131 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 5132 | << Value.getAsString(Context, ParamType); |
| 5133 | return ExprError(); |
| 5134 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5135 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5136 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5137 | assert((!VD || !ParamType->isNullPtrType()) && |
| 5138 | "non-null value of type nullptr_t?"); |
| 5139 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5140 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5141 | break; |
| 5142 | } |
| 5143 | case APValue::AddrLabelDiff: |
| 5144 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 5145 | case APValue::Float: |
| 5146 | case APValue::ComplexInt: |
| 5147 | case APValue::ComplexFloat: |
| 5148 | case APValue::Vector: |
| 5149 | case APValue::Array: |
| 5150 | case APValue::Struct: |
| 5151 | case APValue::Union: |
| 5152 | llvm_unreachable("invalid kind for template argument"); |
| 5153 | } |
| 5154 | |
| 5155 | return ArgResult.get(); |
| 5156 | } |
| 5157 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5158 | // C++ [temp.arg.nontype]p5: |
| 5159 | // The following conversions are performed on each expression used |
| 5160 | // as a non-type template-argument. If a non-type |
| 5161 | // template-argument cannot be converted to the type of the |
| 5162 | // corresponding template-parameter then the program is |
| 5163 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5164 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5165 | // C++11: |
| 5166 | // -- for a non-type template-parameter of integral or |
| 5167 | // enumeration type, conversions permitted in a converted |
| 5168 | // constant expression are applied. |
| 5169 | // |
| 5170 | // C++98: |
| 5171 | // -- for a non-type template-parameter of integral or |
| 5172 | // enumeration type, integral promotions (4.5) and integral |
| 5173 | // conversions (4.7) are applied. |
| 5174 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5175 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5176 | // We can't check arbitrary value-dependent arguments. |
| 5177 | // FIXME: If there's no viable conversion to the template parameter type, |
| 5178 | // we should be able to diagnose that prior to instantiation. |
| 5179 | if (Arg->isValueDependent()) { |
| 5180 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5181 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | // C++ [temp.arg.nontype]p1: |
| 5185 | // A template-argument for a non-type, non-template template-parameter |
| 5186 | // shall be one of: |
| 5187 | // |
| 5188 | // -- for a non-type template-parameter of integral or enumeration |
| 5189 | // type, a converted constant expression of the type of the |
| 5190 | // template-parameter; or |
| 5191 | llvm::APSInt Value; |
| 5192 | ExprResult ArgResult = |
| 5193 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5194 | CCEK_TemplateArg); |
| 5195 | if (ArgResult.isInvalid()) |
| 5196 | return ExprError(); |
| 5197 | |
| 5198 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5199 | // always a no-op, except when the parameter type is bool. In |
| 5200 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5201 | QualType IntegerType = ParamType; |
| 5202 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5203 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5204 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5205 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5206 | Converted = TemplateArgument(Context, Value, |
| 5207 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5208 | return ArgResult; |
| 5209 | } |
| 5210 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5211 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5212 | if (ArgResult.isInvalid()) |
| 5213 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5214 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5215 | |
| 5216 | QualType ArgType = Arg->getType(); |
| 5217 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5218 | // C++ [temp.arg.nontype]p1: |
| 5219 | // A template-argument for a non-type, non-template |
| 5220 | // template-parameter shall be one of: |
| 5221 | // |
| 5222 | // -- an integral constant-expression of integral or enumeration |
| 5223 | // type; or |
| 5224 | // -- the name of a non-type template-parameter; or |
| 5225 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5226 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5227 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5228 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5229 | diag::err_template_arg_not_integral_or_enumeral) |
| 5230 | << ArgType << Arg->getSourceRange(); |
| 5231 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5232 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5233 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5234 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5235 | QualType T; |
| 5236 | |
| 5237 | public: |
| 5238 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5239 | |
| 5240 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5241 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5242 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5243 | } |
| 5244 | } Diagnoser(ArgType); |
| 5245 | |
| 5246 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5247 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5248 | if (!Arg) |
| 5249 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5250 | } |
| 5251 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5252 | // From here on out, all we care about is the unqualified form |
| 5253 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5254 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5255 | |
| 5256 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5257 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5258 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5259 | } else if (ParamType->isBooleanType()) { |
| 5260 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5261 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5262 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5263 | !ParamType->isEnumeralType()) { |
| 5264 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5265 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5266 | } else { |
| 5267 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5268 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5269 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5270 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5271 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5272 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5273 | } |
| 5274 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5275 | // Add the value of this argument to the list of converted |
| 5276 | // arguments. We use the bitwidth and signedness of the template |
| 5277 | // parameter. |
| 5278 | if (Arg->isValueDependent()) { |
| 5279 | // The argument is value-dependent. Create a new |
| 5280 | // TemplateArgument with the converted expression. |
| 5281 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5282 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5285 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5286 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5287 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5288 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5289 | if (ParamType->isBooleanType()) { |
| 5290 | // Value must be zero or one. |
| 5291 | Value = Value != 0; |
| 5292 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5293 | if (Value.getBitWidth() != AllowedBits) |
| 5294 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5295 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5296 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5297 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5298 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5299 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5300 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5301 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5302 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5303 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5304 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5305 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5306 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5307 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5308 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5309 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5310 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5311 | << Arg->getSourceRange(); |
| 5312 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5313 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5314 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5315 | // Complain if we overflowed the template parameter's type. |
| 5316 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5317 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5318 | RequiredBits = OldValue.getActiveBits(); |
| 5319 | else if (OldValue.isUnsigned()) |
| 5320 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5321 | else |
| 5322 | RequiredBits = OldValue.getMinSignedBits(); |
| 5323 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5324 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5325 | diag::warn_template_arg_too_large) |
| 5326 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5327 | << Arg->getSourceRange(); |
| 5328 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5329 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5330 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5331 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5332 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5333 | ParamType->isEnumeralType() |
| 5334 | ? Context.getCanonicalType(ParamType) |
| 5335 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5336 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5337 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5338 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5339 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5340 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5341 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5342 | // Handle pointer-to-function, reference-to-function, and |
| 5343 | // pointer-to-member-function all in (roughly) the same way. |
| 5344 | if (// -- For a non-type template-parameter of type pointer to |
| 5345 | // function, only the function-to-pointer conversion (4.3) is |
| 5346 | // applied. If the template-argument represents a set of |
| 5347 | // overloaded functions (or a pointer to such), the matching |
| 5348 | // function is selected from the set (13.4). |
| 5349 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5350 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5351 | // -- For a non-type template-parameter of type reference to |
| 5352 | // function, no conversions apply. If the template-argument |
| 5353 | // represents a set of overloaded functions, the matching |
| 5354 | // function is selected from the set (13.4). |
| 5355 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5356 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5357 | // -- For a non-type template-parameter of type pointer to |
| 5358 | // member function, no conversions apply. If the |
| 5359 | // template-argument represents a set of overloaded member |
| 5360 | // functions, the matching member function is selected from |
| 5361 | // the set (13.4). |
| 5362 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5363 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5364 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5365 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5366 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5367 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5368 | true, |
| 5369 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5370 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5371 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5372 | |
| 5373 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5374 | ArgType = Arg->getType(); |
| 5375 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5376 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5377 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5378 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5379 | if (!ParamType->isMemberPointerType()) { |
| 5380 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5381 | ParamType, |
| 5382 | Arg, Converted)) |
| 5383 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5384 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5385 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5386 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5387 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5388 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5389 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5390 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5393 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5394 | // -- for a non-type template-parameter of type pointer to |
| 5395 | // object, qualification conversions (4.4) and the |
| 5396 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5397 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5398 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5399 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5400 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5401 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5402 | ParamType, |
| 5403 | Arg, Converted)) |
| 5404 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5405 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5406 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5407 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5408 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5409 | // -- For a non-type template-parameter of type reference to |
| 5410 | // object, no conversions apply. The type referred to by the |
| 5411 | // reference may be more cv-qualified than the (otherwise |
| 5412 | // identical) type of the template-argument. The |
| 5413 | // template-parameter is bound directly to the |
| 5414 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5415 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5416 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5417 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5418 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5419 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5420 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5421 | true, |
| 5422 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5423 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5424 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5425 | |
| 5426 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5427 | ArgType = Arg->getType(); |
| 5428 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5429 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5430 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5431 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5432 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5433 | ParamType, |
| 5434 | Arg, Converted)) |
| 5435 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5436 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5437 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5438 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5439 | // Deal with parameters of type std::nullptr_t. |
| 5440 | if (ParamType->isNullPtrType()) { |
| 5441 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5442 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5443 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5444 | } |
| 5445 | |
| 5446 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5447 | case NPV_NotNullPointer: |
| 5448 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5449 | << Arg->getType() << ParamType; |
| 5450 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5451 | return ExprError(); |
| 5452 | |
| 5453 | case NPV_Error: |
| 5454 | return ExprError(); |
| 5455 | |
| 5456 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5457 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5458 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5459 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5460 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5461 | } |
| 5462 | } |
| 5463 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5464 | // -- For a non-type template-parameter of type pointer to data |
| 5465 | // member, qualification conversions (4.4) are applied. |
| 5466 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5467 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5468 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5469 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5470 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5471 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
| 5474 | /// \brief Check a template argument against its corresponding |
| 5475 | /// template template parameter. |
| 5476 | /// |
| 5477 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5478 | /// It returns true if an error occurred, and false otherwise. |
| 5479 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5480 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5481 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5482 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5483 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5484 | if (!Template) { |
| 5485 | // Any dependent template name is fine. |
| 5486 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5487 | return false; |
| 5488 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5489 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5490 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5491 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5492 | // the name of a class template or an alias template, expressed as an |
| 5493 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5494 | // primary class templates are considered when matching the |
| 5495 | // template template argument with the corresponding parameter; |
| 5496 | // partial specializations are not considered even if their |
| 5497 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5498 | // |
| 5499 | // Note that we also allow template template parameters here, which |
| 5500 | // will happen when we are dealing with, e.g., class template |
| 5501 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5502 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5503 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5504 | !isa<TypeAliasTemplateDecl>(Template) && |
| 5505 | !isa<BuiltinTemplateDecl>(Template)) { |
| 5506 | assert(isa<FunctionTemplateDecl>(Template) && |
| 5507 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 5508 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5509 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 5510 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5513 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5514 | if (Param->isExpandedParameterPack()) |
| 5515 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5516 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5517 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5518 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5519 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5520 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5521 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5524 | /// \brief Given a non-type template argument that refers to a |
| 5525 | /// declaration and the type of its corresponding non-type template |
| 5526 | /// parameter, produce an expression that properly refers to that |
| 5527 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5528 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5529 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5530 | QualType ParamType, |
| 5531 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5532 | // C++ [temp.param]p8: |
| 5533 | // |
| 5534 | // A non-type template-parameter of type "array of T" or |
| 5535 | // "function returning T" is adjusted to be of type "pointer to |
| 5536 | // T" or "pointer to function returning T", respectively. |
| 5537 | if (ParamType->isArrayType()) |
| 5538 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5539 | else if (ParamType->isFunctionType()) |
| 5540 | ParamType = Context.getPointerType(ParamType); |
| 5541 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5542 | // For a NULL non-type template argument, return nullptr casted to the |
| 5543 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5544 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5545 | return ImpCastExprToType( |
| 5546 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5547 | ParamType, |
| 5548 | ParamType->getAs<MemberPointerType>() |
| 5549 | ? CK_NullToMemberPointer |
| 5550 | : CK_NullToPointer); |
| 5551 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5552 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5553 | "Only declaration template arguments permitted here"); |
| 5554 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5555 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5556 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5557 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5558 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5559 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5560 | // If the value is a class member, we might have a pointer-to-member. |
| 5561 | // Determine whether the non-type template template parameter is of |
| 5562 | // pointer-to-member type. If so, we need to build an appropriate |
| 5563 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5564 | // would refer to the member itself. |
| 5565 | if (ParamType->isMemberPointerType()) { |
| 5566 | QualType ClassType |
| 5567 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5568 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5569 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5570 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5571 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5572 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5573 | |
| 5574 | // The actual value-ness of this is unimportant, but for |
| 5575 | // internal consistency's sake, references to instance methods |
| 5576 | // are r-values. |
| 5577 | ExprValueKind VK = VK_LValue; |
| 5578 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5579 | VK = VK_RValue; |
| 5580 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5581 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5582 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5583 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5584 | Loc, |
| 5585 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5586 | if (RefExpr.isInvalid()) |
| 5587 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5588 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5589 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5590 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5591 | // We might need to perform a trailing qualification conversion, since |
| 5592 | // the element type on the parameter could be more qualified than the |
| 5593 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5594 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5595 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5596 | ParamType.getUnqualifiedType(), false, |
| 5597 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5598 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5599 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5600 | assert(!RefExpr.isInvalid() && |
| 5601 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5602 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5603 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5604 | } |
| 5605 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5606 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5607 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5608 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5609 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5610 | // When the non-type template parameter is a pointer, take the |
| 5611 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5612 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5613 | if (RefExpr.isInvalid()) |
| 5614 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5615 | |
| 5616 | if (T->isFunctionType() || T->isArrayType()) { |
| 5617 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5618 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5619 | if (RefExpr.isInvalid()) |
| 5620 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5621 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5622 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5623 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5624 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5625 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5626 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5627 | } |
| 5628 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5629 | ExprValueKind VK = VK_RValue; |
| 5630 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5631 | // If the non-type template parameter has reference type, qualify the |
| 5632 | // resulting declaration reference with the extra qualifiers on the |
| 5633 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5634 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5635 | VK = VK_LValue; |
| 5636 | T = Context.getQualifiedType(T, |
| 5637 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5638 | } else if (isa<FunctionDecl>(VD)) { |
| 5639 | // References to functions are always lvalues. |
| 5640 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5641 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5642 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5643 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5644 | } |
| 5645 | |
| 5646 | /// \brief Construct a new expression that refers to the given |
| 5647 | /// integral template argument with the given source-location |
| 5648 | /// information. |
| 5649 | /// |
| 5650 | /// This routine takes care of the mapping from an integral template |
| 5651 | /// argument (which may have any integral type) to the appropriate |
| 5652 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5653 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5654 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5655 | SourceLocation Loc) { |
| 5656 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5657 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5658 | QualType OrigT = Arg.getIntegralType(); |
| 5659 | |
| 5660 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5661 | // type the same size as the enumerator. We don't want to build an |
| 5662 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5663 | // any integral type with C++11 enum classes, make sure we create the right |
| 5664 | // type of literal for it. |
| 5665 | QualType T = OrigT; |
| 5666 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5667 | T = ET->getDecl()->getIntegerType(); |
| 5668 | |
| 5669 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5670 | if (T->isAnyCharacterType()) { |
Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 5671 | // This does not need to handle u8 character literals because those are |
| 5672 | // 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] | 5673 | CharacterLiteral::CharacterKind Kind; |
| 5674 | if (T->isWideCharType()) |
| 5675 | Kind = CharacterLiteral::Wide; |
| 5676 | else if (T->isChar16Type()) |
| 5677 | Kind = CharacterLiteral::UTF16; |
| 5678 | else if (T->isChar32Type()) |
| 5679 | Kind = CharacterLiteral::UTF32; |
| 5680 | else |
| 5681 | Kind = CharacterLiteral::Ascii; |
| 5682 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5683 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5684 | Kind, T, Loc); |
| 5685 | } else if (T->isBooleanType()) { |
| 5686 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5687 | T, Loc); |
| 5688 | } else if (T->isNullPtrType()) { |
| 5689 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5690 | } else { |
| 5691 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5692 | } |
| 5693 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5694 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5695 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5696 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5697 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5698 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5699 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5700 | Loc, Loc); |
| 5701 | } |
| 5702 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5703 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5704 | } |
| 5705 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5706 | /// \brief Match two template parameters within template parameter lists. |
| 5707 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5708 | bool Complain, |
| 5709 | Sema::TemplateParameterListEqualKind Kind, |
| 5710 | SourceLocation TemplateArgLoc) { |
| 5711 | // Check the actual kind (type, non-type, template). |
| 5712 | if (Old->getKind() != New->getKind()) { |
| 5713 | if (Complain) { |
| 5714 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5715 | if (TemplateArgLoc.isValid()) { |
| 5716 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5717 | NextDiag = diag::note_template_param_different_kind; |
| 5718 | } |
| 5719 | S.Diag(New->getLocation(), NextDiag) |
| 5720 | << (Kind != Sema::TPL_TemplateMatch); |
| 5721 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5722 | << (Kind != Sema::TPL_TemplateMatch); |
| 5723 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5724 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5725 | return false; |
| 5726 | } |
| 5727 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5728 | // Check that both are parameter packs are neither are parameter packs. |
| 5729 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5730 | // template template parameter, the template template parameter can have |
| 5731 | // a parameter pack where the template template argument does not. |
| 5732 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5733 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5734 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5735 | if (Complain) { |
| 5736 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5737 | if (TemplateArgLoc.isValid()) { |
| 5738 | S.Diag(TemplateArgLoc, |
| 5739 | diag::err_template_arg_template_params_mismatch); |
| 5740 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5741 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5742 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5743 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5744 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5745 | : 2; |
| 5746 | S.Diag(New->getLocation(), NextDiag) |
| 5747 | << ParamKind << New->isParameterPack(); |
| 5748 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5749 | << ParamKind << Old->isParameterPack(); |
| 5750 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5751 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5752 | return false; |
| 5753 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5754 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5755 | // For non-type template parameters, check the type of the parameter. |
| 5756 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5757 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5758 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5759 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5760 | // If we are matching a template template argument to a template |
| 5761 | // template parameter and one of the non-type template parameter types |
| 5762 | // is dependent, then we must wait until template instantiation time |
| 5763 | // to actually compare the arguments. |
| 5764 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5765 | (OldNTTP->getType()->isDependentType() || |
| 5766 | NewNTTP->getType()->isDependentType())) |
| 5767 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5768 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5769 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5770 | if (Complain) { |
| 5771 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5772 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5773 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5774 | diag::err_template_arg_template_params_mismatch); |
| 5775 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5776 | } |
| 5777 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5778 | << NewNTTP->getType() |
| 5779 | << (Kind != Sema::TPL_TemplateMatch); |
| 5780 | S.Diag(OldNTTP->getLocation(), |
| 5781 | diag::note_template_nontype_parm_prev_declaration) |
| 5782 | << OldNTTP->getType(); |
| 5783 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5784 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5785 | return false; |
| 5786 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5787 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5788 | return true; |
| 5789 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5790 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5791 | // For template template parameters, check the template parameter types. |
| 5792 | // The template parameter lists of template template |
| 5793 | // parameters must agree. |
| 5794 | if (TemplateTemplateParmDecl *OldTTP |
| 5795 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5796 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5797 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5798 | OldTTP->getTemplateParameters(), |
| 5799 | Complain, |
| 5800 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5801 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5802 | : Kind), |
| 5803 | TemplateArgLoc); |
| 5804 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5805 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5806 | return true; |
| 5807 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5808 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5809 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5810 | /// lists. |
| 5811 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5812 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5813 | TemplateParameterList *New, |
| 5814 | TemplateParameterList *Old, |
| 5815 | Sema::TemplateParameterListEqualKind Kind, |
| 5816 | SourceLocation TemplateArgLoc) { |
| 5817 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5818 | if (TemplateArgLoc.isValid()) { |
| 5819 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5820 | NextDiag = diag::note_template_param_list_different_arity; |
| 5821 | } |
| 5822 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5823 | << (New->size() > Old->size()) |
| 5824 | << (Kind != Sema::TPL_TemplateMatch) |
| 5825 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5826 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5827 | << (Kind != Sema::TPL_TemplateMatch) |
| 5828 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5829 | } |
| 5830 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5831 | /// \brief Determine whether the given template parameter lists are |
| 5832 | /// equivalent. |
| 5833 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5834 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5835 | /// source code as part of a new template declaration. |
| 5836 | /// |
| 5837 | /// \param Old The old template parameter list, typically found via |
| 5838 | /// name lookup of the template declared with this template parameter |
| 5839 | /// list. |
| 5840 | /// |
| 5841 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5842 | /// the template parameter lists are not equivalent. |
| 5843 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5844 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5845 | /// |
| 5846 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5847 | /// are actually checking the template parameter list of a template |
| 5848 | /// argument (New) against the template parameter list of its |
| 5849 | /// corresponding template template parameter (Old). We produce |
| 5850 | /// slightly different diagnostics in this scenario. |
| 5851 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5852 | /// \returns True if the template parameter lists are equal, false |
| 5853 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5854 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5855 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5856 | TemplateParameterList *Old, |
| 5857 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5858 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5859 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5860 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5861 | if (Complain) |
| 5862 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5863 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5864 | |
| 5865 | return false; |
| 5866 | } |
| 5867 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5868 | // C++0x [temp.arg.template]p3: |
| 5869 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5870 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5871 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5872 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5873 | // template-parameter-list of P. [...] |
| 5874 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5875 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5876 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5877 | OldParmEnd = Old->end(); |
| 5878 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5879 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5880 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5881 | if (NewParm == NewParmEnd) { |
| 5882 | if (Complain) |
| 5883 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5884 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5885 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5886 | return false; |
| 5887 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5888 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5889 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5890 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5891 | return false; |
| 5892 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5893 | ++NewParm; |
| 5894 | continue; |
| 5895 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5896 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5897 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5898 | // [...] When P's template- parameter-list contains a template parameter |
| 5899 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5900 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5901 | // template-parameter-list of A with the same type and form as the |
| 5902 | // template parameter pack in P (ignoring whether those template |
| 5903 | // parameters are template parameter packs). |
| 5904 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5905 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5906 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5907 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5908 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5909 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5910 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5911 | // Make sure we exhausted all of the arguments. |
| 5912 | if (NewParm != NewParmEnd) { |
| 5913 | if (Complain) |
| 5914 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5915 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5916 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5917 | return false; |
| 5918 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5919 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5920 | return true; |
| 5921 | } |
| 5922 | |
| 5923 | /// \brief Check whether a template can be declared within this scope. |
| 5924 | /// |
| 5925 | /// If the template declaration is valid in this scope, returns |
| 5926 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5927 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5928 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5929 | if (!S) |
| 5930 | return false; |
| 5931 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5932 | // Find the nearest enclosing declaration scope. |
| 5933 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5934 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5935 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5936 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5937 | // C++ [temp]p4: |
| 5938 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5939 | DeclContext *Ctx = S->getEntity(); |
Alex Lorenz | 560ae56 | 2016-11-02 15:46:34 +0000 | [diff] [blame] | 5940 | if (Ctx && Ctx->isExternCContext()) { |
| 5941 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 5942 | << TemplateParams->getSourceRange(); |
| 5943 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 5944 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 5945 | return true; |
| 5946 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5947 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5948 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5949 | // C++ [temp]p2: |
| 5950 | // A template-declaration can appear only as a namespace scope or |
| 5951 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5952 | if (Ctx) { |
| 5953 | if (Ctx->isFileContext()) |
| 5954 | return false; |
| 5955 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5956 | // C++ [temp.mem]p2: |
| 5957 | // A local class shall not have member templates. |
| 5958 | if (RD->isLocalClass()) |
| 5959 | return Diag(TemplateParams->getTemplateLoc(), |
| 5960 | diag::err_template_inside_local_class) |
| 5961 | << TemplateParams->getSourceRange(); |
| 5962 | else |
| 5963 | return false; |
| 5964 | } |
| 5965 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5966 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5967 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5968 | diag::err_template_outside_namespace_or_class_scope) |
| 5969 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5970 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5971 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5972 | /// \brief Determine what kind of template specialization the given declaration |
| 5973 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5974 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5975 | if (!D) |
| 5976 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5977 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5978 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5979 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5980 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5981 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5982 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5983 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5984 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5985 | return TSK_Undeclared; |
| 5986 | } |
| 5987 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5988 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5989 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5990 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5991 | /// This routine determines whether a template specialization can be declared |
| 5992 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5993 | /// |
| 5994 | /// \param S the semantic analysis object for which this check is being |
| 5995 | /// performed. |
| 5996 | /// |
| 5997 | /// \param Specialized the entity being specialized or instantiated, which |
| 5998 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6000 | /// member class). |
| 6001 | /// |
| 6002 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 6003 | /// |
| 6004 | /// \param Loc the location of the explicit specialization or instantiation of |
| 6005 | /// this entity. |
| 6006 | /// |
| 6007 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 6008 | /// a class template. |
| 6009 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6010 | /// \returns true if there was an error that we cannot recover from, false |
| 6011 | /// otherwise. |
| 6012 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 6013 | NamedDecl *Specialized, |
| 6014 | NamedDecl *PrevDecl, |
| 6015 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6016 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6017 | // Keep these "kind" numbers in sync with the %select statements in the |
| 6018 | // various diagnostics emitted by this routine. |
| 6019 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6020 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6021 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6022 | else if (isa<VarTemplateDecl>(Specialized)) |
| 6023 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 6024 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6025 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6026 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6027 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6028 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6029 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6030 | else if (isa<RecordDecl>(Specialized)) |
| 6031 | EntityKind = 7; |
| 6032 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 6033 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6034 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6035 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6036 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6037 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6038 | return true; |
| 6039 | } |
| 6040 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6041 | // C++ [temp.expl.spec]p2: |
| 6042 | // An explicit specialization shall be declared in the namespace |
| 6043 | // of which the template is a member, or, for member templates, in |
| 6044 | // the namespace of which the enclosing class or enclosing class |
| 6045 | // template is a member. An explicit specialization of a member |
| 6046 | // function, member class or static data member of a class |
| 6047 | // template shall be declared in the namespace of which the class |
| 6048 | // template is a member. Such a declaration may also be a |
| 6049 | // definition. If the declaration is not a definition, the |
| 6050 | // specialization may be defined later in the name- space in which |
| 6051 | // the explicit specialization was declared, or in a namespace |
| 6052 | // that encloses the one in which the explicit specialization was |
| 6053 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6054 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6055 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6056 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6057 | return true; |
| 6058 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6059 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6060 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6061 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 6062 | // Do not warn for class scope explicit specialization during |
| 6063 | // instantiation, warning was already emitted during pattern |
| 6064 | // semantic analysis. |
| 6065 | if (!S.ActiveTemplateInstantiations.size()) |
| 6066 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 6067 | << Specialized; |
| 6068 | } else { |
| 6069 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6070 | << Specialized; |
| 6071 | return true; |
| 6072 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6073 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6074 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 6075 | if (S.CurContext->isRecord() && |
| 6076 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 6077 | // Make sure that we're specializing in the right record context. |
| 6078 | // Otherwise, things can go horribly wrong. |
| 6079 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6080 | << Specialized; |
| 6081 | return true; |
| 6082 | } |
| 6083 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6084 | // C++ [temp.class.spec]p6: |
| 6085 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6086 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 6087 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6088 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6089 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6090 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6091 | |
| 6092 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 6093 | // namespace. |
| 6094 | // Note that HandleDeclarator() performs this check for explicit |
| 6095 | // specializations of function templates, static data members, and member |
| 6096 | // functions, so we skip the check here for those kinds of entities. |
| 6097 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 6098 | // Should we refactor that check, so that it occurs later? |
| 6099 | if (!DC->Encloses(SpecializedContext) && |
| 6100 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 6101 | isa<FunctionDecl>(Specialized) || |
| 6102 | isa<VarTemplateDecl>(Specialized) || |
| 6103 | isa<VarDecl>(Specialized))) { |
| 6104 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 6105 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 6106 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 6107 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6108 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 6109 | if (S.getLangOpts().MicrosoftExt) |
| 6110 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 6111 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 6112 | << cast<NamedDecl>(SpecializedContext); |
| 6113 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6114 | llvm_unreachable("unexpected namespace context for specialization"); |
| 6115 | |
| 6116 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 6117 | } else if ((!PrevDecl || |
| 6118 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 6119 | getTemplateSpecializationKind(PrevDecl) == |
| 6120 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6121 | // C++ [temp.exp.spec]p2: |
| 6122 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6123 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6124 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6125 | // An explicit specialization of a member function, member class or |
| 6126 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6127 | // namespace of which the class template is a member. |
| 6128 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6129 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6130 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6131 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6132 | // C++11 [temp.explicit]p3: |
| 6133 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 6134 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6135 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6136 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6137 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6138 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6139 | "DC encloses TU but isn't in enclosing namespace set"); |
| 6140 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 6141 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6142 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6143 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6144 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6145 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6146 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6147 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 6148 | else |
| 6149 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 6150 | S.Diag(Loc, Diag) |
| 6151 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 6152 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6153 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6154 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6155 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6156 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6157 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6158 | return false; |
| 6159 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6160 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6161 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 6162 | if (!E->isInstantiationDependent()) |
| 6163 | return SourceLocation(); |
| 6164 | DependencyChecker Checker(Depth); |
| 6165 | Checker.TraverseStmt(E); |
| 6166 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6167 | return E->getSourceRange(); |
| 6168 | return Checker.MatchLoc; |
| 6169 | } |
| 6170 | |
| 6171 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6172 | if (!TL.getType()->isDependentType()) |
| 6173 | return SourceLocation(); |
| 6174 | DependencyChecker Checker(Depth); |
| 6175 | Checker.TraverseTypeLoc(TL); |
| 6176 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6177 | return TL.getSourceRange(); |
| 6178 | return Checker.MatchLoc; |
| 6179 | } |
| 6180 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6181 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6182 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6183 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6184 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6185 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6186 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6187 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6188 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6189 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6190 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6191 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6192 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6193 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6194 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6195 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6196 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6197 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6198 | |
| 6199 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6200 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6201 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6202 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6203 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6204 | |
| 6205 | // Strip off any implicit casts we added as part of type checking. |
| 6206 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6207 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6208 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6209 | // C++ [temp.class.spec]p8: |
| 6210 | // A non-type argument is non-specialized if it is the name of a |
| 6211 | // non-type parameter. All other non-type arguments are |
| 6212 | // specialized. |
| 6213 | // |
| 6214 | // Below, we check the two conditions that only apply to |
| 6215 | // specialized non-type arguments, so skip any non-specialized |
| 6216 | // arguments. |
| 6217 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6218 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6219 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6220 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6221 | // C++ [temp.class.spec]p9: |
| 6222 | // Within the argument list of a class template partial |
| 6223 | // specialization, the following restrictions apply: |
| 6224 | // -- A partially specialized non-type argument expression |
| 6225 | // shall not involve a template parameter of the partial |
| 6226 | // specialization except when the argument expression is a |
| 6227 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6228 | SourceRange ParamUseRange = |
| 6229 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 6230 | if (ParamUseRange.isValid()) { |
| 6231 | if (IsDefaultArgument) { |
| 6232 | S.Diag(TemplateNameLoc, |
| 6233 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6234 | S.Diag(ParamUseRange.getBegin(), |
| 6235 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6236 | << ParamUseRange; |
| 6237 | } else { |
| 6238 | S.Diag(ParamUseRange.getBegin(), |
| 6239 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6240 | << ParamUseRange; |
| 6241 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6242 | return true; |
| 6243 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6244 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6245 | // -- The type of a template parameter corresponding to a |
| 6246 | // specialized non-type argument shall not be dependent on a |
| 6247 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6248 | // |
| 6249 | // FIXME: We need to delay this check until instantiation in some cases: |
| 6250 | // |
| 6251 | // template<template<typename> class X> struct A { |
| 6252 | // template<typename T, X<T> N> struct B; |
| 6253 | // template<typename T> struct B<T, 0>; |
| 6254 | // }; |
| 6255 | // template<typename> using X = int; |
| 6256 | // A<X>::B<int, 0> b; |
| 6257 | ParamUseRange = findTemplateParameter( |
| 6258 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 6259 | if (ParamUseRange.isValid()) { |
| 6260 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6261 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 6262 | << Param->getType() << ParamUseRange; |
| 6263 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 6264 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6265 | return true; |
| 6266 | } |
| 6267 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6268 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6269 | return false; |
| 6270 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6271 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6272 | /// \brief Check the non-type template arguments of a class template |
| 6273 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6274 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6275 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6276 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6277 | /// template. |
| 6278 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6279 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6280 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6281 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6282 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6283 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6284 | Sema &S, SourceLocation TemplateNameLoc, |
| 6285 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6286 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6287 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 6288 | |
| 6289 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6290 | NonTypeTemplateParmDecl *Param |
| 6291 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6292 | if (!Param) |
| 6293 | continue; |
| 6294 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6295 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 6296 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6297 | return true; |
| 6298 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6299 | |
| 6300 | return false; |
| 6301 | } |
| 6302 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6303 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6304 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6305 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6306 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6307 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6308 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6309 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6310 | MultiTemplateParamsArg |
| 6311 | TemplateParameterLists, |
| 6312 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6313 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6314 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6315 | CXXScopeSpec &SS = TemplateId.SS; |
| 6316 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6317 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6318 | // store the location of the outermost template keyword in the declaration. |
| 6319 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6320 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6321 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6322 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6323 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6324 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6325 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6326 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6327 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6328 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6329 | |
| 6330 | if (!ClassTemplate) { |
| 6331 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6332 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6333 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6334 | return true; |
| 6335 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6336 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6337 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6338 | bool isPartialSpecialization = false; |
| 6339 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6340 | // Check the validity of the template headers that introduce this |
| 6341 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6342 | // FIXME: We probably shouldn't complain about these headers for |
| 6343 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6344 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6345 | TemplateParameterList *TemplateParams = |
| 6346 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6347 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6348 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6349 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6350 | if (Invalid) |
| 6351 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6352 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6353 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6354 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6355 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6356 | if (TUK == TUK_Friend) { |
| 6357 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6358 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6359 | return true; |
| 6360 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6361 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6362 | // C++ [temp.class.spec]p10: |
| 6363 | // The template parameter list of a specialization shall not |
| 6364 | // contain default template argument values. |
| 6365 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6366 | Decl *Param = TemplateParams->getParam(I); |
| 6367 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6368 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6369 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6370 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6371 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6372 | } |
| 6373 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6374 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6375 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6376 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6377 | diag::err_default_arg_in_partial_spec) |
| 6378 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6379 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6380 | } |
| 6381 | } else { |
| 6382 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6383 | if (TTP->hasDefaultArgument()) { |
| 6384 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6385 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6386 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6387 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6388 | } |
| 6389 | } |
| 6390 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6391 | } else if (TemplateParams) { |
| 6392 | if (TUK == TUK_Friend) |
| 6393 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6394 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6395 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6396 | TemplateParams->getRAngleLoc())) |
| 6397 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6398 | else |
| 6399 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6400 | } else { |
| 6401 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6402 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6403 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6404 | // Check that the specialization uses the same tag kind as the |
| 6405 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6406 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6407 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6408 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6409 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 6410 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6411 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6412 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6413 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6414 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6415 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6416 | diag::note_previous_use); |
| 6417 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6418 | } |
| 6419 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6420 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6421 | TemplateArgumentListInfo TemplateArgs = |
| 6422 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6423 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6424 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6425 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6426 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6427 | UPPC_PartialSpecialization)) |
| 6428 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6429 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6430 | // Check that the template argument list is well-formed for this |
| 6431 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6432 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6433 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6434 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6435 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6436 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6437 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6438 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6439 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6440 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6441 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6442 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6443 | return true; |
| 6444 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6445 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6446 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6447 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6448 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6449 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6450 | << ClassTemplate->getDeclName(); |
| 6451 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6452 | } |
| 6453 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6454 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6455 | void *InsertPos = nullptr; |
| 6456 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6457 | |
| 6458 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6459 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6460 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6461 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6462 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6463 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6464 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6465 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6466 | // Check whether we can declare a class template specialization in |
| 6467 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6468 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6469 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6470 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6471 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6472 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6473 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6474 | // The canonical type |
| 6475 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6476 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6477 | // Build the canonical type that describes the converted template |
| 6478 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6479 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6480 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6481 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6482 | |
| 6483 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6484 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6485 | // C++ [temp.class.spec]p9b3: |
| 6486 | // |
| 6487 | // -- The argument list of the specialization shall not be identical |
| 6488 | // to the implicit argument list of the primary template. |
| 6489 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6490 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6491 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6492 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6493 | ClassTemplate->getIdentifier(), |
| 6494 | TemplateNameLoc, |
| 6495 | Attr, |
| 6496 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6497 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6498 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6499 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6500 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6501 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6502 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6503 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6504 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6505 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6506 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6507 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6508 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6509 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6510 | TemplateParams, |
| 6511 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6512 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6513 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6514 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6515 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6516 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6517 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6518 | Partial->setTemplateParameterListsInfo( |
| 6519 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6520 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6521 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6522 | if (!PrevPartial) |
| 6523 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6524 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6525 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6526 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6527 | // template specialization, make a note of that. |
| 6528 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6529 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6530 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6531 | // Check that all of the template parameters of the class template |
| 6532 | // partial specialization are deducible from the template |
| 6533 | // arguments. If not, this class template partial specialization |
| 6534 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6535 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6536 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6537 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6538 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6539 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6540 | if (!DeducibleParams.all()) { |
| 6541 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6542 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6543 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6544 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6545 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6546 | if (!DeducibleParams[I]) { |
| 6547 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6548 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6549 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6550 | diag::note_partial_spec_unused_parameter) |
| 6551 | << Param->getDeclName(); |
| 6552 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6553 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6554 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6555 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6556 | } |
| 6557 | } |
| 6558 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6559 | } else { |
| 6560 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6561 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6562 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6563 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6564 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6565 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6566 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6567 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6568 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6569 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6570 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6571 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6572 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6573 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6574 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6575 | if (!PrevDecl) |
| 6576 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6577 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6578 | if (CurContext->isDependentContext()) { |
| 6579 | // -fms-extensions permits specialization of nested classes without |
| 6580 | // fully specializing the outer class(es). |
| 6581 | assert(getLangOpts().MicrosoftExt && |
| 6582 | "Only possible with -fms-extensions!"); |
| 6583 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6584 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6585 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6586 | } else { |
| 6587 | CanonType = Context.getTypeDeclType(Specialization); |
| 6588 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6589 | } |
| 6590 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6591 | // C++ [temp.expl.spec]p6: |
| 6592 | // 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] | 6593 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6594 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6595 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6596 | // use occurs; no diagnostic is required. |
| 6597 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6598 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6599 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6600 | // Is there any previous explicit specialization declaration? |
| 6601 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6602 | Okay = true; |
| 6603 | break; |
| 6604 | } |
| 6605 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6606 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6607 | if (!Okay) { |
| 6608 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6609 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6610 | << Context.getTypeDeclType(Specialization) << Range; |
| 6611 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6612 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6613 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6614 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6615 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6616 | return true; |
| 6617 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6618 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6619 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6620 | // If this is not a friend, note that this is an explicit specialization. |
| 6621 | if (TUK != TUK_Friend) |
| 6622 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6623 | |
| 6624 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6625 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6626 | RecordDecl *Def = Specialization->getDefinition(); |
| 6627 | NamedDecl *Hidden = nullptr; |
| 6628 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6629 | SkipBody->ShouldSkip = true; |
| 6630 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6631 | // From here on out, treat this as just a redeclaration. |
| 6632 | TUK = TUK_Declaration; |
| 6633 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6634 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6635 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6636 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6637 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6638 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6639 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6640 | } |
| 6641 | } |
| 6642 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6643 | if (Attr) |
| 6644 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6645 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6646 | // Add alignment attributes if necessary; these attributes are checked when |
| 6647 | // the ASTContext lays out the structure. |
| 6648 | if (TUK == TUK_Definition) { |
| 6649 | AddAlignmentAttributesForRecord(Specialization); |
| 6650 | AddMsStructLayoutForRecord(Specialization); |
| 6651 | } |
| 6652 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6653 | if (ModulePrivateLoc.isValid()) |
| 6654 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6655 | << (isPartialSpecialization? 1 : 0) |
| 6656 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6657 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6658 | // Build the fully-sugared type for this class template |
| 6659 | // specialization as the user wrote in the specialization |
| 6660 | // itself. This means that we'll pretty-print the type retrieved |
| 6661 | // from the specialization's declaration the way that the user |
| 6662 | // actually wrote the specialization, rather than formatting the |
| 6663 | // name based on the "canonical" representation used to store the |
| 6664 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6665 | TypeSourceInfo *WrittenTy |
| 6666 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6667 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6668 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6669 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6670 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6671 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6672 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6673 | // C++ [temp.expl.spec]p9: |
| 6674 | // A template explicit specialization is in the scope of the |
| 6675 | // namespace in which the template was defined. |
| 6676 | // |
| 6677 | // We actually implement this paragraph where we set the semantic |
| 6678 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6679 | // but we also maintain the lexical context where the actual |
| 6680 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6681 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6682 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6683 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6684 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6685 | Specialization->startDefinition(); |
| 6686 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6687 | if (TUK == TUK_Friend) { |
| 6688 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6689 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6690 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6691 | /*FIXME:*/KWLoc); |
| 6692 | Friend->setAccess(AS_public); |
| 6693 | CurContext->addDecl(Friend); |
| 6694 | } else { |
| 6695 | // Add the specialization into its lexical context, so that it can |
| 6696 | // be seen when iterating through the list of declarations in that |
| 6697 | // context. However, specializations are not found by name lookup. |
| 6698 | CurContext->addDecl(Specialization); |
| 6699 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6700 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6701 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6702 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6703 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6704 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6705 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6706 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6707 | ActOnDocumentableDecl(NewDecl); |
| 6708 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6709 | } |
| 6710 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6711 | /// \brief Strips various properties off an implicit instantiation |
| 6712 | /// that has just been explicitly specialized. |
| 6713 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6714 | D->dropAttr<DLLImportAttr>(); |
| 6715 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6716 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6717 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6718 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6719 | } |
| 6720 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6721 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6722 | // declaration or definition. |
| 6723 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6724 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6725 | // Explicit instantiations following a specialization have no effect and |
| 6726 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6727 | // until a valid name loc is found. |
| 6728 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6729 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6730 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6731 | PrevDiagLoc = Prev->getLocation(); |
| 6732 | } |
| 6733 | assert(PrevDiagLoc.isValid() && |
| 6734 | "Explicit instantiation without point of instantiation?"); |
| 6735 | return PrevDiagLoc; |
| 6736 | } |
| 6737 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6738 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6739 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6740 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6741 | /// new specialization/instantiation will have any effect. |
| 6742 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6743 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6744 | /// instantiation. |
| 6745 | /// |
| 6746 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6747 | /// |
| 6748 | /// \param PrevDecl the previous declaration of the entity. |
| 6749 | /// |
| 6750 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6751 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6752 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6753 | /// declaration was instantiated (either implicitly or explicitly). |
| 6754 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6755 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6756 | /// specialization or instantiation has no effect and should be ignored. |
| 6757 | /// |
| 6758 | /// \returns true if there was an error that should prevent the introduction of |
| 6759 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6760 | bool |
| 6761 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6762 | TemplateSpecializationKind NewTSK, |
| 6763 | NamedDecl *PrevDecl, |
| 6764 | TemplateSpecializationKind PrevTSK, |
| 6765 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6766 | bool &HasNoEffect) { |
| 6767 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6768 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6769 | switch (NewTSK) { |
| 6770 | case TSK_Undeclared: |
| 6771 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6772 | assert( |
| 6773 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6774 | "previous declaration must be implicit!"); |
| 6775 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6776 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6777 | case TSK_ExplicitSpecialization: |
| 6778 | switch (PrevTSK) { |
| 6779 | case TSK_Undeclared: |
| 6780 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6781 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6782 | // explicitly specialized or has merely been mentioned without any |
| 6783 | // instantiation. |
| 6784 | return false; |
| 6785 | |
| 6786 | case TSK_ImplicitInstantiation: |
| 6787 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6788 | // The declaration itself has not actually been instantiated, so it is |
| 6789 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6790 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6791 | return false; |
| 6792 | } |
| 6793 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6794 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6795 | case TSK_ExplicitInstantiationDeclaration: |
| 6796 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6797 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6798 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6799 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6800 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6801 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6802 | // 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] | 6803 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6804 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6805 | // implicit instantiation to take place, in every translation unit in |
| 6806 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6807 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6808 | // Is there any previous explicit specialization declaration? |
| 6809 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6810 | return false; |
| 6811 | } |
| 6812 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6813 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6814 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6815 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6816 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6817 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6818 | return true; |
| 6819 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6820 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6821 | case TSK_ExplicitInstantiationDeclaration: |
| 6822 | switch (PrevTSK) { |
| 6823 | case TSK_ExplicitInstantiationDeclaration: |
| 6824 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6825 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6826 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6827 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6828 | case TSK_Undeclared: |
| 6829 | case TSK_ImplicitInstantiation: |
| 6830 | // We're explicitly instantiating something that may have already been |
| 6831 | // implicitly instantiated; that's fine. |
| 6832 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6833 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6834 | case TSK_ExplicitSpecialization: |
| 6835 | // C++0x [temp.explicit]p4: |
| 6836 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6837 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6838 | // specialization for that template, the explicit instantiation has no |
| 6839 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6840 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6841 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6842 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6843 | case TSK_ExplicitInstantiationDefinition: |
| 6844 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6845 | // If an entity is the subject of both an explicit instantiation |
| 6846 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6847 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6848 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6849 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6850 | |
| 6851 | // Explicit instantiations following a specialization have no effect and |
| 6852 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6853 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6854 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6855 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6856 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6857 | return false; |
| 6858 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6859 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6860 | case TSK_ExplicitInstantiationDefinition: |
| 6861 | switch (PrevTSK) { |
| 6862 | case TSK_Undeclared: |
| 6863 | case TSK_ImplicitInstantiation: |
| 6864 | // We're explicitly instantiating something that may have already been |
| 6865 | // implicitly instantiated; that's fine. |
| 6866 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6867 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6868 | case TSK_ExplicitSpecialization: |
| 6869 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6870 | // For a given set of template parameters, if an explicit |
| 6871 | // instantiation of a template appears after a declaration of |
| 6872 | // an explicit specialization for that template, the explicit |
| 6873 | // instantiation has no effect. |
Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 6874 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6875 | << PrevDecl; |
| 6876 | Diag(PrevDecl->getLocation(), |
| 6877 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6878 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6879 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6880 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6881 | case TSK_ExplicitInstantiationDeclaration: |
| 6882 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6883 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6884 | |
| 6885 | // C++0x [temp.explicit]p4: |
| 6886 | // For a given set of template parameters, if an explicit instantiation |
| 6887 | // of a template appears after a declaration of an explicit |
| 6888 | // specialization for that template, the explicit instantiation has no |
| 6889 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6890 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6891 | // Is there any previous explicit specialization declaration? |
| 6892 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6893 | HasNoEffect = true; |
| 6894 | break; |
| 6895 | } |
| 6896 | } |
| 6897 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6898 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6899 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6900 | case TSK_ExplicitInstantiationDefinition: |
| 6901 | // C++0x [temp.spec]p5: |
| 6902 | // For a given template and a given set of template-arguments, |
| 6903 | // - an explicit instantiation definition shall appear at most once |
| 6904 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6905 | |
| 6906 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6907 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6908 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6909 | : diag::err_explicit_instantiation_duplicate) |
| 6910 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6911 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6912 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6913 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6914 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6915 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6916 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6917 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6918 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6919 | } |
| 6920 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6921 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6922 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6923 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6924 | /// The only possible way to get a dependent function template specialization |
| 6925 | /// is with a friend declaration, like so: |
| 6926 | /// |
| 6927 | /// \code |
| 6928 | /// template \<class T> void foo(T); |
| 6929 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6930 | /// friend void foo<>(T); |
| 6931 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6932 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6933 | /// |
| 6934 | /// There really isn't any useful analysis we can do here, so we |
| 6935 | /// just store the information. |
| 6936 | bool |
| 6937 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6938 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6939 | LookupResult &Previous) { |
| 6940 | // Remove anything from Previous that isn't a function template in |
| 6941 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6942 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6943 | LookupResult::Filter F = Previous.makeFilter(); |
| 6944 | while (F.hasNext()) { |
| 6945 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6946 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6947 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6948 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6949 | F.erase(); |
| 6950 | } |
| 6951 | F.done(); |
| 6952 | |
| 6953 | // Should this be diagnosed here? |
| 6954 | if (Previous.empty()) return true; |
| 6955 | |
| 6956 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6957 | ExplicitTemplateArgs); |
| 6958 | return false; |
| 6959 | } |
| 6960 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6961 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6962 | /// specialization. |
| 6963 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6964 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6965 | /// explicit function template specialization. On successful completion, |
| 6966 | /// the function declaration \p FD will become a function template |
| 6967 | /// specialization. |
| 6968 | /// |
| 6969 | /// \param FD the function declaration, which will be updated to become a |
| 6970 | /// function template specialization. |
| 6971 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6972 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6973 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6974 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6975 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6976 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6977 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6978 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6979 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6980 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6981 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6982 | // The set of function template specializations that could match this |
| 6983 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6984 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6985 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 6986 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6987 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6988 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 6989 | ConvertedTemplateArgs; |
| 6990 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6991 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6992 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6993 | I != E; ++I) { |
| 6994 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6995 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6996 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6997 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6998 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6999 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7000 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7001 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7002 | // When matching a constexpr member function template specialization |
| 7003 | // against the primary template, we don't yet know whether the |
| 7004 | // specialization has an implicit 'const' (because we don't know whether |
| 7005 | // it will be a static member function until we know which template it |
| 7006 | // specializes), so adjust it now assuming it specializes this template. |
| 7007 | QualType FT = FD->getType(); |
| 7008 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7009 | CXXMethodDecl *OldMD = |
| 7010 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7011 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 7012 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7013 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7014 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7015 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7016 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 7017 | } |
| 7018 | } |
| 7019 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7020 | TemplateArgumentListInfo Args; |
| 7021 | if (ExplicitTemplateArgs) |
| 7022 | Args = *ExplicitTemplateArgs; |
| 7023 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7024 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7025 | // A trailing template-argument can be left unspecified in the |
| 7026 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7027 | // provided it can be deduced from the function argument type. |
| 7028 | // Perform template argument deduction to determine whether we may be |
| 7029 | // specializing this template. |
| 7030 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7031 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7032 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 7033 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 7034 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7035 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 7036 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7037 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7038 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7039 | FailedCandidates.addCandidate().set( |
| 7040 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 7041 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7042 | (void)TDK; |
| 7043 | continue; |
| 7044 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7045 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7046 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7047 | if (ExplicitTemplateArgs) |
| 7048 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7049 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7050 | } |
| 7051 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7052 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 7053 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7054 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7055 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7056 | FD->getLocation(), |
| 7057 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 7058 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7059 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7060 | PDiag(diag::note_function_template_spec_matched)); |
| 7061 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7062 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7063 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7064 | |
| 7065 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 7066 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7067 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7068 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare [...] |
| 7069 | // an explicit specialization (14.8.3) [...] of a concept definition. |
| 7070 | if (Specialization->getPrimaryTemplate()->isConcept()) { |
| 7071 | Diag(FD->getLocation(), diag::err_concept_specialized) |
| 7072 | << 0 /*function*/ << 1 /*explicitly specialized*/; |
| 7073 | Diag(Specialization->getLocation(), diag::note_previous_declaration); |
| 7074 | return true; |
| 7075 | } |
| 7076 | |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7077 | FunctionTemplateSpecializationInfo *SpecInfo |
| 7078 | = Specialization->getTemplateSpecializationInfo(); |
| 7079 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7080 | |
| 7081 | // Note: do not overwrite location info if previous template |
| 7082 | // specialization kind was explicit. |
| 7083 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7084 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7085 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7086 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 7087 | // function can differ from the template declaration with respect to |
| 7088 | // the constexpr specifier. |
| 7089 | Specialization->setConstexpr(FD->isConstexpr()); |
| 7090 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7091 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7092 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7093 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7094 | |
| 7095 | // If this is a friend declaration, then we're not really declaring |
| 7096 | // an explicit specialization. |
| 7097 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7098 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7099 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7100 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7101 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7102 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7103 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7104 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7105 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7106 | |
| 7107 | // C++ [temp.expl.spec]p6: |
| 7108 | // 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] | 7109 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7110 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7111 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7112 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7113 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7114 | if (!isFriend && |
| 7115 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7116 | TSK_ExplicitSpecialization, |
| 7117 | Specialization, |
| 7118 | SpecInfo->getTemplateSpecializationKind(), |
| 7119 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7120 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7121 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7122 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7123 | // Mark the prior declaration as an explicit specialization, so that later |
| 7124 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7125 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 7126 | // Since explicit specializations do not inherit '=delete' from their |
| 7127 | // primary function template - check if the 'specialization' that was |
| 7128 | // implicitly generated (during template argument deduction for partial |
| 7129 | // ordering) from the most specialized of all the function templates that |
| 7130 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 7131 | // first check that it was implicitly generated during template argument |
| 7132 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 7133 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 7134 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 7135 | !Specialization->getCanonicalDecl()->isReferenced()) { |
| 7136 | assert( |
| 7137 | Specialization->getCanonicalDecl() == Specialization && |
| 7138 | "This must be the only existing declaration of this specialization"); |
| 7139 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7140 | } |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7141 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7142 | MarkUnusedFileScopedDecl(Specialization); |
| 7143 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7144 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7145 | // Turn the given function declaration into a function template |
| 7146 | // specialization, with the template arguments from the previous |
| 7147 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7148 | // Take copies of (semantic and syntactic) template argument lists. |
| 7149 | const TemplateArgumentList* TemplArgs = new (Context) |
| 7150 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7151 | FD->setFunctionTemplateSpecialization( |
| 7152 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 7153 | SpecInfo->getTemplateSpecializationKind(), |
| 7154 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7155 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7156 | // The "previous declaration" for this function template specialization is |
| 7157 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7158 | Previous.clear(); |
| 7159 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7160 | return false; |
| 7161 | } |
| 7162 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7163 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7164 | /// specialization. |
| 7165 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7166 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7167 | /// explicit member function specialization. On successful completion, |
| 7168 | /// the function declaration \p FD will become a member function |
| 7169 | /// specialization. |
| 7170 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7171 | /// \param Member the member declaration, which will be updated to become a |
| 7172 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7173 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7174 | /// \param Previous the set of declarations, one of which may be specialized |
| 7175 | /// by this function specialization; the set will be modified to contain the |
| 7176 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7177 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7178 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7179 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7180 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7181 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7182 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7183 | NamedDecl *Instantiation = nullptr; |
| 7184 | NamedDecl *InstantiatedFrom = nullptr; |
| 7185 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7186 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7187 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7188 | // Nowhere to look anyway. |
| 7189 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7190 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7191 | I != E; ++I) { |
| 7192 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7193 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7194 | QualType Adjusted = Function->getType(); |
| 7195 | if (!hasExplicitCallingConv(Adjusted)) |
| 7196 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7197 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7198 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7199 | Instantiation = Method; |
| 7200 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7201 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7202 | break; |
| 7203 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7204 | } |
| 7205 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7206 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7207 | VarDecl *PrevVar; |
| 7208 | if (Previous.isSingleResult() && |
| 7209 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7210 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7211 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7212 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7213 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7214 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7215 | } |
| 7216 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7217 | CXXRecordDecl *PrevRecord; |
| 7218 | if (Previous.isSingleResult() && |
| 7219 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7220 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7221 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7222 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7223 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7224 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7225 | } else if (isa<EnumDecl>(Member)) { |
| 7226 | EnumDecl *PrevEnum; |
| 7227 | if (Previous.isSingleResult() && |
| 7228 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7229 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7230 | Instantiation = PrevEnum; |
| 7231 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7232 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7233 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7234 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7235 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7236 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7237 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7238 | // specializations are always out-of-line, the caller will complain about |
| 7239 | // this mismatch later. |
| 7240 | return false; |
| 7241 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7242 | |
| 7243 | // If this is a friend, just bail out here before we start turning |
| 7244 | // things into explicit specializations. |
| 7245 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7246 | // Preserve instantiation information. |
| 7247 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7248 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7249 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7250 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7251 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7252 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7253 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7254 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7255 | } |
| 7256 | |
| 7257 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7258 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7259 | return false; |
| 7260 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7261 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7262 | // Make sure that this is a specialization of a member. |
| 7263 | if (!InstantiatedFrom) { |
| 7264 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 7265 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7266 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 7267 | return true; |
| 7268 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7269 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7270 | // C++ [temp.expl.spec]p6: |
| 7271 | // 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] | 7272 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7273 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7274 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7275 | // use occurs; no diagnostic is required. |
| 7276 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7277 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7278 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7279 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7280 | TSK_ExplicitSpecialization, |
| 7281 | Instantiation, |
| 7282 | MSInfo->getTemplateSpecializationKind(), |
| 7283 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7284 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7285 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7286 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7287 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7288 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7289 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7290 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7291 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7292 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7293 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7294 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7295 | // the original declaration to note that it is an explicit specialization |
| 7296 | // (if it was previously an implicit instantiation). This latter step |
| 7297 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7298 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7299 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7300 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7301 | TSK_ImplicitInstantiation) { |
| 7302 | InstantiationFunction->setTemplateSpecializationKind( |
| 7303 | TSK_ExplicitSpecialization); |
| 7304 | InstantiationFunction->setLocation(Member->getLocation()); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7305 | // Explicit specializations of member functions of class templates do not |
| 7306 | // inherit '=delete' from the member function they are specializing. |
| 7307 | if (InstantiationFunction->isDeleted()) { |
| 7308 | assert(InstantiationFunction->getCanonicalDecl() == |
| 7309 | InstantiationFunction); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 7310 | InstantiationFunction->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7311 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7312 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7313 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7314 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7315 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7316 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7317 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7318 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7319 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7320 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7321 | TSK_ImplicitInstantiation) { |
| 7322 | InstantiationVar->setTemplateSpecializationKind( |
| 7323 | TSK_ExplicitSpecialization); |
| 7324 | InstantiationVar->setLocation(Member->getLocation()); |
| 7325 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7326 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7327 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7328 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7329 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7330 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7331 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7332 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7333 | TSK_ImplicitInstantiation) { |
| 7334 | InstantiationClass->setTemplateSpecializationKind( |
| 7335 | TSK_ExplicitSpecialization); |
| 7336 | InstantiationClass->setLocation(Member->getLocation()); |
| 7337 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7338 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7339 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7340 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7341 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7342 | } else { |
| 7343 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7344 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7345 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7346 | TSK_ImplicitInstantiation) { |
| 7347 | InstantiationEnum->setTemplateSpecializationKind( |
| 7348 | TSK_ExplicitSpecialization); |
| 7349 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7350 | } |
| 7351 | |
| 7352 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7353 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7354 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7355 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7356 | // Save the caller the trouble of having to figure out which declaration |
| 7357 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7358 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7359 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7360 | return false; |
| 7361 | } |
| 7362 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7363 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7364 | /// |
| 7365 | /// \returns true if a serious error occurs, false otherwise. |
| 7366 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7367 | SourceLocation InstLoc, |
| 7368 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7369 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7370 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7371 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7372 | if (CurContext->isRecord()) { |
| 7373 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7374 | << D; |
| 7375 | return true; |
| 7376 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7377 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7378 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7379 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7380 | // template. If the name declared in the explicit instantiation is an |
| 7381 | // unqualified name, the explicit instantiation shall appear in the |
| 7382 | // namespace where its template is declared or, if that namespace is inline |
| 7383 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7384 | // |
| 7385 | // 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] | 7386 | if (WasQualifiedName) { |
| 7387 | if (CurContext->Encloses(OrigContext)) |
| 7388 | return false; |
| 7389 | } else { |
| 7390 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7391 | return false; |
| 7392 | } |
| 7393 | |
| 7394 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7395 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7396 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7397 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7398 | diag::err_explicit_instantiation_out_of_scope : |
| 7399 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7400 | << D << NS; |
| 7401 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7402 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7403 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7404 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7405 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7406 | << D << NS; |
| 7407 | } else |
| 7408 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7409 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7410 | diag::err_explicit_instantiation_must_be_global : |
| 7411 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7412 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7413 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7414 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7415 | } |
| 7416 | |
| 7417 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7418 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7419 | if (!SS.isSet()) |
| 7420 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7421 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7422 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7423 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7424 | // or a static data member of a class template specialization, the name of |
| 7425 | // the class template specialization in the qualified-id for the member |
| 7426 | // name shall be a simple-template-id. |
| 7427 | // |
| 7428 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7429 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7430 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7431 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7432 | if (isa<TemplateSpecializationType>(T)) |
| 7433 | return true; |
| 7434 | |
| 7435 | return false; |
| 7436 | } |
| 7437 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7438 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7439 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7440 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7441 | SourceLocation ExternLoc, |
| 7442 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7443 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7444 | SourceLocation KWLoc, |
| 7445 | const CXXScopeSpec &SS, |
| 7446 | TemplateTy TemplateD, |
| 7447 | SourceLocation TemplateNameLoc, |
| 7448 | SourceLocation LAngleLoc, |
| 7449 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7450 | SourceLocation RAngleLoc, |
| 7451 | AttributeList *Attr) { |
| 7452 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7453 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7454 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7455 | // Check that the specialization uses the same tag kind as the |
| 7456 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7457 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7458 | assert(Kind != TTK_Enum && |
| 7459 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7460 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7461 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 7462 | |
| 7463 | if (!ClassTemplate) { |
Reid Kleckner | f33bfcb0 | 2016-10-03 18:34:23 +0000 | [diff] [blame] | 7464 | NonTagKind NTK = getNonTagTypeDeclKind(TD); |
| 7465 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << NTK; |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7466 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7467 | return true; |
| 7468 | } |
| 7469 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7470 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7471 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7472 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7473 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7474 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7475 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7476 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7477 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7478 | diag::note_previous_use); |
| 7479 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7480 | } |
| 7481 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7482 | // C++0x [temp.explicit]p2: |
| 7483 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7484 | // definition and an explicit instantiation declaration. An explicit |
| 7485 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7486 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7487 | ? TSK_ExplicitInstantiationDefinition |
| 7488 | : TSK_ExplicitInstantiationDeclaration; |
| 7489 | |
| 7490 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7491 | // Check for dllexport class template instantiation declarations. |
| 7492 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7493 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7494 | Diag(ExternLoc, |
| 7495 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7496 | Diag(A->getLoc(), diag::note_attribute); |
| 7497 | break; |
| 7498 | } |
| 7499 | } |
| 7500 | |
| 7501 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7502 | Diag(ExternLoc, |
| 7503 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7504 | Diag(A->getLocation(), diag::note_attribute); |
| 7505 | } |
| 7506 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7507 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7508 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 7509 | // instantiation declarations for most purposes. |
| 7510 | bool DLLImportExplicitInstantiationDef = false; |
| 7511 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 7512 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7513 | // Check for dllimport class template instantiation definitions. |
| 7514 | bool DLLImport = |
| 7515 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 7516 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7517 | if (A->getKind() == AttributeList::AT_DLLImport) |
| 7518 | DLLImport = true; |
| 7519 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7520 | // dllexport trumps dllimport here. |
| 7521 | DLLImport = false; |
| 7522 | break; |
| 7523 | } |
| 7524 | } |
| 7525 | if (DLLImport) { |
| 7526 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 7527 | DLLImportExplicitInstantiationDef = true; |
| 7528 | } |
| 7529 | } |
| 7530 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7531 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7532 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7533 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7534 | |
| 7535 | // Check that the template argument list is well-formed for this |
| 7536 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7537 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7538 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7539 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7540 | return true; |
| 7541 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7542 | // Find the class template specialization declaration that |
| 7543 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7544 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7545 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7546 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7547 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7548 | TemplateSpecializationKind PrevDecl_TSK |
| 7549 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7550 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7551 | // C++0x [temp.explicit]p2: |
| 7552 | // [...] An explicit instantiation shall appear in an enclosing |
| 7553 | // namespace of its template. [...] |
| 7554 | // |
| 7555 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7556 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7557 | SS.isSet())) |
| 7558 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7559 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7560 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7561 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7562 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7563 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7564 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7565 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7566 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7567 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7568 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7569 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7570 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7571 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7572 | |
| 7573 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7574 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7575 | // Since the only prior class template specialization with these |
| 7576 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7577 | // declaration node as our own, updating the source location |
| 7578 | // for the template name to reflect our new declaration. |
| 7579 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7580 | Specialization = PrevDecl; |
| 7581 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7582 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7583 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7584 | |
| 7585 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7586 | DLLImportExplicitInstantiationDef) { |
| 7587 | // The new specialization might add a dllimport attribute. |
| 7588 | HasNoEffect = false; |
| 7589 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7590 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7591 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7592 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7593 | // Create a new class template specialization declaration node for |
| 7594 | // this explicit specialization. |
| 7595 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7596 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7597 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7598 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7599 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7600 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7601 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7602 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7603 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7604 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7605 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7606 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7607 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7608 | } |
| 7609 | |
| 7610 | // Build the fully-sugared type for this explicit instantiation as |
| 7611 | // the user wrote in the explicit instantiation itself. This means |
| 7612 | // that we'll pretty-print the type retrieved from the |
| 7613 | // specialization's declaration the way that the user actually wrote |
| 7614 | // the explicit instantiation, rather than formatting the name based |
| 7615 | // on the "canonical" representation used to store the template |
| 7616 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7617 | TypeSourceInfo *WrittenTy |
| 7618 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7619 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7620 | Context.getTypeDeclType(Specialization)); |
| 7621 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7622 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7623 | // Set source locations for keywords. |
| 7624 | Specialization->setExternLoc(ExternLoc); |
| 7625 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 7626 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7627 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7628 | if (Attr) |
| 7629 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7630 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7631 | // Add the explicit instantiation into its lexical context. However, |
| 7632 | // since explicit instantiations are never found by name lookup, we |
| 7633 | // just put it into the declaration context directly. |
| 7634 | Specialization->setLexicalDeclContext(CurContext); |
| 7635 | CurContext->addDecl(Specialization); |
| 7636 | |
| 7637 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7638 | if (HasNoEffect) { |
| 7639 | // Set the template specialization kind. |
| 7640 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7641 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7642 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7643 | |
| 7644 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7645 | // A definition of a class template or class member template |
| 7646 | // shall be in scope at the point of the explicit instantiation of |
| 7647 | // the class template or class member template. |
| 7648 | // |
| 7649 | // This check comes when we actually try to perform the |
| 7650 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7651 | ClassTemplateSpecializationDecl *Def |
| 7652 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7653 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7654 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7655 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7656 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7657 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7658 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7659 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7660 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7661 | // Instantiate the members of this class template specialization. |
| 7662 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7663 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7664 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7665 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7666 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7667 | // TSK_ExplicitInstantiationDefinition |
| 7668 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7669 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 7670 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7671 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7672 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7673 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7674 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
Shoaib Meenai | ab3f96c | 2016-11-09 23:52:20 +0000 | [diff] [blame] | 7675 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 7676 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7677 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7678 | // attribute to a template with a previous instantiation declaration. |
| 7679 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7680 | auto *A = cast<InheritableAttr>( |
| 7681 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 7682 | A->setInherited(true); |
| 7683 | Def->addAttr(A); |
Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 7684 | |
| 7685 | // We reject explicit instantiations in class scope, so there should |
| 7686 | // never be any delayed exported classes to worry about. |
| 7687 | assert(DelayedDllExportClasses.empty() && |
| 7688 | "delayed exports present at explicit instantiation"); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7689 | checkClassLevelDLLAttribute(Def); |
Hans Wennborg | fce87ca | 2015-06-09 00:39:09 +0000 | [diff] [blame] | 7690 | |
| 7691 | // Propagate attribute to base class templates. |
| 7692 | for (auto &B : Def->bases()) { |
| 7693 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 7694 | B.getType()->getAsCXXRecordDecl())) |
| 7695 | propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 7696 | } |
Saleem Abdulrasool | 25dbdfa | 2016-12-03 01:57:47 +0000 | [diff] [blame] | 7697 | |
| 7698 | referenceDLLExportedClassMethods(); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7699 | } |
| 7700 | } |
| 7701 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7702 | // Set the template specialization kind. Make sure it is set before |
| 7703 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 7704 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7705 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7706 | } else { |
| 7707 | |
| 7708 | // Set the template specialization kind. |
| 7709 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7710 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7711 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7712 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7713 | } |
| 7714 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7715 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7716 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7717 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7718 | SourceLocation ExternLoc, |
| 7719 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7720 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7721 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7722 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7723 | IdentifierInfo *Name, |
| 7724 | SourceLocation NameLoc, |
| 7725 | AttributeList *Attr) { |
| 7726 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7727 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7728 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7729 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7730 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7731 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7732 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7733 | SourceLocation(), false, TypeResult(), |
| 7734 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7735 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7736 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7737 | if (!TagD) |
| 7738 | return true; |
| 7739 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7740 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7741 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7742 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7743 | if (Tag->isInvalidDecl()) |
| 7744 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7745 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7746 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7747 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7748 | if (!Pattern) { |
| 7749 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7750 | << Context.getTypeDeclType(Record); |
| 7751 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7752 | return true; |
| 7753 | } |
| 7754 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7755 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7756 | // If the explicit instantiation is for a class or member class, the |
| 7757 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7758 | // simple-template-id. |
| 7759 | // |
| 7760 | // C++98 has the same restriction, just worded differently. |
| 7761 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7762 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7763 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7764 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7765 | // C++0x [temp.explicit]p2: |
| 7766 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7767 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7768 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7769 | TemplateSpecializationKind TSK |
| 7770 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7771 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7772 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7773 | // C++0x [temp.explicit]p2: |
| 7774 | // [...] An explicit instantiation shall appear in an enclosing |
| 7775 | // namespace of its template. [...] |
| 7776 | // |
| 7777 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7778 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7779 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7780 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7781 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7782 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7783 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7784 | PrevDecl = Record; |
| 7785 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7786 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7787 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7788 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7789 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7790 | PrevDecl, |
| 7791 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7792 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7793 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7794 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7795 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7796 | return TagD; |
| 7797 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7798 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7799 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7800 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7801 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7802 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7803 | // 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] | 7804 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7805 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7806 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7807 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7808 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7809 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7810 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7811 | << Pattern; |
| 7812 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7813 | } else { |
| 7814 | if (InstantiateClass(NameLoc, Record, Def, |
| 7815 | getTemplateInstantiationArgs(Record), |
| 7816 | TSK)) |
| 7817 | return true; |
| 7818 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7819 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7820 | if (!RecordDef) |
| 7821 | return true; |
| 7822 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7823 | } |
| 7824 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7825 | // Instantiate all of the members of the class. |
| 7826 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7827 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7828 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7829 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7830 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7831 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7832 | // FIXME: We don't have any representation for explicit instantiations of |
| 7833 | // member classes. Such a representation is not needed for compilation, but it |
| 7834 | // should be available for clients that want to see all of the declarations in |
| 7835 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7836 | return TagD; |
| 7837 | } |
| 7838 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7839 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7840 | SourceLocation ExternLoc, |
| 7841 | SourceLocation TemplateLoc, |
| 7842 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7843 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7844 | // TODO: check if/when DNInfo should replace Name. |
| 7845 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7846 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7847 | if (!Name) { |
| 7848 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7849 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7850 | diag::err_explicit_instantiation_requires_name) |
| 7851 | << D.getDeclSpec().getSourceRange() |
| 7852 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7853 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7854 | return true; |
| 7855 | } |
| 7856 | |
| 7857 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7858 | // we find one that is. |
| 7859 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7860 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7861 | S = S->getParent(); |
| 7862 | |
| 7863 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7864 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7865 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7866 | if (R.isNull()) |
| 7867 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7868 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7869 | // C++ [dcl.stc]p1: |
| 7870 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7871 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7872 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7873 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7874 | << Name; |
| 7875 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7876 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7877 | != DeclSpec::SCS_unspecified) { |
| 7878 | // Complain about then remove the storage class specifier. |
| 7879 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7880 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7881 | |
| 7882 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7883 | } |
| 7884 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7885 | // C++0x [temp.explicit]p1: |
| 7886 | // [...] An explicit instantiation of a function template shall not use the |
| 7887 | // inline or constexpr specifiers. |
| 7888 | // Presumably, this also applies to member functions of class templates as |
| 7889 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7890 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7891 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7892 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7893 | diag::err_explicit_instantiation_inline : |
| 7894 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7895 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7896 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7897 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7898 | // not already specified. |
| 7899 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7900 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7901 | |
Nathan Wilson | de49845 | 2016-02-08 05:34:00 +0000 | [diff] [blame] | 7902 | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
| 7903 | // applied only to the definition of a function template or variable template, |
| 7904 | // declared in namespace scope. |
| 7905 | if (D.getDeclSpec().isConceptSpecified()) { |
| 7906 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
| 7907 | diag::err_concept_specified_specialization) << 0; |
| 7908 | return true; |
| 7909 | } |
| 7910 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7911 | // C++0x [temp.explicit]p2: |
| 7912 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7913 | // definition and an explicit instantiation declaration. An explicit |
| 7914 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7915 | TemplateSpecializationKind TSK |
| 7916 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7917 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7918 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7919 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7920 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7921 | |
| 7922 | if (!R->isFunctionType()) { |
| 7923 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7924 | // A [...] static data member of a class template can be explicitly |
| 7925 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7926 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7927 | // C++1y [temp.explicit]p1: |
| 7928 | // A [...] variable [...] template specialization can be explicitly |
| 7929 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7930 | if (Previous.isAmbiguous()) |
| 7931 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7932 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7933 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7934 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7935 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7936 | if (!PrevTemplate) { |
| 7937 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7938 | // We expect to see a data data member here. |
| 7939 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7940 | << Name; |
| 7941 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7942 | P != PEnd; ++P) |
| 7943 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7944 | return true; |
| 7945 | } |
| 7946 | |
| 7947 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7948 | // FIXME: Check for explicit specialization? |
| 7949 | Diag(D.getIdentifierLoc(), |
| 7950 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7951 | << Prev; |
| 7952 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7953 | // FIXME: Can we provide a note showing where this was declared? |
| 7954 | return true; |
| 7955 | } |
| 7956 | } else { |
| 7957 | // Explicitly instantiate a variable template. |
| 7958 | |
| 7959 | // C++1y [dcl.spec.auto]p6: |
| 7960 | // ... A program that uses auto or decltype(auto) in a context not |
| 7961 | // explicitly allowed in this section is ill-formed. |
| 7962 | // |
| 7963 | // This includes auto-typed variable template instantiations. |
| 7964 | if (R->isUndeducedType()) { |
| 7965 | Diag(T->getTypeLoc().getLocStart(), |
| 7966 | diag::err_auto_not_allowed_var_inst); |
| 7967 | return true; |
| 7968 | } |
| 7969 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7970 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7971 | // C++1y [temp.explicit]p3: |
| 7972 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7973 | // in the declaration shall be a template-id. |
| 7974 | Diag(D.getIdentifierLoc(), |
| 7975 | diag::err_explicit_instantiation_without_template_id) |
| 7976 | << PrevTemplate; |
| 7977 | Diag(PrevTemplate->getLocation(), |
| 7978 | diag::note_explicit_instantiation_here); |
| 7979 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7980 | } |
| 7981 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7982 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 7983 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 7984 | if (PrevTemplate->isConcept()) { |
| 7985 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 7986 | << 1 /*variable*/ << 0 /*explicitly instantiated*/; |
| 7987 | Diag(PrevTemplate->getLocation(), diag::note_previous_declaration); |
| 7988 | return true; |
| 7989 | } |
| 7990 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7991 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7992 | TemplateArgumentListInfo TemplateArgs = |
| 7993 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7994 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7995 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7996 | D.getIdentifierLoc(), TemplateArgs); |
| 7997 | if (Res.isInvalid()) |
| 7998 | return true; |
| 7999 | |
| 8000 | // Ignore access control bits, we don't need them for redeclaration |
| 8001 | // checking. |
| 8002 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8003 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8004 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8005 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8006 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8007 | // or a static data member of a class template specialization, the name of |
| 8008 | // the class template specialization in the qualified-id for the member |
| 8009 | // name shall be a simple-template-id. |
| 8010 | // |
| 8011 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8012 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 8013 | // This does not apply to variable template specializations, where the |
| 8014 | // template-id is in the unqualified-id instead. |
| 8015 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8016 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8017 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8018 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8019 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8020 | // Check the scope of this explicit instantiation. |
| 8021 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8022 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8023 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8024 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 8025 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8026 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8027 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8028 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 8029 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8030 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8031 | if (!HasNoEffect) { |
| 8032 | // Instantiate static data member or variable template. |
| 8033 | |
| 8034 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 8035 | if (PrevTemplate) { |
| 8036 | // Merge attributes. |
| 8037 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 8038 | ProcessDeclAttributeList(S, Prev, Attr); |
| 8039 | } |
| 8040 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 8041 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 8042 | } |
| 8043 | |
| 8044 | // Check the new variable specialization against the parsed input. |
| 8045 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 8046 | Diag(T->getTypeLoc().getLocStart(), |
| 8047 | diag::err_invalid_var_template_spec_type) |
| 8048 | << 0 << PrevTemplate << R << Prev->getType(); |
| 8049 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 8050 | << 2 << PrevTemplate->getDeclName(); |
| 8051 | return true; |
| 8052 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8053 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8054 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8055 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8056 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8057 | |
| 8058 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 8059 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8060 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8061 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8062 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8063 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8064 | HasExplicitTemplateArgs = true; |
| 8065 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8066 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8067 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8068 | // A [...] function [...] can be explicitly instantiated from its template. |
| 8069 | // A member function [...] of a class template can be explicitly |
| 8070 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8071 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8072 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8073 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8074 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8075 | P != PEnd; ++P) { |
| 8076 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8077 | if (!HasExplicitTemplateArgs) { |
| 8078 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 8079 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 8080 | /*AdjustExceptionSpec*/true); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 8081 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8082 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8083 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8084 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8085 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 8086 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8087 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8088 | } |
| 8089 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8090 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8091 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 8092 | if (!FunTmpl) |
| 8093 | continue; |
| 8094 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8095 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8096 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8097 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8098 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8099 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 8100 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8101 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8102 | // Keep track of almost-matches. |
| 8103 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8104 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8105 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8106 | (void)TDK; |
| 8107 | continue; |
| 8108 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8109 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8110 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8111 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8112 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8113 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8114 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 8115 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8116 | D.getIdentifierLoc(), |
| 8117 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 8118 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 8119 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8120 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8121 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8122 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8123 | |
| 8124 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 8125 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8126 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 8127 | // C++11 [except.spec]p4 |
| 8128 | // In an explicit instantiation an exception-specification may be specified, |
| 8129 | // but is not required. |
| 8130 | // If an exception-specification is specified in an explicit instantiation |
| 8131 | // directive, it shall be compatible with the exception-specifications of |
| 8132 | // other declarations of that function. |
| 8133 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 8134 | if (FPT->hasExceptionSpec()) { |
| 8135 | unsigned DiagID = |
| 8136 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 8137 | if (getLangOpts().MicrosoftExt) |
| 8138 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 8139 | bool Result = CheckEquivalentExceptionSpec( |
| 8140 | PDiag(DiagID) << Specialization->getType(), |
| 8141 | PDiag(diag::note_explicit_instantiation_here), |
| 8142 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 8143 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 8144 | // In Microsoft mode, mismatching exception specifications just cause a |
| 8145 | // warning. |
| 8146 | if (!getLangOpts().MicrosoftExt && Result) |
| 8147 | return true; |
| 8148 | } |
| 8149 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8150 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8151 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8152 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 8153 | << Specialization |
| 8154 | << (Specialization->getTemplateSpecializationKind() == |
| 8155 | TSK_ExplicitSpecialization); |
| 8156 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 8157 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8158 | } |
| 8159 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8160 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8161 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 8162 | PrevDecl = Specialization; |
| 8163 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8164 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8165 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8166 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8167 | PrevDecl, |
| 8168 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8169 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8170 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8171 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8172 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8173 | // FIXME: We may still want to build some representation of this |
| 8174 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8175 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8176 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8177 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 8178 | |
| 8179 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 8180 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 8181 | if (Attr) |
| 8182 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8183 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8184 | if (Specialization->isDefined()) { |
| 8185 | // Let the ASTConsumer know that this function has been explicitly |
| 8186 | // instantiated now, and its linkage might have changed. |
| 8187 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 8188 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 8189 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8190 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8191 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8192 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8193 | // or a static data member of a class template specialization, the name of |
| 8194 | // the class template specialization in the qualified-id for the member |
| 8195 | // name shall be a simple-template-id. |
| 8196 | // |
| 8197 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8198 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8199 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8200 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8201 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8202 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8203 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8204 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8205 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8206 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8207 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8208 | if (FunTmpl && FunTmpl->isConcept() && |
| 8209 | !D.getDeclSpec().isConceptSpecified()) { |
| 8210 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8211 | << 0 /*function*/ << 0 /*explicitly instantiated*/; |
| 8212 | Diag(FunTmpl->getLocation(), diag::note_previous_declaration); |
| 8213 | return true; |
| 8214 | } |
| 8215 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8216 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8217 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8218 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8219 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8220 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8221 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8222 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8223 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8224 | } |
| 8225 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8226 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8227 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 8228 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 8229 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 8230 | // This has to hold, because SS is expected to be defined. |
| 8231 | assert(Name && "Expected a name in a dependent tag"); |
| 8232 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8233 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8234 | if (!NNS) |
| 8235 | return true; |
| 8236 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8237 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 8238 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8239 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 8240 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8241 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8242 | return true; |
| 8243 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8244 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8245 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8246 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8247 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 8248 | |
| 8249 | // Create type-source location information for this type. |
| 8250 | TypeLocBuilder TLB; |
| 8251 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8252 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8253 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8254 | TL.setNameLoc(NameLoc); |
| 8255 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8256 | } |
| 8257 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8258 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8259 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 8260 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 8261 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8262 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8263 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8264 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8265 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8266 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8267 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8268 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8269 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8270 | << FixItHint::CreateRemoval(TypenameLoc); |
| 8271 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8272 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8273 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 8274 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 8275 | if (T.isNull()) |
| 8276 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8277 | |
| 8278 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8279 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8280 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8281 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8282 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8283 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8284 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8285 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8286 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8287 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8288 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8289 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8290 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8291 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8292 | } |
| 8293 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8294 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8295 | Sema::ActOnTypenameType(Scope *S, |
| 8296 | SourceLocation TypenameLoc, |
| 8297 | const CXXScopeSpec &SS, |
| 8298 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8299 | TemplateTy TemplateIn, |
| 8300 | SourceLocation TemplateNameLoc, |
| 8301 | SourceLocation LAngleLoc, |
| 8302 | ASTTemplateArgsPtr TemplateArgsIn, |
| 8303 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8304 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8305 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8306 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8307 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8308 | diag::ext_typename_outside_of_template) |
| 8309 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8310 | |
| 8311 | // Translate the parser's template argument list in our AST format. |
| 8312 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8313 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 8314 | |
| 8315 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8316 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 8317 | // Construct a dependent template specialization type. |
| 8318 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8319 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8320 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 8321 | DTN->getQualifier(), |
| 8322 | DTN->getIdentifier(), |
| 8323 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8324 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8325 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8326 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8327 | DependentTemplateSpecializationTypeLoc SpecTL |
| 8328 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8329 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 8330 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 8331 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8332 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8333 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8334 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8335 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8336 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8337 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 8338 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8339 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8340 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 8341 | if (T.isNull()) |
| 8342 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8343 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8344 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8345 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8346 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8347 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8348 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 8349 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8350 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8351 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8352 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8353 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 8354 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8355 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 8356 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8357 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8358 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8359 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8360 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 8361 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 8362 | } |
| 8363 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8364 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8365 | /// Determine whether this failed name lookup should be treated as being |
| 8366 | /// disabled by a usage of std::enable_if. |
| 8367 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8368 | SourceRange &CondRange) { |
| 8369 | // We must be looking for a ::type... |
| 8370 | if (!II.isStr("type")) |
| 8371 | return false; |
| 8372 | |
| 8373 | // ... within an explicitly-written template specialization... |
| 8374 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8375 | return false; |
| 8376 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8377 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8378 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8379 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8380 | return false; |
| 8381 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8382 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8383 | |
| 8384 | // ... which names a complete class template declaration... |
| 8385 | const TemplateDecl *EnableIfDecl = |
| 8386 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8387 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8388 | return false; |
| 8389 | |
| 8390 | // ... called "enable_if". |
| 8391 | const IdentifierInfo *EnableIfII = |
| 8392 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8393 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8394 | return false; |
| 8395 | |
| 8396 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8397 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8398 | return true; |
| 8399 | } |
| 8400 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8401 | /// \brief Build the type that describes a C++ typename specifier, |
| 8402 | /// e.g., "typename T::type". |
| 8403 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8404 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 8405 | SourceLocation KeywordLoc, |
| 8406 | NestedNameSpecifierLoc QualifierLoc, |
| 8407 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8408 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8409 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8410 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8411 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8412 | DeclContext *Ctx = computeDeclContext(SS); |
| 8413 | if (!Ctx) { |
| 8414 | // If the nested-name-specifier is dependent and couldn't be |
| 8415 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8416 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 8417 | return Context.getDependentNameType(Keyword, |
| 8418 | QualifierLoc.getNestedNameSpecifier(), |
| 8419 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8420 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8421 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8422 | // If the nested-name-specifier refers to the current instantiation, |
| 8423 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8424 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8425 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8426 | // 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] | 8427 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8428 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8429 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8430 | |
| 8431 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8432 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8433 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8434 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8435 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8436 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8437 | case LookupResult::NotFound: { |
| 8438 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8439 | // a more specific diagnostic. |
| 8440 | SourceRange CondRange; |
| 8441 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8442 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8443 | << Ctx << CondRange; |
| 8444 | return QualType(); |
| 8445 | } |
| 8446 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8447 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8448 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8449 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8450 | |
| 8451 | case LookupResult::FoundUnresolvedValue: { |
| 8452 | // We found a using declaration that is a value. Most likely, the using |
| 8453 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8454 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8455 | IILoc); |
| 8456 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8457 | << Name << Ctx << FullRange; |
| 8458 | if (UnresolvedUsingValueDecl *Using |
| 8459 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8460 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8461 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8462 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8463 | } |
| 8464 | } |
| 8465 | // Fall through to create a dependent typename type, from which we can recover |
| 8466 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8467 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8468 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8469 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8470 | return Context.getDependentNameType(Keyword, |
| 8471 | QualifierLoc.getNestedNameSpecifier(), |
| 8472 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8473 | |
| 8474 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8475 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8476 | // We found a type. Build an ElaboratedType, since the |
| 8477 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8478 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8479 | return Context.getElaboratedType(ETK_Typename, |
| 8480 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8481 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8482 | } |
| 8483 | |
| 8484 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8485 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8486 | break; |
| 8487 | |
| 8488 | case LookupResult::FoundOverloaded: |
| 8489 | DiagID = diag::err_typename_nested_not_type; |
| 8490 | Referenced = *Result.begin(); |
| 8491 | break; |
| 8492 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8493 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8494 | return QualType(); |
| 8495 | } |
| 8496 | |
| 8497 | // If we get here, it's because name lookup did not find a |
| 8498 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8499 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8500 | IILoc); |
| 8501 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8502 | if (Referenced) |
| 8503 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8504 | << Name; |
| 8505 | return QualType(); |
| 8506 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8507 | |
| 8508 | namespace { |
| 8509 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8510 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8511 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8512 | SourceLocation Loc; |
| 8513 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8514 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8515 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8516 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8517 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8518 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8519 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8520 | DeclarationName Entity) |
| 8521 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8522 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8523 | |
| 8524 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8525 | /// transformed. |
| 8526 | /// |
| 8527 | /// For the purposes of type reconstruction, a type has already been |
| 8528 | /// transformed if it is NULL or if it is not dependent. |
| 8529 | bool AlreadyTransformed(QualType T) { |
| 8530 | return T.isNull() || !T->isDependentType(); |
| 8531 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8532 | |
| 8533 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8534 | /// rebuilt. |
| 8535 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8536 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8537 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8538 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8539 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8540 | /// \brief Sets the "base" location and entity when that |
| 8541 | /// information is known based on another transformation. |
| 8542 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8543 | this->Loc = Loc; |
| 8544 | this->Entity = Entity; |
| 8545 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8546 | |
| 8547 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8548 | // Lambdas never need to be transformed. |
| 8549 | return E; |
| 8550 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8551 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 8552 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8553 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8554 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8555 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8556 | /// 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] | 8557 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8558 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8559 | /// partial specialization thereof). This routine will rebuild that type now |
| 8560 | /// that we have entered the declarator's scope, which may produce different |
| 8561 | /// canonical types, e.g., |
| 8562 | /// |
| 8563 | /// \code |
| 8564 | /// template<typename T> |
| 8565 | /// struct X { |
| 8566 | /// typedef T* pointer; |
| 8567 | /// pointer data(); |
| 8568 | /// }; |
| 8569 | /// |
| 8570 | /// template<typename T> |
| 8571 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8572 | /// \endcode |
| 8573 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8574 | /// 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] | 8575 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8576 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8577 | /// 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] | 8578 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8579 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8580 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8581 | SourceLocation Loc, |
| 8582 | DeclarationName Name) { |
| 8583 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8584 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8585 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8586 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8587 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8588 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8589 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8590 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8591 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8592 | DeclarationName()); |
| 8593 | return Rebuilder.TransformExpr(E); |
| 8594 | } |
| 8595 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8596 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8597 | if (SS.isInvalid()) |
| 8598 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8599 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8600 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8601 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8602 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8603 | NestedNameSpecifierLoc Rebuilt |
| 8604 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8605 | if (!Rebuilt) |
| 8606 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8607 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8608 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8609 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8610 | } |
| 8611 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8612 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8613 | /// instantiation. |
| 8614 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8615 | TemplateParameterList *Params) { |
| 8616 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8617 | Decl *Param = Params->getParam(I); |
| 8618 | |
| 8619 | // There is nothing to rebuild in a type parameter. |
| 8620 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8621 | continue; |
| 8622 | |
| 8623 | // Rebuild the template parameter list of a template template parameter. |
| 8624 | if (TemplateTemplateParmDecl *TTP |
| 8625 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8626 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8627 | TTP->getTemplateParameters())) |
| 8628 | return true; |
| 8629 | |
| 8630 | continue; |
| 8631 | } |
| 8632 | |
| 8633 | // Rebuild the type of a non-type template parameter. |
| 8634 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8635 | TypeSourceInfo *NewTSI |
| 8636 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8637 | NTTP->getLocation(), |
| 8638 | NTTP->getDeclName()); |
| 8639 | if (!NewTSI) |
| 8640 | return true; |
| 8641 | |
| 8642 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8643 | NTTP->setTypeSourceInfo(NewTSI); |
| 8644 | NTTP->setType(NewTSI->getType()); |
| 8645 | } |
| 8646 | } |
| 8647 | |
| 8648 | return false; |
| 8649 | } |
| 8650 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8651 | /// \brief Produces a formatted string that describes the binding of |
| 8652 | /// template parameters to template arguments. |
| 8653 | std::string |
| 8654 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8655 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8656 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8657 | } |
| 8658 | |
| 8659 | std::string |
| 8660 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8661 | const TemplateArgument *Args, |
| 8662 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8663 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8664 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8665 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8666 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8667 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8668 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8669 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8670 | if (I >= NumArgs) |
| 8671 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8672 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8673 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8674 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8675 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8676 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8677 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8678 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8679 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8680 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8681 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8682 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8683 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8684 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8685 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8686 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8687 | |
| 8688 | Out << ']'; |
| 8689 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8690 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8691 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8692 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8693 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8694 | if (!FD) |
| 8695 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8696 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 8697 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8698 | |
| 8699 | // Take tokens to avoid allocations |
| 8700 | LPT->Toks.swap(Toks); |
| 8701 | LPT->D = FnD; |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 8702 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8703 | |
| 8704 | FD->setLateTemplateParsed(true); |
| 8705 | } |
| 8706 | |
| 8707 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8708 | if (!FD) |
| 8709 | return; |
| 8710 | FD->setLateTemplateParsed(false); |
| 8711 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8712 | |
| 8713 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8714 | DeclContext *DC = CurContext; |
| 8715 | |
| 8716 | while (DC) { |
| 8717 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8718 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8719 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8720 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8721 | return false; |
| 8722 | |
| 8723 | DC = DC->getParent(); |
| 8724 | } |
| 8725 | return false; |
| 8726 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8727 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 8728 | namespace { |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8729 | /// \brief Walk the path from which a declaration was instantiated, and check |
| 8730 | /// that every explicit specialization along that path is visible. This enforces |
| 8731 | /// C++ [temp.expl.spec]/6: |
| 8732 | /// |
| 8733 | /// If a template, a member template or a member of a class template is |
| 8734 | /// explicitly specialized then that specialization shall be declared before |
| 8735 | /// the first use of that specialization that would cause an implicit |
| 8736 | /// instantiation to take place, in every translation unit in which such a |
| 8737 | /// use occurs; no diagnostic is required. |
| 8738 | /// |
| 8739 | /// and also C++ [temp.class.spec]/1: |
| 8740 | /// |
| 8741 | /// A partial specialization shall be declared before the first use of a |
| 8742 | /// class template specialization that would make use of the partial |
| 8743 | /// specialization as the result of an implicit or explicit instantiation |
| 8744 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 8745 | /// required. |
| 8746 | class ExplicitSpecializationVisibilityChecker { |
| 8747 | Sema &S; |
| 8748 | SourceLocation Loc; |
| 8749 | llvm::SmallVector<Module *, 8> Modules; |
| 8750 | |
| 8751 | public: |
| 8752 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 8753 | : S(S), Loc(Loc) {} |
| 8754 | |
| 8755 | void check(NamedDecl *ND) { |
| 8756 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 8757 | return checkImpl(FD); |
| 8758 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 8759 | return checkImpl(RD); |
| 8760 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 8761 | return checkImpl(VD); |
| 8762 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 8763 | return checkImpl(ED); |
| 8764 | } |
| 8765 | |
| 8766 | private: |
| 8767 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 8768 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 8769 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 8770 | const bool Recover = true; |
| 8771 | |
| 8772 | // If we got a custom set of modules (because only a subset of the |
| 8773 | // declarations are interesting), use them, otherwise let |
| 8774 | // diagnoseMissingImport intelligently pick some. |
| 8775 | if (Modules.empty()) |
| 8776 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 8777 | else |
| 8778 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 8779 | } |
| 8780 | |
| 8781 | // Check a specific declaration. There are three problematic cases: |
| 8782 | // |
| 8783 | // 1) The declaration is an explicit specialization of a template |
| 8784 | // specialization. |
| 8785 | // 2) The declaration is an explicit specialization of a member of an |
| 8786 | // templated class. |
| 8787 | // 3) The declaration is an instantiation of a template, and that template |
| 8788 | // is an explicit specialization of a member of a templated class. |
| 8789 | // |
| 8790 | // We don't need to go any deeper than that, as the instantiation of the |
| 8791 | // surrounding class / etc is not triggered by whatever triggered this |
| 8792 | // instantiation, and thus should be checked elsewhere. |
| 8793 | template<typename SpecDecl> |
| 8794 | void checkImpl(SpecDecl *Spec) { |
| 8795 | bool IsHiddenExplicitSpecialization = false; |
| 8796 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 8797 | IsHiddenExplicitSpecialization = |
| 8798 | Spec->getMemberSpecializationInfo() |
| 8799 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
| 8800 | : !S.hasVisibleDeclaration(Spec); |
| 8801 | } else { |
| 8802 | checkInstantiated(Spec); |
| 8803 | } |
| 8804 | |
| 8805 | if (IsHiddenExplicitSpecialization) |
| 8806 | diagnose(Spec->getMostRecentDecl(), false); |
| 8807 | } |
| 8808 | |
| 8809 | void checkInstantiated(FunctionDecl *FD) { |
| 8810 | if (auto *TD = FD->getPrimaryTemplate()) |
| 8811 | checkTemplate(TD); |
| 8812 | } |
| 8813 | |
| 8814 | void checkInstantiated(CXXRecordDecl *RD) { |
| 8815 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 8816 | if (!SD) |
| 8817 | return; |
| 8818 | |
| 8819 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 8820 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 8821 | checkTemplate(TD); |
| 8822 | else if (auto *TD = |
| 8823 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 8824 | if (!S.hasVisibleDeclaration(TD)) |
| 8825 | diagnose(TD, true); |
| 8826 | checkTemplate(TD); |
| 8827 | } |
| 8828 | } |
| 8829 | |
| 8830 | void checkInstantiated(VarDecl *RD) { |
| 8831 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 8832 | if (!SD) |
| 8833 | return; |
| 8834 | |
| 8835 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 8836 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 8837 | checkTemplate(TD); |
| 8838 | else if (auto *TD = |
| 8839 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 8840 | if (!S.hasVisibleDeclaration(TD)) |
| 8841 | diagnose(TD, true); |
| 8842 | checkTemplate(TD); |
| 8843 | } |
| 8844 | } |
| 8845 | |
| 8846 | void checkInstantiated(EnumDecl *FD) {} |
| 8847 | |
| 8848 | template<typename TemplDecl> |
| 8849 | void checkTemplate(TemplDecl *TD) { |
| 8850 | if (TD->isMemberSpecialization()) { |
| 8851 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 8852 | diagnose(TD->getMostRecentDecl(), false); |
| 8853 | } |
| 8854 | } |
| 8855 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 8856 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8857 | |
| 8858 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 8859 | if (!getLangOpts().Modules) |
| 8860 | return; |
| 8861 | |
| 8862 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 8863 | } |
| 8864 | |
| 8865 | /// \brief Check whether a template partial specialization that we've discovered |
| 8866 | /// is hidden, and produce suitable diagnostics if so. |
| 8867 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 8868 | NamedDecl *Spec) { |
| 8869 | llvm::SmallVector<Module *, 8> Modules; |
| 8870 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 8871 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 8872 | MissingImportKind::PartialSpecialization, |
| 8873 | /*Recover*/true); |
| 8874 | } |