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 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 459 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 460 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 461 | /// declaration at location Loc. Returns true to indicate that this is |
| 462 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 463 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 464 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 465 | |
| 466 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 467 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 468 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 469 | |
| 470 | // C++ [temp.local]p4: |
| 471 | // A template-parameter shall not be redeclared within its |
| 472 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 474 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 475 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 478 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 479 | /// the parameter D to reference the templated declaration and return a pointer |
| 480 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 481 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 482 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 483 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 484 | return Temp; |
| 485 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 486 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 489 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 490 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 491 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 492 | "Only template template arguments can be pack expansions here"); |
| 493 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 494 | "Template template argument pack expansion without packs"); |
| 495 | ParsedTemplateArgument Result(*this); |
| 496 | Result.EllipsisLoc = EllipsisLoc; |
| 497 | return Result; |
| 498 | } |
| 499 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 500 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 501 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 502 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 503 | switch (Arg.getKind()) { |
| 504 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 505 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 506 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 507 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 508 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 509 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 510 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 511 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 512 | case ParsedTemplateArgument::NonType: { |
| 513 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 514 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 515 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 516 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 517 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 518 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 519 | TemplateArgument TArg; |
| 520 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 521 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 522 | else |
| 523 | TArg = Template; |
| 524 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 525 | Arg.getScopeSpec().getWithLocInContext( |
| 526 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 527 | Arg.getLocation(), |
| 528 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 531 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 532 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 533 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 534 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 535 | /// \brief Translates template arguments as provided by the parser |
| 536 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 537 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 538 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 539 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 540 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 541 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 542 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 543 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 544 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 545 | SourceLocation Loc, |
| 546 | IdentifierInfo *Name) { |
| 547 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 548 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 549 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 550 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 551 | } |
| 552 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 553 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 554 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 555 | /// the keyword "typename" was used to declare the type parameter |
| 556 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 557 | /// "class" or "typename" keyword. ParamName is the name of the |
| 558 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 559 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 560 | /// If the type parameter has a default argument, it will be added |
| 561 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 562 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 563 | SourceLocation EllipsisLoc, |
| 564 | SourceLocation KeyLoc, |
| 565 | IdentifierInfo *ParamName, |
| 566 | SourceLocation ParamNameLoc, |
| 567 | unsigned Depth, unsigned Position, |
| 568 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 569 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | assert(S->isTemplateParamScope() && |
| 571 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 572 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 573 | SourceLocation Loc = ParamNameLoc; |
| 574 | if (!ParamName) |
| 575 | Loc = KeyLoc; |
| 576 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 577 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 578 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 579 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 580 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 581 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 582 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 583 | |
| 584 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 585 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 586 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 587 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 588 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 589 | IdResolver.AddDecl(Param); |
| 590 | } |
| 591 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 592 | // C++0x [temp.param]p9: |
| 593 | // A default template-argument may be specified for any kind of |
| 594 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 595 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 596 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 597 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 600 | // Handle the default argument, if provided. |
| 601 | if (DefaultArg) { |
| 602 | TypeSourceInfo *DefaultTInfo; |
| 603 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 605 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 607 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 608 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 609 | UPPC_DefaultArgument)) |
| 610 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 612 | // Check the template argument itself. |
| 613 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 614 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 615 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 616 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 617 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 618 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 619 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 620 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 621 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 624 | /// \brief Check that the type of a non-type template parameter is |
| 625 | /// well-formed. |
| 626 | /// |
| 627 | /// \returns the (possibly-promoted) parameter type if valid; |
| 628 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | QualType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 630 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 631 | // We don't allow variably-modified types as the type of non-type template |
| 632 | // parameters. |
| 633 | if (T->isVariablyModifiedType()) { |
| 634 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 635 | << T; |
| 636 | return QualType(); |
| 637 | } |
| 638 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 639 | // C++ [temp.param]p4: |
| 640 | // |
| 641 | // A non-type template-parameter shall have one of the following |
| 642 | // (optionally cv-qualified) types: |
| 643 | // |
| 644 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 645 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 647 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 649 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 650 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 651 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 652 | // -- std::nullptr_t. |
| 653 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 654 | // If T is a dependent type, we can't do the check now, so we |
| 655 | // assume that it is well-formed. |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 656 | T->isDependentType()) { |
| 657 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 658 | // are ignored when determining its type. |
| 659 | return T.getUnqualifiedType(); |
| 660 | } |
| 661 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 662 | // C++ [temp.param]p8: |
| 663 | // |
| 664 | // A non-type template-parameter of type "array of T" or |
| 665 | // "function returning T" is adjusted to be of type "pointer to |
| 666 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 667 | else if (T->isArrayType() || T->isFunctionType()) |
| 668 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 669 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 670 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 671 | << T; |
| 672 | |
| 673 | return QualType(); |
| 674 | } |
| 675 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 676 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 677 | unsigned Depth, |
| 678 | unsigned Position, |
| 679 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 680 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 681 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 682 | QualType T = TInfo->getType(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 684 | assert(S->isTemplateParamScope() && |
| 685 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 686 | bool Invalid = false; |
| 687 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 688 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 689 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 690 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 691 | Invalid = true; |
| 692 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 693 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 694 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 695 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 696 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 697 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 698 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 699 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 700 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 701 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 702 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 703 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 704 | if (Invalid) |
| 705 | Param->setInvalidDecl(); |
| 706 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 707 | if (ParamName) { |
| 708 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 709 | ParamName); |
| 710 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 711 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 712 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 713 | IdResolver.AddDecl(Param); |
| 714 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 715 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 716 | // C++0x [temp.param]p9: |
| 717 | // A default template-argument may be specified for any kind of |
| 718 | // template-parameter that is not a template parameter pack. |
| 719 | if (Default && IsParameterPack) { |
| 720 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 721 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 724 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 725 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 726 | // Check for unexpanded parameter packs. |
| 727 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 728 | return Param; |
| 729 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 730 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 731 | ExprResult DefaultRes = |
| 732 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 733 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 734 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 735 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 736 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 737 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 738 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 739 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 740 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 741 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 742 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 743 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 745 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 746 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 747 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 748 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 749 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 750 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 751 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 752 | IdentifierInfo *Name, |
| 753 | SourceLocation NameLoc, |
| 754 | unsigned Depth, |
| 755 | unsigned Position, |
| 756 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 757 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 758 | assert(S->isTemplateParamScope() && |
| 759 | "Template template parameter not in template parameter scope!"); |
| 760 | |
| 761 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 762 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 763 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 764 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 765 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 766 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 767 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 768 | Param->setAccess(AS_public); |
| 769 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 770 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 771 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 772 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 773 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 774 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 775 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 776 | IdResolver.AddDecl(Param); |
| 777 | } |
| 778 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 779 | if (Params->size() == 0) { |
| 780 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 781 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 782 | Param->setInvalidDecl(); |
| 783 | } |
| 784 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 785 | // C++0x [temp.param]p9: |
| 786 | // A default template-argument may be specified for any kind of |
| 787 | // template-parameter that is not a template parameter pack. |
| 788 | if (IsParameterPack && !Default.isInvalid()) { |
| 789 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 790 | Default = ParsedTemplateArgument(); |
| 791 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 792 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 793 | if (!Default.isInvalid()) { |
| 794 | // Check only that we have a template template argument. We don't want to |
| 795 | // try to check well-formedness now, because our template template parameter |
| 796 | // might have dependent types in its template parameters, which we wouldn't |
| 797 | // be able to match now. |
| 798 | // |
| 799 | // If none of the template template parameter's template arguments mention |
| 800 | // other template parameters, we could actually perform more checking here. |
| 801 | // However, it isn't worth doing. |
| 802 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 803 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 804 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 805 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 806 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 807 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 809 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 810 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 811 | DefaultArg.getArgument().getAsTemplate(), |
| 812 | UPPC_DefaultArgument)) |
| 813 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 814 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 815 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 816 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 817 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 818 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 821 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 822 | /// constrained by RequiresClause, that contains the template parameters in |
| 823 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 824 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 825 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 826 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 828 | SourceLocation LAngleLoc, |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 829 | ArrayRef<Decl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 830 | SourceLocation RAngleLoc, |
| 831 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 832 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 833 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 834 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 835 | // FIXME: store RequiresClause |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 836 | return TemplateParameterList::Create( |
| 837 | Context, TemplateLoc, LAngleLoc, |
| 838 | llvm::makeArrayRef((NamedDecl *const *)Params.data(), Params.size()), |
| 839 | RAngleLoc); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 840 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 841 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 842 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 843 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 844 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 845 | } |
| 846 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 847 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 848 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 849 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 850 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 851 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 852 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 853 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 854 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 855 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 856 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 857 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 859 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 860 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 861 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 862 | |
| 863 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 864 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 865 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 866 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 867 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 868 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 869 | |
| 870 | // There is no such thing as an unnamed class template. |
| 871 | if (!Name) { |
| 872 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 873 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 876 | // Find any previous declaration with this name. For a friend with no |
| 877 | // scope explicitly specified, we only look for tag declarations (per |
| 878 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 879 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 880 | LookupResult Previous(*this, Name, NameLoc, |
| 881 | (SS.isEmpty() && TUK == TUK_Friend) |
| 882 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 883 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 884 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 885 | SemanticContext = computeDeclContext(SS, true); |
| 886 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 887 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 888 | // in the AST, and historically we have just ignored such friend |
| 889 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 890 | Diag(NameLoc, TUK == TUK_Friend |
| 891 | ? diag::warn_template_qualified_friend_ignored |
| 892 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 893 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 894 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 895 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 897 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 898 | return true; |
| 899 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 900 | // If we're adding a template to a dependent context, we may need to |
| 901 | // rebuilding some of the types used within the template parameter list, |
| 902 | // now that we know what the current instantiation is. |
| 903 | if (SemanticContext->isDependentContext()) { |
| 904 | ContextRAII SavedContext(*this, SemanticContext); |
| 905 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 906 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 907 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 908 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 909 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 910 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 911 | } else { |
| 912 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 913 | |
| 914 | // C++14 [class.mem]p14: |
| 915 | // If T is the name of a class, then each of the following shall have a |
| 916 | // name different from T: |
| 917 | // -- every member template of class T |
| 918 | if (TUK != TUK_Friend && |
| 919 | DiagnoseClassNameShadow(SemanticContext, |
| 920 | DeclarationNameInfo(Name, NameLoc))) |
| 921 | return true; |
| 922 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 923 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 924 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 926 | if (Previous.isAmbiguous()) |
| 927 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 928 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 929 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 930 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 931 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 932 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 933 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 934 | // Maybe we will complain about the shadowed template parameter. |
| 935 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 936 | // Just pretend that we didn't see the previous declaration. |
| 937 | PrevDecl = nullptr; |
| 938 | } |
| 939 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 940 | // If there is a previous declaration with the same name, check |
| 941 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 943 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 944 | |
| 945 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 946 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 947 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 948 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 949 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 950 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 951 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 952 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 953 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 954 | PrevClassTemplate |
| 955 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 956 | ->getSpecializedTemplate(); |
| 957 | } |
| 958 | } |
| 959 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 960 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 961 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 962 | // [...] When looking for a prior declaration of a class or a function |
| 963 | // 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] | 964 | // function is neither a qualified name nor a template-id, scopes outside |
| 965 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 966 | if (!SS.isSet()) { |
| 967 | DeclContext *OutermostContext = CurContext; |
| 968 | while (!OutermostContext->isFileContext()) |
| 969 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 970 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 971 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 972 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 973 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 974 | SemanticContext = PrevDecl->getDeclContext(); |
| 975 | } else { |
| 976 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 977 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 978 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 979 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 980 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 981 | |
| 982 | // Check that the chosen semantic context doesn't already contain a |
| 983 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 984 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 985 | DeclContext *LookupContext = SemanticContext; |
| 986 | while (LookupContext->isTransparentContext()) |
| 987 | LookupContext = LookupContext->getLookupParent(); |
| 988 | LookupQualifiedName(Previous, LookupContext); |
| 989 | |
| 990 | if (Previous.isAmbiguous()) |
| 991 | return true; |
| 992 | |
| 993 | if (Previous.begin() != Previous.end()) |
| 994 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 995 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 996 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 997 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 998 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 999 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1000 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1001 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1002 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1003 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1004 | if (SS.isEmpty() && |
| 1005 | !(PrevClassTemplate && |
| 1006 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1007 | SemanticContext->getRedeclContext()))) { |
| 1008 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1009 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1010 | diag::note_using_decl_target); |
| 1011 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1012 | // Recover by ignoring the old declaration. |
| 1013 | PrevDecl = PrevClassTemplate = nullptr; |
| 1014 | } |
| 1015 | } |
| 1016 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1017 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1018 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1019 | // for a friend in a dependent context: the template parameter list itself |
| 1020 | // could be dependent. |
| 1021 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1022 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1023 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1024 | /*Complain=*/true, |
| 1025 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1026 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1027 | |
| 1028 | // C++ [temp.class]p4: |
| 1029 | // In a redeclaration, partial specialization, explicit |
| 1030 | // specialization or explicit instantiation of a class template, |
| 1031 | // the class-key shall agree in kind with the original class |
| 1032 | // template declaration (7.1.5.3). |
| 1033 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1034 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1035 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1037 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1038 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1039 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1040 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1043 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1044 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1045 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1046 | // If we have a prior definition that is not visible, treat this as |
| 1047 | // simply making that previous definition visible. |
| 1048 | NamedDecl *Hidden = nullptr; |
| 1049 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1050 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1051 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1052 | assert(Tmpl && "original definition of a class template is not a " |
| 1053 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1054 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1055 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1056 | return Def; |
| 1057 | } |
| 1058 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1059 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1060 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1061 | // FIXME: Would it make sense to try to "forget" the previous |
| 1062 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1063 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1064 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1065 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1066 | } else if (PrevDecl) { |
| 1067 | // C++ [temp]p5: |
| 1068 | // A class template shall not have the same name as any other |
| 1069 | // template, class, function, object, enumeration, enumerator, |
| 1070 | // namespace, or type in the same scope (3.3), except as specified |
| 1071 | // in (14.5.4). |
| 1072 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1073 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1074 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1077 | // Check the template parameter list of this declaration, possibly |
| 1078 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1079 | // template declaration. Skip this check for a friend in a dependent |
| 1080 | // context, because the template parameter list might be dependent. |
| 1081 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1082 | CheckTemplateParameterList( |
| 1083 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1084 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1085 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1086 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1087 | SemanticContext->isDependentContext()) |
| 1088 | ? TPC_ClassTemplateMember |
| 1089 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1090 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1091 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1093 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1094 | // 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] | 1095 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1096 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1097 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1098 | : diag::err_member_decl_does_not_match) |
| 1099 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1100 | Invalid = true; |
| 1101 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1105 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1107 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1108 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1109 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1110 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1111 | NewClass->setTemplateParameterListsInfo( |
| 1112 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1113 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1114 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1115 | // Add alignment attributes if necessary; these attributes are checked when |
| 1116 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1117 | if (TUK == TUK_Definition) { |
| 1118 | AddAlignmentAttributesForRecord(NewClass); |
| 1119 | AddMsStructLayoutForRecord(NewClass); |
| 1120 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1122 | ClassTemplateDecl *NewTemplate |
| 1123 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1124 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1125 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1126 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1127 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1128 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1129 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1130 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1131 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1132 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1133 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1134 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1135 | (void)T; |
| 1136 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1137 | // 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] | 1138 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1139 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1140 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1141 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1142 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1143 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1144 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1145 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1147 | // Set the lexical context of these templates |
| 1148 | NewClass->setLexicalDeclContext(CurContext); |
| 1149 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1150 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1151 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1152 | NewClass->startDefinition(); |
| 1153 | |
| 1154 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1155 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1156 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1157 | if (PrevClassTemplate) |
| 1158 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1159 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1160 | AddPushedVisibilityAttribute(NewClass); |
| 1161 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1162 | if (TUK != TUK_Friend) { |
| 1163 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1164 | Scope *Outer = S; |
| 1165 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1166 | Outer = Outer->getParent(); |
| 1167 | PushOnScopeChains(NewTemplate, Outer); |
| 1168 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1169 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1170 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1171 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1172 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1173 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1174 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1175 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1176 | // Friend templates are visible in fairly strange ways. |
| 1177 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1178 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1179 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1180 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1181 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1182 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1183 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1184 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1185 | FriendDecl *Friend = FriendDecl::Create( |
| 1186 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1187 | Friend->setAccess(AS_public); |
| 1188 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1189 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1191 | if (Invalid) { |
| 1192 | NewTemplate->setInvalidDecl(); |
| 1193 | NewClass->setInvalidDecl(); |
| 1194 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1195 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1196 | ActOnDocumentableDecl(NewTemplate); |
| 1197 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1198 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1201 | /// \brief Diagnose the presence of a default template argument on a |
| 1202 | /// template parameter, which is ill-formed in certain contexts. |
| 1203 | /// |
| 1204 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1205 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1206 | Sema::TemplateParamListContext TPC, |
| 1207 | SourceLocation ParamLoc, |
| 1208 | SourceRange DefArgRange) { |
| 1209 | switch (TPC) { |
| 1210 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1211 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1212 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1213 | return false; |
| 1214 | |
| 1215 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1216 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1217 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1218 | // A default template-argument shall not be specified in a |
| 1219 | // function template declaration or a function template |
| 1220 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1221 | // If a friend function template declaration specifies a default |
| 1222 | // template-argument, that declaration shall be a definition and shall be |
| 1223 | // the only declaration of the function template in the translation unit. |
| 1224 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1225 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1226 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1227 | : diag::ext_template_parameter_default_in_function_template) |
| 1228 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1229 | return false; |
| 1230 | |
| 1231 | case Sema::TPC_ClassTemplateMember: |
| 1232 | // C++0x [temp.param]p9: |
| 1233 | // A default template-argument shall not be specified in the |
| 1234 | // template-parameter-lists of the definition of a member of a |
| 1235 | // class template that appears outside of the member's class. |
| 1236 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1237 | << DefArgRange; |
| 1238 | return true; |
| 1239 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1240 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1241 | case Sema::TPC_FriendFunctionTemplate: |
| 1242 | // C++ [temp.param]p9: |
| 1243 | // A default template-argument shall not be specified in a |
| 1244 | // friend template declaration. |
| 1245 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1246 | << DefArgRange; |
| 1247 | return true; |
| 1248 | |
| 1249 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1250 | // for friend function templates if there is only a single |
| 1251 | // declaration (and it is a definition). Strange! |
| 1252 | } |
| 1253 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1254 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1257 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1258 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1259 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1260 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1261 | // A template template parameter which is a parameter pack is also a pack |
| 1262 | // expansion. |
| 1263 | if (TTP->isParameterPack()) |
| 1264 | return false; |
| 1265 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1266 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1267 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1268 | NamedDecl *P = Params->getParam(I); |
| 1269 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1270 | if (!NTTP->isParameterPack() && |
| 1271 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1272 | NTTP->getTypeSourceInfo(), |
| 1273 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1274 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1275 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1276 | continue; |
| 1277 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1278 | |
| 1279 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1280 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1281 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1282 | return true; |
| 1283 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1284 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1285 | return false; |
| 1286 | } |
| 1287 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1288 | /// \brief Checks the validity of a template parameter list, possibly |
| 1289 | /// considering the template parameter list from a previous |
| 1290 | /// declaration. |
| 1291 | /// |
| 1292 | /// If an "old" template parameter list is provided, it must be |
| 1293 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1294 | /// template parameter list. |
| 1295 | /// |
| 1296 | /// \param NewParams Template parameter list for a new template |
| 1297 | /// declaration. This template parameter list will be updated with any |
| 1298 | /// default arguments that are carried through from the previous |
| 1299 | /// template parameter list. |
| 1300 | /// |
| 1301 | /// \param OldParams If provided, template parameter list from a |
| 1302 | /// previous declaration of the same template. Default template |
| 1303 | /// arguments will be merged from the old template parameter list to |
| 1304 | /// the new template parameter list. |
| 1305 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1306 | /// \param TPC Describes the context in which we are checking the given |
| 1307 | /// template parameter list. |
| 1308 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1309 | /// \returns true if an error occurred, false otherwise. |
| 1310 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1311 | TemplateParameterList *OldParams, |
| 1312 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1313 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1315 | // C++ [temp.param]p10: |
| 1316 | // The set of default template-arguments available for use with a |
| 1317 | // template declaration or definition is obtained by merging the |
| 1318 | // default arguments from the definition (if in scope) and all |
| 1319 | // declarations in scope in the same way default function |
| 1320 | // arguments are (8.3.6). |
| 1321 | bool SawDefaultArgument = false; |
| 1322 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1323 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1324 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1325 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1326 | if (OldParams) |
| 1327 | OldParam = OldParams->begin(); |
| 1328 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1329 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1330 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1331 | NewParamEnd = NewParams->end(); |
| 1332 | NewParam != NewParamEnd; ++NewParam) { |
| 1333 | // Variables used to diagnose redundant default arguments |
| 1334 | bool RedundantDefaultArg = false; |
| 1335 | SourceLocation OldDefaultLoc; |
| 1336 | SourceLocation NewDefaultLoc; |
| 1337 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1338 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1339 | bool MissingDefaultArg = false; |
| 1340 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1341 | // Variable used to diagnose non-final parameter packs |
| 1342 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1344 | if (TemplateTypeParmDecl *NewTypeParm |
| 1345 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1346 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1347 | if (NewTypeParm->hasDefaultArgument() && |
| 1348 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1349 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1350 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1351 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1352 | NewTypeParm->removeDefaultArgument(); |
| 1353 | |
| 1354 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1355 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1356 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1357 | if (NewTypeParm->isParameterPack()) { |
| 1358 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1359 | "Parameter packs can't have a default argument!"); |
| 1360 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1361 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1362 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1363 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1364 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1365 | SawDefaultArgument = true; |
| 1366 | RedundantDefaultArg = true; |
| 1367 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1368 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1369 | // Merge the default argument from the old declaration to the |
| 1370 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1371 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1372 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1373 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1374 | SawDefaultArgument = true; |
| 1375 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1376 | } else if (SawDefaultArgument) |
| 1377 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1378 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1379 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1380 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1381 | if (!NewNonTypeParm->isParameterPack() && |
| 1382 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1383 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1384 | UPPC_NonTypeTemplateParameterType)) { |
| 1385 | Invalid = true; |
| 1386 | continue; |
| 1387 | } |
| 1388 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1389 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1390 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1391 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1392 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1393 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1394 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1397 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1398 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1399 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1400 | if (NewNonTypeParm->isParameterPack()) { |
| 1401 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1402 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1403 | if (!NewNonTypeParm->isPackExpansion()) |
| 1404 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1405 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1406 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1407 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1408 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1409 | SawDefaultArgument = true; |
| 1410 | RedundantDefaultArg = true; |
| 1411 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1412 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1413 | // Merge the default argument from the old declaration to the |
| 1414 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1415 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1416 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1417 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1418 | SawDefaultArgument = true; |
| 1419 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1420 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1422 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1423 | TemplateTemplateParmDecl *NewTemplateParm |
| 1424 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1425 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1426 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1427 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1428 | Invalid = true; |
| 1429 | continue; |
| 1430 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1431 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1432 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1433 | if (NewTemplateParm->hasDefaultArgument() && |
| 1434 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1435 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1436 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1437 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1438 | |
| 1439 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1440 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1441 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1442 | if (NewTemplateParm->isParameterPack()) { |
| 1443 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1444 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1445 | if (!NewTemplateParm->isPackExpansion()) |
| 1446 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1447 | } else if (OldTemplateParm && |
| 1448 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 1449 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1450 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1451 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1452 | SawDefaultArgument = true; |
| 1453 | RedundantDefaultArg = true; |
| 1454 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1455 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1456 | // Merge the default argument from the old declaration to the |
| 1457 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1458 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1459 | PreviousDefaultArgLoc |
| 1460 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1461 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1462 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1463 | PreviousDefaultArgLoc |
| 1464 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1465 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1469 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1470 | // If a template parameter of a primary class template or alias template |
| 1471 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1472 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1473 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1474 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1475 | Diag((*NewParam)->getLocation(), |
| 1476 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1477 | Invalid = true; |
| 1478 | } |
| 1479 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1480 | if (RedundantDefaultArg) { |
| 1481 | // C++ [temp.param]p12: |
| 1482 | // A template-parameter shall not be given default arguments |
| 1483 | // by two different declarations in the same scope. |
| 1484 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1485 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1486 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1487 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1488 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1489 | // If a template-parameter of a class template has a default |
| 1490 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1491 | // have a default template-argument supplied or be a template parameter |
| 1492 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1494 | diag::err_template_param_default_arg_missing); |
| 1495 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1496 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1497 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | // If we have an old template parameter list that we're merging |
| 1501 | // in, move on to the next parameter. |
| 1502 | if (OldParams) |
| 1503 | ++OldParam; |
| 1504 | } |
| 1505 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1506 | // We were missing some default arguments at the end of the list, so remove |
| 1507 | // all of the default arguments. |
| 1508 | if (RemoveDefaultArguments) { |
| 1509 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1510 | NewParamEnd = NewParams->end(); |
| 1511 | NewParam != NewParamEnd; ++NewParam) { |
| 1512 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1513 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1514 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1515 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1516 | NTTP->removeDefaultArgument(); |
| 1517 | else |
| 1518 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1519 | } |
| 1520 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1521 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1522 | return Invalid; |
| 1523 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1524 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1525 | namespace { |
| 1526 | |
| 1527 | /// A class which looks for a use of a certain level of template |
| 1528 | /// parameter. |
| 1529 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1530 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1531 | |
| 1532 | unsigned Depth; |
| 1533 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1534 | SourceLocation MatchLoc; |
| 1535 | |
| 1536 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1537 | |
| 1538 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1539 | NamedDecl *ND = Params->getParam(0); |
| 1540 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1541 | Depth = PD->getDepth(); |
| 1542 | } else if (NonTypeTemplateParmDecl *PD = |
| 1543 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1544 | Depth = PD->getDepth(); |
| 1545 | } else { |
| 1546 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1547 | } |
| 1548 | } |
| 1549 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1550 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1551 | if (ParmDepth >= Depth) { |
| 1552 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1553 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1554 | return true; |
| 1555 | } |
| 1556 | return false; |
| 1557 | } |
| 1558 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1559 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1560 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1561 | } |
| 1562 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1563 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1564 | return !Matches(T->getDepth()); |
| 1565 | } |
| 1566 | |
| 1567 | bool TraverseTemplateName(TemplateName N) { |
| 1568 | if (TemplateTemplateParmDecl *PD = |
| 1569 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1570 | if (Matches(PD->getDepth())) |
| 1571 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1572 | return super::TraverseTemplateName(N); |
| 1573 | } |
| 1574 | |
| 1575 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1576 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1577 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1578 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1579 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1580 | return super::VisitDeclRefExpr(E); |
| 1581 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1582 | |
| 1583 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1584 | return TraverseType(T->getReplacementType()); |
| 1585 | } |
| 1586 | |
| 1587 | bool |
| 1588 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1589 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1590 | } |
| 1591 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1592 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1593 | return TraverseType(T->getInjectedSpecializationType()); |
| 1594 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1595 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1596 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1597 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1598 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1599 | /// list. |
| 1600 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1601 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1602 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1603 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1604 | return Checker.Match; |
| 1605 | } |
| 1606 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1607 | // Find the source range corresponding to the named type in the given |
| 1608 | // nested-name-specifier, if any. |
| 1609 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1610 | QualType T, |
| 1611 | const CXXScopeSpec &SS) { |
| 1612 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1613 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1614 | if (const Type *CurType = NNS->getAsType()) { |
| 1615 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1616 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1617 | } else |
| 1618 | break; |
| 1619 | |
| 1620 | NNSLoc = NNSLoc.getPrefix(); |
| 1621 | } |
| 1622 | |
| 1623 | return SourceRange(); |
| 1624 | } |
| 1625 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1627 | /// specifier, returning the template parameter list that applies to the |
| 1628 | /// name. |
| 1629 | /// |
| 1630 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1631 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1633 | /// \param DeclLoc The location of the declaration itself. |
| 1634 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1635 | /// \param SS the scope specifier that will be matched to the given template |
| 1636 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1637 | /// being declared. |
| 1638 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1639 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1640 | /// is one. Used to check for a missing 'template<>'. |
| 1641 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1642 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1643 | /// innermost template parameter lists. |
| 1644 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1645 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1646 | /// matching template parameters to scope specifiers in friend |
| 1647 | /// declarations. |
| 1648 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1649 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1650 | /// declared is an explicit specialization, false otherwise. |
| 1651 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1653 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1654 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1656 | /// 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] | 1657 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1658 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1659 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1660 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1661 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1662 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1663 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1664 | Invalid = false; |
| 1665 | |
| 1666 | // The sequence of nested types to which we will match up the template |
| 1667 | // parameter lists. We first build this list by starting with the type named |
| 1668 | // 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] | 1669 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1670 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1671 | if (SS.getScopeRep()) { |
| 1672 | if (CXXRecordDecl *Record |
| 1673 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1674 | T = Context.getTypeDeclType(Record); |
| 1675 | else |
| 1676 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1677 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1678 | |
| 1679 | // If we found an explicit specialization that prevents us from needing |
| 1680 | // 'template<>' headers, this will be set to the location of that |
| 1681 | // explicit specialization. |
| 1682 | SourceLocation ExplicitSpecLoc; |
| 1683 | |
| 1684 | while (!T.isNull()) { |
| 1685 | NestedTypes.push_back(T); |
| 1686 | |
| 1687 | // Retrieve the parent of a record type. |
| 1688 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1689 | // If this type is an explicit specialization, we're done. |
| 1690 | if (ClassTemplateSpecializationDecl *Spec |
| 1691 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1692 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1693 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1694 | ExplicitSpecLoc = Spec->getLocation(); |
| 1695 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1696 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1697 | } else if (Record->getTemplateSpecializationKind() |
| 1698 | == TSK_ExplicitSpecialization) { |
| 1699 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1700 | break; |
| 1701 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1702 | |
| 1703 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1704 | T = Context.getTypeDeclType(Parent); |
| 1705 | else |
| 1706 | T = QualType(); |
| 1707 | continue; |
| 1708 | } |
| 1709 | |
| 1710 | if (const TemplateSpecializationType *TST |
| 1711 | = T->getAs<TemplateSpecializationType>()) { |
| 1712 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1713 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1714 | T = Context.getTypeDeclType(Parent); |
| 1715 | else |
| 1716 | T = QualType(); |
| 1717 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1718 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | // Look one step prior in a dependent template specialization type. |
| 1722 | if (const DependentTemplateSpecializationType *DependentTST |
| 1723 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1724 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1725 | T = QualType(NNS->getAsType(), 0); |
| 1726 | else |
| 1727 | T = QualType(); |
| 1728 | continue; |
| 1729 | } |
| 1730 | |
| 1731 | // Look one step prior in a dependent name type. |
| 1732 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1733 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1734 | T = QualType(NNS->getAsType(), 0); |
| 1735 | else |
| 1736 | T = QualType(); |
| 1737 | continue; |
| 1738 | } |
| 1739 | |
| 1740 | // Retrieve the parent of an enumeration type. |
| 1741 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1742 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1743 | // check here. |
| 1744 | EnumDecl *Enum = EnumT->getDecl(); |
| 1745 | |
| 1746 | // Get to the parent type. |
| 1747 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1748 | T = Context.getTypeDeclType(Parent); |
| 1749 | else |
| 1750 | T = QualType(); |
| 1751 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1752 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1754 | T = QualType(); |
| 1755 | } |
| 1756 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1757 | // to the innermost while checking template-parameter-lists. |
| 1758 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1759 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1760 | // C++0x [temp.expl.spec]p17: |
| 1761 | // A member or a member template may be nested within many |
| 1762 | // enclosing class templates. In an explicit specialization for |
| 1763 | // such a member, the member declaration shall be preceded by a |
| 1764 | // template<> for each enclosing class template that is |
| 1765 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1766 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1767 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1768 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1769 | if (SawNonEmptyTemplateParameterList) { |
| 1770 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1771 | << !Recovery << Range; |
| 1772 | Invalid = true; |
| 1773 | IsExplicitSpecialization = false; |
| 1774 | return true; |
| 1775 | } |
| 1776 | |
| 1777 | return false; |
| 1778 | }; |
| 1779 | |
| 1780 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1781 | // Check that we can have an explicit specialization here. |
| 1782 | if (CheckExplicitSpecialization(Range, true)) |
| 1783 | return true; |
| 1784 | |
| 1785 | // We don't have a template header, but we should. |
| 1786 | SourceLocation ExpectedTemplateLoc; |
| 1787 | if (!ParamLists.empty()) |
| 1788 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1789 | else |
| 1790 | ExpectedTemplateLoc = DeclStartLoc; |
| 1791 | |
| 1792 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1793 | << Range |
| 1794 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1795 | return false; |
| 1796 | }; |
| 1797 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1798 | unsigned ParamIdx = 0; |
| 1799 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1800 | ++TypeIdx) { |
| 1801 | T = NestedTypes[TypeIdx]; |
| 1802 | |
| 1803 | // Whether we expect a 'template<>' header. |
| 1804 | bool NeedEmptyTemplateHeader = false; |
| 1805 | |
| 1806 | // Whether we expect a template header with parameters. |
| 1807 | bool NeedNonemptyTemplateHeader = false; |
| 1808 | |
| 1809 | // For a dependent type, the set of template parameters that we |
| 1810 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1811 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1812 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1813 | // C++0x [temp.expl.spec]p15: |
| 1814 | // A member or a member template may be nested within many enclosing |
| 1815 | // class templates. In an explicit specialization for such a member, the |
| 1816 | // member declaration shall be preceded by a template<> for each |
| 1817 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1818 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1819 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1820 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1821 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1822 | NeedNonemptyTemplateHeader = true; |
| 1823 | } else if (Record->isDependentType()) { |
| 1824 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1825 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1826 | ->getTemplateParameters(); |
| 1827 | NeedNonemptyTemplateHeader = true; |
| 1828 | } |
| 1829 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1830 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1831 | // C++0x [temp.expl.spec]p4: |
| 1832 | // Members of an explicitly specialized class template are defined |
| 1833 | // in the same manner as members of normal classes, and not using |
| 1834 | // the template<> syntax. |
| 1835 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1836 | NeedEmptyTemplateHeader = true; |
| 1837 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1838 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1839 | } else if (Record->getTemplateSpecializationKind()) { |
| 1840 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1841 | != TSK_ExplicitSpecialization && |
| 1842 | TypeIdx == NumTypes - 1) |
| 1843 | IsExplicitSpecialization = true; |
| 1844 | |
| 1845 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1846 | } |
| 1847 | } else if (const TemplateSpecializationType *TST |
| 1848 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 1849 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1850 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1851 | NeedNonemptyTemplateHeader = true; |
| 1852 | } |
| 1853 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1854 | // FIXME: We actually could/should check the template arguments here |
| 1855 | // against the corresponding template parameter list. |
| 1856 | NeedNonemptyTemplateHeader = false; |
| 1857 | } |
| 1858 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1859 | // C++ [temp.expl.spec]p16: |
| 1860 | // In an explicit specialization declaration for a member of a class |
| 1861 | // template or a member template that ap- pears in namespace scope, the |
| 1862 | // member template and some of its enclosing class templates may remain |
| 1863 | // unspecialized, except that the declaration shall not explicitly |
| 1864 | // specialize a class member template if its en- closing class templates |
| 1865 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1866 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1867 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1868 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1869 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1870 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1871 | } else |
| 1872 | SawNonEmptyTemplateParameterList = true; |
| 1873 | } |
| 1874 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1875 | if (NeedEmptyTemplateHeader) { |
| 1876 | // If we're on the last of the types, and we need a 'template<>' header |
| 1877 | // here, then it's an explicit specialization. |
| 1878 | if (TypeIdx == NumTypes - 1) |
| 1879 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1880 | |
| 1881 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1882 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1883 | // The header has template parameters when it shouldn't. Complain. |
| 1884 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1885 | diag::err_template_param_list_matches_nontemplate) |
| 1886 | << T |
| 1887 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1888 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1889 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1890 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1891 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1892 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1893 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1894 | // Consume this template header. |
| 1895 | ++ParamIdx; |
| 1896 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1897 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1898 | |
| 1899 | if (!IsFriend) |
| 1900 | if (DiagnoseMissingExplicitSpecialization( |
| 1901 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1902 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1903 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1904 | continue; |
| 1905 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1906 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1907 | if (NeedNonemptyTemplateHeader) { |
| 1908 | // In friend declarations we can have template-ids which don't |
| 1909 | // depend on the corresponding template parameter lists. But |
| 1910 | // assume that empty parameter lists are supposed to match this |
| 1911 | // template-id. |
| 1912 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1913 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1914 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1915 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1916 | else |
| 1917 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1919 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1920 | if (ParamIdx < ParamLists.size()) { |
| 1921 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1922 | if (ExpectedTemplateParams && |
| 1923 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1924 | ExpectedTemplateParams, |
| 1925 | true, TPL_TemplateMatch)) |
| 1926 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1927 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1928 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1929 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1930 | TPC_ClassTemplateMember)) |
| 1931 | Invalid = true; |
| 1932 | |
| 1933 | ++ParamIdx; |
| 1934 | continue; |
| 1935 | } |
| 1936 | |
| 1937 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1938 | << T |
| 1939 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1940 | Invalid = true; |
| 1941 | continue; |
| 1942 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1943 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1945 | // If there were at least as many template-ids as there were template |
| 1946 | // parameter lists, then there are no template parameter lists remaining for |
| 1947 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1948 | if (ParamIdx >= ParamLists.size()) { |
| 1949 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1950 | // We don't have a template header for the declaration itself, but we |
| 1951 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1952 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1953 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 1954 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1955 | |
| 1956 | // Fabricate an empty template parameter list for the invented header. |
| 1957 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1958 | SourceLocation(), None, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1959 | SourceLocation()); |
| 1960 | } |
| 1961 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1962 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1963 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1965 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1966 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1967 | bool HasAnyExplicitSpecHeader = false; |
| 1968 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1969 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1970 | if (ParamLists[I]->size() == 0) |
| 1971 | HasAnyExplicitSpecHeader = true; |
| 1972 | else |
| 1973 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1974 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1976 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1977 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 1978 | : diag::err_template_spec_extra_headers) |
| 1979 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1980 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1981 | |
| 1982 | // If there was a specialization somewhere, such that 'template<>' is |
| 1983 | // not required, and there were any 'template<>' headers, note where the |
| 1984 | // specialization occurred. |
| 1985 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1986 | Diag(ExplicitSpecLoc, |
| 1987 | diag::note_explicit_template_spec_does_not_need_header) |
| 1988 | << NestedTypes.back(); |
| 1989 | |
| 1990 | // We have a template parameter list with no corresponding scope, which |
| 1991 | // means that the resulting template declaration can't be instantiated |
| 1992 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1993 | if (!AllExplicitSpecHeaders) |
| 1994 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1995 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1997 | // C++ [temp.expl.spec]p16: |
| 1998 | // In an explicit specialization declaration for a member of a class |
| 1999 | // template or a member template that ap- pears in namespace scope, the |
| 2000 | // member template and some of its enclosing class templates may remain |
| 2001 | // unspecialized, except that the declaration shall not explicitly |
| 2002 | // specialize a class member template if its en- closing class templates |
| 2003 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2004 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2005 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2006 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2007 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2008 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2009 | // Return the last template parameter list, which corresponds to the |
| 2010 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2011 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2014 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2015 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2016 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2017 | << (isa<FunctionTemplateDecl>(Template) |
| 2018 | ? 0 |
| 2019 | : isa<ClassTemplateDecl>(Template) |
| 2020 | ? 1 |
| 2021 | : isa<VarTemplateDecl>(Template) |
| 2022 | ? 2 |
| 2023 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2024 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | } |
| 2027 | |
| 2028 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 2029 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 2030 | IEnd = OST->end(); |
| 2031 | I != IEnd; ++I) |
| 2032 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2033 | << 0 << (*I)->getDeclName(); |
| 2034 | |
| 2035 | return; |
| 2036 | } |
| 2037 | } |
| 2038 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2039 | static QualType |
| 2040 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2041 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2042 | SourceLocation TemplateLoc, |
| 2043 | TemplateArgumentListInfo &TemplateArgs) { |
| 2044 | ASTContext &Context = SemaRef.getASTContext(); |
| 2045 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2046 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2047 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2048 | // S<T, 0, ..., N-1>. |
| 2049 | |
| 2050 | // C++14 [inteseq.intseq]p1: |
| 2051 | // T shall be an integer type. |
| 2052 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2053 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2054 | diag::err_integer_sequence_integral_element_type); |
| 2055 | return QualType(); |
| 2056 | } |
| 2057 | |
| 2058 | // C++14 [inteseq.make]p1: |
| 2059 | // If N is negative the program is ill-formed. |
| 2060 | TemplateArgument NumArgsArg = Converted[2]; |
| 2061 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2062 | if (NumArgs < 0) { |
| 2063 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2064 | diag::err_integer_sequence_negative_length); |
| 2065 | return QualType(); |
| 2066 | } |
| 2067 | |
| 2068 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2069 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2070 | // The type argument gets reused as the first template argument in the |
| 2071 | // synthetic template argument list. |
| 2072 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2073 | // Expand N into 0 ... N-1. |
| 2074 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2075 | I < NumArgs; ++I) { |
| 2076 | TemplateArgument TA(Context, I, ArgTy); |
| 2077 | Expr *E = SemaRef.BuildExpressionFromIntegralTemplateArgument( |
| 2078 | TA, TemplateArgs[2].getLocation()) |
| 2079 | .getAs<Expr>(); |
| 2080 | SyntheticTemplateArgs.addArgument( |
| 2081 | TemplateArgumentLoc(TemplateArgument(E), E)); |
| 2082 | } |
| 2083 | // The first template argument will be reused as the template decl that |
| 2084 | // our synthetic template arguments will be applied to. |
| 2085 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2086 | TemplateLoc, SyntheticTemplateArgs); |
| 2087 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2088 | |
| 2089 | case BTK__type_pack_element: |
| 2090 | // Specializations of |
| 2091 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2092 | // are treated like T_Index. |
| 2093 | assert(Converted.size() == 2 && |
| 2094 | "__type_pack_element should be given an index and a parameter pack"); |
| 2095 | |
| 2096 | // If the Index is out of bounds, the program is ill-formed. |
| 2097 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2098 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2099 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2100 | "type std::size_t, and hence be non-negative"); |
| 2101 | if (Index >= Ts.pack_size()) { |
| 2102 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2103 | diag::err_type_pack_element_out_of_bounds); |
| 2104 | return QualType(); |
| 2105 | } |
| 2106 | |
| 2107 | // We simply return the type at index `Index`. |
| 2108 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2109 | return Nth->getAsType(); |
| 2110 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2111 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2112 | } |
| 2113 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2114 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2115 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2116 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2117 | DependentTemplateName *DTN |
| 2118 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2119 | if (DTN && DTN->isIdentifier()) |
| 2120 | // When building a template-id where the template-name is dependent, |
| 2121 | // assume the template is a type template. Either our assumption is |
| 2122 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2123 | // dependent name is substituted. |
| 2124 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2125 | DTN->getQualifier(), |
| 2126 | DTN->getIdentifier(), |
| 2127 | TemplateArgs); |
| 2128 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2129 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2130 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2131 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2132 | // We might have a substituted template template parameter pack. If so, |
| 2133 | // build a template specialization type for it. |
| 2134 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2135 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2137 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2138 | << Name; |
| 2139 | NoteAllFoundTemplates(Name); |
| 2140 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2141 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2142 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2143 | // Check that the template argument list is well-formed for this |
| 2144 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2145 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2146 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2147 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2148 | return QualType(); |
| 2149 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2150 | QualType CanonType; |
| 2151 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2152 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2153 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2154 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2155 | // Find the canonical type for this type alias template specialization. |
| 2156 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2157 | if (Pattern->isInvalidDecl()) |
| 2158 | return QualType(); |
| 2159 | |
| 2160 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 2161 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2162 | |
| 2163 | // Only substitute for the innermost template argument list. |
| 2164 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2165 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2166 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2167 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2168 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2169 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2170 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2171 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2172 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2173 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2174 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2175 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2176 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2177 | AliasTemplate->getDeclName()); |
| 2178 | if (CanonType.isNull()) |
| 2179 | return QualType(); |
| 2180 | } else if (Name.isDependent() || |
| 2181 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2182 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2183 | // This class template specialization is a dependent |
| 2184 | // type. Therefore, its canonical type is another class template |
| 2185 | // specialization type that contains all of the converted |
| 2186 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2187 | // A<T, T> have identical types when A is declared as: |
| 2188 | // |
| 2189 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2190 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2192 | Converted.data(), |
| 2193 | Converted.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2195 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2196 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2197 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2198 | // build the canonical type and return that to us. |
| 2199 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2200 | |
| 2201 | // This might work out to be a current instantiation, in which |
| 2202 | // case the canonical type needs to be the InjectedClassNameType. |
| 2203 | // |
| 2204 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2205 | // changes to CurContext don't change the set of current |
| 2206 | // instantiations. |
| 2207 | if (isa<ClassTemplateDecl>(Template)) { |
| 2208 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2209 | // If we get out to a namespace, we're done. |
| 2210 | if (Ctx->isFileContext()) break; |
| 2211 | |
| 2212 | // If this isn't a record, keep looking. |
| 2213 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2214 | if (!Record) continue; |
| 2215 | |
| 2216 | // Look for one of the two cases with InjectedClassNameTypes |
| 2217 | // and check whether it's the same template. |
| 2218 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2219 | !Record->getDescribedClassTemplate()) |
| 2220 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2221 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2222 | // Fetch the injected class name type and check whether its |
| 2223 | // injected type is equal to the type we just built. |
| 2224 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2225 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2226 | ->getInjectedSpecializationType(); |
| 2227 | |
| 2228 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2229 | continue; |
| 2230 | |
| 2231 | // If so, the canonical type of this TST is the injected |
| 2232 | // class name type of the record we just found. |
| 2233 | assert(ICNT.isCanonical()); |
| 2234 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2235 | break; |
| 2236 | } |
| 2237 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2239 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2240 | // Find the class template specialization declaration that |
| 2241 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2242 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2243 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2244 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2245 | if (!Decl) { |
| 2246 | // This is the first time we have referenced this class template |
| 2247 | // specialization. Create the canonical declaration and add it to |
| 2248 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2250 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2251 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2252 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2253 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2254 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 2255 | Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2256 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2257 | if (ClassTemplate->isOutOfLine()) |
| 2258 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2261 | // Diagnose uses of this specialization. |
| 2262 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2263 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2264 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2265 | assert(isa<RecordType>(CanonType) && |
| 2266 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2267 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2268 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2269 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2270 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2271 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2272 | // Build the fully-sugared type for this class template |
| 2273 | // specialization, which refers back to the class template |
| 2274 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2275 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2278 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2279 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2280 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2281 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2282 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2283 | SourceLocation RAngleLoc, |
| 2284 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2285 | if (SS.isInvalid()) |
| 2286 | return true; |
| 2287 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2288 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2289 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2290 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2291 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2292 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2293 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2294 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2295 | QualType T |
| 2296 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2297 | DTN->getQualifier(), |
| 2298 | DTN->getIdentifier(), |
| 2299 | TemplateArgs); |
| 2300 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2301 | TypeLocBuilder TLB; |
| 2302 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2303 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2304 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2305 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2306 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2307 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2308 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2309 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2310 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2311 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2312 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2313 | } |
| 2314 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2315 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2316 | |
| 2317 | if (Result.isNull()) |
| 2318 | return true; |
| 2319 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2320 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2321 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2322 | TemplateSpecializationTypeLoc SpecTL |
| 2323 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2324 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2325 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2326 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2327 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2328 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2329 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2330 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2331 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2332 | // constructor or destructor name (in such a case, the scope specifier |
| 2333 | // will be attached to the enclosing Decl or Expr node). |
| 2334 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2335 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2336 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2337 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2338 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2339 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2340 | } |
| 2341 | |
| 2342 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2343 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2344 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2345 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2346 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2347 | SourceLocation TagLoc, |
| 2348 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2349 | SourceLocation TemplateKWLoc, |
| 2350 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2351 | SourceLocation TemplateLoc, |
| 2352 | SourceLocation LAngleLoc, |
| 2353 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2354 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2355 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2356 | |
| 2357 | // Translate the parser's template argument list in our AST format. |
| 2358 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2359 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2360 | |
| 2361 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2362 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2363 | ElaboratedTypeKeyword Keyword |
| 2364 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2365 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2366 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2367 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2368 | DTN->getQualifier(), |
| 2369 | DTN->getIdentifier(), |
| 2370 | TemplateArgs); |
| 2371 | |
| 2372 | // Build type-source information. |
| 2373 | TypeLocBuilder TLB; |
| 2374 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2375 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2376 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2377 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2378 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2379 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2380 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2381 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2382 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2383 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2384 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2385 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2386 | |
| 2387 | if (TypeAliasTemplateDecl *TAT = |
| 2388 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2389 | // C++0x [dcl.type.elab]p2: |
| 2390 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2391 | // resolves to an alias template specialization, the |
| 2392 | // elaborated-type-specifier is ill-formed. |
| 2393 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2394 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2395 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2396 | |
| 2397 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2398 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2399 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2400 | |
| 2401 | // Check the tag kind |
| 2402 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2403 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2404 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2405 | IdentifierInfo *Id = D->getIdentifier(); |
| 2406 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2407 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2408 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 2409 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2410 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2411 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2412 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2413 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2414 | } |
| 2415 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2416 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2417 | // Provide source-location information for the template specialization. |
| 2418 | TypeLocBuilder TLB; |
| 2419 | TemplateSpecializationTypeLoc SpecTL |
| 2420 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2421 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2422 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2423 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2424 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2425 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2426 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2427 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2428 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2429 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2430 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2431 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2432 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2433 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2434 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2437 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2438 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2439 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2440 | |
| 2441 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2442 | NamedDecl *PrevDecl, |
| 2443 | SourceLocation Loc, |
| 2444 | bool IsPartialSpecialization); |
| 2445 | |
| 2446 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2447 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2448 | static bool isTemplateArgumentTemplateParameter( |
| 2449 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2450 | switch (Arg.getKind()) { |
| 2451 | case TemplateArgument::Null: |
| 2452 | case TemplateArgument::NullPtr: |
| 2453 | case TemplateArgument::Integral: |
| 2454 | case TemplateArgument::Declaration: |
| 2455 | case TemplateArgument::Pack: |
| 2456 | case TemplateArgument::TemplateExpansion: |
| 2457 | return false; |
| 2458 | |
| 2459 | case TemplateArgument::Type: { |
| 2460 | QualType Type = Arg.getAsType(); |
| 2461 | const TemplateTypeParmType *TPT = |
| 2462 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2463 | return TPT && !Type.hasQualifiers() && |
| 2464 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2465 | } |
| 2466 | |
| 2467 | case TemplateArgument::Expression: { |
| 2468 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2469 | if (!DRE || !DRE->getDecl()) |
| 2470 | return false; |
| 2471 | const NonTypeTemplateParmDecl *NTTP = |
| 2472 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2473 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2474 | } |
| 2475 | |
| 2476 | case TemplateArgument::Template: |
| 2477 | const TemplateTemplateParmDecl *TTP = |
| 2478 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2479 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2480 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2481 | } |
| 2482 | llvm_unreachable("unexpected kind of template argument"); |
| 2483 | } |
| 2484 | |
| 2485 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2486 | ArrayRef<TemplateArgument> Args) { |
| 2487 | if (Params->size() != Args.size()) |
| 2488 | return false; |
| 2489 | |
| 2490 | unsigned Depth = Params->getDepth(); |
| 2491 | |
| 2492 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2493 | TemplateArgument Arg = Args[I]; |
| 2494 | |
| 2495 | // If the parameter is a pack expansion, the argument must be a pack |
| 2496 | // whose only element is a pack expansion. |
| 2497 | if (Params->getParam(I)->isParameterPack()) { |
| 2498 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2499 | !Arg.pack_begin()->isPackExpansion()) |
| 2500 | return false; |
| 2501 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2502 | } |
| 2503 | |
| 2504 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2505 | return false; |
| 2506 | } |
| 2507 | |
| 2508 | return true; |
| 2509 | } |
| 2510 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2511 | /// Convert the parser's template argument list representation into our form. |
| 2512 | static TemplateArgumentListInfo |
| 2513 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2514 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2515 | TemplateId.RAngleLoc); |
| 2516 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2517 | TemplateId.NumArgs); |
| 2518 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2519 | return TemplateArgs; |
| 2520 | } |
| 2521 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2522 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2523 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2524 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2525 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2526 | // D must be variable template id. |
| 2527 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2528 | "Variable template specialization is declared with a template it."); |
| 2529 | |
| 2530 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2531 | TemplateArgumentListInfo TemplateArgs = |
| 2532 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2533 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2534 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2535 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2536 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2537 | TemplateName Name = TemplateId->Template.get(); |
| 2538 | |
| 2539 | // The template-id must name a variable template. |
| 2540 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2541 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2542 | if (!VarTemplate) { |
| 2543 | NamedDecl *FnTemplate; |
| 2544 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2545 | FnTemplate = *OTS->begin(); |
| 2546 | else |
| 2547 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2548 | if (FnTemplate) |
| 2549 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2550 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2551 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2552 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2553 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2554 | |
| 2555 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2556 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2557 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2558 | UPPC_PartialSpecialization)) |
| 2559 | return true; |
| 2560 | |
| 2561 | // Check that the template argument list is well-formed for this |
| 2562 | // template. |
| 2563 | SmallVector<TemplateArgument, 4> Converted; |
| 2564 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2565 | false, Converted)) |
| 2566 | return true; |
| 2567 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2568 | // Find the variable template (partial) specialization declaration that |
| 2569 | // corresponds to these arguments. |
| 2570 | if (IsPartialSpecialization) { |
| 2571 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2572 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2573 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2574 | return true; |
| 2575 | |
| 2576 | bool InstantiationDependent; |
| 2577 | if (!Name.isDependent() && |
| 2578 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2579 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 2580 | InstantiationDependent)) { |
| 2581 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2582 | << VarTemplate->getDeclName(); |
| 2583 | IsPartialSpecialization = false; |
| 2584 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2585 | |
| 2586 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2587 | Converted)) { |
| 2588 | // C++ [temp.class.spec]p9b3: |
| 2589 | // |
| 2590 | // -- The argument list of the specialization shall not be identical |
| 2591 | // to the implicit argument list of the primary template. |
| 2592 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2593 | << /*variable template*/ 1 |
| 2594 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2595 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2596 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2597 | // of the primary template. |
| 2598 | return true; |
| 2599 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2602 | void *InsertPos = nullptr; |
| 2603 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2604 | |
| 2605 | if (IsPartialSpecialization) |
| 2606 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2607 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2608 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2609 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2610 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2611 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2612 | |
| 2613 | // Check whether we can declare a variable template specialization in |
| 2614 | // the current scope. |
| 2615 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2616 | TemplateNameLoc, |
| 2617 | IsPartialSpecialization)) |
| 2618 | return true; |
| 2619 | |
| 2620 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2621 | // Since the only prior variable template specialization with these |
| 2622 | // arguments was referenced but not declared, reuse that |
| 2623 | // declaration node as our own, updating its source location and |
| 2624 | // the list of outer template parameters to reflect our new declaration. |
| 2625 | Specialization = PrevDecl; |
| 2626 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2627 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2628 | } else if (IsPartialSpecialization) { |
| 2629 | // Create a new class template partial specialization declaration node. |
| 2630 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2631 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2632 | VarTemplatePartialSpecializationDecl *Partial = |
| 2633 | VarTemplatePartialSpecializationDecl::Create( |
| 2634 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2635 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 2636 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2637 | |
| 2638 | if (!PrevPartial) |
| 2639 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2640 | Specialization = Partial; |
| 2641 | |
| 2642 | // If we are providing an explicit specialization of a member variable |
| 2643 | // template specialization, make a note of that. |
| 2644 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2645 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2646 | |
| 2647 | // Check that all of the template parameters of the variable template |
| 2648 | // partial specialization are deducible from the template |
| 2649 | // arguments. If not, this variable template partial specialization |
| 2650 | // will never be used. |
| 2651 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2652 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2653 | TemplateParams->getDepth(), DeducibleParams); |
| 2654 | |
| 2655 | if (!DeducibleParams.all()) { |
| 2656 | unsigned NumNonDeducible = |
| 2657 | DeducibleParams.size() - DeducibleParams.count(); |
| 2658 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2659 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2660 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2661 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2662 | if (!DeducibleParams[I]) { |
| 2663 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2664 | if (Param->getDeclName()) |
| 2665 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2666 | << Param->getDeclName(); |
| 2667 | else |
| 2668 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2669 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | } |
| 2673 | } else { |
| 2674 | // Create a new class template specialization declaration node for |
| 2675 | // this explicit specialization or friend declaration. |
| 2676 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2677 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 2678 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2679 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2680 | |
| 2681 | if (!PrevDecl) |
| 2682 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2683 | } |
| 2684 | |
| 2685 | // C++ [temp.expl.spec]p6: |
| 2686 | // If a template, a member template or the member of a class template is |
| 2687 | // explicitly specialized then that specialization shall be declared |
| 2688 | // before the first use of that specialization that would cause an implicit |
| 2689 | // instantiation to take place, in every translation unit in which such a |
| 2690 | // use occurs; no diagnostic is required. |
| 2691 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2692 | bool Okay = false; |
| 2693 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2694 | // Is there any previous explicit specialization declaration? |
| 2695 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2696 | Okay = true; |
| 2697 | break; |
| 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | if (!Okay) { |
| 2702 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2703 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2704 | << Name << Range; |
| 2705 | |
| 2706 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2707 | diag::note_instantiation_required_here) |
| 2708 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2709 | TSK_ImplicitInstantiation); |
| 2710 | return true; |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2715 | Specialization->setLexicalDeclContext(CurContext); |
| 2716 | |
| 2717 | // Add the specialization into its lexical context, so that it can |
| 2718 | // be seen when iterating through the list of declarations in that |
| 2719 | // context. However, specializations are not found by name lookup. |
| 2720 | CurContext->addDecl(Specialization); |
| 2721 | |
| 2722 | // Note that this is an explicit specialization. |
| 2723 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2724 | |
| 2725 | if (PrevDecl) { |
| 2726 | // Check that this isn't a redefinition of this specialization, |
| 2727 | // merging with previous declarations. |
| 2728 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2729 | ForRedeclaration); |
| 2730 | PrevSpec.addDecl(PrevDecl); |
| 2731 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2732 | } else if (Specialization->isStaticDataMember() && |
| 2733 | Specialization->isOutOfLine()) { |
| 2734 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | // Link instantiations of static data members back to the template from |
| 2738 | // which they were instantiated. |
| 2739 | if (Specialization->isStaticDataMember()) |
| 2740 | Specialization->setInstantiationOfStaticDataMember( |
| 2741 | VarTemplate->getTemplatedDecl(), |
| 2742 | Specialization->getSpecializationKind()); |
| 2743 | |
| 2744 | return Specialization; |
| 2745 | } |
| 2746 | |
| 2747 | namespace { |
| 2748 | /// \brief A partial specialization whose template arguments have matched |
| 2749 | /// a given template-id. |
| 2750 | struct PartialSpecMatchResult { |
| 2751 | VarTemplatePartialSpecializationDecl *Partial; |
| 2752 | TemplateArgumentList *Args; |
| 2753 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2754 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2755 | |
| 2756 | DeclResult |
| 2757 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2758 | SourceLocation TemplateNameLoc, |
| 2759 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2760 | assert(Template && "A variable template id without template?"); |
| 2761 | |
| 2762 | // Check that the template argument list is well-formed for this template. |
| 2763 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2764 | if (CheckTemplateArgumentList( |
| 2765 | Template, TemplateNameLoc, |
| 2766 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2767 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2768 | return true; |
| 2769 | |
| 2770 | // Find the variable template specialization declaration that |
| 2771 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2772 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2773 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2774 | Converted, InsertPos)) { |
| 2775 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2776 | // If we already have a variable template specialization, return it. |
| 2777 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2778 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2779 | |
| 2780 | // This is the first time we have referenced this variable template |
| 2781 | // specialization. Create the canonical declaration and add it to |
| 2782 | // the set of specializations, based on the closest partial specialization |
| 2783 | // that it represents. That is, |
| 2784 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2785 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 2786 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2787 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2788 | bool AmbiguousPartialSpec = false; |
| 2789 | typedef PartialSpecMatchResult MatchResult; |
| 2790 | SmallVector<MatchResult, 4> Matched; |
| 2791 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2792 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 2793 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2794 | |
| 2795 | // 1. Attempt to find the closest partial specialization that this |
| 2796 | // specializes, if any. |
| 2797 | // If any of the template arguments is dependent, then this is probably |
| 2798 | // a placeholder for an incomplete declarative context; which must be |
| 2799 | // complete by instantiation time. Thus, do not search through the partial |
| 2800 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2801 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2802 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2803 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2804 | bool InstantiationDependent = false; |
| 2805 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2806 | TemplateArgs, InstantiationDependent)) { |
| 2807 | |
| 2808 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2809 | Template->getPartialSpecializations(PartialSpecs); |
| 2810 | |
| 2811 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2812 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2813 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2814 | |
| 2815 | if (TemplateDeductionResult Result = |
| 2816 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2817 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2818 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 2819 | FailedCandidates.addCandidate().set( |
| 2820 | DeclAccessPair::make(Template, AS_public), Partial, |
| 2821 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2822 | (void)Result; |
| 2823 | } else { |
| 2824 | Matched.push_back(PartialSpecMatchResult()); |
| 2825 | Matched.back().Partial = Partial; |
| 2826 | Matched.back().Args = Info.take(); |
| 2827 | } |
| 2828 | } |
| 2829 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2830 | if (Matched.size() >= 1) { |
| 2831 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2832 | if (Matched.size() == 1) { |
| 2833 | // -- If exactly one matching specialization is found, the |
| 2834 | // instantiation is generated from that specialization. |
| 2835 | // We don't need to do anything for this. |
| 2836 | } else { |
| 2837 | // -- If more than one matching specialization is found, the |
| 2838 | // partial order rules (14.5.4.2) are used to determine |
| 2839 | // whether one of the specializations is more specialized |
| 2840 | // than the others. If none of the specializations is more |
| 2841 | // specialized than all of the other matching |
| 2842 | // specializations, then the use of the variable template is |
| 2843 | // ambiguous and the program is ill-formed. |
| 2844 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2845 | PEnd = Matched.end(); |
| 2846 | P != PEnd; ++P) { |
| 2847 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2848 | PointOfInstantiation) == |
| 2849 | P->Partial) |
| 2850 | Best = P; |
| 2851 | } |
| 2852 | |
| 2853 | // Determine if the best partial specialization is more specialized than |
| 2854 | // the others. |
| 2855 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2856 | PEnd = Matched.end(); |
| 2857 | P != PEnd; ++P) { |
| 2858 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2859 | P->Partial, Best->Partial, |
| 2860 | PointOfInstantiation) != Best->Partial) { |
| 2861 | AmbiguousPartialSpec = true; |
| 2862 | break; |
| 2863 | } |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | // Instantiate using the best variable template partial specialization. |
| 2868 | InstantiationPattern = Best->Partial; |
| 2869 | InstantiationArgs = Best->Args; |
| 2870 | } else { |
| 2871 | // -- If no match is found, the instantiation is generated |
| 2872 | // from the primary template. |
| 2873 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2874 | } |
| 2875 | } |
| 2876 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2877 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2878 | // Note that we do not instantiate a definition until we see an odr-use |
| 2879 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2880 | // FIXME: LateAttrs et al.? |
| 2881 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2882 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2883 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2884 | if (!Decl) |
| 2885 | return true; |
| 2886 | |
| 2887 | if (AmbiguousPartialSpec) { |
| 2888 | // Partial ordering did not produce a clear winner. Complain. |
| 2889 | Decl->setInvalidDecl(); |
| 2890 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2891 | << Decl; |
| 2892 | |
| 2893 | // Print the matching partial specializations. |
| 2894 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2895 | PEnd = Matched.end(); |
| 2896 | P != PEnd; ++P) |
| 2897 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2898 | << getTemplateArgumentBindingsText( |
| 2899 | P->Partial->getTemplateParameters(), *P->Args); |
| 2900 | return true; |
| 2901 | } |
| 2902 | |
| 2903 | if (VarTemplatePartialSpecializationDecl *D = |
| 2904 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2905 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2906 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2907 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 2908 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2909 | assert(Decl && "No variable template specialization?"); |
| 2910 | return Decl; |
| 2911 | } |
| 2912 | |
| 2913 | ExprResult |
| 2914 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2915 | const DeclarationNameInfo &NameInfo, |
| 2916 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2917 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2918 | |
| 2919 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2920 | *TemplateArgs); |
| 2921 | if (Decl.isInvalid()) |
| 2922 | return ExprError(); |
| 2923 | |
| 2924 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2925 | if (!Var->getTemplateSpecializationKind()) |
| 2926 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2927 | NameInfo.getLoc()); |
| 2928 | |
| 2929 | // Build an ordinary singleton decl ref. |
| 2930 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2931 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2934 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2935 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2936 | LookupResult &R, |
| 2937 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2938 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2939 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2940 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2941 | // 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] | 2942 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2943 | // foo<int> could identify a single function unambiguously |
| 2944 | // This approach does NOT work, since f<int>(1); |
| 2945 | // gets resolved prior to resorting to overload resolution |
| 2946 | // i.e., template<class T> void f(double); |
| 2947 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2948 | |
| 2949 | // These should be filtered out by our callers. |
| 2950 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2951 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2952 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2953 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 2954 | bool InstantiationDependent; |
| 2955 | if (R.getAsSingle<VarTemplateDecl>() && |
| 2956 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2957 | *TemplateArgs, InstantiationDependent)) { |
| 2958 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 2959 | R.getAsSingle<VarTemplateDecl>(), |
| 2960 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2963 | // We don't want lookup warnings at this point. |
| 2964 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2965 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2966 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2967 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2968 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2969 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2970 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2971 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2972 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2973 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2974 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2977 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2978 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2979 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2980 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2981 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2982 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2983 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2984 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2985 | DeclContext *DC; |
| 2986 | if (!(DC = computeDeclContext(SS, false)) || |
| 2987 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2988 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 2989 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2990 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2991 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2992 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2993 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2994 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2996 | if (R.isAmbiguous()) |
| 2997 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2998 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2999 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3000 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 3001 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3002 | return ExprError(); |
| 3003 | } |
| 3004 | |
| 3005 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3006 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3007 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 3008 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3009 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 3010 | return ExprError(); |
| 3011 | } |
| 3012 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3013 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3016 | /// \brief Form a dependent template name. |
| 3017 | /// |
| 3018 | /// This action forms a dependent template name given the template |
| 3019 | /// name and its (presumably dependent) scope specifier. For |
| 3020 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 3021 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 3022 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3023 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3024 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3025 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3026 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3027 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3028 | bool EnteringContext, |
| 3029 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3030 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 3031 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3032 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3033 | diag::warn_cxx98_compat_template_outside_of_template : |
| 3034 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3035 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 3036 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3037 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3038 | if (SS.isSet()) |
| 3039 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 3040 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3041 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3042 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3043 | // C++0x [temp.names]p5: |
| 3044 | // If a name prefixed by the keyword template is not the name of |
| 3045 | // a template, the program is ill-formed. [Note: the keyword |
| 3046 | // template may not be applied to non-template members of class |
| 3047 | // templates. -end note ] [ Note: as is the case with the |
| 3048 | // typename prefix, the template prefix is allowed in cases |
| 3049 | // where it is not strictly necessary; i.e., when the |
| 3050 | // nested-name-specifier or the expression on the left of the -> |
| 3051 | // or . is not dependent on a template-parameter, or the use |
| 3052 | // does not appear in the scope of a template. -end note] |
| 3053 | // |
| 3054 | // Note: C++03 was more strict here, because it banned the use of |
| 3055 | // the "template" keyword prior to a template-name that was not a |
| 3056 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3057 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3058 | // 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] | 3059 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3060 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3061 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3062 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3063 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3064 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3065 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3066 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3067 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3068 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3069 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3070 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3071 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3072 | << Name.getSourceRange() |
| 3073 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3074 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3075 | } else { |
| 3076 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3077 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3078 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3081 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3082 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3083 | switch (Name.getKind()) { |
| 3084 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3085 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3086 | Name.Identifier)); |
| 3087 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3088 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3089 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3090 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3091 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3092 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3093 | |
| 3094 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3095 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3096 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3097 | default: |
| 3098 | break; |
| 3099 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3100 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3101 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3102 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3103 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3104 | << Name.getSourceRange() |
| 3105 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3106 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3109 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3110 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3111 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3112 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3113 | QualType ArgType; |
| 3114 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3115 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3116 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3117 | switch(Arg.getKind()) { |
| 3118 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3119 | // C++ [temp.arg.type]p1: |
| 3120 | // A template-argument for a template-parameter which is a |
| 3121 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3122 | ArgType = Arg.getAsType(); |
| 3123 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3124 | break; |
| 3125 | case TemplateArgument::Template: { |
| 3126 | // We have a template type parameter but the template argument |
| 3127 | // is a template without any arguments. |
| 3128 | SourceRange SR = AL.getSourceRange(); |
| 3129 | TemplateName Name = Arg.getAsTemplate(); |
| 3130 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3131 | << Name << SR; |
| 3132 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3133 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3134 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3135 | return true; |
| 3136 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3137 | case TemplateArgument::Expression: { |
| 3138 | // We have a template type parameter but the template argument is an |
| 3139 | // expression; see if maybe it is missing the "typename" keyword. |
| 3140 | CXXScopeSpec SS; |
| 3141 | DeclarationNameInfo NameInfo; |
| 3142 | |
| 3143 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3144 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3145 | NameInfo = ArgExpr->getNameInfo(); |
| 3146 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3147 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3148 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3149 | NameInfo = ArgExpr->getNameInfo(); |
| 3150 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3151 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3152 | if (ArgExpr->isImplicitAccess()) { |
| 3153 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3154 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3155 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3158 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3159 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3160 | LookupParsedName(Result, CurScope, &SS); |
| 3161 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3162 | if (Result.getAsSingle<TypeDecl>() || |
| 3163 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3164 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3165 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3166 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3167 | Diag(Loc, getLangOpts().MSVCCompat |
| 3168 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3169 | : diag::err_template_arg_must_be_type_suggest) |
| 3170 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3171 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3172 | |
| 3173 | // Recover by synthesizing a type using the location information that we |
| 3174 | // already have. |
| 3175 | ArgType = |
| 3176 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3177 | TypeLocBuilder TLB; |
| 3178 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3179 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3180 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3181 | TL.setNameLoc(NameInfo.getLoc()); |
| 3182 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3183 | |
| 3184 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3185 | // properly. |
| 3186 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3187 | TemplateArgumentLocInfo(TSI)); |
| 3188 | |
| 3189 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3190 | } |
| 3191 | } |
| 3192 | // fallthrough |
| 3193 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3194 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3195 | // We have a template type parameter but the template argument |
| 3196 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3197 | SourceRange SR = AL.getSourceRange(); |
| 3198 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3199 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3201 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3202 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3203 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3204 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3205 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3206 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3208 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3209 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3210 | |
| 3211 | // Objective-C ARC: |
| 3212 | // If an explicitly-specified template argument type is a lifetime type |
| 3213 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3214 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3215 | ArgType->isObjCLifetimeType() && |
| 3216 | !ArgType.getObjCLifetime()) { |
| 3217 | Qualifiers Qs; |
| 3218 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3219 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3220 | } |
| 3221 | |
| 3222 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3223 | return false; |
| 3224 | } |
| 3225 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3226 | /// \brief Substitute template arguments into the default template argument for |
| 3227 | /// the given template type parameter. |
| 3228 | /// |
| 3229 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3230 | /// the substitution. |
| 3231 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3232 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3233 | /// for. |
| 3234 | /// |
| 3235 | /// \param TemplateLoc the location of the template name that started the |
| 3236 | /// template-id we are checking. |
| 3237 | /// |
| 3238 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3239 | /// terminates the template-id. |
| 3240 | /// |
| 3241 | /// \param Param the template template parameter whose default we are |
| 3242 | /// substituting into. |
| 3243 | /// |
| 3244 | /// \param Converted the list of template arguments provided for template |
| 3245 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3246 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3247 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3248 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3249 | TemplateDecl *Template, |
| 3250 | SourceLocation TemplateLoc, |
| 3251 | SourceLocation RAngleLoc, |
| 3252 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3253 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3254 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3255 | |
| 3256 | // If the argument type is dependent, instantiate it now based |
| 3257 | // on the previously-computed template arguments. |
| 3258 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3259 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3260 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3261 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3262 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3263 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3264 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 3265 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3266 | |
| 3267 | // Only substitute for the innermost template argument list. |
| 3268 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3269 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3270 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3271 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3272 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3273 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3274 | ArgType = |
| 3275 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3276 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | return ArgType; |
| 3280 | } |
| 3281 | |
| 3282 | /// \brief Substitute template arguments into the default template argument for |
| 3283 | /// the given non-type template parameter. |
| 3284 | /// |
| 3285 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3286 | /// the substitution. |
| 3287 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3288 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3289 | /// for. |
| 3290 | /// |
| 3291 | /// \param TemplateLoc the location of the template name that started the |
| 3292 | /// template-id we are checking. |
| 3293 | /// |
| 3294 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3295 | /// terminates the template-id. |
| 3296 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3297 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3298 | /// substituting into. |
| 3299 | /// |
| 3300 | /// \param Converted the list of template arguments provided for template |
| 3301 | /// parameters that precede \p Param in the template parameter list. |
| 3302 | /// |
| 3303 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3304 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3305 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3306 | TemplateDecl *Template, |
| 3307 | SourceLocation TemplateLoc, |
| 3308 | SourceLocation RAngleLoc, |
| 3309 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3310 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3311 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3312 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3313 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3314 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3315 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3316 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 3317 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3318 | |
| 3319 | // Only substitute for the innermost template argument list. |
| 3320 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3321 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3322 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3323 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3324 | |
Faisal Vali | 48401eb | 2015-11-19 19:20:17 +0000 | [diff] [blame] | 3325 | EnterExpressionEvaluationContext ConstantEvaluated(SemaRef, |
| 3326 | Sema::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3327 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3330 | /// \brief Substitute template arguments into the default template argument for |
| 3331 | /// the given template template parameter. |
| 3332 | /// |
| 3333 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3334 | /// the substitution. |
| 3335 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3336 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3337 | /// for. |
| 3338 | /// |
| 3339 | /// \param TemplateLoc the location of the template name that started the |
| 3340 | /// template-id we are checking. |
| 3341 | /// |
| 3342 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3343 | /// terminates the template-id. |
| 3344 | /// |
| 3345 | /// \param Param the template template parameter whose default we are |
| 3346 | /// substituting into. |
| 3347 | /// |
| 3348 | /// \param Converted the list of template arguments provided for template |
| 3349 | /// parameters that precede \p Param in the template parameter list. |
| 3350 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3351 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3352 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3353 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3354 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3355 | static TemplateName |
| 3356 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3357 | TemplateDecl *Template, |
| 3358 | SourceLocation TemplateLoc, |
| 3359 | SourceLocation RAngleLoc, |
| 3360 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3361 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3362 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3363 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3364 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3365 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3366 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3367 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 3368 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3369 | |
| 3370 | // Only substitute for the innermost template argument list. |
| 3371 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3372 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3373 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3374 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3375 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3376 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3377 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3378 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3379 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3380 | QualifierLoc = |
| 3381 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3382 | if (!QualifierLoc) |
| 3383 | return TemplateName(); |
| 3384 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3385 | |
| 3386 | return SemaRef.SubstTemplateName( |
| 3387 | QualifierLoc, |
| 3388 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3389 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3390 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3393 | /// \brief If the given template parameter has a default template |
| 3394 | /// argument, substitute into that default template argument and |
| 3395 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3396 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3397 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3398 | SourceLocation TemplateLoc, |
| 3399 | SourceLocation RAngleLoc, |
| 3400 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3401 | SmallVectorImpl<TemplateArgument> |
| 3402 | &Converted, |
| 3403 | bool &HasDefaultArg) { |
| 3404 | HasDefaultArg = false; |
| 3405 | |
| 3406 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3407 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3408 | return TemplateArgumentLoc(); |
| 3409 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3410 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3411 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3412 | TemplateLoc, |
| 3413 | RAngleLoc, |
| 3414 | TypeParm, |
| 3415 | Converted); |
| 3416 | if (DI) |
| 3417 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3418 | |
| 3419 | return TemplateArgumentLoc(); |
| 3420 | } |
| 3421 | |
| 3422 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3423 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3424 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3425 | return TemplateArgumentLoc(); |
| 3426 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3427 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3428 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3429 | TemplateLoc, |
| 3430 | RAngleLoc, |
| 3431 | NonTypeParm, |
| 3432 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3433 | if (Arg.isInvalid()) |
| 3434 | return TemplateArgumentLoc(); |
| 3435 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3436 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3437 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3438 | } |
| 3439 | |
| 3440 | TemplateTemplateParmDecl *TempTempParm |
| 3441 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3442 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3443 | return TemplateArgumentLoc(); |
| 3444 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3445 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3446 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3447 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3448 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3449 | RAngleLoc, |
| 3450 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3451 | Converted, |
| 3452 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3453 | if (TName.isNull()) |
| 3454 | return TemplateArgumentLoc(); |
| 3455 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3456 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3457 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3458 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3459 | } |
| 3460 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3461 | /// \brief Check that the given template argument corresponds to the given |
| 3462 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3463 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3464 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3465 | /// checked. |
| 3466 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3467 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3468 | /// |
| 3469 | /// \param Template The template in which the template argument resides. |
| 3470 | /// |
| 3471 | /// \param TemplateLoc The location of the template name for the template |
| 3472 | /// whose argument list we're matching. |
| 3473 | /// |
| 3474 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3475 | /// the template argument list. |
| 3476 | /// |
| 3477 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3478 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3479 | /// |
| 3480 | /// \param Converted The checked, converted argument will be added to the |
| 3481 | /// end of this small vector. |
| 3482 | /// |
| 3483 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3484 | /// explicitly written, deduced, etc. |
| 3485 | /// |
| 3486 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3487 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3488 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3489 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3490 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3491 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3492 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3493 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3494 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3495 | // Check template type parameters. |
| 3496 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3497 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3499 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3500 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3501 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3502 | // with the template arguments we've seen thus far. But if the |
| 3503 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3504 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3505 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3506 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3507 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3508 | if (NTTPType->isDependentType() && |
| 3509 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3510 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3511 | // Do substitution on the type of the non-type template parameter. |
| 3512 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3513 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3514 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3515 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3516 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3517 | |
| 3518 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 3519 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3520 | NTTPType = SubstType(NTTPType, |
| 3521 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3522 | NTTP->getLocation(), |
| 3523 | NTTP->getDeclName()); |
| 3524 | // If that worked, check the non-type template parameter type |
| 3525 | // for validity. |
| 3526 | if (!NTTPType.isNull()) |
| 3527 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3528 | NTTP->getLocation()); |
| 3529 | if (NTTPType.isNull()) |
| 3530 | return true; |
| 3531 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3532 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3533 | switch (Arg.getArgument().getKind()) { |
| 3534 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3535 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3536 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3537 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3538 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3539 | ExprResult Res = |
| 3540 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3541 | Result, CTAK); |
| 3542 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3543 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3544 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3545 | // If the resulting expression is new, then use it in place of the |
| 3546 | // old expression in the template argument. |
| 3547 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3548 | TemplateArgument TA(Res.get()); |
| 3549 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3550 | } |
| 3551 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3552 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3553 | break; |
| 3554 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3555 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3556 | case TemplateArgument::Declaration: |
| 3557 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3558 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3559 | // We've already checked this template argument, so just copy |
| 3560 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3561 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3562 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3563 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3564 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3565 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3566 | // We were given a template template argument. It may not be ill-formed; |
| 3567 | // see below. |
| 3568 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3569 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3570 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3571 | // We have a template argument such as \c T::template X, which we |
| 3572 | // parsed as a template template argument. However, since we now |
| 3573 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3574 | // template name into an expression. |
| 3575 | |
| 3576 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3577 | Arg.getTemplateNameLoc()); |
| 3578 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3579 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3580 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3581 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3582 | // so it was provided with a template keyword. However, its source |
| 3583 | // location is not stored in the template argument structure. |
| 3584 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3585 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3586 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3587 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3588 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3589 | // If we parsed the template argument as a pack expansion, create a |
| 3590 | // pack expansion expression. |
| 3591 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3592 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3593 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3594 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3595 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3596 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3597 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3598 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3599 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3600 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3601 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3602 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3603 | break; |
| 3604 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3605 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3606 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3607 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3608 | // therefore cannot be a non-type template argument. |
| 3609 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3610 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3611 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3612 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3613 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3614 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3615 | case TemplateArgument::Type: { |
| 3616 | // We have a non-type template parameter but the template |
| 3617 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3618 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3619 | // C++ [temp.arg]p2: |
| 3620 | // In a template-argument, an ambiguity between a type-id and |
| 3621 | // an expression is resolved to a type-id, regardless of the |
| 3622 | // form of the corresponding template-parameter. |
| 3623 | // |
| 3624 | // We warn specifically about this case, since it can be rather |
| 3625 | // confusing for users. |
| 3626 | QualType T = Arg.getArgument().getAsType(); |
| 3627 | SourceRange SR = Arg.getSourceRange(); |
| 3628 | if (T->isFunctionType()) |
| 3629 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3630 | else |
| 3631 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3632 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3633 | return true; |
| 3634 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3635 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3636 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3637 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3638 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3639 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3640 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3644 | // Check template template parameters. |
| 3645 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3647 | // Substitute into the template parameter list of the template |
| 3648 | // template parameter, since previously-supplied template arguments |
| 3649 | // may appear within the template template parameter. |
| 3650 | { |
| 3651 | // Set up a template instantiation context. |
| 3652 | LocalInstantiationScope Scope(*this); |
| 3653 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3654 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3655 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3656 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3657 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3658 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 3659 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3660 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3661 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3662 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3663 | if (!TempParm) |
| 3664 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3665 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3666 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3667 | switch (Arg.getArgument().getKind()) { |
| 3668 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3669 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3670 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3671 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3672 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3673 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3674 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3675 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3676 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3677 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3678 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3679 | case TemplateArgument::Expression: |
| 3680 | case TemplateArgument::Type: |
| 3681 | // We have a template template parameter but the template |
| 3682 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3683 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3684 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3685 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3687 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3688 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3689 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3690 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3691 | case TemplateArgument::NullPtr: |
| 3692 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3693 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3694 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3695 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3696 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3697 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3698 | return false; |
| 3699 | } |
| 3700 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3701 | /// \brief Diagnose an arity mismatch in the |
| 3702 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3703 | SourceLocation TemplateLoc, |
| 3704 | TemplateArgumentListInfo &TemplateArgs) { |
| 3705 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3706 | unsigned NumParams = Params->size(); |
| 3707 | unsigned NumArgs = TemplateArgs.size(); |
| 3708 | |
| 3709 | SourceRange Range; |
| 3710 | if (NumArgs > NumParams) |
| 3711 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3712 | TemplateArgs.getRAngleLoc()); |
| 3713 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3714 | << (NumArgs > NumParams) |
| 3715 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3716 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3717 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3718 | << Template << Range; |
| 3719 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3720 | << Params->getSourceRange(); |
| 3721 | return true; |
| 3722 | } |
| 3723 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3724 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3725 | /// determine the number of parameters produced by that expansion. For instance: |
| 3726 | /// |
| 3727 | /// \code |
| 3728 | /// template<typename ...Ts> struct A { |
| 3729 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3730 | /// }; |
| 3731 | /// \endcode |
| 3732 | /// |
| 3733 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3734 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3735 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3736 | if (NonTypeTemplateParmDecl *NTTP |
| 3737 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3738 | if (NTTP->isExpandedParameterPack()) |
| 3739 | return NTTP->getNumExpansionTypes(); |
| 3740 | } |
| 3741 | |
| 3742 | if (TemplateTemplateParmDecl *TTP |
| 3743 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3744 | if (TTP->isExpandedParameterPack()) |
| 3745 | return TTP->getNumExpansionTemplateParameters(); |
| 3746 | } |
| 3747 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3748 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3751 | /// Diagnose a missing template argument. |
| 3752 | template<typename TemplateParmDecl> |
| 3753 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 3754 | TemplateDecl *TD, |
| 3755 | const TemplateParmDecl *D, |
| 3756 | TemplateArgumentListInfo &Args) { |
| 3757 | // Dig out the most recent declaration of the template parameter; there may be |
| 3758 | // declarations of the template that are more recent than TD. |
| 3759 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 3760 | ->getTemplateParameters() |
| 3761 | ->getParam(D->getIndex())); |
| 3762 | |
| 3763 | // If there's a default argument that's not visible, diagnose that we're |
| 3764 | // missing a module import. |
| 3765 | llvm::SmallVector<Module*, 8> Modules; |
| 3766 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 3767 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 3768 | D->getDefaultArgumentLoc(), Modules, |
| 3769 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3770 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3771 | return true; |
| 3772 | } |
| 3773 | |
| 3774 | // FIXME: If there's a more recent default argument that *is* visible, |
| 3775 | // diagnose that it was declared too late. |
| 3776 | |
| 3777 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 3778 | } |
| 3779 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3780 | /// \brief Check that the given template argument list is well-formed |
| 3781 | /// for specializing the given template. |
| 3782 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3783 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3784 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3785 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3786 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3787 | // Make a copy of the template arguments for processing. Only make the |
| 3788 | // changes at the end when successful in matching the arguments to the |
| 3789 | // template. |
| 3790 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3791 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3792 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3793 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3794 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3795 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3796 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3797 | // [...] The type and form of each template-argument specified in |
| 3798 | // a template-id shall match the type and form specified for the |
| 3799 | // corresponding parameter declared by the template in its |
| 3800 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3801 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3802 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3803 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3804 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3805 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3806 | ParamEnd = Params->end(); |
| 3807 | Param != ParamEnd; /* increment in loop */) { |
| 3808 | // If we have an expanded parameter pack, make sure we don't have too |
| 3809 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3810 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3811 | if (*Expansions == ArgumentPack.size()) { |
| 3812 | // We're done with this parameter pack. Pack up its arguments and add |
| 3813 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3814 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3815 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3816 | ArgumentPack.clear(); |
| 3817 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3818 | // This argument is assigned to the next parameter. |
| 3819 | ++Param; |
| 3820 | continue; |
| 3821 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3822 | // Not enough arguments for this parameter pack. |
| 3823 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3824 | << false |
| 3825 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3826 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3827 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3828 | << Template; |
| 3829 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3830 | << Params->getSourceRange(); |
| 3831 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3832 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3833 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3834 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3835 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3836 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3837 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3838 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3839 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3840 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3841 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3842 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3843 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3844 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3845 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3846 | // 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] | 3847 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3848 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3849 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3850 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3851 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3852 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3853 | return true; |
| 3854 | } |
| 3855 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3856 | // We're now done with this argument. |
| 3857 | ++ArgIdx; |
| 3858 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3859 | if ((*Param)->isTemplateParameterPack()) { |
| 3860 | // The template parameter was a template parameter pack, so take the |
| 3861 | // deduced argument and place it on the argument pack. Note that we |
| 3862 | // stay on the same template parameter so that we can deduce more |
| 3863 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3864 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3865 | } else { |
| 3866 | // Move to the next template parameter. |
| 3867 | ++Param; |
| 3868 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3869 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3870 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3871 | // the remaining arguments, because we don't know what parameters they'll |
| 3872 | // match up with. |
| 3873 | if (PackExpansionIntoNonPack) { |
| 3874 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3875 | // If we were part way through filling in an expanded parameter pack, |
| 3876 | // fall back to just producing individual arguments. |
| 3877 | Converted.insert(Converted.end(), |
| 3878 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3879 | ArgumentPack.clear(); |
| 3880 | } |
| 3881 | |
| 3882 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3883 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3884 | ++ArgIdx; |
| 3885 | } |
| 3886 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3887 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3888 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3889 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3890 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3891 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3892 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3893 | // If we're checking a partial template argument list, we're done. |
| 3894 | if (PartialTemplateArgs) { |
| 3895 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3896 | Converted.push_back( |
| 3897 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 3898 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3899 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3902 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3903 | // 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] | 3904 | if ((*Param)->isTemplateParameterPack()) { |
| 3905 | assert(!getExpandedPackSize(*Param) && |
| 3906 | "Should have dealt with this already"); |
| 3907 | |
| 3908 | // A non-expanded parameter pack before the end of the parameter list |
| 3909 | // only occurs for an ill-formed template parameter list, unless we've |
| 3910 | // got a partial argument list for a function template, so just bail out. |
| 3911 | if (Param + 1 != ParamEnd) |
| 3912 | return true; |
| 3913 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3914 | Converted.push_back( |
| 3915 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3916 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3917 | |
| 3918 | ++Param; |
| 3919 | continue; |
| 3920 | } |
| 3921 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3922 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3923 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3924 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3925 | // Retrieve the default template argument from the template |
| 3926 | // parameter. For each kind of template parameter, we substitute the |
| 3927 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3928 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3929 | // the default argument. |
| 3930 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3931 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3932 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 3933 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3934 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3935 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3936 | Template, |
| 3937 | TemplateLoc, |
| 3938 | RAngleLoc, |
| 3939 | TTP, |
| 3940 | Converted); |
| 3941 | if (!ArgType) |
| 3942 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3943 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3944 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3945 | ArgType); |
| 3946 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3947 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3948 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3949 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 3950 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3951 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3952 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3953 | TemplateLoc, |
| 3954 | RAngleLoc, |
| 3955 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3956 | Converted); |
| 3957 | if (E.isInvalid()) |
| 3958 | return true; |
| 3959 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3960 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3961 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3962 | } else { |
| 3963 | TemplateTemplateParmDecl *TempParm |
| 3964 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3965 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3966 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3967 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 3968 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3969 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3970 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3971 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3972 | TemplateLoc, |
| 3973 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3974 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3975 | Converted, |
| 3976 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3977 | if (Name.isNull()) |
| 3978 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3979 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3980 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3981 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3982 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3984 | // Introduce an instantiation record that describes where we are using |
| 3985 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3986 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 3987 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3988 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3989 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3991 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3992 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3993 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3994 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3995 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3996 | // Core issue 150 (assumed resolution): if this is a template template |
| 3997 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3998 | // template definition. |
| 3999 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4000 | NewArgs.addArgument(Arg); |
| 4001 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4002 | // Move to the next template parameter and argument. |
| 4003 | ++Param; |
| 4004 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4005 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4006 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4007 | // If we're performing a partial argument substitution, allow any trailing |
| 4008 | // pack expansions; they might be empty. This can happen even if |
| 4009 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 4010 | // still dependent). |
| 4011 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 4012 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4013 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 4014 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4015 | } |
| 4016 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4017 | // If we have any leftover arguments, then there were too many arguments. |
| 4018 | // Complain and fail. |
| 4019 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4020 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 4021 | |
| 4022 | // No problems found with the new argument list, propagate changes back |
| 4023 | // to caller. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 4024 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4025 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4026 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4029 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4030 | class UnnamedLocalNoLinkageFinder |
| 4031 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4032 | { |
| 4033 | Sema &S; |
| 4034 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4035 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4036 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4037 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4038 | public: |
| 4039 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 4040 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4041 | bool Visit(QualType T) { |
| 4042 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4043 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4045 | #define TYPE(Class, Parent) \ |
| 4046 | bool Visit##Class##Type(const Class##Type *); |
| 4047 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4048 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4049 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4050 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4051 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4052 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4053 | bool VisitTagDecl(const TagDecl *Tag); |
| 4054 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4055 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4056 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4057 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4058 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4059 | return false; |
| 4060 | } |
| 4061 | |
| 4062 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4063 | return Visit(T->getElementType()); |
| 4064 | } |
| 4065 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4066 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4067 | return Visit(T->getPointeeType()); |
| 4068 | } |
| 4069 | |
| 4070 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4071 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4072 | return Visit(T->getPointeeType()); |
| 4073 | } |
| 4074 | |
| 4075 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4076 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4077 | return Visit(T->getPointeeType()); |
| 4078 | } |
| 4079 | |
| 4080 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4081 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4082 | return Visit(T->getPointeeType()); |
| 4083 | } |
| 4084 | |
| 4085 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4086 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4087 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4088 | } |
| 4089 | |
| 4090 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4091 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4092 | return Visit(T->getElementType()); |
| 4093 | } |
| 4094 | |
| 4095 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4096 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4097 | return Visit(T->getElementType()); |
| 4098 | } |
| 4099 | |
| 4100 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4101 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4102 | return Visit(T->getElementType()); |
| 4103 | } |
| 4104 | |
| 4105 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4106 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4107 | return Visit(T->getElementType()); |
| 4108 | } |
| 4109 | |
| 4110 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4111 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4112 | return Visit(T->getElementType()); |
| 4113 | } |
| 4114 | |
| 4115 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4116 | return Visit(T->getElementType()); |
| 4117 | } |
| 4118 | |
| 4119 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4120 | return Visit(T->getElementType()); |
| 4121 | } |
| 4122 | |
| 4123 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4124 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4125 | for (const auto &A : T->param_types()) { |
| 4126 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4127 | return true; |
| 4128 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4129 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4130 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4134 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4135 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4139 | const UnresolvedUsingType*) { |
| 4140 | return false; |
| 4141 | } |
| 4142 | |
| 4143 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4144 | return false; |
| 4145 | } |
| 4146 | |
| 4147 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4148 | return Visit(T->getUnderlyingType()); |
| 4149 | } |
| 4150 | |
| 4151 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4152 | return false; |
| 4153 | } |
| 4154 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4155 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4156 | const UnaryTransformType*) { |
| 4157 | return false; |
| 4158 | } |
| 4159 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4160 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4161 | return Visit(T->getDeducedType()); |
| 4162 | } |
| 4163 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4164 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4165 | return VisitTagDecl(T->getDecl()); |
| 4166 | } |
| 4167 | |
| 4168 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4169 | return VisitTagDecl(T->getDecl()); |
| 4170 | } |
| 4171 | |
| 4172 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4173 | const TemplateTypeParmType*) { |
| 4174 | return false; |
| 4175 | } |
| 4176 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4177 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4178 | const SubstTemplateTypeParmPackType *) { |
| 4179 | return false; |
| 4180 | } |
| 4181 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4182 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4183 | const TemplateSpecializationType*) { |
| 4184 | return false; |
| 4185 | } |
| 4186 | |
| 4187 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4188 | const InjectedClassNameType* T) { |
| 4189 | return VisitTagDecl(T->getDecl()); |
| 4190 | } |
| 4191 | |
| 4192 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4193 | const DependentNameType* T) { |
| 4194 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4195 | } |
| 4196 | |
| 4197 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4198 | const DependentTemplateSpecializationType* T) { |
| 4199 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4200 | } |
| 4201 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4202 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4203 | const PackExpansionType* T) { |
| 4204 | return Visit(T->getPattern()); |
| 4205 | } |
| 4206 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4207 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4208 | return false; |
| 4209 | } |
| 4210 | |
| 4211 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4212 | const ObjCInterfaceType *) { |
| 4213 | return false; |
| 4214 | } |
| 4215 | |
| 4216 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4217 | const ObjCObjectPointerType *) { |
| 4218 | return false; |
| 4219 | } |
| 4220 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4221 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4222 | return Visit(T->getValueType()); |
| 4223 | } |
| 4224 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4225 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 4226 | return false; |
| 4227 | } |
| 4228 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4229 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4230 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4231 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4232 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4233 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4234 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4235 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4236 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4239 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4240 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4241 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4242 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4243 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4244 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4245 | return true; |
| 4246 | } |
| 4247 | |
| 4248 | return false; |
| 4249 | } |
| 4250 | |
| 4251 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4252 | NestedNameSpecifier *NNS) { |
| 4253 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4254 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4255 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4256 | switch (NNS->getKind()) { |
| 4257 | case NestedNameSpecifier::Identifier: |
| 4258 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4259 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4260 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4261 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4262 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4264 | case NestedNameSpecifier::TypeSpec: |
| 4265 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4266 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4267 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4268 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4269 | } |
| 4270 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4271 | /// \brief Check a template argument against its corresponding |
| 4272 | /// template type parameter. |
| 4273 | /// |
| 4274 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4275 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4277 | TypeSourceInfo *ArgInfo) { |
| 4278 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4279 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4280 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4281 | |
| 4282 | if (Arg->isVariablyModifiedType()) { |
| 4283 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4284 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4285 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4288 | // C++03 [temp.arg.type]p2: |
| 4289 | // A local type, a type with no linkage, an unnamed type or a type |
| 4290 | // compounded from any of these types shall not be used as a |
| 4291 | // template-argument for a template type-parameter. |
| 4292 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4293 | // 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] | 4294 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4295 | bool NeedsCheck; |
| 4296 | if (LangOpts.CPlusPlus11) |
| 4297 | NeedsCheck = |
| 4298 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4299 | SR.getBegin()) || |
| 4300 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4301 | SR.getBegin()); |
| 4302 | else |
| 4303 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4304 | |
| 4305 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4306 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4307 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4308 | } |
| 4309 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4310 | return false; |
| 4311 | } |
| 4312 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4313 | enum NullPointerValueKind { |
| 4314 | NPV_NotNullPointer, |
| 4315 | NPV_NullPointer, |
| 4316 | NPV_Error |
| 4317 | }; |
| 4318 | |
| 4319 | /// \brief Determine whether the given template argument is a null pointer |
| 4320 | /// value of the appropriate type. |
| 4321 | static NullPointerValueKind |
| 4322 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4323 | QualType ParamType, Expr *Arg) { |
| 4324 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4325 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4326 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4327 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 4328 | llvm_unreachable( |
| 4329 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4330 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4331 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4332 | return NPV_NotNullPointer; |
| 4333 | |
| 4334 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4335 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4336 | if (ArgRV.isInvalid()) |
| 4337 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4338 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4339 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4340 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4341 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4342 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4343 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4344 | EvalResult.HasSideEffects) { |
| 4345 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4346 | |
| 4347 | // If our only note is the usual "invalid subexpression" note, just point |
| 4348 | // the caret at its location rather than producing an essentially |
| 4349 | // redundant note. |
| 4350 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4351 | diag::note_invalid_subexpr_in_const_expr) { |
| 4352 | DiagLoc = Notes[0].first; |
| 4353 | Notes.clear(); |
| 4354 | } |
| 4355 | |
| 4356 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4357 | << Arg->getType() << Arg->getSourceRange(); |
| 4358 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4359 | S.Diag(Notes[I].first, Notes[I].second); |
| 4360 | |
| 4361 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4362 | return NPV_Error; |
| 4363 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4364 | |
| 4365 | // C++11 [temp.arg.nontype]p1: |
| 4366 | // - an address constant expression of type std::nullptr_t |
| 4367 | if (Arg->getType()->isNullPtrType()) |
| 4368 | return NPV_NullPointer; |
| 4369 | |
| 4370 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4371 | // - a constant expression that evaluates to a null member pointer value |
| 4372 | // (4.11); or |
| 4373 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4374 | (EvalResult.Val.isMemberPointer() && |
| 4375 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4376 | // If our expression has an appropriate type, we've succeeded. |
| 4377 | bool ObjCLifetimeConversion; |
| 4378 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4379 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4380 | ObjCLifetimeConversion)) |
| 4381 | return NPV_NullPointer; |
| 4382 | |
| 4383 | // The types didn't match, but we know we got a null pointer; complain, |
| 4384 | // then recover as if the types were correct. |
| 4385 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4386 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4387 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4388 | return NPV_NullPointer; |
| 4389 | } |
| 4390 | |
| 4391 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4392 | // constant, suggest a cast to the appropriate type. |
| 4393 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4394 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4395 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4396 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4397 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4398 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4399 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4400 | return NPV_NullPointer; |
| 4401 | } |
| 4402 | |
| 4403 | // FIXME: If we ever want to support general, address-constant expressions |
| 4404 | // as non-type template arguments, we should return the ExprResult here to |
| 4405 | // be interpreted by the caller. |
| 4406 | return NPV_NotNullPointer; |
| 4407 | } |
| 4408 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4409 | /// \brief Checks whether the given template argument is compatible with its |
| 4410 | /// template parameter. |
| 4411 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4412 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4413 | Expr *Arg, QualType ArgType) { |
| 4414 | bool ObjCLifetimeConversion; |
| 4415 | if (ParamType->isPointerType() && |
| 4416 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4417 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4418 | ObjCLifetimeConversion)) { |
| 4419 | // For pointer-to-object types, qualification conversions are |
| 4420 | // permitted. |
| 4421 | } else { |
| 4422 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4423 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4424 | // C++ [temp.arg.nontype]p5b3: |
| 4425 | // For a non-type template-parameter of type reference to |
| 4426 | // object, no conversions apply. The type referred to by the |
| 4427 | // reference may be more cv-qualified than the (otherwise |
| 4428 | // identical) type of the template- argument. The |
| 4429 | // template-parameter is bound directly to the |
| 4430 | // template-argument, which shall be an lvalue. |
| 4431 | |
| 4432 | // FIXME: Other qualifiers? |
| 4433 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4434 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4435 | |
| 4436 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4437 | S.Diag(Arg->getLocStart(), |
| 4438 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4439 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4440 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4441 | return true; |
| 4442 | } |
| 4443 | } |
| 4444 | } |
| 4445 | |
| 4446 | // At this point, the template argument refers to an object or |
| 4447 | // function with external linkage. We now need to check whether the |
| 4448 | // argument and parameter types are compatible. |
| 4449 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4450 | ParamType.getNonReferenceType())) { |
| 4451 | // We can't perform this conversion or binding. |
| 4452 | if (ParamType->isReferenceType()) |
| 4453 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4454 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4455 | else |
| 4456 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4457 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4458 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4459 | return true; |
| 4460 | } |
| 4461 | } |
| 4462 | |
| 4463 | return false; |
| 4464 | } |
| 4465 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4466 | /// \brief Checks whether the given template argument is the address |
| 4467 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4468 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4469 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4470 | NonTypeTemplateParmDecl *Param, |
| 4471 | QualType ParamType, |
| 4472 | Expr *ArgIn, |
| 4473 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4474 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4475 | Expr *Arg = ArgIn; |
| 4476 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4477 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4478 | bool AddressTaken = false; |
| 4479 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4480 | if (S.getLangOpts().MicrosoftExt) { |
| 4481 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4482 | // dereference and address-of operators. |
| 4483 | Arg = Arg->IgnoreParenCasts(); |
| 4484 | |
| 4485 | bool ExtWarnMSTemplateArg = false; |
| 4486 | UnaryOperatorKind FirstOpKind; |
| 4487 | SourceLocation FirstOpLoc; |
| 4488 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4489 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4490 | if (UnOpKind == UO_Deref) |
| 4491 | ExtWarnMSTemplateArg = true; |
| 4492 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4493 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4494 | if (!AddrOpLoc.isValid()) { |
| 4495 | FirstOpKind = UnOpKind; |
| 4496 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4497 | } |
| 4498 | } else |
| 4499 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4500 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4501 | if (FirstOpLoc.isValid()) { |
| 4502 | if (ExtWarnMSTemplateArg) |
| 4503 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4504 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4505 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4506 | if (FirstOpKind == UO_AddrOf) |
| 4507 | AddressTaken = true; |
| 4508 | else if (Arg->getType()->isPointerType()) { |
| 4509 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4510 | // constant expression. |
| 4511 | assert(FirstOpKind == UO_Deref); |
| 4512 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4513 | << Arg->getSourceRange(); |
| 4514 | } |
| 4515 | } |
| 4516 | } else { |
| 4517 | // See through any implicit casts we added to fix the type. |
| 4518 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4519 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4520 | // C++ [temp.arg.nontype]p1: |
| 4521 | // |
| 4522 | // A template-argument for a non-type, non-template |
| 4523 | // template-parameter shall be one of: [...] |
| 4524 | // |
| 4525 | // -- the address of an object or function with external |
| 4526 | // linkage, including function templates and function |
| 4527 | // template-ids but excluding non-static class members, |
| 4528 | // expressed as & id-expression where the & is optional if |
| 4529 | // the name refers to a function or array, or if the |
| 4530 | // corresponding template-parameter is a reference; or |
| 4531 | |
| 4532 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4533 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4534 | bool ExtraParens = false; |
| 4535 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4536 | if (!Invalid && !ExtraParens) { |
| 4537 | S.Diag(Arg->getLocStart(), |
| 4538 | S.getLangOpts().CPlusPlus11 |
| 4539 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4540 | : diag::ext_template_arg_extra_parens) |
| 4541 | << Arg->getSourceRange(); |
| 4542 | ExtraParens = true; |
| 4543 | } |
| 4544 | |
| 4545 | Arg = Parens->getSubExpr(); |
| 4546 | } |
| 4547 | |
| 4548 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4549 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4550 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4551 | |
| 4552 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4553 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4554 | Arg = UnOp->getSubExpr(); |
| 4555 | AddressTaken = true; |
| 4556 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4557 | } |
| 4558 | } |
| 4559 | |
| 4560 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4561 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4562 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4563 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4564 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4565 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4566 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4567 | |
| 4568 | // If our parameter has pointer type, check for a null template value. |
| 4569 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4570 | NullPointerValueKind NPV; |
| 4571 | // dllimport'd entities aren't constant but are available inside of template |
| 4572 | // arguments. |
| 4573 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4574 | NPV = NPV_NotNullPointer; |
| 4575 | else |
| 4576 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4577 | switch (NPV) { |
| 4578 | case NPV_NullPointer: |
| 4579 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4580 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4581 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4582 | return false; |
| 4583 | |
| 4584 | case NPV_Error: |
| 4585 | return true; |
| 4586 | |
| 4587 | case NPV_NotNullPointer: |
| 4588 | break; |
| 4589 | } |
| 4590 | } |
| 4591 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4592 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4593 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4594 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4595 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4596 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4597 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4598 | |
| 4599 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4600 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4601 | ArgIn, Arg, ArgType)) |
| 4602 | return true; |
| 4603 | |
| 4604 | Converted = TemplateArgument(ArgIn); |
| 4605 | return false; |
| 4606 | } |
| 4607 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4608 | if (!DRE) { |
| 4609 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4610 | << Arg->getSourceRange(); |
| 4611 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4612 | return true; |
| 4613 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4614 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4615 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4616 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4617 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4618 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4619 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4620 | return true; |
| 4621 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4622 | |
| 4623 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4624 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4625 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4626 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4627 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4628 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4629 | return true; |
| 4630 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4631 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4632 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4633 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4634 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4635 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4636 | // A non-type template argument must refer to an object or function. |
| 4637 | if (!Func && !Var) { |
| 4638 | // We found something, but we don't know specifically what it is. |
| 4639 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4640 | << Arg->getSourceRange(); |
| 4641 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4642 | return true; |
| 4643 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4644 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4645 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4646 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4647 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4648 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4649 | diag::ext_template_arg_object_internal) |
| 4650 | << !Func << Entity << Arg->getSourceRange(); |
| 4651 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4652 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4653 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4654 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4655 | << !Func << Entity << Arg->getSourceRange(); |
| 4656 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4657 | << !Func; |
| 4658 | return true; |
| 4659 | } |
| 4660 | |
| 4661 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4662 | // If the template parameter has pointer type, the function decays. |
| 4663 | if (ParamType->isPointerType() && !AddressTaken) |
| 4664 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4665 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4666 | // If we originally had an address-of operator, but the |
| 4667 | // parameter has reference type, complain and (if things look |
| 4668 | // like they will work) drop the address-of operator. |
| 4669 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4670 | ParamType.getNonReferenceType())) { |
| 4671 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4672 | << ParamType; |
| 4673 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4674 | return true; |
| 4675 | } |
| 4676 | |
| 4677 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4678 | << ParamType |
| 4679 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4680 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4681 | |
| 4682 | ArgType = Func->getType(); |
| 4683 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4684 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4685 | // A value of reference type is not an object. |
| 4686 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4687 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4688 | diag::err_template_arg_reference_var) |
| 4689 | << Var->getType() << Arg->getSourceRange(); |
| 4690 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4691 | return true; |
| 4692 | } |
| 4693 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4694 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4695 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4696 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4697 | << Arg->getSourceRange(); |
| 4698 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4699 | return true; |
| 4700 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4701 | |
| 4702 | // If the template parameter has pointer type, we must have taken |
| 4703 | // the address of this object. |
| 4704 | if (ParamType->isReferenceType()) { |
| 4705 | if (AddressTaken) { |
| 4706 | // If we originally had an address-of operator, but the |
| 4707 | // parameter has reference type, complain and (if things look |
| 4708 | // like they will work) drop the address-of operator. |
| 4709 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4710 | ParamType.getNonReferenceType())) { |
| 4711 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4712 | << ParamType; |
| 4713 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4714 | return true; |
| 4715 | } |
| 4716 | |
| 4717 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4718 | << ParamType |
| 4719 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4720 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4721 | |
| 4722 | ArgType = Var->getType(); |
| 4723 | } |
| 4724 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4725 | if (Var->getType()->isArrayType()) { |
| 4726 | // Array-to-pointer decay. |
| 4727 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4728 | } else { |
| 4729 | // If the template parameter has pointer type but the address of |
| 4730 | // this object was not taken, complain and (possibly) recover by |
| 4731 | // taking the address of the entity. |
| 4732 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4733 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4734 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4735 | << ParamType; |
| 4736 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4737 | return true; |
| 4738 | } |
| 4739 | |
| 4740 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4741 | << ParamType |
| 4742 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4743 | |
| 4744 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4745 | } |
| 4746 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4747 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4748 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4749 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4750 | Arg, ArgType)) |
| 4751 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4752 | |
| 4753 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4754 | Converted = |
| 4755 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4756 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4757 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | /// \brief Checks whether the given template argument is a pointer to |
| 4761 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4762 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4763 | NonTypeTemplateParmDecl *Param, |
| 4764 | QualType ParamType, |
| 4765 | Expr *&ResultArg, |
| 4766 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4767 | bool Invalid = false; |
| 4768 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4769 | // Check for a null pointer value. |
| 4770 | Expr *Arg = ResultArg; |
| 4771 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4772 | case NPV_Error: |
| 4773 | return true; |
| 4774 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4775 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4776 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4777 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4778 | return false; |
| 4779 | case NPV_NotNullPointer: |
| 4780 | break; |
| 4781 | } |
| 4782 | |
| 4783 | bool ObjCLifetimeConversion; |
| 4784 | if (S.IsQualificationConversion(Arg->getType(), |
| 4785 | ParamType.getNonReferenceType(), |
| 4786 | false, ObjCLifetimeConversion)) { |
| 4787 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4788 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4789 | ResultArg = Arg; |
| 4790 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4791 | ParamType.getNonReferenceType())) { |
| 4792 | // We can't perform this conversion. |
| 4793 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4794 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4795 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4796 | return true; |
| 4797 | } |
| 4798 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4799 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4800 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4801 | Arg = Cast->getSubExpr(); |
| 4802 | |
| 4803 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4804 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4805 | // A template-argument for a non-type, non-template |
| 4806 | // template-parameter shall be one of: [...] |
| 4807 | // |
| 4808 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4809 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4810 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4811 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4812 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4813 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4814 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4815 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4816 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4817 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4818 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4819 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4820 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4821 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | Arg = Parens->getSubExpr(); |
| 4825 | } |
| 4826 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4827 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4828 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4829 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4830 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4831 | // A pointer-to-member constant written &Class::member. |
| 4832 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4833 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4834 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4835 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4836 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4837 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4838 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4839 | // A constant of pointer-to-member type. |
| 4840 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4841 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4842 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4843 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4844 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4845 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4846 | } else { |
| 4847 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4848 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4849 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4850 | return Invalid; |
| 4851 | } |
| 4852 | } |
| 4853 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4854 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4855 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4856 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4857 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4858 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4859 | return S.Diag(Arg->getLocStart(), |
| 4860 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4861 | << Arg->getSourceRange(); |
| 4862 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4863 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4864 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4865 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4866 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4867 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4868 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4869 | "Only non-static member pointers can make it here"); |
| 4870 | |
| 4871 | // Okay: this is the address of a non-static member, and therefore |
| 4872 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4873 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4874 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4875 | } else { |
| 4876 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4877 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4878 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4879 | return Invalid; |
| 4880 | } |
| 4881 | |
| 4882 | // 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] | 4883 | S.Diag(Arg->getLocStart(), |
| 4884 | diag::err_template_arg_not_pointer_to_member_form) |
| 4885 | << Arg->getSourceRange(); |
| 4886 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4887 | return true; |
| 4888 | } |
| 4889 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4890 | /// \brief Check a template argument against its corresponding |
| 4891 | /// non-type template parameter. |
| 4892 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4893 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4894 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4895 | /// returns the converted template argument. \p ParamType is the |
| 4896 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4897 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4898 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4899 | TemplateArgument &Converted, |
| 4900 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4901 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4903 | // If either the parameter has a dependent type or the argument is |
| 4904 | // type-dependent, there's nothing we can check now. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4905 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4906 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4907 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4908 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4909 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4910 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4911 | // We should have already dropped all cv-qualifiers by now. |
| 4912 | assert(!ParamType.hasQualifiers() && |
| 4913 | "non-type template parameter type cannot be qualified"); |
| 4914 | |
| 4915 | if (CTAK == CTAK_Deduced && |
| 4916 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4917 | // C++ [temp.deduct.type]p17: |
| 4918 | // If, in the declaration of a function template with a non-type |
| 4919 | // template-parameter, the non-type template-parameter is used |
| 4920 | // in an expression in the function parameter-list and, if the |
| 4921 | // corresponding template-argument is deduced, the |
| 4922 | // template-argument type shall match the type of the |
| 4923 | // template-parameter exactly, except that a template-argument |
| 4924 | // deduced from an array bound may be of any integral type. |
| 4925 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4926 | << Arg->getType().getUnqualifiedType() |
| 4927 | << ParamType.getUnqualifiedType(); |
| 4928 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4929 | return ExprError(); |
| 4930 | } |
| 4931 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4932 | if (getLangOpts().CPlusPlus1z) { |
| 4933 | // FIXME: We can do some limited checking for a value-dependent but not |
| 4934 | // type-dependent argument. |
| 4935 | if (Arg->isValueDependent()) { |
| 4936 | Converted = TemplateArgument(Arg); |
| 4937 | return Arg; |
| 4938 | } |
| 4939 | |
| 4940 | // C++1z [temp.arg.nontype]p1: |
| 4941 | // A template-argument for a non-type template parameter shall be |
| 4942 | // a converted constant expression of the type of the template-parameter. |
| 4943 | APValue Value; |
| 4944 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 4945 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 4946 | if (ArgResult.isInvalid()) |
| 4947 | return ExprError(); |
| 4948 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4949 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 4950 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4951 | // Convert the APValue to a TemplateArgument. |
| 4952 | switch (Value.getKind()) { |
| 4953 | case APValue::Uninitialized: |
| 4954 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4955 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4956 | break; |
| 4957 | case APValue::Int: |
| 4958 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4959 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4960 | break; |
| 4961 | case APValue::MemberPointer: { |
| 4962 | assert(ParamType->isMemberPointerType()); |
| 4963 | |
| 4964 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 4965 | if (!Value.getMemberPointerPath().empty()) { |
| 4966 | Diag(Arg->getLocStart(), |
| 4967 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 4968 | << Value.getMemberPointerDecl() << ParamType |
| 4969 | << Arg->getSourceRange(); |
| 4970 | return ExprError(); |
| 4971 | } |
| 4972 | |
| 4973 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4974 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4975 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4976 | break; |
| 4977 | } |
| 4978 | case APValue::LValue: { |
| 4979 | // For a non-type template-parameter of pointer or reference type, |
| 4980 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4981 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 4982 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4983 | // -- a temporary object |
| 4984 | // -- a string literal |
| 4985 | // -- the result of a typeid expression, or |
| 4986 | // -- a predefind __func__ variable |
| 4987 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 4988 | if (isa<CXXUuidofExpr>(E)) { |
| 4989 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 4990 | break; |
| 4991 | } |
| 4992 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4993 | << Arg->getSourceRange(); |
| 4994 | return ExprError(); |
| 4995 | } |
| 4996 | auto *VD = const_cast<ValueDecl *>( |
| 4997 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 4998 | // -- a subobject |
| 4999 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 5000 | VD && VD->getType()->isArrayType() && |
| 5001 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 5002 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 5003 | // Per defect report (no number yet): |
| 5004 | // ... other than a pointer to the first element of a complete array |
| 5005 | // object. |
| 5006 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 5007 | Value.isLValueOnePastTheEnd()) { |
| 5008 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 5009 | << Value.getAsString(Context, ParamType); |
| 5010 | return ExprError(); |
| 5011 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5012 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5013 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5014 | assert((!VD || !ParamType->isNullPtrType()) && |
| 5015 | "non-null value of type nullptr_t?"); |
| 5016 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5017 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5018 | break; |
| 5019 | } |
| 5020 | case APValue::AddrLabelDiff: |
| 5021 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 5022 | case APValue::Float: |
| 5023 | case APValue::ComplexInt: |
| 5024 | case APValue::ComplexFloat: |
| 5025 | case APValue::Vector: |
| 5026 | case APValue::Array: |
| 5027 | case APValue::Struct: |
| 5028 | case APValue::Union: |
| 5029 | llvm_unreachable("invalid kind for template argument"); |
| 5030 | } |
| 5031 | |
| 5032 | return ArgResult.get(); |
| 5033 | } |
| 5034 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5035 | // C++ [temp.arg.nontype]p5: |
| 5036 | // The following conversions are performed on each expression used |
| 5037 | // as a non-type template-argument. If a non-type |
| 5038 | // template-argument cannot be converted to the type of the |
| 5039 | // corresponding template-parameter then the program is |
| 5040 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5041 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5042 | // C++11: |
| 5043 | // -- for a non-type template-parameter of integral or |
| 5044 | // enumeration type, conversions permitted in a converted |
| 5045 | // constant expression are applied. |
| 5046 | // |
| 5047 | // C++98: |
| 5048 | // -- for a non-type template-parameter of integral or |
| 5049 | // enumeration type, integral promotions (4.5) and integral |
| 5050 | // conversions (4.7) are applied. |
| 5051 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5052 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5053 | // We can't check arbitrary value-dependent arguments. |
| 5054 | // FIXME: If there's no viable conversion to the template parameter type, |
| 5055 | // we should be able to diagnose that prior to instantiation. |
| 5056 | if (Arg->isValueDependent()) { |
| 5057 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5058 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | // C++ [temp.arg.nontype]p1: |
| 5062 | // A template-argument for a non-type, non-template template-parameter |
| 5063 | // shall be one of: |
| 5064 | // |
| 5065 | // -- for a non-type template-parameter of integral or enumeration |
| 5066 | // type, a converted constant expression of the type of the |
| 5067 | // template-parameter; or |
| 5068 | llvm::APSInt Value; |
| 5069 | ExprResult ArgResult = |
| 5070 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5071 | CCEK_TemplateArg); |
| 5072 | if (ArgResult.isInvalid()) |
| 5073 | return ExprError(); |
| 5074 | |
| 5075 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5076 | // always a no-op, except when the parameter type is bool. In |
| 5077 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5078 | QualType IntegerType = ParamType; |
| 5079 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5080 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5081 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5082 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5083 | Converted = TemplateArgument(Context, Value, |
| 5084 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5085 | return ArgResult; |
| 5086 | } |
| 5087 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5088 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5089 | if (ArgResult.isInvalid()) |
| 5090 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5091 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5092 | |
| 5093 | QualType ArgType = Arg->getType(); |
| 5094 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5095 | // C++ [temp.arg.nontype]p1: |
| 5096 | // A template-argument for a non-type, non-template |
| 5097 | // template-parameter shall be one of: |
| 5098 | // |
| 5099 | // -- an integral constant-expression of integral or enumeration |
| 5100 | // type; or |
| 5101 | // -- the name of a non-type template-parameter; or |
| 5102 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5103 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5104 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5105 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5106 | diag::err_template_arg_not_integral_or_enumeral) |
| 5107 | << ArgType << Arg->getSourceRange(); |
| 5108 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5109 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5110 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5111 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5112 | QualType T; |
| 5113 | |
| 5114 | public: |
| 5115 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5116 | |
| 5117 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5118 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5119 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5120 | } |
| 5121 | } Diagnoser(ArgType); |
| 5122 | |
| 5123 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5124 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5125 | if (!Arg) |
| 5126 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5129 | // From here on out, all we care about is the unqualified form |
| 5130 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5131 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5132 | |
| 5133 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5134 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5135 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5136 | } else if (ParamType->isBooleanType()) { |
| 5137 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5138 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5139 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5140 | !ParamType->isEnumeralType()) { |
| 5141 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5142 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5143 | } else { |
| 5144 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5145 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5146 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5147 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5148 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5149 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5152 | // Add the value of this argument to the list of converted |
| 5153 | // arguments. We use the bitwidth and signedness of the template |
| 5154 | // parameter. |
| 5155 | if (Arg->isValueDependent()) { |
| 5156 | // The argument is value-dependent. Create a new |
| 5157 | // TemplateArgument with the converted expression. |
| 5158 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5159 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5162 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5163 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5164 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5165 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5166 | if (ParamType->isBooleanType()) { |
| 5167 | // Value must be zero or one. |
| 5168 | Value = Value != 0; |
| 5169 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5170 | if (Value.getBitWidth() != AllowedBits) |
| 5171 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5172 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5173 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5174 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5175 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5176 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5177 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5178 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5179 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5180 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5181 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5182 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5183 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5184 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5185 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5186 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5187 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5188 | << Arg->getSourceRange(); |
| 5189 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5190 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5191 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5192 | // Complain if we overflowed the template parameter's type. |
| 5193 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5194 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5195 | RequiredBits = OldValue.getActiveBits(); |
| 5196 | else if (OldValue.isUnsigned()) |
| 5197 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5198 | else |
| 5199 | RequiredBits = OldValue.getMinSignedBits(); |
| 5200 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5201 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5202 | diag::warn_template_arg_too_large) |
| 5203 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5204 | << Arg->getSourceRange(); |
| 5205 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5206 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5207 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5208 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5209 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5210 | ParamType->isEnumeralType() |
| 5211 | ? Context.getCanonicalType(ParamType) |
| 5212 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5213 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5214 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5215 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5216 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5217 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5218 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5219 | // Handle pointer-to-function, reference-to-function, and |
| 5220 | // pointer-to-member-function all in (roughly) the same way. |
| 5221 | if (// -- For a non-type template-parameter of type pointer to |
| 5222 | // function, only the function-to-pointer conversion (4.3) is |
| 5223 | // applied. If the template-argument represents a set of |
| 5224 | // overloaded functions (or a pointer to such), the matching |
| 5225 | // function is selected from the set (13.4). |
| 5226 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5227 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5228 | // -- For a non-type template-parameter of type reference to |
| 5229 | // function, no conversions apply. If the template-argument |
| 5230 | // represents a set of overloaded functions, the matching |
| 5231 | // function is selected from the set (13.4). |
| 5232 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5233 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5234 | // -- For a non-type template-parameter of type pointer to |
| 5235 | // member function, no conversions apply. If the |
| 5236 | // template-argument represents a set of overloaded member |
| 5237 | // functions, the matching member function is selected from |
| 5238 | // the set (13.4). |
| 5239 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5240 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5241 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5242 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5243 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5244 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5245 | true, |
| 5246 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5247 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5248 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5249 | |
| 5250 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5251 | ArgType = Arg->getType(); |
| 5252 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5253 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5254 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5255 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5256 | if (!ParamType->isMemberPointerType()) { |
| 5257 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5258 | ParamType, |
| 5259 | Arg, Converted)) |
| 5260 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5261 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5262 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5263 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5264 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5265 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5266 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5267 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5270 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5271 | // -- for a non-type template-parameter of type pointer to |
| 5272 | // object, qualification conversions (4.4) and the |
| 5273 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5274 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5275 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5276 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5277 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5278 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5279 | ParamType, |
| 5280 | Arg, Converted)) |
| 5281 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5282 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5283 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5285 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5286 | // -- For a non-type template-parameter of type reference to |
| 5287 | // object, no conversions apply. The type referred to by the |
| 5288 | // reference may be more cv-qualified than the (otherwise |
| 5289 | // identical) type of the template-argument. The |
| 5290 | // template-parameter is bound directly to the |
| 5291 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5292 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5293 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5294 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5295 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5296 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5297 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5298 | true, |
| 5299 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5300 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5301 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5302 | |
| 5303 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5304 | ArgType = Arg->getType(); |
| 5305 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5306 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5307 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5308 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5309 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5310 | ParamType, |
| 5311 | Arg, Converted)) |
| 5312 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5313 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5314 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5315 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5316 | // Deal with parameters of type std::nullptr_t. |
| 5317 | if (ParamType->isNullPtrType()) { |
| 5318 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5319 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5320 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5321 | } |
| 5322 | |
| 5323 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5324 | case NPV_NotNullPointer: |
| 5325 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5326 | << Arg->getType() << ParamType; |
| 5327 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5328 | return ExprError(); |
| 5329 | |
| 5330 | case NPV_Error: |
| 5331 | return ExprError(); |
| 5332 | |
| 5333 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5334 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5335 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5336 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5337 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5338 | } |
| 5339 | } |
| 5340 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5341 | // -- For a non-type template-parameter of type pointer to data |
| 5342 | // member, qualification conversions (4.4) are applied. |
| 5343 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5344 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5345 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5346 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5347 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5348 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
| 5351 | /// \brief Check a template argument against its corresponding |
| 5352 | /// template template parameter. |
| 5353 | /// |
| 5354 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5355 | /// It returns true if an error occurred, and false otherwise. |
| 5356 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5357 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5358 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5359 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5360 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5361 | if (!Template) { |
| 5362 | // Any dependent template name is fine. |
| 5363 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5364 | return false; |
| 5365 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5366 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5367 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5368 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5369 | // the name of a class template or an alias template, expressed as an |
| 5370 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5371 | // primary class templates are considered when matching the |
| 5372 | // template template argument with the corresponding parameter; |
| 5373 | // partial specializations are not considered even if their |
| 5374 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5375 | // |
| 5376 | // Note that we also allow template template parameters here, which |
| 5377 | // will happen when we are dealing with, e.g., class template |
| 5378 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5379 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5380 | !isa<TemplateTemplateParmDecl>(Template) && |
| 5381 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5382 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5383 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 5384 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5385 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5386 | << Template; |
| 5387 | } |
| 5388 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5389 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5390 | if (Param->isExpandedParameterPack()) |
| 5391 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5392 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5393 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5394 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5395 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5396 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5397 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5400 | /// \brief Given a non-type template argument that refers to a |
| 5401 | /// declaration and the type of its corresponding non-type template |
| 5402 | /// parameter, produce an expression that properly refers to that |
| 5403 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5404 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5405 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5406 | QualType ParamType, |
| 5407 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5408 | // C++ [temp.param]p8: |
| 5409 | // |
| 5410 | // A non-type template-parameter of type "array of T" or |
| 5411 | // "function returning T" is adjusted to be of type "pointer to |
| 5412 | // T" or "pointer to function returning T", respectively. |
| 5413 | if (ParamType->isArrayType()) |
| 5414 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5415 | else if (ParamType->isFunctionType()) |
| 5416 | ParamType = Context.getPointerType(ParamType); |
| 5417 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5418 | // For a NULL non-type template argument, return nullptr casted to the |
| 5419 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5420 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5421 | return ImpCastExprToType( |
| 5422 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5423 | ParamType, |
| 5424 | ParamType->getAs<MemberPointerType>() |
| 5425 | ? CK_NullToMemberPointer |
| 5426 | : CK_NullToPointer); |
| 5427 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5428 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5429 | "Only declaration template arguments permitted here"); |
| 5430 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5431 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5432 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5433 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5434 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5435 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5436 | // If the value is a class member, we might have a pointer-to-member. |
| 5437 | // Determine whether the non-type template template parameter is of |
| 5438 | // pointer-to-member type. If so, we need to build an appropriate |
| 5439 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5440 | // would refer to the member itself. |
| 5441 | if (ParamType->isMemberPointerType()) { |
| 5442 | QualType ClassType |
| 5443 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5444 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5445 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5446 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5447 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5448 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5449 | |
| 5450 | // The actual value-ness of this is unimportant, but for |
| 5451 | // internal consistency's sake, references to instance methods |
| 5452 | // are r-values. |
| 5453 | ExprValueKind VK = VK_LValue; |
| 5454 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5455 | VK = VK_RValue; |
| 5456 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5457 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5458 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5459 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5460 | Loc, |
| 5461 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5462 | if (RefExpr.isInvalid()) |
| 5463 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5464 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5465 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5466 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5467 | // We might need to perform a trailing qualification conversion, since |
| 5468 | // the element type on the parameter could be more qualified than the |
| 5469 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5470 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5471 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5472 | ParamType.getUnqualifiedType(), false, |
| 5473 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5474 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5475 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5476 | assert(!RefExpr.isInvalid() && |
| 5477 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5478 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5479 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5480 | } |
| 5481 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5482 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5483 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5484 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5485 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5486 | // When the non-type template parameter is a pointer, take the |
| 5487 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5488 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5489 | if (RefExpr.isInvalid()) |
| 5490 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5491 | |
| 5492 | if (T->isFunctionType() || T->isArrayType()) { |
| 5493 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5494 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5495 | if (RefExpr.isInvalid()) |
| 5496 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5497 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5498 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5500 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5501 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5502 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5503 | } |
| 5504 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5505 | ExprValueKind VK = VK_RValue; |
| 5506 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5507 | // If the non-type template parameter has reference type, qualify the |
| 5508 | // resulting declaration reference with the extra qualifiers on the |
| 5509 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5510 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5511 | VK = VK_LValue; |
| 5512 | T = Context.getQualifiedType(T, |
| 5513 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5514 | } else if (isa<FunctionDecl>(VD)) { |
| 5515 | // References to functions are always lvalues. |
| 5516 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5518 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5519 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5520 | } |
| 5521 | |
| 5522 | /// \brief Construct a new expression that refers to the given |
| 5523 | /// integral template argument with the given source-location |
| 5524 | /// information. |
| 5525 | /// |
| 5526 | /// This routine takes care of the mapping from an integral template |
| 5527 | /// argument (which may have any integral type) to the appropriate |
| 5528 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5529 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5530 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5531 | SourceLocation Loc) { |
| 5532 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5533 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5534 | QualType OrigT = Arg.getIntegralType(); |
| 5535 | |
| 5536 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5537 | // type the same size as the enumerator. We don't want to build an |
| 5538 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5539 | // any integral type with C++11 enum classes, make sure we create the right |
| 5540 | // type of literal for it. |
| 5541 | QualType T = OrigT; |
| 5542 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5543 | T = ET->getDecl()->getIntegerType(); |
| 5544 | |
| 5545 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5546 | if (T->isAnyCharacterType()) { |
Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 5547 | // This does not need to handle u8 character literals because those are |
| 5548 | // 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] | 5549 | CharacterLiteral::CharacterKind Kind; |
| 5550 | if (T->isWideCharType()) |
| 5551 | Kind = CharacterLiteral::Wide; |
| 5552 | else if (T->isChar16Type()) |
| 5553 | Kind = CharacterLiteral::UTF16; |
| 5554 | else if (T->isChar32Type()) |
| 5555 | Kind = CharacterLiteral::UTF32; |
| 5556 | else |
| 5557 | Kind = CharacterLiteral::Ascii; |
| 5558 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5559 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5560 | Kind, T, Loc); |
| 5561 | } else if (T->isBooleanType()) { |
| 5562 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5563 | T, Loc); |
| 5564 | } else if (T->isNullPtrType()) { |
| 5565 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5566 | } else { |
| 5567 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5570 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5571 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5572 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5573 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5574 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5575 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5576 | Loc, Loc); |
| 5577 | } |
| 5578 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5579 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5580 | } |
| 5581 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5582 | /// \brief Match two template parameters within template parameter lists. |
| 5583 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5584 | bool Complain, |
| 5585 | Sema::TemplateParameterListEqualKind Kind, |
| 5586 | SourceLocation TemplateArgLoc) { |
| 5587 | // Check the actual kind (type, non-type, template). |
| 5588 | if (Old->getKind() != New->getKind()) { |
| 5589 | if (Complain) { |
| 5590 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5591 | if (TemplateArgLoc.isValid()) { |
| 5592 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5593 | NextDiag = diag::note_template_param_different_kind; |
| 5594 | } |
| 5595 | S.Diag(New->getLocation(), NextDiag) |
| 5596 | << (Kind != Sema::TPL_TemplateMatch); |
| 5597 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5598 | << (Kind != Sema::TPL_TemplateMatch); |
| 5599 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5600 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5601 | return false; |
| 5602 | } |
| 5603 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5604 | // Check that both are parameter packs are neither are parameter packs. |
| 5605 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5606 | // template template parameter, the template template parameter can have |
| 5607 | // a parameter pack where the template template argument does not. |
| 5608 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5609 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5610 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5611 | if (Complain) { |
| 5612 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5613 | if (TemplateArgLoc.isValid()) { |
| 5614 | S.Diag(TemplateArgLoc, |
| 5615 | diag::err_template_arg_template_params_mismatch); |
| 5616 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5617 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5618 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5619 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5620 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5621 | : 2; |
| 5622 | S.Diag(New->getLocation(), NextDiag) |
| 5623 | << ParamKind << New->isParameterPack(); |
| 5624 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5625 | << ParamKind << Old->isParameterPack(); |
| 5626 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5627 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5628 | return false; |
| 5629 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5630 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5631 | // For non-type template parameters, check the type of the parameter. |
| 5632 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5633 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5634 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5635 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5636 | // If we are matching a template template argument to a template |
| 5637 | // template parameter and one of the non-type template parameter types |
| 5638 | // is dependent, then we must wait until template instantiation time |
| 5639 | // to actually compare the arguments. |
| 5640 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5641 | (OldNTTP->getType()->isDependentType() || |
| 5642 | NewNTTP->getType()->isDependentType())) |
| 5643 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5644 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5645 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5646 | if (Complain) { |
| 5647 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5648 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5649 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5650 | diag::err_template_arg_template_params_mismatch); |
| 5651 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5652 | } |
| 5653 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5654 | << NewNTTP->getType() |
| 5655 | << (Kind != Sema::TPL_TemplateMatch); |
| 5656 | S.Diag(OldNTTP->getLocation(), |
| 5657 | diag::note_template_nontype_parm_prev_declaration) |
| 5658 | << OldNTTP->getType(); |
| 5659 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5660 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5661 | return false; |
| 5662 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5663 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5664 | return true; |
| 5665 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5666 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5667 | // For template template parameters, check the template parameter types. |
| 5668 | // The template parameter lists of template template |
| 5669 | // parameters must agree. |
| 5670 | if (TemplateTemplateParmDecl *OldTTP |
| 5671 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5672 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5673 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5674 | OldTTP->getTemplateParameters(), |
| 5675 | Complain, |
| 5676 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5677 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5678 | : Kind), |
| 5679 | TemplateArgLoc); |
| 5680 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5681 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5682 | return true; |
| 5683 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5684 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5685 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5686 | /// lists. |
| 5687 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5688 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5689 | TemplateParameterList *New, |
| 5690 | TemplateParameterList *Old, |
| 5691 | Sema::TemplateParameterListEqualKind Kind, |
| 5692 | SourceLocation TemplateArgLoc) { |
| 5693 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5694 | if (TemplateArgLoc.isValid()) { |
| 5695 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5696 | NextDiag = diag::note_template_param_list_different_arity; |
| 5697 | } |
| 5698 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5699 | << (New->size() > Old->size()) |
| 5700 | << (Kind != Sema::TPL_TemplateMatch) |
| 5701 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5702 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5703 | << (Kind != Sema::TPL_TemplateMatch) |
| 5704 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5705 | } |
| 5706 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5707 | /// \brief Determine whether the given template parameter lists are |
| 5708 | /// equivalent. |
| 5709 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5710 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5711 | /// source code as part of a new template declaration. |
| 5712 | /// |
| 5713 | /// \param Old The old template parameter list, typically found via |
| 5714 | /// name lookup of the template declared with this template parameter |
| 5715 | /// list. |
| 5716 | /// |
| 5717 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5718 | /// the template parameter lists are not equivalent. |
| 5719 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5720 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5721 | /// |
| 5722 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5723 | /// are actually checking the template parameter list of a template |
| 5724 | /// argument (New) against the template parameter list of its |
| 5725 | /// corresponding template template parameter (Old). We produce |
| 5726 | /// slightly different diagnostics in this scenario. |
| 5727 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5728 | /// \returns True if the template parameter lists are equal, false |
| 5729 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5730 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5731 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5732 | TemplateParameterList *Old, |
| 5733 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5734 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5735 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5736 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5737 | if (Complain) |
| 5738 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5739 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5740 | |
| 5741 | return false; |
| 5742 | } |
| 5743 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5744 | // C++0x [temp.arg.template]p3: |
| 5745 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5746 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5747 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5748 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5749 | // template-parameter-list of P. [...] |
| 5750 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5751 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5752 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5753 | OldParmEnd = Old->end(); |
| 5754 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5755 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5756 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5757 | if (NewParm == NewParmEnd) { |
| 5758 | if (Complain) |
| 5759 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5760 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5761 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5762 | return false; |
| 5763 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5764 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5765 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5766 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5767 | return false; |
| 5768 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5769 | ++NewParm; |
| 5770 | continue; |
| 5771 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5772 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5773 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5774 | // [...] When P's template- parameter-list contains a template parameter |
| 5775 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5776 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5777 | // template-parameter-list of A with the same type and form as the |
| 5778 | // template parameter pack in P (ignoring whether those template |
| 5779 | // parameters are template parameter packs). |
| 5780 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5781 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5782 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5783 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5784 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5785 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5786 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5787 | // Make sure we exhausted all of the arguments. |
| 5788 | if (NewParm != NewParmEnd) { |
| 5789 | if (Complain) |
| 5790 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5791 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5792 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5793 | return false; |
| 5794 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5795 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5796 | return true; |
| 5797 | } |
| 5798 | |
| 5799 | /// \brief Check whether a template can be declared within this scope. |
| 5800 | /// |
| 5801 | /// If the template declaration is valid in this scope, returns |
| 5802 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5803 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5804 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5805 | if (!S) |
| 5806 | return false; |
| 5807 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5808 | // Find the nearest enclosing declaration scope. |
| 5809 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5810 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5811 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5812 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5813 | // C++ [temp]p4: |
| 5814 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5815 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5816 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5817 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5818 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5819 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5820 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5821 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5822 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5823 | // C++ [temp]p2: |
| 5824 | // A template-declaration can appear only as a namespace scope or |
| 5825 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5826 | if (Ctx) { |
| 5827 | if (Ctx->isFileContext()) |
| 5828 | return false; |
| 5829 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5830 | // C++ [temp.mem]p2: |
| 5831 | // A local class shall not have member templates. |
| 5832 | if (RD->isLocalClass()) |
| 5833 | return Diag(TemplateParams->getTemplateLoc(), |
| 5834 | diag::err_template_inside_local_class) |
| 5835 | << TemplateParams->getSourceRange(); |
| 5836 | else |
| 5837 | return false; |
| 5838 | } |
| 5839 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5840 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5841 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5842 | diag::err_template_outside_namespace_or_class_scope) |
| 5843 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5844 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5845 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5846 | /// \brief Determine what kind of template specialization the given declaration |
| 5847 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5848 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5849 | if (!D) |
| 5850 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5851 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5852 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5853 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5854 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5855 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5856 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5857 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5858 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5859 | return TSK_Undeclared; |
| 5860 | } |
| 5861 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5862 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5863 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5864 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5865 | /// This routine determines whether a template specialization can be declared |
| 5866 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5867 | /// |
| 5868 | /// \param S the semantic analysis object for which this check is being |
| 5869 | /// performed. |
| 5870 | /// |
| 5871 | /// \param Specialized the entity being specialized or instantiated, which |
| 5872 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5873 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5874 | /// member class). |
| 5875 | /// |
| 5876 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5877 | /// |
| 5878 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5879 | /// this entity. |
| 5880 | /// |
| 5881 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5882 | /// a class template. |
| 5883 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5884 | /// \returns true if there was an error that we cannot recover from, false |
| 5885 | /// otherwise. |
| 5886 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5887 | NamedDecl *Specialized, |
| 5888 | NamedDecl *PrevDecl, |
| 5889 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5890 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5891 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5892 | // various diagnostics emitted by this routine. |
| 5893 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5894 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5895 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5896 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5897 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5898 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5899 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5900 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5901 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5902 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5903 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5904 | else if (isa<RecordDecl>(Specialized)) |
| 5905 | EntityKind = 7; |
| 5906 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5907 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5908 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5909 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5910 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5911 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5912 | return true; |
| 5913 | } |
| 5914 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5915 | // C++ [temp.expl.spec]p2: |
| 5916 | // An explicit specialization shall be declared in the namespace |
| 5917 | // of which the template is a member, or, for member templates, in |
| 5918 | // the namespace of which the enclosing class or enclosing class |
| 5919 | // template is a member. An explicit specialization of a member |
| 5920 | // function, member class or static data member of a class |
| 5921 | // template shall be declared in the namespace of which the class |
| 5922 | // template is a member. Such a declaration may also be a |
| 5923 | // definition. If the declaration is not a definition, the |
| 5924 | // specialization may be defined later in the name- space in which |
| 5925 | // the explicit specialization was declared, or in a namespace |
| 5926 | // that encloses the one in which the explicit specialization was |
| 5927 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5928 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5929 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5930 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5931 | return true; |
| 5932 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5933 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5934 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5935 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5936 | // Do not warn for class scope explicit specialization during |
| 5937 | // instantiation, warning was already emitted during pattern |
| 5938 | // semantic analysis. |
| 5939 | if (!S.ActiveTemplateInstantiations.size()) |
| 5940 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 5941 | << Specialized; |
| 5942 | } else { |
| 5943 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5944 | << Specialized; |
| 5945 | return true; |
| 5946 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5947 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5948 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5949 | if (S.CurContext->isRecord() && |
| 5950 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 5951 | // Make sure that we're specializing in the right record context. |
| 5952 | // Otherwise, things can go horribly wrong. |
| 5953 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5954 | << Specialized; |
| 5955 | return true; |
| 5956 | } |
| 5957 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5958 | // C++ [temp.class.spec]p6: |
| 5959 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5960 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 5961 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5962 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5963 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5964 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5965 | |
| 5966 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 5967 | // namespace. |
| 5968 | // Note that HandleDeclarator() performs this check for explicit |
| 5969 | // specializations of function templates, static data members, and member |
| 5970 | // functions, so we skip the check here for those kinds of entities. |
| 5971 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 5972 | // Should we refactor that check, so that it occurs later? |
| 5973 | if (!DC->Encloses(SpecializedContext) && |
| 5974 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 5975 | isa<FunctionDecl>(Specialized) || |
| 5976 | isa<VarTemplateDecl>(Specialized) || |
| 5977 | isa<VarDecl>(Specialized))) { |
| 5978 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5979 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5980 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 5981 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5982 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 5983 | if (S.getLangOpts().MicrosoftExt) |
| 5984 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 5985 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 5986 | << cast<NamedDecl>(SpecializedContext); |
| 5987 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5988 | llvm_unreachable("unexpected namespace context for specialization"); |
| 5989 | |
| 5990 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 5991 | } else if ((!PrevDecl || |
| 5992 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 5993 | getTemplateSpecializationKind(PrevDecl) == |
| 5994 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5995 | // C++ [temp.exp.spec]p2: |
| 5996 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5997 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5998 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | // An explicit specialization of a member function, member class or |
| 6000 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6001 | // namespace of which the class template is a member. |
| 6002 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6003 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6004 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6005 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6006 | // C++11 [temp.explicit]p3: |
| 6007 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 6008 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6009 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6010 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6011 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6012 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6013 | "DC encloses TU but isn't in enclosing namespace set"); |
| 6014 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 6015 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6016 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6017 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6018 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6019 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6020 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6021 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 6022 | else |
| 6023 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 6024 | S.Diag(Loc, Diag) |
| 6025 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 6026 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6027 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6028 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6029 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6030 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6031 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6032 | return false; |
| 6033 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6034 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6035 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 6036 | if (!E->isInstantiationDependent()) |
| 6037 | return SourceLocation(); |
| 6038 | DependencyChecker Checker(Depth); |
| 6039 | Checker.TraverseStmt(E); |
| 6040 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6041 | return E->getSourceRange(); |
| 6042 | return Checker.MatchLoc; |
| 6043 | } |
| 6044 | |
| 6045 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6046 | if (!TL.getType()->isDependentType()) |
| 6047 | return SourceLocation(); |
| 6048 | DependencyChecker Checker(Depth); |
| 6049 | Checker.TraverseTypeLoc(TL); |
| 6050 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6051 | return TL.getSourceRange(); |
| 6052 | return Checker.MatchLoc; |
| 6053 | } |
| 6054 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6055 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6056 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6057 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6058 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6059 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6060 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6061 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6062 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6063 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6064 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6065 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6066 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6067 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6068 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6069 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6070 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6071 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6072 | |
| 6073 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6074 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6075 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6076 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6077 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6078 | |
| 6079 | // Strip off any implicit casts we added as part of type checking. |
| 6080 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6081 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6082 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6083 | // C++ [temp.class.spec]p8: |
| 6084 | // A non-type argument is non-specialized if it is the name of a |
| 6085 | // non-type parameter. All other non-type arguments are |
| 6086 | // specialized. |
| 6087 | // |
| 6088 | // Below, we check the two conditions that only apply to |
| 6089 | // specialized non-type arguments, so skip any non-specialized |
| 6090 | // arguments. |
| 6091 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6092 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6093 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6094 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6095 | // C++ [temp.class.spec]p9: |
| 6096 | // Within the argument list of a class template partial |
| 6097 | // specialization, the following restrictions apply: |
| 6098 | // -- A partially specialized non-type argument expression |
| 6099 | // shall not involve a template parameter of the partial |
| 6100 | // specialization except when the argument expression is a |
| 6101 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6102 | SourceRange ParamUseRange = |
| 6103 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 6104 | if (ParamUseRange.isValid()) { |
| 6105 | if (IsDefaultArgument) { |
| 6106 | S.Diag(TemplateNameLoc, |
| 6107 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6108 | S.Diag(ParamUseRange.getBegin(), |
| 6109 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6110 | << ParamUseRange; |
| 6111 | } else { |
| 6112 | S.Diag(ParamUseRange.getBegin(), |
| 6113 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6114 | << ParamUseRange; |
| 6115 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6116 | return true; |
| 6117 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6118 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6119 | // -- The type of a template parameter corresponding to a |
| 6120 | // specialized non-type argument shall not be dependent on a |
| 6121 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6122 | // |
| 6123 | // FIXME: We need to delay this check until instantiation in some cases: |
| 6124 | // |
| 6125 | // template<template<typename> class X> struct A { |
| 6126 | // template<typename T, X<T> N> struct B; |
| 6127 | // template<typename T> struct B<T, 0>; |
| 6128 | // }; |
| 6129 | // template<typename> using X = int; |
| 6130 | // A<X>::B<int, 0> b; |
| 6131 | ParamUseRange = findTemplateParameter( |
| 6132 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 6133 | if (ParamUseRange.isValid()) { |
| 6134 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6135 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 6136 | << Param->getType() << ParamUseRange; |
| 6137 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 6138 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6139 | return true; |
| 6140 | } |
| 6141 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6142 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6143 | return false; |
| 6144 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6145 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6146 | /// \brief Check the non-type template arguments of a class template |
| 6147 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6148 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6149 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6150 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6151 | /// template. |
| 6152 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6153 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6154 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6155 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6156 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6157 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6158 | Sema &S, SourceLocation TemplateNameLoc, |
| 6159 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6160 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6161 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 6162 | |
| 6163 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6164 | NonTypeTemplateParmDecl *Param |
| 6165 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6166 | if (!Param) |
| 6167 | continue; |
| 6168 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6169 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 6170 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6171 | return true; |
| 6172 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6173 | |
| 6174 | return false; |
| 6175 | } |
| 6176 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6177 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6178 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6179 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6180 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6181 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6182 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6183 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6184 | MultiTemplateParamsArg |
| 6185 | TemplateParameterLists, |
| 6186 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6187 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6188 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6189 | CXXScopeSpec &SS = TemplateId.SS; |
| 6190 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6191 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6192 | // store the location of the outermost template keyword in the declaration. |
| 6193 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6194 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6195 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6196 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6197 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6198 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6199 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6200 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6201 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6202 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6203 | |
| 6204 | if (!ClassTemplate) { |
| 6205 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6206 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6207 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6208 | return true; |
| 6209 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6210 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6211 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6212 | bool isPartialSpecialization = false; |
| 6213 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6214 | // Check the validity of the template headers that introduce this |
| 6215 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6216 | // FIXME: We probably shouldn't complain about these headers for |
| 6217 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6218 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6219 | TemplateParameterList *TemplateParams = |
| 6220 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6221 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6222 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6223 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6224 | if (Invalid) |
| 6225 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6226 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6227 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6228 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6229 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6230 | if (TUK == TUK_Friend) { |
| 6231 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6232 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6233 | return true; |
| 6234 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6235 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6236 | // C++ [temp.class.spec]p10: |
| 6237 | // The template parameter list of a specialization shall not |
| 6238 | // contain default template argument values. |
| 6239 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6240 | Decl *Param = TemplateParams->getParam(I); |
| 6241 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6242 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6243 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6244 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6245 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6246 | } |
| 6247 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6248 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6249 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6250 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6251 | diag::err_default_arg_in_partial_spec) |
| 6252 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6253 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6254 | } |
| 6255 | } else { |
| 6256 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6257 | if (TTP->hasDefaultArgument()) { |
| 6258 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6259 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6260 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6261 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6262 | } |
| 6263 | } |
| 6264 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6265 | } else if (TemplateParams) { |
| 6266 | if (TUK == TUK_Friend) |
| 6267 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6268 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6269 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6270 | TemplateParams->getRAngleLoc())) |
| 6271 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6272 | else |
| 6273 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6274 | } else { |
| 6275 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6276 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6277 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6278 | // Check that the specialization uses the same tag kind as the |
| 6279 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6280 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6281 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6282 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6283 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 6284 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6285 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6286 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6287 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6288 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6289 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6290 | diag::note_previous_use); |
| 6291 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6292 | } |
| 6293 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6294 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6295 | TemplateArgumentListInfo TemplateArgs = |
| 6296 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6297 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6298 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6299 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6300 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6301 | UPPC_PartialSpecialization)) |
| 6302 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6303 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6304 | // Check that the template argument list is well-formed for this |
| 6305 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6306 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6307 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6308 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6309 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6310 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6311 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6312 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6313 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6314 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6315 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6316 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6317 | return true; |
| 6318 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6319 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6320 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6321 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6322 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6323 | TemplateArgs.size(), |
| 6324 | InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6325 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6326 | << ClassTemplate->getDeclName(); |
| 6327 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6328 | } |
| 6329 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6330 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6331 | void *InsertPos = nullptr; |
| 6332 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6333 | |
| 6334 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6335 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6336 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6337 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6338 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6339 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6340 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6341 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6342 | // Check whether we can declare a class template specialization in |
| 6343 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6344 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6345 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6346 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6347 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6348 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6349 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6350 | // The canonical type |
| 6351 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6352 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6353 | // Build the canonical type that describes the converted template |
| 6354 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6355 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6356 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6357 | Converted.data(), |
| 6358 | Converted.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6359 | |
| 6360 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6361 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6362 | // C++ [temp.class.spec]p9b3: |
| 6363 | // |
| 6364 | // -- The argument list of the specialization shall not be identical |
| 6365 | // to the implicit argument list of the primary template. |
| 6366 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6367 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6368 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6369 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6370 | ClassTemplate->getIdentifier(), |
| 6371 | TemplateNameLoc, |
| 6372 | Attr, |
| 6373 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6374 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6375 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6376 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6377 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6378 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6379 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6380 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6381 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6382 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6383 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6384 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6385 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6386 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6387 | TemplateParams, |
| 6388 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 6389 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6390 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6391 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6392 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6393 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6394 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6395 | Partial->setTemplateParameterListsInfo( |
| 6396 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6397 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6398 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6399 | if (!PrevPartial) |
| 6400 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6401 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6402 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6403 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6404 | // template specialization, make a note of that. |
| 6405 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6406 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6407 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6408 | // Check that all of the template parameters of the class template |
| 6409 | // partial specialization are deducible from the template |
| 6410 | // arguments. If not, this class template partial specialization |
| 6411 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6412 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6413 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6414 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6415 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6416 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6417 | if (!DeducibleParams.all()) { |
| 6418 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6419 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6420 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6421 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6422 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6423 | if (!DeducibleParams[I]) { |
| 6424 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6425 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6426 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6427 | diag::note_partial_spec_unused_parameter) |
| 6428 | << Param->getDeclName(); |
| 6429 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6430 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6431 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6432 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6433 | } |
| 6434 | } |
| 6435 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6436 | } else { |
| 6437 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6438 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6439 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6440 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6441 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6442 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6443 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 6444 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6445 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6446 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6447 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6448 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6449 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6450 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6451 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6452 | if (!PrevDecl) |
| 6453 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6454 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6455 | if (CurContext->isDependentContext()) { |
| 6456 | // -fms-extensions permits specialization of nested classes without |
| 6457 | // fully specializing the outer class(es). |
| 6458 | assert(getLangOpts().MicrosoftExt && |
| 6459 | "Only possible with -fms-extensions!"); |
| 6460 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6461 | CanonType = Context.getTemplateSpecializationType( |
| 6462 | CanonTemplate, Converted.data(), Converted.size()); |
| 6463 | } else { |
| 6464 | CanonType = Context.getTypeDeclType(Specialization); |
| 6465 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6468 | // C++ [temp.expl.spec]p6: |
| 6469 | // 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] | 6470 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6471 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6472 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6473 | // use occurs; no diagnostic is required. |
| 6474 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6475 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6476 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6477 | // Is there any previous explicit specialization declaration? |
| 6478 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6479 | Okay = true; |
| 6480 | break; |
| 6481 | } |
| 6482 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6483 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6484 | if (!Okay) { |
| 6485 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6486 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6487 | << Context.getTypeDeclType(Specialization) << Range; |
| 6488 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6489 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6490 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6491 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6492 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6493 | return true; |
| 6494 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6495 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6496 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6497 | // If this is not a friend, note that this is an explicit specialization. |
| 6498 | if (TUK != TUK_Friend) |
| 6499 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6500 | |
| 6501 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6502 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6503 | RecordDecl *Def = Specialization->getDefinition(); |
| 6504 | NamedDecl *Hidden = nullptr; |
| 6505 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6506 | SkipBody->ShouldSkip = true; |
| 6507 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6508 | // From here on out, treat this as just a redeclaration. |
| 6509 | TUK = TUK_Declaration; |
| 6510 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6511 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6512 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6513 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6514 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6515 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6516 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6517 | } |
| 6518 | } |
| 6519 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6520 | if (Attr) |
| 6521 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6522 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6523 | // Add alignment attributes if necessary; these attributes are checked when |
| 6524 | // the ASTContext lays out the structure. |
| 6525 | if (TUK == TUK_Definition) { |
| 6526 | AddAlignmentAttributesForRecord(Specialization); |
| 6527 | AddMsStructLayoutForRecord(Specialization); |
| 6528 | } |
| 6529 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6530 | if (ModulePrivateLoc.isValid()) |
| 6531 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6532 | << (isPartialSpecialization? 1 : 0) |
| 6533 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6534 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6535 | // Build the fully-sugared type for this class template |
| 6536 | // specialization as the user wrote in the specialization |
| 6537 | // itself. This means that we'll pretty-print the type retrieved |
| 6538 | // from the specialization's declaration the way that the user |
| 6539 | // actually wrote the specialization, rather than formatting the |
| 6540 | // name based on the "canonical" representation used to store the |
| 6541 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6542 | TypeSourceInfo *WrittenTy |
| 6543 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6544 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6545 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6546 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6547 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6548 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6549 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6550 | // C++ [temp.expl.spec]p9: |
| 6551 | // A template explicit specialization is in the scope of the |
| 6552 | // namespace in which the template was defined. |
| 6553 | // |
| 6554 | // We actually implement this paragraph where we set the semantic |
| 6555 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6556 | // but we also maintain the lexical context where the actual |
| 6557 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6558 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6559 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6560 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6561 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6562 | Specialization->startDefinition(); |
| 6563 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6564 | if (TUK == TUK_Friend) { |
| 6565 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6566 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6567 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6568 | /*FIXME:*/KWLoc); |
| 6569 | Friend->setAccess(AS_public); |
| 6570 | CurContext->addDecl(Friend); |
| 6571 | } else { |
| 6572 | // Add the specialization into its lexical context, so that it can |
| 6573 | // be seen when iterating through the list of declarations in that |
| 6574 | // context. However, specializations are not found by name lookup. |
| 6575 | CurContext->addDecl(Specialization); |
| 6576 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6577 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6578 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6579 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6580 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6581 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6582 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6583 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6584 | ActOnDocumentableDecl(NewDecl); |
| 6585 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6586 | } |
| 6587 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6588 | /// \brief Strips various properties off an implicit instantiation |
| 6589 | /// that has just been explicitly specialized. |
| 6590 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6591 | D->dropAttr<DLLImportAttr>(); |
| 6592 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6593 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6594 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6595 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6598 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6599 | // declaration or definition. |
| 6600 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6601 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6602 | // Explicit instantiations following a specialization have no effect and |
| 6603 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6604 | // until a valid name loc is found. |
| 6605 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6606 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6607 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6608 | PrevDiagLoc = Prev->getLocation(); |
| 6609 | } |
| 6610 | assert(PrevDiagLoc.isValid() && |
| 6611 | "Explicit instantiation without point of instantiation?"); |
| 6612 | return PrevDiagLoc; |
| 6613 | } |
| 6614 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6615 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6616 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6617 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6618 | /// new specialization/instantiation will have any effect. |
| 6619 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6620 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6621 | /// instantiation. |
| 6622 | /// |
| 6623 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6624 | /// |
| 6625 | /// \param PrevDecl the previous declaration of the entity. |
| 6626 | /// |
| 6627 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6628 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6629 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6630 | /// declaration was instantiated (either implicitly or explicitly). |
| 6631 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6632 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6633 | /// specialization or instantiation has no effect and should be ignored. |
| 6634 | /// |
| 6635 | /// \returns true if there was an error that should prevent the introduction of |
| 6636 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6637 | bool |
| 6638 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6639 | TemplateSpecializationKind NewTSK, |
| 6640 | NamedDecl *PrevDecl, |
| 6641 | TemplateSpecializationKind PrevTSK, |
| 6642 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6643 | bool &HasNoEffect) { |
| 6644 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6645 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6646 | switch (NewTSK) { |
| 6647 | case TSK_Undeclared: |
| 6648 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6649 | assert( |
| 6650 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6651 | "previous declaration must be implicit!"); |
| 6652 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6653 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6654 | case TSK_ExplicitSpecialization: |
| 6655 | switch (PrevTSK) { |
| 6656 | case TSK_Undeclared: |
| 6657 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6658 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6659 | // explicitly specialized or has merely been mentioned without any |
| 6660 | // instantiation. |
| 6661 | return false; |
| 6662 | |
| 6663 | case TSK_ImplicitInstantiation: |
| 6664 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6665 | // The declaration itself has not actually been instantiated, so it is |
| 6666 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6667 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6668 | return false; |
| 6669 | } |
| 6670 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6671 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6672 | case TSK_ExplicitInstantiationDeclaration: |
| 6673 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6674 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6675 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6676 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6677 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6678 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6679 | // 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] | 6680 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6681 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6682 | // implicit instantiation to take place, in every translation unit in |
| 6683 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6684 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6685 | // Is there any previous explicit specialization declaration? |
| 6686 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6687 | return false; |
| 6688 | } |
| 6689 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6690 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6691 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6692 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6693 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6694 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6695 | return true; |
| 6696 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6697 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6698 | case TSK_ExplicitInstantiationDeclaration: |
| 6699 | switch (PrevTSK) { |
| 6700 | case TSK_ExplicitInstantiationDeclaration: |
| 6701 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6702 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6703 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6704 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6705 | case TSK_Undeclared: |
| 6706 | case TSK_ImplicitInstantiation: |
| 6707 | // We're explicitly instantiating something that may have already been |
| 6708 | // implicitly instantiated; that's fine. |
| 6709 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6710 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6711 | case TSK_ExplicitSpecialization: |
| 6712 | // C++0x [temp.explicit]p4: |
| 6713 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6714 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6715 | // specialization for that template, the explicit instantiation has no |
| 6716 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6717 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6718 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6719 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6720 | case TSK_ExplicitInstantiationDefinition: |
| 6721 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6722 | // If an entity is the subject of both an explicit instantiation |
| 6723 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6724 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6725 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6726 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6727 | |
| 6728 | // Explicit instantiations following a specialization have no effect and |
| 6729 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6730 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6731 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6732 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6733 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6734 | return false; |
| 6735 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6736 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6737 | case TSK_ExplicitInstantiationDefinition: |
| 6738 | switch (PrevTSK) { |
| 6739 | case TSK_Undeclared: |
| 6740 | case TSK_ImplicitInstantiation: |
| 6741 | // We're explicitly instantiating something that may have already been |
| 6742 | // implicitly instantiated; that's fine. |
| 6743 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6744 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6745 | case TSK_ExplicitSpecialization: |
| 6746 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6747 | // For a given set of template parameters, if an explicit |
| 6748 | // instantiation of a template appears after a declaration of |
| 6749 | // an explicit specialization for that template, the explicit |
| 6750 | // instantiation has no effect. |
| 6751 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6752 | // In C++98/03 mode, we only give an extension warning here, because it |
Douglas Gregor | 06aa5041 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 6753 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6754 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6755 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6756 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6757 | diag::ext_explicit_instantiation_after_specialization) |
| 6758 | << PrevDecl; |
| 6759 | Diag(PrevDecl->getLocation(), |
| 6760 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6761 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6762 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6763 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6764 | case TSK_ExplicitInstantiationDeclaration: |
| 6765 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6766 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6767 | |
| 6768 | // C++0x [temp.explicit]p4: |
| 6769 | // For a given set of template parameters, if an explicit instantiation |
| 6770 | // of a template appears after a declaration of an explicit |
| 6771 | // specialization for that template, the explicit instantiation has no |
| 6772 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6773 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6774 | // Is there any previous explicit specialization declaration? |
| 6775 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6776 | HasNoEffect = true; |
| 6777 | break; |
| 6778 | } |
| 6779 | } |
| 6780 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6781 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6782 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6783 | case TSK_ExplicitInstantiationDefinition: |
| 6784 | // C++0x [temp.spec]p5: |
| 6785 | // For a given template and a given set of template-arguments, |
| 6786 | // - an explicit instantiation definition shall appear at most once |
| 6787 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6788 | |
| 6789 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6790 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6791 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6792 | : diag::err_explicit_instantiation_duplicate) |
| 6793 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6794 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6795 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6796 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6797 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6798 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6799 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6800 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6801 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6802 | } |
| 6803 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6804 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6805 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6806 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6807 | /// The only possible way to get a dependent function template specialization |
| 6808 | /// is with a friend declaration, like so: |
| 6809 | /// |
| 6810 | /// \code |
| 6811 | /// template \<class T> void foo(T); |
| 6812 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6813 | /// friend void foo<>(T); |
| 6814 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6815 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6816 | /// |
| 6817 | /// There really isn't any useful analysis we can do here, so we |
| 6818 | /// just store the information. |
| 6819 | bool |
| 6820 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6821 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6822 | LookupResult &Previous) { |
| 6823 | // Remove anything from Previous that isn't a function template in |
| 6824 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6825 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6826 | LookupResult::Filter F = Previous.makeFilter(); |
| 6827 | while (F.hasNext()) { |
| 6828 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6829 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6830 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6831 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6832 | F.erase(); |
| 6833 | } |
| 6834 | F.done(); |
| 6835 | |
| 6836 | // Should this be diagnosed here? |
| 6837 | if (Previous.empty()) return true; |
| 6838 | |
| 6839 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6840 | ExplicitTemplateArgs); |
| 6841 | return false; |
| 6842 | } |
| 6843 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6844 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6845 | /// specialization. |
| 6846 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6847 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6848 | /// explicit function template specialization. On successful completion, |
| 6849 | /// the function declaration \p FD will become a function template |
| 6850 | /// specialization. |
| 6851 | /// |
| 6852 | /// \param FD the function declaration, which will be updated to become a |
| 6853 | /// function template specialization. |
| 6854 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6855 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6856 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6857 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6858 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6859 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6860 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6861 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6862 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6863 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6864 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6865 | // The set of function template specializations that could match this |
| 6866 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6867 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6868 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 6869 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6870 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6871 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 6872 | ConvertedTemplateArgs; |
| 6873 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6874 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6875 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6876 | I != E; ++I) { |
| 6877 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6878 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6879 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6880 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6881 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6882 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6883 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6884 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6885 | // When matching a constexpr member function template specialization |
| 6886 | // against the primary template, we don't yet know whether the |
| 6887 | // specialization has an implicit 'const' (because we don't know whether |
| 6888 | // it will be a static member function until we know which template it |
| 6889 | // specializes), so adjust it now assuming it specializes this template. |
| 6890 | QualType FT = FD->getType(); |
| 6891 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6892 | CXXMethodDecl *OldMD = |
| 6893 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6894 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6895 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6896 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6897 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6898 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6899 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6900 | } |
| 6901 | } |
| 6902 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6903 | TemplateArgumentListInfo Args; |
| 6904 | if (ExplicitTemplateArgs) |
| 6905 | Args = *ExplicitTemplateArgs; |
| 6906 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6907 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6908 | // A trailing template-argument can be left unspecified in the |
| 6909 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6910 | // provided it can be deduced from the function argument type. |
| 6911 | // Perform template argument deduction to determine whether we may be |
| 6912 | // specializing this template. |
| 6913 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6914 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6915 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6916 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6917 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6918 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 6919 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6920 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6921 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6922 | FailedCandidates.addCandidate().set( |
| 6923 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 6924 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6925 | (void)TDK; |
| 6926 | continue; |
| 6927 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6928 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6929 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6930 | if (ExplicitTemplateArgs) |
| 6931 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6932 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6933 | } |
| 6934 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6935 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6936 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6937 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6938 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6939 | FD->getLocation(), |
| 6940 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6941 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6942 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6943 | PDiag(diag::note_function_template_spec_matched)); |
| 6944 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6945 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6946 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6947 | |
| 6948 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 6949 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6950 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 6951 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare [...] |
| 6952 | // an explicit specialization (14.8.3) [...] of a concept definition. |
| 6953 | if (Specialization->getPrimaryTemplate()->isConcept()) { |
| 6954 | Diag(FD->getLocation(), diag::err_concept_specialized) |
| 6955 | << 0 /*function*/ << 1 /*explicitly specialized*/; |
| 6956 | Diag(Specialization->getLocation(), diag::note_previous_declaration); |
| 6957 | return true; |
| 6958 | } |
| 6959 | |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6960 | FunctionTemplateSpecializationInfo *SpecInfo |
| 6961 | = Specialization->getTemplateSpecializationInfo(); |
| 6962 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6963 | |
| 6964 | // Note: do not overwrite location info if previous template |
| 6965 | // specialization kind was explicit. |
| 6966 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6967 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6968 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6969 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 6970 | // function can differ from the template declaration with respect to |
| 6971 | // the constexpr specifier. |
| 6972 | Specialization->setConstexpr(FD->isConstexpr()); |
| 6973 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6974 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6975 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6976 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6977 | |
| 6978 | // If this is a friend declaration, then we're not really declaring |
| 6979 | // an explicit specialization. |
| 6980 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6981 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6982 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6983 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6984 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6985 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6986 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6987 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6988 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6989 | |
| 6990 | // C++ [temp.expl.spec]p6: |
| 6991 | // 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] | 6992 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6993 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6994 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6995 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6996 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6997 | if (!isFriend && |
| 6998 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6999 | TSK_ExplicitSpecialization, |
| 7000 | Specialization, |
| 7001 | SpecInfo->getTemplateSpecializationKind(), |
| 7002 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7003 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7004 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7005 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7006 | // Mark the prior declaration as an explicit specialization, so that later |
| 7007 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7008 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 7009 | // Since explicit specializations do not inherit '=delete' from their |
| 7010 | // primary function template - check if the 'specialization' that was |
| 7011 | // implicitly generated (during template argument deduction for partial |
| 7012 | // ordering) from the most specialized of all the function templates that |
| 7013 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 7014 | // first check that it was implicitly generated during template argument |
| 7015 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 7016 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 7017 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 7018 | !Specialization->getCanonicalDecl()->isReferenced()) { |
| 7019 | assert( |
| 7020 | Specialization->getCanonicalDecl() == Specialization && |
| 7021 | "This must be the only existing declaration of this specialization"); |
| 7022 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7023 | } |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7024 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7025 | MarkUnusedFileScopedDecl(Specialization); |
| 7026 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7027 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7028 | // Turn the given function declaration into a function template |
| 7029 | // specialization, with the template arguments from the previous |
| 7030 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7031 | // Take copies of (semantic and syntactic) template argument lists. |
| 7032 | const TemplateArgumentList* TemplArgs = new (Context) |
| 7033 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7034 | FD->setFunctionTemplateSpecialization( |
| 7035 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 7036 | SpecInfo->getTemplateSpecializationKind(), |
| 7037 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7038 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7039 | // The "previous declaration" for this function template specialization is |
| 7040 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7041 | Previous.clear(); |
| 7042 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7043 | return false; |
| 7044 | } |
| 7045 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7046 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7047 | /// specialization. |
| 7048 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7049 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7050 | /// explicit member function specialization. On successful completion, |
| 7051 | /// the function declaration \p FD will become a member function |
| 7052 | /// specialization. |
| 7053 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7054 | /// \param Member the member declaration, which will be updated to become a |
| 7055 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7056 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7057 | /// \param Previous the set of declarations, one of which may be specialized |
| 7058 | /// by this function specialization; the set will be modified to contain the |
| 7059 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7060 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7061 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7062 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7063 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7064 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7065 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7066 | NamedDecl *Instantiation = nullptr; |
| 7067 | NamedDecl *InstantiatedFrom = nullptr; |
| 7068 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7069 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7070 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7071 | // Nowhere to look anyway. |
| 7072 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7073 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7074 | I != E; ++I) { |
| 7075 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7076 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7077 | QualType Adjusted = Function->getType(); |
| 7078 | if (!hasExplicitCallingConv(Adjusted)) |
| 7079 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7080 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7081 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7082 | Instantiation = Method; |
| 7083 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7084 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7085 | break; |
| 7086 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7087 | } |
| 7088 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7089 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7090 | VarDecl *PrevVar; |
| 7091 | if (Previous.isSingleResult() && |
| 7092 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7093 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7094 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7095 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7096 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7097 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7098 | } |
| 7099 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7100 | CXXRecordDecl *PrevRecord; |
| 7101 | if (Previous.isSingleResult() && |
| 7102 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7103 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7104 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7105 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7106 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7107 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7108 | } else if (isa<EnumDecl>(Member)) { |
| 7109 | EnumDecl *PrevEnum; |
| 7110 | if (Previous.isSingleResult() && |
| 7111 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7112 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7113 | Instantiation = PrevEnum; |
| 7114 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7115 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7116 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7117 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7118 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7119 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7120 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7121 | // specializations are always out-of-line, the caller will complain about |
| 7122 | // this mismatch later. |
| 7123 | return false; |
| 7124 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7125 | |
| 7126 | // If this is a friend, just bail out here before we start turning |
| 7127 | // things into explicit specializations. |
| 7128 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7129 | // Preserve instantiation information. |
| 7130 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7131 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7132 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7133 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7134 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7135 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7136 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7137 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7138 | } |
| 7139 | |
| 7140 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7141 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7142 | return false; |
| 7143 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7144 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7145 | // Make sure that this is a specialization of a member. |
| 7146 | if (!InstantiatedFrom) { |
| 7147 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 7148 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7149 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 7150 | return true; |
| 7151 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7152 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7153 | // C++ [temp.expl.spec]p6: |
| 7154 | // 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] | 7155 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7156 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7157 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7158 | // use occurs; no diagnostic is required. |
| 7159 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7160 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7161 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7162 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7163 | TSK_ExplicitSpecialization, |
| 7164 | Instantiation, |
| 7165 | MSInfo->getTemplateSpecializationKind(), |
| 7166 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7167 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7168 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7169 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7170 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7171 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7172 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7173 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7174 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7175 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7176 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7177 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7178 | // the original declaration to note that it is an explicit specialization |
| 7179 | // (if it was previously an implicit instantiation). This latter step |
| 7180 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7181 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7182 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7183 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7184 | TSK_ImplicitInstantiation) { |
| 7185 | InstantiationFunction->setTemplateSpecializationKind( |
| 7186 | TSK_ExplicitSpecialization); |
| 7187 | InstantiationFunction->setLocation(Member->getLocation()); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7188 | // Explicit specializations of member functions of class templates do not |
| 7189 | // inherit '=delete' from the member function they are specializing. |
| 7190 | if (InstantiationFunction->isDeleted()) { |
| 7191 | assert(InstantiationFunction->getCanonicalDecl() == |
| 7192 | InstantiationFunction); |
| 7193 | InstantiationFunction->setDeletedAsWritten(false);
|
| 7194 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7195 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7196 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7197 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7198 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7199 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7200 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7201 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7202 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7203 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7204 | TSK_ImplicitInstantiation) { |
| 7205 | InstantiationVar->setTemplateSpecializationKind( |
| 7206 | TSK_ExplicitSpecialization); |
| 7207 | InstantiationVar->setLocation(Member->getLocation()); |
| 7208 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7209 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7210 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7211 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7212 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7213 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7214 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7215 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7216 | TSK_ImplicitInstantiation) { |
| 7217 | InstantiationClass->setTemplateSpecializationKind( |
| 7218 | TSK_ExplicitSpecialization); |
| 7219 | InstantiationClass->setLocation(Member->getLocation()); |
| 7220 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7221 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7222 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7223 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7224 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7225 | } else { |
| 7226 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7227 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7228 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7229 | TSK_ImplicitInstantiation) { |
| 7230 | InstantiationEnum->setTemplateSpecializationKind( |
| 7231 | TSK_ExplicitSpecialization); |
| 7232 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7233 | } |
| 7234 | |
| 7235 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7236 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7237 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7238 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7239 | // Save the caller the trouble of having to figure out which declaration |
| 7240 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7241 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7242 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7243 | return false; |
| 7244 | } |
| 7245 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7246 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7247 | /// |
| 7248 | /// \returns true if a serious error occurs, false otherwise. |
| 7249 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7250 | SourceLocation InstLoc, |
| 7251 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7252 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7253 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7254 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7255 | if (CurContext->isRecord()) { |
| 7256 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7257 | << D; |
| 7258 | return true; |
| 7259 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7260 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7261 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7262 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7263 | // template. If the name declared in the explicit instantiation is an |
| 7264 | // unqualified name, the explicit instantiation shall appear in the |
| 7265 | // namespace where its template is declared or, if that namespace is inline |
| 7266 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7267 | // |
| 7268 | // 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] | 7269 | if (WasQualifiedName) { |
| 7270 | if (CurContext->Encloses(OrigContext)) |
| 7271 | return false; |
| 7272 | } else { |
| 7273 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7274 | return false; |
| 7275 | } |
| 7276 | |
| 7277 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7278 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7279 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7280 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7281 | diag::err_explicit_instantiation_out_of_scope : |
| 7282 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7283 | << D << NS; |
| 7284 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7285 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7286 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7287 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7288 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7289 | << D << NS; |
| 7290 | } else |
| 7291 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7292 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7293 | diag::err_explicit_instantiation_must_be_global : |
| 7294 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7295 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7296 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7297 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7298 | } |
| 7299 | |
| 7300 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7301 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7302 | if (!SS.isSet()) |
| 7303 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7304 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7305 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7306 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7307 | // or a static data member of a class template specialization, the name of |
| 7308 | // the class template specialization in the qualified-id for the member |
| 7309 | // name shall be a simple-template-id. |
| 7310 | // |
| 7311 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7312 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7313 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7314 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7315 | if (isa<TemplateSpecializationType>(T)) |
| 7316 | return true; |
| 7317 | |
| 7318 | return false; |
| 7319 | } |
| 7320 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7321 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7322 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7323 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7324 | SourceLocation ExternLoc, |
| 7325 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7326 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7327 | SourceLocation KWLoc, |
| 7328 | const CXXScopeSpec &SS, |
| 7329 | TemplateTy TemplateD, |
| 7330 | SourceLocation TemplateNameLoc, |
| 7331 | SourceLocation LAngleLoc, |
| 7332 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7333 | SourceLocation RAngleLoc, |
| 7334 | AttributeList *Attr) { |
| 7335 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7336 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7337 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7338 | // Check that the specialization uses the same tag kind as the |
| 7339 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7340 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7341 | assert(Kind != TTK_Enum && |
| 7342 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7343 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7344 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 7345 | |
| 7346 | if (!ClassTemplate) { |
| 7347 | unsigned ErrorKind = 0; |
| 7348 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7349 | ErrorKind = 4; |
| 7350 | } else if (isa<TemplateTemplateParmDecl>(TD)) { |
| 7351 | ErrorKind = 5; |
| 7352 | } |
| 7353 | |
| 7354 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << ErrorKind; |
| 7355 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7356 | return true; |
| 7357 | } |
| 7358 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7359 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7360 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7361 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7362 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7363 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7364 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7365 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7366 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7367 | diag::note_previous_use); |
| 7368 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7369 | } |
| 7370 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7371 | // C++0x [temp.explicit]p2: |
| 7372 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7373 | // definition and an explicit instantiation declaration. An explicit |
| 7374 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7375 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7376 | ? TSK_ExplicitInstantiationDefinition |
| 7377 | : TSK_ExplicitInstantiationDeclaration; |
| 7378 | |
| 7379 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7380 | // Check for dllexport class template instantiation declarations. |
| 7381 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7382 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7383 | Diag(ExternLoc, |
| 7384 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7385 | Diag(A->getLoc(), diag::note_attribute); |
| 7386 | break; |
| 7387 | } |
| 7388 | } |
| 7389 | |
| 7390 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7391 | Diag(ExternLoc, |
| 7392 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7393 | Diag(A->getLocation(), diag::note_attribute); |
| 7394 | } |
| 7395 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7396 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7397 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 7398 | // instantiation declarations for most purposes. |
| 7399 | bool DLLImportExplicitInstantiationDef = false; |
| 7400 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 7401 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7402 | // Check for dllimport class template instantiation definitions. |
| 7403 | bool DLLImport = |
| 7404 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 7405 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7406 | if (A->getKind() == AttributeList::AT_DLLImport) |
| 7407 | DLLImport = true; |
| 7408 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7409 | // dllexport trumps dllimport here. |
| 7410 | DLLImport = false; |
| 7411 | break; |
| 7412 | } |
| 7413 | } |
| 7414 | if (DLLImport) { |
| 7415 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 7416 | DLLImportExplicitInstantiationDef = true; |
| 7417 | } |
| 7418 | } |
| 7419 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7420 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7421 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7422 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7423 | |
| 7424 | // Check that the template argument list is well-formed for this |
| 7425 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7426 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7427 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7428 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7429 | return true; |
| 7430 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7431 | // Find the class template specialization declaration that |
| 7432 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7433 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7434 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7435 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7436 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7437 | TemplateSpecializationKind PrevDecl_TSK |
| 7438 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7439 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7440 | // C++0x [temp.explicit]p2: |
| 7441 | // [...] An explicit instantiation shall appear in an enclosing |
| 7442 | // namespace of its template. [...] |
| 7443 | // |
| 7444 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7445 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7446 | SS.isSet())) |
| 7447 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7448 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7449 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7450 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7451 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7452 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7453 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7454 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7455 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7456 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7457 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7458 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7459 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7460 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7461 | |
| 7462 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7463 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7464 | // Since the only prior class template specialization with these |
| 7465 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7466 | // declaration node as our own, updating the source location |
| 7467 | // for the template name to reflect our new declaration. |
| 7468 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7469 | Specialization = PrevDecl; |
| 7470 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7471 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7472 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7473 | |
| 7474 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7475 | DLLImportExplicitInstantiationDef) { |
| 7476 | // The new specialization might add a dllimport attribute. |
| 7477 | HasNoEffect = false; |
| 7478 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7479 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7480 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7481 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7482 | // Create a new class template specialization declaration node for |
| 7483 | // this explicit specialization. |
| 7484 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7485 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7486 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7487 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7488 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame^] | 7489 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7490 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7491 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7492 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7493 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7494 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7495 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7496 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7497 | } |
| 7498 | |
| 7499 | // Build the fully-sugared type for this explicit instantiation as |
| 7500 | // the user wrote in the explicit instantiation itself. This means |
| 7501 | // that we'll pretty-print the type retrieved from the |
| 7502 | // specialization's declaration the way that the user actually wrote |
| 7503 | // the explicit instantiation, rather than formatting the name based |
| 7504 | // on the "canonical" representation used to store the template |
| 7505 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7506 | TypeSourceInfo *WrittenTy |
| 7507 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7508 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7509 | Context.getTypeDeclType(Specialization)); |
| 7510 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7511 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7512 | // Set source locations for keywords. |
| 7513 | Specialization->setExternLoc(ExternLoc); |
| 7514 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | 40bcfd7 | 2013-04-22 23:23:42 +0000 | [diff] [blame] | 7515 | Specialization->setRBraceLoc(SourceLocation()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7516 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7517 | if (Attr) |
| 7518 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7519 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7520 | // Add the explicit instantiation into its lexical context. However, |
| 7521 | // since explicit instantiations are never found by name lookup, we |
| 7522 | // just put it into the declaration context directly. |
| 7523 | Specialization->setLexicalDeclContext(CurContext); |
| 7524 | CurContext->addDecl(Specialization); |
| 7525 | |
| 7526 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7527 | if (HasNoEffect) { |
| 7528 | // Set the template specialization kind. |
| 7529 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7530 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7531 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7532 | |
| 7533 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7534 | // A definition of a class template or class member template |
| 7535 | // shall be in scope at the point of the explicit instantiation of |
| 7536 | // the class template or class member template. |
| 7537 | // |
| 7538 | // This check comes when we actually try to perform the |
| 7539 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7540 | ClassTemplateSpecializationDecl *Def |
| 7541 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7542 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7543 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7544 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7545 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7546 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7547 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7548 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7549 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7550 | // Instantiate the members of this class template specialization. |
| 7551 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7552 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7553 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7554 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7555 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7556 | // TSK_ExplicitInstantiationDefinition |
| 7557 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7558 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 7559 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7560 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7561 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7562 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7563 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
| 7564 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7565 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7566 | // attribute to a template with a previous instantiation declaration. |
| 7567 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7568 | auto *A = cast<InheritableAttr>( |
| 7569 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 7570 | A->setInherited(true); |
| 7571 | Def->addAttr(A); |
Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 7572 | |
| 7573 | // We reject explicit instantiations in class scope, so there should |
| 7574 | // never be any delayed exported classes to worry about. |
| 7575 | assert(DelayedDllExportClasses.empty() && |
| 7576 | "delayed exports present at explicit instantiation"); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7577 | checkClassLevelDLLAttribute(Def); |
Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 7578 | referenceDLLExportedClassMethods(); |
Hans Wennborg | fce87ca | 2015-06-09 00:39:09 +0000 | [diff] [blame] | 7579 | |
| 7580 | // Propagate attribute to base class templates. |
| 7581 | for (auto &B : Def->bases()) { |
| 7582 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 7583 | B.getType()->getAsCXXRecordDecl())) |
| 7584 | propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 7585 | } |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7586 | } |
| 7587 | } |
| 7588 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7589 | // Set the template specialization kind. Make sure it is set before |
| 7590 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 7591 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7592 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7593 | } else { |
| 7594 | |
| 7595 | // Set the template specialization kind. |
| 7596 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7597 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7598 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7599 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7600 | } |
| 7601 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7602 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7603 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7604 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7605 | SourceLocation ExternLoc, |
| 7606 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7607 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7608 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7609 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7610 | IdentifierInfo *Name, |
| 7611 | SourceLocation NameLoc, |
| 7612 | AttributeList *Attr) { |
| 7613 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7614 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7615 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7616 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7617 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7618 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7619 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7620 | SourceLocation(), false, TypeResult(), |
| 7621 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7622 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7623 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7624 | if (!TagD) |
| 7625 | return true; |
| 7626 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7627 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7628 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7629 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7630 | if (Tag->isInvalidDecl()) |
| 7631 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7632 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7633 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7634 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7635 | if (!Pattern) { |
| 7636 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7637 | << Context.getTypeDeclType(Record); |
| 7638 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7639 | return true; |
| 7640 | } |
| 7641 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7642 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7643 | // If the explicit instantiation is for a class or member class, the |
| 7644 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7645 | // simple-template-id. |
| 7646 | // |
| 7647 | // C++98 has the same restriction, just worded differently. |
| 7648 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7649 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7650 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7651 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7652 | // C++0x [temp.explicit]p2: |
| 7653 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7654 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7655 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7656 | TemplateSpecializationKind TSK |
| 7657 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7658 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7659 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7660 | // C++0x [temp.explicit]p2: |
| 7661 | // [...] An explicit instantiation shall appear in an enclosing |
| 7662 | // namespace of its template. [...] |
| 7663 | // |
| 7664 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7665 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7666 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7667 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7668 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7669 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7670 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7671 | PrevDecl = Record; |
| 7672 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7673 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7674 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7675 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7676 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7677 | PrevDecl, |
| 7678 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7679 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7680 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7681 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7682 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7683 | return TagD; |
| 7684 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7685 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7686 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7687 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7688 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7689 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7690 | // 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] | 7691 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7692 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7693 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7694 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7695 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7696 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7697 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7698 | << Pattern; |
| 7699 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7700 | } else { |
| 7701 | if (InstantiateClass(NameLoc, Record, Def, |
| 7702 | getTemplateInstantiationArgs(Record), |
| 7703 | TSK)) |
| 7704 | return true; |
| 7705 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7706 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7707 | if (!RecordDef) |
| 7708 | return true; |
| 7709 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7710 | } |
| 7711 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7712 | // Instantiate all of the members of the class. |
| 7713 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7714 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7715 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7716 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7717 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7718 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7719 | // FIXME: We don't have any representation for explicit instantiations of |
| 7720 | // member classes. Such a representation is not needed for compilation, but it |
| 7721 | // should be available for clients that want to see all of the declarations in |
| 7722 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7723 | return TagD; |
| 7724 | } |
| 7725 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7726 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7727 | SourceLocation ExternLoc, |
| 7728 | SourceLocation TemplateLoc, |
| 7729 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7730 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7731 | // TODO: check if/when DNInfo should replace Name. |
| 7732 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7733 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7734 | if (!Name) { |
| 7735 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7736 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7737 | diag::err_explicit_instantiation_requires_name) |
| 7738 | << D.getDeclSpec().getSourceRange() |
| 7739 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7740 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7741 | return true; |
| 7742 | } |
| 7743 | |
| 7744 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7745 | // we find one that is. |
| 7746 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7747 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7748 | S = S->getParent(); |
| 7749 | |
| 7750 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7751 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7752 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7753 | if (R.isNull()) |
| 7754 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7755 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7756 | // C++ [dcl.stc]p1: |
| 7757 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7758 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7759 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7760 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7761 | << Name; |
| 7762 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7763 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7764 | != DeclSpec::SCS_unspecified) { |
| 7765 | // Complain about then remove the storage class specifier. |
| 7766 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7767 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7768 | |
| 7769 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7770 | } |
| 7771 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7772 | // C++0x [temp.explicit]p1: |
| 7773 | // [...] An explicit instantiation of a function template shall not use the |
| 7774 | // inline or constexpr specifiers. |
| 7775 | // Presumably, this also applies to member functions of class templates as |
| 7776 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7777 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7778 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7779 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7780 | diag::err_explicit_instantiation_inline : |
| 7781 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7782 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7783 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7784 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7785 | // not already specified. |
| 7786 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7787 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7788 | |
Nathan Wilson | de49845 | 2016-02-08 05:34:00 +0000 | [diff] [blame] | 7789 | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
| 7790 | // applied only to the definition of a function template or variable template, |
| 7791 | // declared in namespace scope. |
| 7792 | if (D.getDeclSpec().isConceptSpecified()) { |
| 7793 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
| 7794 | diag::err_concept_specified_specialization) << 0; |
| 7795 | return true; |
| 7796 | } |
| 7797 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7798 | // C++0x [temp.explicit]p2: |
| 7799 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7800 | // definition and an explicit instantiation declaration. An explicit |
| 7801 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7802 | TemplateSpecializationKind TSK |
| 7803 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7804 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7805 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7806 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7807 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7808 | |
| 7809 | if (!R->isFunctionType()) { |
| 7810 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7811 | // A [...] static data member of a class template can be explicitly |
| 7812 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7813 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7814 | // C++1y [temp.explicit]p1: |
| 7815 | // A [...] variable [...] template specialization can be explicitly |
| 7816 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7817 | if (Previous.isAmbiguous()) |
| 7818 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7819 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7820 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7821 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7822 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7823 | if (!PrevTemplate) { |
| 7824 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7825 | // We expect to see a data data member here. |
| 7826 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7827 | << Name; |
| 7828 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7829 | P != PEnd; ++P) |
| 7830 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7831 | return true; |
| 7832 | } |
| 7833 | |
| 7834 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7835 | // FIXME: Check for explicit specialization? |
| 7836 | Diag(D.getIdentifierLoc(), |
| 7837 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7838 | << Prev; |
| 7839 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7840 | // FIXME: Can we provide a note showing where this was declared? |
| 7841 | return true; |
| 7842 | } |
| 7843 | } else { |
| 7844 | // Explicitly instantiate a variable template. |
| 7845 | |
| 7846 | // C++1y [dcl.spec.auto]p6: |
| 7847 | // ... A program that uses auto or decltype(auto) in a context not |
| 7848 | // explicitly allowed in this section is ill-formed. |
| 7849 | // |
| 7850 | // This includes auto-typed variable template instantiations. |
| 7851 | if (R->isUndeducedType()) { |
| 7852 | Diag(T->getTypeLoc().getLocStart(), |
| 7853 | diag::err_auto_not_allowed_var_inst); |
| 7854 | return true; |
| 7855 | } |
| 7856 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7857 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7858 | // C++1y [temp.explicit]p3: |
| 7859 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7860 | // in the declaration shall be a template-id. |
| 7861 | Diag(D.getIdentifierLoc(), |
| 7862 | diag::err_explicit_instantiation_without_template_id) |
| 7863 | << PrevTemplate; |
| 7864 | Diag(PrevTemplate->getLocation(), |
| 7865 | diag::note_explicit_instantiation_here); |
| 7866 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7867 | } |
| 7868 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7869 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 7870 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 7871 | if (PrevTemplate->isConcept()) { |
| 7872 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 7873 | << 1 /*variable*/ << 0 /*explicitly instantiated*/; |
| 7874 | Diag(PrevTemplate->getLocation(), diag::note_previous_declaration); |
| 7875 | return true; |
| 7876 | } |
| 7877 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7878 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7879 | TemplateArgumentListInfo TemplateArgs = |
| 7880 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7881 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7882 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7883 | D.getIdentifierLoc(), TemplateArgs); |
| 7884 | if (Res.isInvalid()) |
| 7885 | return true; |
| 7886 | |
| 7887 | // Ignore access control bits, we don't need them for redeclaration |
| 7888 | // checking. |
| 7889 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7890 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7891 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7892 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7893 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7894 | // or a static data member of a class template specialization, the name of |
| 7895 | // the class template specialization in the qualified-id for the member |
| 7896 | // name shall be a simple-template-id. |
| 7897 | // |
| 7898 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7899 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7900 | // This does not apply to variable template specializations, where the |
| 7901 | // template-id is in the unqualified-id instead. |
| 7902 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7903 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7904 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7905 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7906 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7907 | // Check the scope of this explicit instantiation. |
| 7908 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7909 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7910 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7911 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7912 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7913 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7914 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7915 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7916 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7917 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7918 | if (!HasNoEffect) { |
| 7919 | // Instantiate static data member or variable template. |
| 7920 | |
| 7921 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7922 | if (PrevTemplate) { |
| 7923 | // Merge attributes. |
| 7924 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7925 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7926 | } |
| 7927 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7928 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7929 | } |
| 7930 | |
| 7931 | // Check the new variable specialization against the parsed input. |
| 7932 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7933 | Diag(T->getTypeLoc().getLocStart(), |
| 7934 | diag::err_invalid_var_template_spec_type) |
| 7935 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7936 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7937 | << 2 << PrevTemplate->getDeclName(); |
| 7938 | return true; |
| 7939 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7940 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7941 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7942 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7943 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7944 | |
| 7945 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7946 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7947 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7948 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7949 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7950 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7951 | HasExplicitTemplateArgs = true; |
| 7952 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7953 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7954 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7955 | // A [...] function [...] can be explicitly instantiated from its template. |
| 7956 | // A member function [...] of a class template can be explicitly |
| 7957 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7958 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7959 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7960 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7961 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7962 | P != PEnd; ++P) { |
| 7963 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7964 | if (!HasExplicitTemplateArgs) { |
| 7965 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 7966 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 7967 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7968 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7969 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7970 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7971 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 7972 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7973 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7974 | } |
| 7975 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7976 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7977 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 7978 | if (!FunTmpl) |
| 7979 | continue; |
| 7980 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7981 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7982 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7983 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7984 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7985 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7986 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7987 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7988 | // Keep track of almost-matches. |
| 7989 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7990 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7991 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7992 | (void)TDK; |
| 7993 | continue; |
| 7994 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7995 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7996 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7997 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7998 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7999 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8000 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 8001 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8002 | D.getIdentifierLoc(), |
| 8003 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 8004 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 8005 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8006 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8007 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8008 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8009 | |
| 8010 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 8011 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8012 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 8013 | // C++11 [except.spec]p4 |
| 8014 | // In an explicit instantiation an exception-specification may be specified, |
| 8015 | // but is not required. |
| 8016 | // If an exception-specification is specified in an explicit instantiation |
| 8017 | // directive, it shall be compatible with the exception-specifications of |
| 8018 | // other declarations of that function. |
| 8019 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 8020 | if (FPT->hasExceptionSpec()) { |
| 8021 | unsigned DiagID = |
| 8022 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 8023 | if (getLangOpts().MicrosoftExt) |
| 8024 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 8025 | bool Result = CheckEquivalentExceptionSpec( |
| 8026 | PDiag(DiagID) << Specialization->getType(), |
| 8027 | PDiag(diag::note_explicit_instantiation_here), |
| 8028 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 8029 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 8030 | // In Microsoft mode, mismatching exception specifications just cause a |
| 8031 | // warning. |
| 8032 | if (!getLangOpts().MicrosoftExt && Result) |
| 8033 | return true; |
| 8034 | } |
| 8035 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8036 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8037 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8038 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 8039 | << Specialization |
| 8040 | << (Specialization->getTemplateSpecializationKind() == |
| 8041 | TSK_ExplicitSpecialization); |
| 8042 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 8043 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8044 | } |
| 8045 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8046 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8047 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 8048 | PrevDecl = Specialization; |
| 8049 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8050 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8051 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8052 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8053 | PrevDecl, |
| 8054 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8055 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8056 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8057 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8058 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8059 | // FIXME: We may still want to build some representation of this |
| 8060 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8061 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8062 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8063 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 8064 | |
| 8065 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 8066 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 8067 | if (Attr) |
| 8068 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8069 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8070 | if (Specialization->isDefined()) { |
| 8071 | // Let the ASTConsumer know that this function has been explicitly |
| 8072 | // instantiated now, and its linkage might have changed. |
| 8073 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 8074 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 8075 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8076 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8077 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8078 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8079 | // or a static data member of a class template specialization, the name of |
| 8080 | // the class template specialization in the qualified-id for the member |
| 8081 | // name shall be a simple-template-id. |
| 8082 | // |
| 8083 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8084 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8085 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8086 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8087 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8088 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8089 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8090 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8091 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8092 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8093 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8094 | if (FunTmpl && FunTmpl->isConcept() && |
| 8095 | !D.getDeclSpec().isConceptSpecified()) { |
| 8096 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8097 | << 0 /*function*/ << 0 /*explicitly instantiated*/; |
| 8098 | Diag(FunTmpl->getLocation(), diag::note_previous_declaration); |
| 8099 | return true; |
| 8100 | } |
| 8101 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8102 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8103 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8104 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8105 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8106 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8107 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8108 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8109 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8110 | } |
| 8111 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8112 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8113 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 8114 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 8115 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 8116 | // This has to hold, because SS is expected to be defined. |
| 8117 | assert(Name && "Expected a name in a dependent tag"); |
| 8118 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8119 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8120 | if (!NNS) |
| 8121 | return true; |
| 8122 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8123 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 8124 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8125 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 8126 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8127 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8128 | return true; |
| 8129 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8130 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8131 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8132 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8133 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 8134 | |
| 8135 | // Create type-source location information for this type. |
| 8136 | TypeLocBuilder TLB; |
| 8137 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8138 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8139 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8140 | TL.setNameLoc(NameLoc); |
| 8141 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8142 | } |
| 8143 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8144 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8145 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 8146 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 8147 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8148 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8149 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8150 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8151 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8152 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8153 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8154 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8155 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8156 | << FixItHint::CreateRemoval(TypenameLoc); |
| 8157 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8158 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8159 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 8160 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 8161 | if (T.isNull()) |
| 8162 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8163 | |
| 8164 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8165 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8166 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8167 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8168 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8169 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8170 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8171 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8172 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8173 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8174 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8175 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8176 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8177 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8178 | } |
| 8179 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8180 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8181 | Sema::ActOnTypenameType(Scope *S, |
| 8182 | SourceLocation TypenameLoc, |
| 8183 | const CXXScopeSpec &SS, |
| 8184 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8185 | TemplateTy TemplateIn, |
| 8186 | SourceLocation TemplateNameLoc, |
| 8187 | SourceLocation LAngleLoc, |
| 8188 | ASTTemplateArgsPtr TemplateArgsIn, |
| 8189 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8190 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8191 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8192 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8193 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8194 | diag::ext_typename_outside_of_template) |
| 8195 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8196 | |
| 8197 | // Translate the parser's template argument list in our AST format. |
| 8198 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8199 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 8200 | |
| 8201 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8202 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 8203 | // Construct a dependent template specialization type. |
| 8204 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8205 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8206 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 8207 | DTN->getQualifier(), |
| 8208 | DTN->getIdentifier(), |
| 8209 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8210 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8211 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8212 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8213 | DependentTemplateSpecializationTypeLoc SpecTL |
| 8214 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8215 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 8216 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 8217 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8218 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8219 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8220 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8221 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8222 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8223 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 8224 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8225 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8226 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 8227 | if (T.isNull()) |
| 8228 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8229 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8230 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8231 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8232 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8233 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8234 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 8235 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8236 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8237 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8238 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8239 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 8240 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8241 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 8242 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8243 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8244 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8245 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8246 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 8247 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 8248 | } |
| 8249 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8250 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8251 | /// Determine whether this failed name lookup should be treated as being |
| 8252 | /// disabled by a usage of std::enable_if. |
| 8253 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8254 | SourceRange &CondRange) { |
| 8255 | // We must be looking for a ::type... |
| 8256 | if (!II.isStr("type")) |
| 8257 | return false; |
| 8258 | |
| 8259 | // ... within an explicitly-written template specialization... |
| 8260 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8261 | return false; |
| 8262 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8263 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8264 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8265 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8266 | return false; |
| 8267 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8268 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8269 | |
| 8270 | // ... which names a complete class template declaration... |
| 8271 | const TemplateDecl *EnableIfDecl = |
| 8272 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8273 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8274 | return false; |
| 8275 | |
| 8276 | // ... called "enable_if". |
| 8277 | const IdentifierInfo *EnableIfII = |
| 8278 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8279 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8280 | return false; |
| 8281 | |
| 8282 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8283 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8284 | return true; |
| 8285 | } |
| 8286 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8287 | /// \brief Build the type that describes a C++ typename specifier, |
| 8288 | /// e.g., "typename T::type". |
| 8289 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8290 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 8291 | SourceLocation KeywordLoc, |
| 8292 | NestedNameSpecifierLoc QualifierLoc, |
| 8293 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8294 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8295 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8296 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8297 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8298 | DeclContext *Ctx = computeDeclContext(SS); |
| 8299 | if (!Ctx) { |
| 8300 | // If the nested-name-specifier is dependent and couldn't be |
| 8301 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8302 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 8303 | return Context.getDependentNameType(Keyword, |
| 8304 | QualifierLoc.getNestedNameSpecifier(), |
| 8305 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8306 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8307 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8308 | // If the nested-name-specifier refers to the current instantiation, |
| 8309 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8310 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8311 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8312 | // 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] | 8313 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8314 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8315 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8316 | |
| 8317 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8318 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8319 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8320 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8321 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8322 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8323 | case LookupResult::NotFound: { |
| 8324 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8325 | // a more specific diagnostic. |
| 8326 | SourceRange CondRange; |
| 8327 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8328 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8329 | << Ctx << CondRange; |
| 8330 | return QualType(); |
| 8331 | } |
| 8332 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8333 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8334 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8335 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8336 | |
| 8337 | case LookupResult::FoundUnresolvedValue: { |
| 8338 | // We found a using declaration that is a value. Most likely, the using |
| 8339 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8340 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8341 | IILoc); |
| 8342 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8343 | << Name << Ctx << FullRange; |
| 8344 | if (UnresolvedUsingValueDecl *Using |
| 8345 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8346 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8347 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8348 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8349 | } |
| 8350 | } |
| 8351 | // Fall through to create a dependent typename type, from which we can recover |
| 8352 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8353 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8354 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8355 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8356 | return Context.getDependentNameType(Keyword, |
| 8357 | QualifierLoc.getNestedNameSpecifier(), |
| 8358 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8359 | |
| 8360 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8361 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8362 | // We found a type. Build an ElaboratedType, since the |
| 8363 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8364 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8365 | return Context.getElaboratedType(ETK_Typename, |
| 8366 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8367 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8368 | } |
| 8369 | |
| 8370 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8371 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8372 | break; |
| 8373 | |
| 8374 | case LookupResult::FoundOverloaded: |
| 8375 | DiagID = diag::err_typename_nested_not_type; |
| 8376 | Referenced = *Result.begin(); |
| 8377 | break; |
| 8378 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8379 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8380 | return QualType(); |
| 8381 | } |
| 8382 | |
| 8383 | // If we get here, it's because name lookup did not find a |
| 8384 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8385 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8386 | IILoc); |
| 8387 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8388 | if (Referenced) |
| 8389 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8390 | << Name; |
| 8391 | return QualType(); |
| 8392 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8393 | |
| 8394 | namespace { |
| 8395 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8396 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8397 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8398 | SourceLocation Loc; |
| 8399 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8400 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8401 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8402 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8403 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8404 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8405 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8406 | DeclarationName Entity) |
| 8407 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8408 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8409 | |
| 8410 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8411 | /// transformed. |
| 8412 | /// |
| 8413 | /// For the purposes of type reconstruction, a type has already been |
| 8414 | /// transformed if it is NULL or if it is not dependent. |
| 8415 | bool AlreadyTransformed(QualType T) { |
| 8416 | return T.isNull() || !T->isDependentType(); |
| 8417 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8418 | |
| 8419 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8420 | /// rebuilt. |
| 8421 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8422 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8423 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8424 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8425 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8426 | /// \brief Sets the "base" location and entity when that |
| 8427 | /// information is known based on another transformation. |
| 8428 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8429 | this->Loc = Loc; |
| 8430 | this->Entity = Entity; |
| 8431 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8432 | |
| 8433 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8434 | // Lambdas never need to be transformed. |
| 8435 | return E; |
| 8436 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8437 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 8438 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8439 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8440 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8441 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8442 | /// 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] | 8443 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8444 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8445 | /// partial specialization thereof). This routine will rebuild that type now |
| 8446 | /// that we have entered the declarator's scope, which may produce different |
| 8447 | /// canonical types, e.g., |
| 8448 | /// |
| 8449 | /// \code |
| 8450 | /// template<typename T> |
| 8451 | /// struct X { |
| 8452 | /// typedef T* pointer; |
| 8453 | /// pointer data(); |
| 8454 | /// }; |
| 8455 | /// |
| 8456 | /// template<typename T> |
| 8457 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8458 | /// \endcode |
| 8459 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8460 | /// 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] | 8461 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8462 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8463 | /// 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] | 8464 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8465 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8466 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8467 | SourceLocation Loc, |
| 8468 | DeclarationName Name) { |
| 8469 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8470 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8471 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8472 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8473 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8474 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8475 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8476 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8477 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8478 | DeclarationName()); |
| 8479 | return Rebuilder.TransformExpr(E); |
| 8480 | } |
| 8481 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8482 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8483 | if (SS.isInvalid()) |
| 8484 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8485 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8486 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8487 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8488 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8489 | NestedNameSpecifierLoc Rebuilt |
| 8490 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8491 | if (!Rebuilt) |
| 8492 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8493 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8494 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8495 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8496 | } |
| 8497 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8498 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8499 | /// instantiation. |
| 8500 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8501 | TemplateParameterList *Params) { |
| 8502 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8503 | Decl *Param = Params->getParam(I); |
| 8504 | |
| 8505 | // There is nothing to rebuild in a type parameter. |
| 8506 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8507 | continue; |
| 8508 | |
| 8509 | // Rebuild the template parameter list of a template template parameter. |
| 8510 | if (TemplateTemplateParmDecl *TTP |
| 8511 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8512 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8513 | TTP->getTemplateParameters())) |
| 8514 | return true; |
| 8515 | |
| 8516 | continue; |
| 8517 | } |
| 8518 | |
| 8519 | // Rebuild the type of a non-type template parameter. |
| 8520 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8521 | TypeSourceInfo *NewTSI |
| 8522 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8523 | NTTP->getLocation(), |
| 8524 | NTTP->getDeclName()); |
| 8525 | if (!NewTSI) |
| 8526 | return true; |
| 8527 | |
| 8528 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8529 | NTTP->setTypeSourceInfo(NewTSI); |
| 8530 | NTTP->setType(NewTSI->getType()); |
| 8531 | } |
| 8532 | } |
| 8533 | |
| 8534 | return false; |
| 8535 | } |
| 8536 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8537 | /// \brief Produces a formatted string that describes the binding of |
| 8538 | /// template parameters to template arguments. |
| 8539 | std::string |
| 8540 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8541 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8542 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8543 | } |
| 8544 | |
| 8545 | std::string |
| 8546 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8547 | const TemplateArgument *Args, |
| 8548 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8549 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8550 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8551 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8552 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8553 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8554 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8555 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8556 | if (I >= NumArgs) |
| 8557 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8558 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8559 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8560 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8561 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8562 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8563 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8564 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8565 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8566 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8567 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8568 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8569 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8570 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8571 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8572 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8573 | |
| 8574 | Out << ']'; |
| 8575 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8576 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8577 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8578 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8579 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8580 | if (!FD) |
| 8581 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8582 | |
| 8583 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8584 | |
| 8585 | // Take tokens to avoid allocations |
| 8586 | LPT->Toks.swap(Toks); |
| 8587 | LPT->D = FnD; |
Chandler Carruth | 52cee4d | 2015-03-26 09:08:15 +0000 | [diff] [blame] | 8588 | LateParsedTemplateMap.insert(std::make_pair(FD, LPT)); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8589 | |
| 8590 | FD->setLateTemplateParsed(true); |
| 8591 | } |
| 8592 | |
| 8593 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8594 | if (!FD) |
| 8595 | return; |
| 8596 | FD->setLateTemplateParsed(false); |
| 8597 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8598 | |
| 8599 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8600 | DeclContext *DC = CurContext; |
| 8601 | |
| 8602 | while (DC) { |
| 8603 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8604 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8605 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8606 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8607 | return false; |
| 8608 | |
| 8609 | DC = DC->getParent(); |
| 8610 | } |
| 8611 | return false; |
| 8612 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8613 | |
| 8614 | /// \brief Walk the path from which a declaration was instantiated, and check |
| 8615 | /// that every explicit specialization along that path is visible. This enforces |
| 8616 | /// C++ [temp.expl.spec]/6: |
| 8617 | /// |
| 8618 | /// If a template, a member template or a member of a class template is |
| 8619 | /// explicitly specialized then that specialization shall be declared before |
| 8620 | /// the first use of that specialization that would cause an implicit |
| 8621 | /// instantiation to take place, in every translation unit in which such a |
| 8622 | /// use occurs; no diagnostic is required. |
| 8623 | /// |
| 8624 | /// and also C++ [temp.class.spec]/1: |
| 8625 | /// |
| 8626 | /// A partial specialization shall be declared before the first use of a |
| 8627 | /// class template specialization that would make use of the partial |
| 8628 | /// specialization as the result of an implicit or explicit instantiation |
| 8629 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 8630 | /// required. |
| 8631 | class ExplicitSpecializationVisibilityChecker { |
| 8632 | Sema &S; |
| 8633 | SourceLocation Loc; |
| 8634 | llvm::SmallVector<Module *, 8> Modules; |
| 8635 | |
| 8636 | public: |
| 8637 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 8638 | : S(S), Loc(Loc) {} |
| 8639 | |
| 8640 | void check(NamedDecl *ND) { |
| 8641 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 8642 | return checkImpl(FD); |
| 8643 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 8644 | return checkImpl(RD); |
| 8645 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 8646 | return checkImpl(VD); |
| 8647 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 8648 | return checkImpl(ED); |
| 8649 | } |
| 8650 | |
| 8651 | private: |
| 8652 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 8653 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 8654 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 8655 | const bool Recover = true; |
| 8656 | |
| 8657 | // If we got a custom set of modules (because only a subset of the |
| 8658 | // declarations are interesting), use them, otherwise let |
| 8659 | // diagnoseMissingImport intelligently pick some. |
| 8660 | if (Modules.empty()) |
| 8661 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 8662 | else |
| 8663 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 8664 | } |
| 8665 | |
| 8666 | // Check a specific declaration. There are three problematic cases: |
| 8667 | // |
| 8668 | // 1) The declaration is an explicit specialization of a template |
| 8669 | // specialization. |
| 8670 | // 2) The declaration is an explicit specialization of a member of an |
| 8671 | // templated class. |
| 8672 | // 3) The declaration is an instantiation of a template, and that template |
| 8673 | // is an explicit specialization of a member of a templated class. |
| 8674 | // |
| 8675 | // We don't need to go any deeper than that, as the instantiation of the |
| 8676 | // surrounding class / etc is not triggered by whatever triggered this |
| 8677 | // instantiation, and thus should be checked elsewhere. |
| 8678 | template<typename SpecDecl> |
| 8679 | void checkImpl(SpecDecl *Spec) { |
| 8680 | bool IsHiddenExplicitSpecialization = false; |
| 8681 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 8682 | IsHiddenExplicitSpecialization = |
| 8683 | Spec->getMemberSpecializationInfo() |
| 8684 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
| 8685 | : !S.hasVisibleDeclaration(Spec); |
| 8686 | } else { |
| 8687 | checkInstantiated(Spec); |
| 8688 | } |
| 8689 | |
| 8690 | if (IsHiddenExplicitSpecialization) |
| 8691 | diagnose(Spec->getMostRecentDecl(), false); |
| 8692 | } |
| 8693 | |
| 8694 | void checkInstantiated(FunctionDecl *FD) { |
| 8695 | if (auto *TD = FD->getPrimaryTemplate()) |
| 8696 | checkTemplate(TD); |
| 8697 | } |
| 8698 | |
| 8699 | void checkInstantiated(CXXRecordDecl *RD) { |
| 8700 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 8701 | if (!SD) |
| 8702 | return; |
| 8703 | |
| 8704 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 8705 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 8706 | checkTemplate(TD); |
| 8707 | else if (auto *TD = |
| 8708 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 8709 | if (!S.hasVisibleDeclaration(TD)) |
| 8710 | diagnose(TD, true); |
| 8711 | checkTemplate(TD); |
| 8712 | } |
| 8713 | } |
| 8714 | |
| 8715 | void checkInstantiated(VarDecl *RD) { |
| 8716 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 8717 | if (!SD) |
| 8718 | return; |
| 8719 | |
| 8720 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 8721 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 8722 | checkTemplate(TD); |
| 8723 | else if (auto *TD = |
| 8724 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 8725 | if (!S.hasVisibleDeclaration(TD)) |
| 8726 | diagnose(TD, true); |
| 8727 | checkTemplate(TD); |
| 8728 | } |
| 8729 | } |
| 8730 | |
| 8731 | void checkInstantiated(EnumDecl *FD) {} |
| 8732 | |
| 8733 | template<typename TemplDecl> |
| 8734 | void checkTemplate(TemplDecl *TD) { |
| 8735 | if (TD->isMemberSpecialization()) { |
| 8736 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 8737 | diagnose(TD->getMostRecentDecl(), false); |
| 8738 | } |
| 8739 | } |
| 8740 | }; |
| 8741 | |
| 8742 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 8743 | if (!getLangOpts().Modules) |
| 8744 | return; |
| 8745 | |
| 8746 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 8747 | } |
| 8748 | |
| 8749 | /// \brief Check whether a template partial specialization that we've discovered |
| 8750 | /// is hidden, and produce suitable diagnostics if so. |
| 8751 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 8752 | NamedDecl *Spec) { |
| 8753 | llvm::SmallVector<Module *, 8> Modules; |
| 8754 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 8755 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 8756 | MissingImportKind::PartialSpecialization, |
| 8757 | /*Recover*/true); |
| 8758 | } |