Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===// |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements semantic analysis for C++ templates. |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 12 | #include "TreeTransform.h" |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/ExprCXX.h" |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeVisitor.h" |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Builtins.h" |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DeclSpec.h" |
| 26 | #include "clang/Sema/Lookup.h" |
| 27 | #include "clang/Sema/ParsedTemplate.h" |
| 28 | #include "clang/Sema/Scope.h" |
| 29 | #include "clang/Sema/SemaInternal.h" |
| 30 | #include "clang/Sema/Template.h" |
| 31 | #include "clang/Sema/TemplateDeduction.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallBitVector.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 35 | |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 36 | #include <iterator> |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 37 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 38 | using namespace sema; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 39 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 40 | // Exported for use by Parser. |
| 41 | SourceRange |
| 42 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 43 | unsigned N) { |
| 44 | if (!N) return SourceRange(); |
| 45 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 46 | } |
| 47 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 48 | /// \brief Determine whether the declaration found is acceptable as the name |
| 49 | /// of a template and, if so, return that template declaration. Otherwise, |
| 50 | /// returns NULL. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 51 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 52 | NamedDecl *Orig, |
| 53 | bool AllowFunctionTemplates) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 54 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 56 | if (isa<TemplateDecl>(D)) { |
| 57 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 58 | return nullptr; |
| 59 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 60 | return Orig; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 61 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 63 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 64 | // C++ [temp.local]p1: |
| 65 | // Like normal (non-template) classes, class templates have an |
| 66 | // injected-class-name (Clause 9). The injected-class-name |
| 67 | // can be used with or without a template-argument-list. When |
| 68 | // it is used without a template-argument-list, it is |
| 69 | // equivalent to the injected-class-name followed by the |
| 70 | // template-parameters of the class template enclosed in |
| 71 | // <>. When it is used with a template-argument-list, it |
| 72 | // refers to the specified class template specialization, |
| 73 | // which could be the current specialization or another |
| 74 | // specialization. |
| 75 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 76 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 77 | if (Record->getDescribedClassTemplate()) |
| 78 | return Record->getDescribedClassTemplate(); |
| 79 | |
| 80 | if (ClassTemplateSpecializationDecl *Spec |
| 81 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 82 | return Spec->getSpecializedTemplate(); |
| 83 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 85 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 86 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 88 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 91 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
| 92 | bool AllowFunctionTemplates) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 93 | // The set of class templates we've already seen. |
| 94 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 95 | LookupResult::Filter filter = R.makeFilter(); |
| 96 | while (filter.hasNext()) { |
| 97 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 98 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
| 99 | AllowFunctionTemplates); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 100 | if (!Repl) |
| 101 | filter.erase(); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 102 | else if (Repl != Orig) { |
| 103 | |
| 104 | // C++ [temp.local]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 105 | // A lookup that finds an injected-class-name (10.2) can result in an |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 106 | // ambiguity in certain cases (for example, if it is found in more than |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 107 | // one base class). If all of the injected-class-names that are found |
| 108 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 109 | // is used as a template-name, the reference refers to the class |
| 110 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 111 | // ambiguous. |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 112 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 113 | if (!ClassTemplates.insert(ClassTmpl).second) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 114 | filter.erase(); |
| 115 | continue; |
| 116 | } |
John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 117 | |
| 118 | // FIXME: we promote access to public here as a workaround to |
| 119 | // the fact that LookupResult doesn't let us remember that we |
| 120 | // found this template through a particular injected class name, |
| 121 | // which means we end up doing nasty things to the invariants. |
| 122 | // Pretending that access is public is *much* safer. |
| 123 | filter.replace(Repl, AS_public); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 124 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 125 | } |
| 126 | filter.done(); |
| 127 | } |
| 128 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 129 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 130 | bool AllowFunctionTemplates) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 131 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 132 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 133 | return true; |
| 134 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 135 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 138 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 139 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 140 | bool hasTemplateKeyword, |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 141 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 142 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 143 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 144 | TemplateTy &TemplateResult, |
| 145 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 146 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 148 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 149 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 151 | switch (Name.getKind()) { |
| 152 | case UnqualifiedId::IK_Identifier: |
| 153 | TName = DeclarationName(Name.Identifier); |
| 154 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 155 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 156 | case UnqualifiedId::IK_OperatorFunctionId: |
| 157 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 158 | Name.OperatorFunctionId.Operator); |
| 159 | break; |
| 160 | |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 161 | case UnqualifiedId::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 162 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 163 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 165 | default: |
| 166 | return TNK_Non_template; |
| 167 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 169 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 171 | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 172 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 173 | MemberOfUnknownSpecialization); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 174 | if (R.empty()) return TNK_Non_template; |
| 175 | if (R.isAmbiguous()) { |
| 176 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 177 | R.suppressDiagnostics(); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 178 | |
| 179 | // FIXME: we might have ambiguous templates, in which case we |
| 180 | // should at least parse them properly! |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 181 | return TNK_Non_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 182 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 183 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 184 | TemplateName Template; |
| 185 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 187 | unsigned ResultCount = R.end() - R.begin(); |
| 188 | if (ResultCount > 1) { |
| 189 | // We assume that we'll preserve the qualifier from a function |
| 190 | // template name in other ways. |
| 191 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 192 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 193 | |
| 194 | // We'll do this lookup again later. |
| 195 | R.suppressDiagnostics(); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 196 | } else { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 197 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 198 | |
| 199 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 200 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 201 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 202 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 203 | } else { |
| 204 | Template = TemplateName(TD); |
| 205 | } |
| 206 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 207 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 208 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 209 | |
| 210 | // We'll do this lookup again later. |
| 211 | R.suppressDiagnostics(); |
| 212 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 213 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 214 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
| 215 | isa<BuiltinTemplateDecl>(TD)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 216 | TemplateKind = |
| 217 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 218 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 219 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 221 | TemplateResult = TemplateTy::make(Template); |
| 222 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 223 | } |
| 224 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 225 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 226 | SourceLocation IILoc, |
| 227 | Scope *S, |
| 228 | const CXXScopeSpec *SS, |
| 229 | TemplateTy &SuggestedTemplate, |
| 230 | TemplateNameKind &SuggestedKind) { |
| 231 | // We can't recover unless there's a dependent scope specifier preceding the |
| 232 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 233 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 234 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 235 | computeDeclContext(*SS)) |
| 236 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 237 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 238 | // The code is missing a 'template' keyword prior to the dependent template |
| 239 | // name. |
| 240 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 241 | Diag(IILoc, diag::err_template_kw_missing) |
| 242 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 243 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 244 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 245 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 246 | SuggestedKind = TNK_Dependent_template_name; |
| 247 | return true; |
| 248 | } |
| 249 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 250 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 251 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 252 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 253 | bool EnteringContext, |
| 254 | bool &MemberOfUnknownSpecialization) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 255 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 256 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 257 | DeclContext *LookupCtx = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 258 | bool isDependent = false; |
| 259 | if (!ObjectType.isNull()) { |
| 260 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 261 | // x->B::f, and we are looking into the type of the object. |
| 262 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 263 | LookupCtx = computeDeclContext(ObjectType); |
| 264 | isDependent = ObjectType->isDependentType(); |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 265 | assert((isDependent || !ObjectType->isIncompleteType() || |
| 266 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 267 | "Caller should have completed object type"); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 268 | |
| 269 | // Template names cannot appear inside an Objective-C class or object type. |
| 270 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 271 | Found.clear(); |
| 272 | return; |
| 273 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 274 | } else if (SS.isSet()) { |
| 275 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 276 | // so long into the context associated with the prior nested-name-specifier. |
| 277 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 278 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 279 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 280 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 281 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | |
| 285 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 286 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 287 | if (LookupCtx) { |
| 288 | // Perform "qualified" name lookup into the declaration context we |
| 289 | // computed, which is either the type of the base of a member access |
| 290 | // expression or the declaration context associated with a prior |
| 291 | // nested-name-specifier. |
| 292 | LookupQualifiedName(Found, LookupCtx); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 293 | if (!ObjectType.isNull() && Found.empty()) { |
| 294 | // C++ [basic.lookup.classref]p1: |
| 295 | // In a class member access expression (5.2.5), if the . or -> token is |
| 296 | // immediately followed by an identifier followed by a <, the |
| 297 | // identifier must be looked up to determine whether the < is the |
| 298 | // beginning of a template argument list (14.2) or a less-than operator. |
| 299 | // The identifier is first looked up in the class of the object |
| 300 | // expression. If the identifier is not found, it is then looked up in |
| 301 | // the context of the entire postfix-expression and shall name a class |
| 302 | // or function template. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 303 | if (S) LookupName(Found, S); |
| 304 | ObjectTypeSearchedInScope = true; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 305 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 306 | } |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 307 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 308 | // We cannot look into a dependent object type or nested nme |
| 309 | // specifier. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 310 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 311 | return; |
| 312 | } else { |
| 313 | // Perform unqualified name lookup in the current scope. |
| 314 | LookupName(Found, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 315 | |
| 316 | if (!ObjectType.isNull()) |
| 317 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 320 | if (Found.empty() && !isDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 321 | // If we did not find any names, attempt to correct any typos. |
| 322 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 323 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 324 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 325 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 326 | FilterCCC->WantTypeSpecifiers = false; |
| 327 | FilterCCC->WantExpressionKeywords = false; |
| 328 | FilterCCC->WantRemainingKeywords = false; |
| 329 | FilterCCC->WantCXXNamedCasts = true; |
| 330 | if (TypoCorrection Corrected = CorrectTypo( |
| 331 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 332 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 333 | Found.setLookupName(Corrected.getCorrection()); |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 334 | if (auto *ND = Corrected.getFoundDecl()) |
| 335 | Found.addDecl(ND); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 336 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 337 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 338 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 339 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 340 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 341 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 342 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 343 | << Name << LookupCtx << DroppedSpecifier |
| 344 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 345 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 346 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 347 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 348 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 349 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 350 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 354 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 355 | if (Found.empty()) { |
| 356 | if (isDependent) |
| 357 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 358 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 359 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 361 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 362 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 363 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 364 | // [...] If the lookup in the class of the object expression finds a |
| 365 | // template, the name is also looked up in the context of the entire |
| 366 | // postfix-expression and [...] |
| 367 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 368 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 369 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 370 | LookupOrdinaryName); |
| 371 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 372 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 373 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 374 | if (FoundOuter.empty()) { |
| 375 | // - if the name is not found, the name found in the class of the |
| 376 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 377 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 378 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 379 | // - if the name is found in the context of the entire |
| 380 | // postfix-expression and does not name a class template, the name |
| 381 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 382 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 383 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 384 | // - if the name found is a class template, it must refer to the same |
| 385 | // entity as the one found in the class of the object expression, |
| 386 | // otherwise the program is ill-formed. |
| 387 | if (!Found.isSingleResult() || |
| 388 | Found.getFoundDecl()->getCanonicalDecl() |
| 389 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 390 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 391 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 392 | << Found.getLookupName() |
| 393 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 394 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 395 | diag::note_ambig_member_ref_object_type) |
| 396 | << ObjectType; |
| 397 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 398 | diag::note_ambig_member_ref_scope); |
| 399 | |
| 400 | // Recover by taking the template that we found in the object |
| 401 | // expression's type. |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 407 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 408 | /// was just parsed. This is only possible with an explicit scope |
| 409 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 410 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 411 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 412 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 413 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 414 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 415 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 416 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 417 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 418 | // C++11 [expr.prim.general]p12: |
| 419 | // An id-expression that denotes a non-static data member or non-static |
| 420 | // member function of a class can only be used: |
| 421 | // (...) |
| 422 | // - if that id-expression denotes a non-static data member and it |
| 423 | // appears in an unevaluated operand. |
| 424 | // |
| 425 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 426 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 427 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 428 | // always a MemberExpr. |
| 429 | bool MightBeCxx11UnevalField = |
| 430 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 431 | |
| 432 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && |
| 433 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 434 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 435 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 436 | // Since the 'this' expression is synthesized, we don't need to |
| 437 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 438 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 439 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 440 | return CXXDependentScopeMemberExpr::Create( |
| 441 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 442 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 443 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 446 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 447 | } |
| 448 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 449 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 450 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 451 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 452 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 453 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 454 | return DependentScopeDeclRefExpr::Create( |
| 455 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 456 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame^] | 459 | |
| 460 | /// Determine whether we would be unable to instantiate this template (because |
| 461 | /// it either has no definition, or is in the process of being instantiated). |
| 462 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 463 | NamedDecl *Instantiation, |
| 464 | bool InstantiatedFromMember, |
| 465 | const NamedDecl *Pattern, |
| 466 | const NamedDecl *PatternDef, |
| 467 | TemplateSpecializationKind TSK, |
| 468 | bool Complain /*= true*/) { |
| 469 | assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation)); |
| 470 | |
| 471 | if (PatternDef && (isa<FunctionDecl>(PatternDef) |
| 472 | || !cast<TagDecl>(PatternDef)->isBeingDefined())) { |
| 473 | NamedDecl *SuggestedDef = nullptr; |
| 474 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 475 | /*OnlyNeedComplete*/false)) { |
| 476 | // If we're allowed to diagnose this and recover, do so. |
| 477 | bool Recover = Complain && !isSFINAEContext(); |
| 478 | if (Complain) |
| 479 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 480 | Sema::MissingImportKind::Definition, Recover); |
| 481 | return !Recover; |
| 482 | } |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | |
| 487 | QualType InstantiationTy; |
| 488 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 489 | InstantiationTy = Context.getTypeDeclType(TD); |
| 490 | else |
| 491 | InstantiationTy = cast<FunctionDecl>(Instantiation)->getType(); |
| 492 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) { |
| 493 | // Say nothing |
| 494 | } else if (PatternDef) { |
| 495 | Diag(PointOfInstantiation, |
| 496 | diag::err_template_instantiate_within_definition) |
| 497 | << (TSK != TSK_ImplicitInstantiation) |
| 498 | << InstantiationTy; |
| 499 | // Not much point in noting the template declaration here, since |
| 500 | // we're lexically inside it. |
| 501 | Instantiation->setInvalidDecl(); |
| 502 | } else if (InstantiatedFromMember) { |
| 503 | Diag(PointOfInstantiation, |
| 504 | diag::err_implicit_instantiate_member_undefined) |
| 505 | << InstantiationTy; |
| 506 | Diag(Pattern->getLocation(), diag::note_member_declared_at); |
| 507 | } else { |
| 508 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 509 | << (TSK != TSK_ImplicitInstantiation) |
| 510 | << InstantiationTy; |
| 511 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 512 | } |
| 513 | |
| 514 | // In general, Instantiation isn't marked invalid to get more than one |
| 515 | // error for multiple undefined instantiations. But the code that does |
| 516 | // explicit declaration -> explicit definition conversion can't handle |
| 517 | // invalid declarations, so mark as invalid in that case. |
| 518 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 519 | Instantiation->setInvalidDecl(); |
| 520 | return true; |
| 521 | } |
| 522 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 523 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 524 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 525 | /// declaration at location Loc. Returns true to indicate that this is |
| 526 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 527 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 528 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 529 | |
| 530 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 531 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 532 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 533 | |
| 534 | // C++ [temp.local]p4: |
| 535 | // A template-parameter shall not be redeclared within its |
| 536 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 538 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 539 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 542 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 543 | /// the parameter D to reference the templated declaration and return a pointer |
| 544 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 545 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 546 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 547 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 548 | return Temp; |
| 549 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 550 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 553 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 554 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 555 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 556 | "Only template template arguments can be pack expansions here"); |
| 557 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 558 | "Template template argument pack expansion without packs"); |
| 559 | ParsedTemplateArgument Result(*this); |
| 560 | Result.EllipsisLoc = EllipsisLoc; |
| 561 | return Result; |
| 562 | } |
| 563 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 564 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 565 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 567 | switch (Arg.getKind()) { |
| 568 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 569 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 570 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 571 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 572 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 573 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 574 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 575 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 576 | case ParsedTemplateArgument::NonType: { |
| 577 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 578 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 579 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 581 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 582 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 583 | TemplateArgument TArg; |
| 584 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 585 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 586 | else |
| 587 | TArg = Template; |
| 588 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 589 | Arg.getScopeSpec().getWithLocInContext( |
| 590 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 591 | Arg.getLocation(), |
| 592 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 595 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 596 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 597 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 599 | /// \brief Translates template arguments as provided by the parser |
| 600 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 601 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 602 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 603 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 604 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 605 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 606 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 608 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 609 | SourceLocation Loc, |
| 610 | IdentifierInfo *Name) { |
| 611 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 612 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 613 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 614 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 615 | } |
| 616 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 617 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 618 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 619 | /// the keyword "typename" was used to declare the type parameter |
| 620 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 621 | /// "class" or "typename" keyword. ParamName is the name of the |
| 622 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 623 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 624 | /// If the type parameter has a default argument, it will be added |
| 625 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 626 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 627 | SourceLocation EllipsisLoc, |
| 628 | SourceLocation KeyLoc, |
| 629 | IdentifierInfo *ParamName, |
| 630 | SourceLocation ParamNameLoc, |
| 631 | unsigned Depth, unsigned Position, |
| 632 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 633 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | assert(S->isTemplateParamScope() && |
| 635 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 636 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 637 | SourceLocation Loc = ParamNameLoc; |
| 638 | if (!ParamName) |
| 639 | Loc = KeyLoc; |
| 640 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 641 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 642 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 643 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 644 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 645 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 646 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 647 | |
| 648 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 649 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 650 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 651 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 652 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 653 | IdResolver.AddDecl(Param); |
| 654 | } |
| 655 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 656 | // C++0x [temp.param]p9: |
| 657 | // A default template-argument may be specified for any kind of |
| 658 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 659 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 660 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 661 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 664 | // Handle the default argument, if provided. |
| 665 | if (DefaultArg) { |
| 666 | TypeSourceInfo *DefaultTInfo; |
| 667 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 668 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 669 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 671 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 672 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 673 | UPPC_DefaultArgument)) |
| 674 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 676 | // Check the template argument itself. |
| 677 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 678 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 679 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 680 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 681 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 682 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 683 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 684 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 685 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 688 | /// \brief Check that the type of a non-type template parameter is |
| 689 | /// well-formed. |
| 690 | /// |
| 691 | /// \returns the (possibly-promoted) parameter type if valid; |
| 692 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | QualType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 694 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 695 | // We don't allow variably-modified types as the type of non-type template |
| 696 | // parameters. |
| 697 | if (T->isVariablyModifiedType()) { |
| 698 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 699 | << T; |
| 700 | return QualType(); |
| 701 | } |
| 702 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 703 | // C++ [temp.param]p4: |
| 704 | // |
| 705 | // A non-type template-parameter shall have one of the following |
| 706 | // (optionally cv-qualified) types: |
| 707 | // |
| 708 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 709 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 711 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 713 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 714 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 715 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 716 | // -- std::nullptr_t. |
| 717 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 718 | // If T is a dependent type, we can't do the check now, so we |
| 719 | // assume that it is well-formed. |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 720 | T->isDependentType()) { |
| 721 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 722 | // are ignored when determining its type. |
| 723 | return T.getUnqualifiedType(); |
| 724 | } |
| 725 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 726 | // C++ [temp.param]p8: |
| 727 | // |
| 728 | // A non-type template-parameter of type "array of T" or |
| 729 | // "function returning T" is adjusted to be of type "pointer to |
| 730 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 731 | else if (T->isArrayType() || T->isFunctionType()) |
| 732 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 733 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 734 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 735 | << T; |
| 736 | |
| 737 | return QualType(); |
| 738 | } |
| 739 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 740 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 741 | unsigned Depth, |
| 742 | unsigned Position, |
| 743 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 744 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 745 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 746 | QualType T = TInfo->getType(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 748 | assert(S->isTemplateParamScope() && |
| 749 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 750 | bool Invalid = false; |
| 751 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 752 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 753 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 754 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 755 | Invalid = true; |
| 756 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 757 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 758 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 759 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 760 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 761 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 762 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 763 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 764 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 765 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 766 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 767 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 768 | if (Invalid) |
| 769 | Param->setInvalidDecl(); |
| 770 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 771 | if (ParamName) { |
| 772 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 773 | ParamName); |
| 774 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 775 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 776 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 777 | IdResolver.AddDecl(Param); |
| 778 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 780 | // C++0x [temp.param]p9: |
| 781 | // A default template-argument may be specified for any kind of |
| 782 | // template-parameter that is not a template parameter pack. |
| 783 | if (Default && IsParameterPack) { |
| 784 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 785 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 788 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 789 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 790 | // Check for unexpanded parameter packs. |
| 791 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 792 | return Param; |
| 793 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 794 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 795 | ExprResult DefaultRes = |
| 796 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 797 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 798 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 799 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 800 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 801 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 802 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 803 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 804 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 805 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 806 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 807 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 809 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 810 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 811 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 812 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 813 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 814 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 815 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 816 | IdentifierInfo *Name, |
| 817 | SourceLocation NameLoc, |
| 818 | unsigned Depth, |
| 819 | unsigned Position, |
| 820 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 821 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 822 | assert(S->isTemplateParamScope() && |
| 823 | "Template template parameter not in template parameter scope!"); |
| 824 | |
| 825 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 826 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 827 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 828 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 829 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 830 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 831 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 832 | Param->setAccess(AS_public); |
| 833 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 834 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 835 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 836 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 837 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 838 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 839 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 840 | IdResolver.AddDecl(Param); |
| 841 | } |
| 842 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 843 | if (Params->size() == 0) { |
| 844 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 845 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 846 | Param->setInvalidDecl(); |
| 847 | } |
| 848 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 849 | // C++0x [temp.param]p9: |
| 850 | // A default template-argument may be specified for any kind of |
| 851 | // template-parameter that is not a template parameter pack. |
| 852 | if (IsParameterPack && !Default.isInvalid()) { |
| 853 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 854 | Default = ParsedTemplateArgument(); |
| 855 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 856 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 857 | if (!Default.isInvalid()) { |
| 858 | // Check only that we have a template template argument. We don't want to |
| 859 | // try to check well-formedness now, because our template template parameter |
| 860 | // might have dependent types in its template parameters, which we wouldn't |
| 861 | // be able to match now. |
| 862 | // |
| 863 | // If none of the template template parameter's template arguments mention |
| 864 | // other template parameters, we could actually perform more checking here. |
| 865 | // However, it isn't worth doing. |
| 866 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 867 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 868 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 869 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 870 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 871 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 872 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 873 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 874 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 875 | DefaultArg.getArgument().getAsTemplate(), |
| 876 | UPPC_DefaultArgument)) |
| 877 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 878 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 879 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 880 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 881 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 882 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 885 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 886 | /// constrained by RequiresClause, that contains the template parameters in |
| 887 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 888 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 889 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 890 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 892 | SourceLocation LAngleLoc, |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 893 | ArrayRef<Decl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 894 | SourceLocation RAngleLoc, |
| 895 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 896 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 897 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 898 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 899 | return TemplateParameterList::Create( |
| 900 | Context, TemplateLoc, LAngleLoc, |
| 901 | llvm::makeArrayRef((NamedDecl *const *)Params.data(), Params.size()), |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 902 | RAngleLoc, RequiresClause); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 903 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 904 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 905 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 906 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 907 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 908 | } |
| 909 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 910 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 911 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 912 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 913 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 914 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 915 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 916 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 917 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 918 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 919 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 920 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 922 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 923 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 924 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 925 | |
| 926 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 927 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 928 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 929 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 930 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 931 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 932 | |
| 933 | // There is no such thing as an unnamed class template. |
| 934 | if (!Name) { |
| 935 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 936 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 939 | // Find any previous declaration with this name. For a friend with no |
| 940 | // scope explicitly specified, we only look for tag declarations (per |
| 941 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 942 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 943 | LookupResult Previous(*this, Name, NameLoc, |
| 944 | (SS.isEmpty() && TUK == TUK_Friend) |
| 945 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 946 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 947 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 948 | SemanticContext = computeDeclContext(SS, true); |
| 949 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 950 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 951 | // in the AST, and historically we have just ignored such friend |
| 952 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 953 | Diag(NameLoc, TUK == TUK_Friend |
| 954 | ? diag::warn_template_qualified_friend_ignored |
| 955 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 956 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 957 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 958 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 960 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 961 | return true; |
| 962 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 963 | // If we're adding a template to a dependent context, we may need to |
| 964 | // rebuilding some of the types used within the template parameter list, |
| 965 | // now that we know what the current instantiation is. |
| 966 | if (SemanticContext->isDependentContext()) { |
| 967 | ContextRAII SavedContext(*this, SemanticContext); |
| 968 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 969 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 970 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 971 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 972 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 973 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 974 | } else { |
| 975 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 976 | |
| 977 | // C++14 [class.mem]p14: |
| 978 | // If T is the name of a class, then each of the following shall have a |
| 979 | // name different from T: |
| 980 | // -- every member template of class T |
| 981 | if (TUK != TUK_Friend && |
| 982 | DiagnoseClassNameShadow(SemanticContext, |
| 983 | DeclarationNameInfo(Name, NameLoc))) |
| 984 | return true; |
| 985 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 986 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 987 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 989 | if (Previous.isAmbiguous()) |
| 990 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 991 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 992 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 993 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 994 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 995 | |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 996 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 997 | // Maybe we will complain about the shadowed template parameter. |
| 998 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 999 | // Just pretend that we didn't see the previous declaration. |
| 1000 | PrevDecl = nullptr; |
| 1001 | } |
| 1002 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1003 | // If there is a previous declaration with the same name, check |
| 1004 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1006 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1007 | |
| 1008 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1009 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1010 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1011 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1012 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1013 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1014 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 1015 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1016 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1017 | PrevClassTemplate |
| 1018 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1019 | ->getSpecializedTemplate(); |
| 1020 | } |
| 1021 | } |
| 1022 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1023 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1024 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1025 | // [...] When looking for a prior declaration of a class or a function |
| 1026 | // 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] | 1027 | // function is neither a qualified name nor a template-id, scopes outside |
| 1028 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1029 | if (!SS.isSet()) { |
| 1030 | DeclContext *OutermostContext = CurContext; |
| 1031 | while (!OutermostContext->isFileContext()) |
| 1032 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 1033 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 1034 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1035 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1036 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1037 | SemanticContext = PrevDecl->getDeclContext(); |
| 1038 | } else { |
| 1039 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1040 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1041 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1042 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1043 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1044 | |
| 1045 | // Check that the chosen semantic context doesn't already contain a |
| 1046 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1047 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 1048 | DeclContext *LookupContext = SemanticContext; |
| 1049 | while (LookupContext->isTransparentContext()) |
| 1050 | LookupContext = LookupContext->getLookupParent(); |
| 1051 | LookupQualifiedName(Previous, LookupContext); |
| 1052 | |
| 1053 | if (Previous.isAmbiguous()) |
| 1054 | return true; |
| 1055 | |
| 1056 | if (Previous.begin() != Previous.end()) |
| 1057 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 1058 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 1059 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 1060 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1061 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1062 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1063 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1064 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 1065 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1066 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1067 | if (SS.isEmpty() && |
| 1068 | !(PrevClassTemplate && |
| 1069 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1070 | SemanticContext->getRedeclContext()))) { |
| 1071 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1072 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1073 | diag::note_using_decl_target); |
| 1074 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1075 | // Recover by ignoring the old declaration. |
| 1076 | PrevDecl = PrevClassTemplate = nullptr; |
| 1077 | } |
| 1078 | } |
| 1079 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1080 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1081 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1082 | // for a friend in a dependent context: the template parameter list itself |
| 1083 | // could be dependent. |
| 1084 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1085 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1086 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1087 | /*Complain=*/true, |
| 1088 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1089 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1090 | |
| 1091 | // C++ [temp.class]p4: |
| 1092 | // In a redeclaration, partial specialization, explicit |
| 1093 | // specialization or explicit instantiation of a class template, |
| 1094 | // the class-key shall agree in kind with the original class |
| 1095 | // template declaration (7.1.5.3). |
| 1096 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1097 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1098 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1100 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1101 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1102 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1103 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1106 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1107 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1108 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1109 | // If we have a prior definition that is not visible, treat this as |
| 1110 | // simply making that previous definition visible. |
| 1111 | NamedDecl *Hidden = nullptr; |
| 1112 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1113 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1114 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1115 | assert(Tmpl && "original definition of a class template is not a " |
| 1116 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1117 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1118 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1119 | return Def; |
| 1120 | } |
| 1121 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1122 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1123 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1124 | // FIXME: Would it make sense to try to "forget" the previous |
| 1125 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1126 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1127 | } |
Serge Pavlov | e50bf75 | 2016-06-10 04:39:07 +0000 | [diff] [blame] | 1128 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1129 | } else if (PrevDecl) { |
| 1130 | // C++ [temp]p5: |
| 1131 | // A class template shall not have the same name as any other |
| 1132 | // template, class, function, object, enumeration, enumerator, |
| 1133 | // namespace, or type in the same scope (3.3), except as specified |
| 1134 | // in (14.5.4). |
| 1135 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1136 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1137 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1140 | // Check the template parameter list of this declaration, possibly |
| 1141 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1142 | // template declaration. Skip this check for a friend in a dependent |
| 1143 | // context, because the template parameter list might be dependent. |
| 1144 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1145 | CheckTemplateParameterList( |
| 1146 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1147 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1148 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1149 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1150 | SemanticContext->isDependentContext()) |
| 1151 | ? TPC_ClassTemplateMember |
| 1152 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1153 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1154 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1156 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1157 | // 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] | 1158 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1159 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1160 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1161 | : diag::err_member_decl_does_not_match) |
| 1162 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1163 | Invalid = true; |
| 1164 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1167 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1168 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1170 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1171 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1172 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1173 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1174 | NewClass->setTemplateParameterListsInfo( |
| 1175 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1176 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1177 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1178 | // Add alignment attributes if necessary; these attributes are checked when |
| 1179 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1180 | if (TUK == TUK_Definition) { |
| 1181 | AddAlignmentAttributesForRecord(NewClass); |
| 1182 | AddMsStructLayoutForRecord(NewClass); |
| 1183 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1184 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1185 | ClassTemplateDecl *NewTemplate |
| 1186 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1187 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1188 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1189 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1191 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1192 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1193 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1194 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1195 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1196 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1197 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1198 | (void)T; |
| 1199 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1200 | // 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] | 1201 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1202 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1203 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1204 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1205 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1206 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1207 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1208 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1210 | // Set the lexical context of these templates |
| 1211 | NewClass->setLexicalDeclContext(CurContext); |
| 1212 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1213 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1214 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1215 | NewClass->startDefinition(); |
| 1216 | |
| 1217 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1218 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1219 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1220 | if (PrevClassTemplate) |
| 1221 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1222 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1223 | AddPushedVisibilityAttribute(NewClass); |
| 1224 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1225 | if (TUK != TUK_Friend) { |
| 1226 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1227 | Scope *Outer = S; |
| 1228 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1229 | Outer = Outer->getParent(); |
| 1230 | PushOnScopeChains(NewTemplate, Outer); |
| 1231 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1232 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1233 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1234 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1235 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1236 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1237 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1238 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1239 | // Friend templates are visible in fairly strange ways. |
| 1240 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1241 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1242 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1243 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1244 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1245 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1246 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1247 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1248 | FriendDecl *Friend = FriendDecl::Create( |
| 1249 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1250 | Friend->setAccess(AS_public); |
| 1251 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1252 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1253 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1254 | if (Invalid) { |
| 1255 | NewTemplate->setInvalidDecl(); |
| 1256 | NewClass->setInvalidDecl(); |
| 1257 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1258 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1259 | ActOnDocumentableDecl(NewTemplate); |
| 1260 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1261 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1264 | /// \brief Diagnose the presence of a default template argument on a |
| 1265 | /// template parameter, which is ill-formed in certain contexts. |
| 1266 | /// |
| 1267 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1268 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1269 | Sema::TemplateParamListContext TPC, |
| 1270 | SourceLocation ParamLoc, |
| 1271 | SourceRange DefArgRange) { |
| 1272 | switch (TPC) { |
| 1273 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1274 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1275 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1276 | return false; |
| 1277 | |
| 1278 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1279 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1280 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1281 | // A default template-argument shall not be specified in a |
| 1282 | // function template declaration or a function template |
| 1283 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1284 | // If a friend function template declaration specifies a default |
| 1285 | // template-argument, that declaration shall be a definition and shall be |
| 1286 | // the only declaration of the function template in the translation unit. |
| 1287 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1288 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1289 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1290 | : diag::ext_template_parameter_default_in_function_template) |
| 1291 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1292 | return false; |
| 1293 | |
| 1294 | case Sema::TPC_ClassTemplateMember: |
| 1295 | // C++0x [temp.param]p9: |
| 1296 | // A default template-argument shall not be specified in the |
| 1297 | // template-parameter-lists of the definition of a member of a |
| 1298 | // class template that appears outside of the member's class. |
| 1299 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1300 | << DefArgRange; |
| 1301 | return true; |
| 1302 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1303 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1304 | case Sema::TPC_FriendFunctionTemplate: |
| 1305 | // C++ [temp.param]p9: |
| 1306 | // A default template-argument shall not be specified in a |
| 1307 | // friend template declaration. |
| 1308 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1309 | << DefArgRange; |
| 1310 | return true; |
| 1311 | |
| 1312 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1313 | // for friend function templates if there is only a single |
| 1314 | // declaration (and it is a definition). Strange! |
| 1315 | } |
| 1316 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1317 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1320 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1321 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1322 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1323 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1324 | // A template template parameter which is a parameter pack is also a pack |
| 1325 | // expansion. |
| 1326 | if (TTP->isParameterPack()) |
| 1327 | return false; |
| 1328 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1329 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1330 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1331 | NamedDecl *P = Params->getParam(I); |
| 1332 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1333 | if (!NTTP->isParameterPack() && |
| 1334 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1335 | NTTP->getTypeSourceInfo(), |
| 1336 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1337 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1338 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1339 | continue; |
| 1340 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1343 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1344 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1345 | return true; |
| 1346 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1347 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1348 | return false; |
| 1349 | } |
| 1350 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1351 | /// \brief Checks the validity of a template parameter list, possibly |
| 1352 | /// considering the template parameter list from a previous |
| 1353 | /// declaration. |
| 1354 | /// |
| 1355 | /// If an "old" template parameter list is provided, it must be |
| 1356 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1357 | /// template parameter list. |
| 1358 | /// |
| 1359 | /// \param NewParams Template parameter list for a new template |
| 1360 | /// declaration. This template parameter list will be updated with any |
| 1361 | /// default arguments that are carried through from the previous |
| 1362 | /// template parameter list. |
| 1363 | /// |
| 1364 | /// \param OldParams If provided, template parameter list from a |
| 1365 | /// previous declaration of the same template. Default template |
| 1366 | /// arguments will be merged from the old template parameter list to |
| 1367 | /// the new template parameter list. |
| 1368 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1369 | /// \param TPC Describes the context in which we are checking the given |
| 1370 | /// template parameter list. |
| 1371 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1372 | /// \returns true if an error occurred, false otherwise. |
| 1373 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1374 | TemplateParameterList *OldParams, |
| 1375 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1376 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1378 | // C++ [temp.param]p10: |
| 1379 | // The set of default template-arguments available for use with a |
| 1380 | // template declaration or definition is obtained by merging the |
| 1381 | // default arguments from the definition (if in scope) and all |
| 1382 | // declarations in scope in the same way default function |
| 1383 | // arguments are (8.3.6). |
| 1384 | bool SawDefaultArgument = false; |
| 1385 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1386 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1387 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1388 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1389 | if (OldParams) |
| 1390 | OldParam = OldParams->begin(); |
| 1391 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1392 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1393 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1394 | NewParamEnd = NewParams->end(); |
| 1395 | NewParam != NewParamEnd; ++NewParam) { |
| 1396 | // Variables used to diagnose redundant default arguments |
| 1397 | bool RedundantDefaultArg = false; |
| 1398 | SourceLocation OldDefaultLoc; |
| 1399 | SourceLocation NewDefaultLoc; |
| 1400 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1401 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1402 | bool MissingDefaultArg = false; |
| 1403 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1404 | // Variable used to diagnose non-final parameter packs |
| 1405 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1406 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1407 | if (TemplateTypeParmDecl *NewTypeParm |
| 1408 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1409 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1410 | if (NewTypeParm->hasDefaultArgument() && |
| 1411 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1412 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1413 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1414 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1415 | NewTypeParm->removeDefaultArgument(); |
| 1416 | |
| 1417 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1419 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1420 | if (NewTypeParm->isParameterPack()) { |
| 1421 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1422 | "Parameter packs can't have a default argument!"); |
| 1423 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1424 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1425 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1426 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1427 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1428 | SawDefaultArgument = true; |
| 1429 | RedundantDefaultArg = true; |
| 1430 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1431 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1432 | // Merge the default argument from the old declaration to the |
| 1433 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1434 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1435 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1436 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1437 | SawDefaultArgument = true; |
| 1438 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1439 | } else if (SawDefaultArgument) |
| 1440 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1441 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1442 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1443 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1444 | if (!NewNonTypeParm->isParameterPack() && |
| 1445 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1446 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1447 | UPPC_NonTypeTemplateParameterType)) { |
| 1448 | Invalid = true; |
| 1449 | continue; |
| 1450 | } |
| 1451 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1452 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1453 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1454 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1455 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1456 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1457 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1460 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1461 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1462 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1463 | if (NewNonTypeParm->isParameterPack()) { |
| 1464 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1465 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1466 | if (!NewNonTypeParm->isPackExpansion()) |
| 1467 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1468 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1469 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1470 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1471 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1472 | SawDefaultArgument = true; |
| 1473 | RedundantDefaultArg = true; |
| 1474 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1475 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1476 | // Merge the default argument from the old declaration to the |
| 1477 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1478 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1479 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1480 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1481 | SawDefaultArgument = true; |
| 1482 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1483 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1485 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1486 | TemplateTemplateParmDecl *NewTemplateParm |
| 1487 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1488 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1489 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1490 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1491 | Invalid = true; |
| 1492 | continue; |
| 1493 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1494 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1495 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1496 | if (NewTemplateParm->hasDefaultArgument() && |
| 1497 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1498 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1499 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1500 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1501 | |
| 1502 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1503 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1504 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1505 | if (NewTemplateParm->isParameterPack()) { |
| 1506 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1507 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1508 | if (!NewTemplateParm->isPackExpansion()) |
| 1509 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1510 | } else if (OldTemplateParm && |
| 1511 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 1512 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1513 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1514 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1515 | SawDefaultArgument = true; |
| 1516 | RedundantDefaultArg = true; |
| 1517 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1518 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1519 | // Merge the default argument from the old declaration to the |
| 1520 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1521 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1522 | PreviousDefaultArgLoc |
| 1523 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1524 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1525 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1526 | PreviousDefaultArgLoc |
| 1527 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1528 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1532 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1533 | // If a template parameter of a primary class template or alias template |
| 1534 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1535 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1536 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1537 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1538 | Diag((*NewParam)->getLocation(), |
| 1539 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1540 | Invalid = true; |
| 1541 | } |
| 1542 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1543 | if (RedundantDefaultArg) { |
| 1544 | // C++ [temp.param]p12: |
| 1545 | // A template-parameter shall not be given default arguments |
| 1546 | // by two different declarations in the same scope. |
| 1547 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1548 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1549 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1550 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1551 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1552 | // If a template-parameter of a class template has a default |
| 1553 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1554 | // have a default template-argument supplied or be a template parameter |
| 1555 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1557 | diag::err_template_param_default_arg_missing); |
| 1558 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1559 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1560 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | // If we have an old template parameter list that we're merging |
| 1564 | // in, move on to the next parameter. |
| 1565 | if (OldParams) |
| 1566 | ++OldParam; |
| 1567 | } |
| 1568 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1569 | // We were missing some default arguments at the end of the list, so remove |
| 1570 | // all of the default arguments. |
| 1571 | if (RemoveDefaultArguments) { |
| 1572 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1573 | NewParamEnd = NewParams->end(); |
| 1574 | NewParam != NewParamEnd; ++NewParam) { |
| 1575 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1576 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1577 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1578 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1579 | NTTP->removeDefaultArgument(); |
| 1580 | else |
| 1581 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1582 | } |
| 1583 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1584 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1585 | return Invalid; |
| 1586 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1587 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1588 | namespace { |
| 1589 | |
| 1590 | /// A class which looks for a use of a certain level of template |
| 1591 | /// parameter. |
| 1592 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1593 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1594 | |
| 1595 | unsigned Depth; |
| 1596 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1597 | SourceLocation MatchLoc; |
| 1598 | |
| 1599 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1600 | |
| 1601 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1602 | NamedDecl *ND = Params->getParam(0); |
| 1603 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1604 | Depth = PD->getDepth(); |
| 1605 | } else if (NonTypeTemplateParmDecl *PD = |
| 1606 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1607 | Depth = PD->getDepth(); |
| 1608 | } else { |
| 1609 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1610 | } |
| 1611 | } |
| 1612 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1613 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1614 | if (ParmDepth >= Depth) { |
| 1615 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1616 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1617 | return true; |
| 1618 | } |
| 1619 | return false; |
| 1620 | } |
| 1621 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1622 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1623 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1624 | } |
| 1625 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1626 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1627 | return !Matches(T->getDepth()); |
| 1628 | } |
| 1629 | |
| 1630 | bool TraverseTemplateName(TemplateName N) { |
| 1631 | if (TemplateTemplateParmDecl *PD = |
| 1632 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1633 | if (Matches(PD->getDepth())) |
| 1634 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1635 | return super::TraverseTemplateName(N); |
| 1636 | } |
| 1637 | |
| 1638 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1639 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1640 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1641 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1642 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1643 | return super::VisitDeclRefExpr(E); |
| 1644 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1645 | |
| 1646 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1647 | return TraverseType(T->getReplacementType()); |
| 1648 | } |
| 1649 | |
| 1650 | bool |
| 1651 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1652 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1653 | } |
| 1654 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1655 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1656 | return TraverseType(T->getInjectedSpecializationType()); |
| 1657 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1658 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1659 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1660 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1661 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1662 | /// list. |
| 1663 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1664 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1665 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1666 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1667 | return Checker.Match; |
| 1668 | } |
| 1669 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1670 | // Find the source range corresponding to the named type in the given |
| 1671 | // nested-name-specifier, if any. |
| 1672 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1673 | QualType T, |
| 1674 | const CXXScopeSpec &SS) { |
| 1675 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1676 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1677 | if (const Type *CurType = NNS->getAsType()) { |
| 1678 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1679 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1680 | } else |
| 1681 | break; |
| 1682 | |
| 1683 | NNSLoc = NNSLoc.getPrefix(); |
| 1684 | } |
| 1685 | |
| 1686 | return SourceRange(); |
| 1687 | } |
| 1688 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1690 | /// specifier, returning the template parameter list that applies to the |
| 1691 | /// name. |
| 1692 | /// |
| 1693 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1694 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1696 | /// \param DeclLoc The location of the declaration itself. |
| 1697 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1698 | /// \param SS the scope specifier that will be matched to the given template |
| 1699 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1700 | /// being declared. |
| 1701 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1702 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1703 | /// is one. Used to check for a missing 'template<>'. |
| 1704 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1705 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1706 | /// innermost template parameter lists. |
| 1707 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1708 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1709 | /// matching template parameters to scope specifiers in friend |
| 1710 | /// declarations. |
| 1711 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1712 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1713 | /// declared is an explicit specialization, false otherwise. |
| 1714 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1716 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1717 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1719 | /// 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] | 1720 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1721 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1722 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1723 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1724 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1725 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1726 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1727 | Invalid = false; |
| 1728 | |
| 1729 | // The sequence of nested types to which we will match up the template |
| 1730 | // parameter lists. We first build this list by starting with the type named |
| 1731 | // 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] | 1732 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1733 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1734 | if (SS.getScopeRep()) { |
| 1735 | if (CXXRecordDecl *Record |
| 1736 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1737 | T = Context.getTypeDeclType(Record); |
| 1738 | else |
| 1739 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1740 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1741 | |
| 1742 | // If we found an explicit specialization that prevents us from needing |
| 1743 | // 'template<>' headers, this will be set to the location of that |
| 1744 | // explicit specialization. |
| 1745 | SourceLocation ExplicitSpecLoc; |
| 1746 | |
| 1747 | while (!T.isNull()) { |
| 1748 | NestedTypes.push_back(T); |
| 1749 | |
| 1750 | // Retrieve the parent of a record type. |
| 1751 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1752 | // If this type is an explicit specialization, we're done. |
| 1753 | if (ClassTemplateSpecializationDecl *Spec |
| 1754 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1755 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1756 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1757 | ExplicitSpecLoc = Spec->getLocation(); |
| 1758 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1759 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1760 | } else if (Record->getTemplateSpecializationKind() |
| 1761 | == TSK_ExplicitSpecialization) { |
| 1762 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1763 | break; |
| 1764 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1765 | |
| 1766 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1767 | T = Context.getTypeDeclType(Parent); |
| 1768 | else |
| 1769 | T = QualType(); |
| 1770 | continue; |
| 1771 | } |
| 1772 | |
| 1773 | if (const TemplateSpecializationType *TST |
| 1774 | = T->getAs<TemplateSpecializationType>()) { |
| 1775 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1776 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1777 | T = Context.getTypeDeclType(Parent); |
| 1778 | else |
| 1779 | T = QualType(); |
| 1780 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1781 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | // Look one step prior in a dependent template specialization type. |
| 1785 | if (const DependentTemplateSpecializationType *DependentTST |
| 1786 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1787 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1788 | T = QualType(NNS->getAsType(), 0); |
| 1789 | else |
| 1790 | T = QualType(); |
| 1791 | continue; |
| 1792 | } |
| 1793 | |
| 1794 | // Look one step prior in a dependent name type. |
| 1795 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1796 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1797 | T = QualType(NNS->getAsType(), 0); |
| 1798 | else |
| 1799 | T = QualType(); |
| 1800 | continue; |
| 1801 | } |
| 1802 | |
| 1803 | // Retrieve the parent of an enumeration type. |
| 1804 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1805 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1806 | // check here. |
| 1807 | EnumDecl *Enum = EnumT->getDecl(); |
| 1808 | |
| 1809 | // Get to the parent type. |
| 1810 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1811 | T = Context.getTypeDeclType(Parent); |
| 1812 | else |
| 1813 | T = QualType(); |
| 1814 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1815 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1817 | T = QualType(); |
| 1818 | } |
| 1819 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1820 | // to the innermost while checking template-parameter-lists. |
| 1821 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1823 | // C++0x [temp.expl.spec]p17: |
| 1824 | // A member or a member template may be nested within many |
| 1825 | // enclosing class templates. In an explicit specialization for |
| 1826 | // such a member, the member declaration shall be preceded by a |
| 1827 | // template<> for each enclosing class template that is |
| 1828 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1829 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1830 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1831 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1832 | if (SawNonEmptyTemplateParameterList) { |
| 1833 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1834 | << !Recovery << Range; |
| 1835 | Invalid = true; |
| 1836 | IsExplicitSpecialization = false; |
| 1837 | return true; |
| 1838 | } |
| 1839 | |
| 1840 | return false; |
| 1841 | }; |
| 1842 | |
| 1843 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1844 | // Check that we can have an explicit specialization here. |
| 1845 | if (CheckExplicitSpecialization(Range, true)) |
| 1846 | return true; |
| 1847 | |
| 1848 | // We don't have a template header, but we should. |
| 1849 | SourceLocation ExpectedTemplateLoc; |
| 1850 | if (!ParamLists.empty()) |
| 1851 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1852 | else |
| 1853 | ExpectedTemplateLoc = DeclStartLoc; |
| 1854 | |
| 1855 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1856 | << Range |
| 1857 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1858 | return false; |
| 1859 | }; |
| 1860 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1861 | unsigned ParamIdx = 0; |
| 1862 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1863 | ++TypeIdx) { |
| 1864 | T = NestedTypes[TypeIdx]; |
| 1865 | |
| 1866 | // Whether we expect a 'template<>' header. |
| 1867 | bool NeedEmptyTemplateHeader = false; |
| 1868 | |
| 1869 | // Whether we expect a template header with parameters. |
| 1870 | bool NeedNonemptyTemplateHeader = false; |
| 1871 | |
| 1872 | // For a dependent type, the set of template parameters that we |
| 1873 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1874 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1875 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1876 | // C++0x [temp.expl.spec]p15: |
| 1877 | // A member or a member template may be nested within many enclosing |
| 1878 | // class templates. In an explicit specialization for such a member, the |
| 1879 | // member declaration shall be preceded by a template<> for each |
| 1880 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1881 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1882 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1883 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1884 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1885 | NeedNonemptyTemplateHeader = true; |
| 1886 | } else if (Record->isDependentType()) { |
| 1887 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1888 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1889 | ->getTemplateParameters(); |
| 1890 | NeedNonemptyTemplateHeader = true; |
| 1891 | } |
| 1892 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1893 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1894 | // C++0x [temp.expl.spec]p4: |
| 1895 | // Members of an explicitly specialized class template are defined |
| 1896 | // in the same manner as members of normal classes, and not using |
| 1897 | // the template<> syntax. |
| 1898 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1899 | NeedEmptyTemplateHeader = true; |
| 1900 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1901 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1902 | } else if (Record->getTemplateSpecializationKind()) { |
| 1903 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1904 | != TSK_ExplicitSpecialization && |
| 1905 | TypeIdx == NumTypes - 1) |
| 1906 | IsExplicitSpecialization = true; |
| 1907 | |
| 1908 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1909 | } |
| 1910 | } else if (const TemplateSpecializationType *TST |
| 1911 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 1912 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1913 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1914 | NeedNonemptyTemplateHeader = true; |
| 1915 | } |
| 1916 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1917 | // FIXME: We actually could/should check the template arguments here |
| 1918 | // against the corresponding template parameter list. |
| 1919 | NeedNonemptyTemplateHeader = false; |
| 1920 | } |
| 1921 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1922 | // C++ [temp.expl.spec]p16: |
| 1923 | // In an explicit specialization declaration for a member of a class |
| 1924 | // template or a member template that ap- pears in namespace scope, the |
| 1925 | // member template and some of its enclosing class templates may remain |
| 1926 | // unspecialized, except that the declaration shall not explicitly |
| 1927 | // specialize a class member template if its en- closing class templates |
| 1928 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1929 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1930 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1931 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1932 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1933 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1934 | } else |
| 1935 | SawNonEmptyTemplateParameterList = true; |
| 1936 | } |
| 1937 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1938 | if (NeedEmptyTemplateHeader) { |
| 1939 | // If we're on the last of the types, and we need a 'template<>' header |
| 1940 | // here, then it's an explicit specialization. |
| 1941 | if (TypeIdx == NumTypes - 1) |
| 1942 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1943 | |
| 1944 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1945 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1946 | // The header has template parameters when it shouldn't. Complain. |
| 1947 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1948 | diag::err_template_param_list_matches_nontemplate) |
| 1949 | << T |
| 1950 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1951 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1952 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1953 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1954 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1955 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1957 | // Consume this template header. |
| 1958 | ++ParamIdx; |
| 1959 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1960 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1961 | |
| 1962 | if (!IsFriend) |
| 1963 | if (DiagnoseMissingExplicitSpecialization( |
| 1964 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1965 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1966 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1967 | continue; |
| 1968 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1969 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1970 | if (NeedNonemptyTemplateHeader) { |
| 1971 | // In friend declarations we can have template-ids which don't |
| 1972 | // depend on the corresponding template parameter lists. But |
| 1973 | // assume that empty parameter lists are supposed to match this |
| 1974 | // template-id. |
| 1975 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1976 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1977 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1978 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1979 | else |
| 1980 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1982 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1983 | if (ParamIdx < ParamLists.size()) { |
| 1984 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1985 | if (ExpectedTemplateParams && |
| 1986 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1987 | ExpectedTemplateParams, |
| 1988 | true, TPL_TemplateMatch)) |
| 1989 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1990 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1991 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1992 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1993 | TPC_ClassTemplateMember)) |
| 1994 | Invalid = true; |
| 1995 | |
| 1996 | ++ParamIdx; |
| 1997 | continue; |
| 1998 | } |
| 1999 | |
| 2000 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2001 | << T |
| 2002 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2003 | Invalid = true; |
| 2004 | continue; |
| 2005 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2006 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2007 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2008 | // If there were at least as many template-ids as there were template |
| 2009 | // parameter lists, then there are no template parameter lists remaining for |
| 2010 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2011 | if (ParamIdx >= ParamLists.size()) { |
| 2012 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2013 | // We don't have a template header for the declaration itself, but we |
| 2014 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2015 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2016 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2017 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2018 | |
| 2019 | // Fabricate an empty template parameter list for the invented header. |
| 2020 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 2021 | SourceLocation(), None, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 2022 | SourceLocation(), nullptr); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2025 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2026 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2028 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2029 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2030 | bool HasAnyExplicitSpecHeader = false; |
| 2031 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2032 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2033 | if (ParamLists[I]->size() == 0) |
| 2034 | HasAnyExplicitSpecHeader = true; |
| 2035 | else |
| 2036 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2037 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2038 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2039 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2040 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2041 | : diag::err_template_spec_extra_headers) |
| 2042 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2043 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 2044 | |
| 2045 | // If there was a specialization somewhere, such that 'template<>' is |
| 2046 | // not required, and there were any 'template<>' headers, note where the |
| 2047 | // specialization occurred. |
| 2048 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 2049 | Diag(ExplicitSpecLoc, |
| 2050 | diag::note_explicit_template_spec_does_not_need_header) |
| 2051 | << NestedTypes.back(); |
| 2052 | |
| 2053 | // We have a template parameter list with no corresponding scope, which |
| 2054 | // means that the resulting template declaration can't be instantiated |
| 2055 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 2056 | if (!AllExplicitSpecHeaders) |
| 2057 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2058 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 2060 | // C++ [temp.expl.spec]p16: |
| 2061 | // In an explicit specialization declaration for a member of a class |
| 2062 | // template or a member template that ap- pears in namespace scope, the |
| 2063 | // member template and some of its enclosing class templates may remain |
| 2064 | // unspecialized, except that the declaration shall not explicitly |
| 2065 | // specialize a class member template if its en- closing class templates |
| 2066 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2067 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2068 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2069 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2070 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2071 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2072 | // Return the last template parameter list, which corresponds to the |
| 2073 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2074 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2077 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2078 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2079 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2080 | << (isa<FunctionTemplateDecl>(Template) |
| 2081 | ? 0 |
| 2082 | : isa<ClassTemplateDecl>(Template) |
| 2083 | ? 1 |
| 2084 | : isa<VarTemplateDecl>(Template) |
| 2085 | ? 2 |
| 2086 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2087 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2088 | return; |
| 2089 | } |
| 2090 | |
| 2091 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 2092 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 2093 | IEnd = OST->end(); |
| 2094 | I != IEnd; ++I) |
| 2095 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2096 | << 0 << (*I)->getDeclName(); |
| 2097 | |
| 2098 | return; |
| 2099 | } |
| 2100 | } |
| 2101 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2102 | static QualType |
| 2103 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2104 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2105 | SourceLocation TemplateLoc, |
| 2106 | TemplateArgumentListInfo &TemplateArgs) { |
| 2107 | ASTContext &Context = SemaRef.getASTContext(); |
| 2108 | switch (BTD->getBuiltinTemplateKind()) { |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2109 | case BTK__make_integer_seq: { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2110 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2111 | // S<T, 0, ..., N-1>. |
| 2112 | |
| 2113 | // C++14 [inteseq.intseq]p1: |
| 2114 | // T shall be an integer type. |
| 2115 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2116 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2117 | diag::err_integer_sequence_integral_element_type); |
| 2118 | return QualType(); |
| 2119 | } |
| 2120 | |
| 2121 | // C++14 [inteseq.make]p1: |
| 2122 | // If N is negative the program is ill-formed. |
| 2123 | TemplateArgument NumArgsArg = Converted[2]; |
| 2124 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2125 | if (NumArgs < 0) { |
| 2126 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2127 | diag::err_integer_sequence_negative_length); |
| 2128 | return QualType(); |
| 2129 | } |
| 2130 | |
| 2131 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2132 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2133 | // The type argument gets reused as the first template argument in the |
| 2134 | // synthetic template argument list. |
| 2135 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2136 | // Expand N into 0 ... N-1. |
| 2137 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2138 | I < NumArgs; ++I) { |
| 2139 | TemplateArgument TA(Context, I, ArgTy); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2140 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2141 | TA, ArgTy, TemplateArgs[2].getLocation())); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2142 | } |
| 2143 | // The first template argument will be reused as the template decl that |
| 2144 | // our synthetic template arguments will be applied to. |
| 2145 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2146 | TemplateLoc, SyntheticTemplateArgs); |
| 2147 | } |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 2148 | |
| 2149 | case BTK__type_pack_element: |
| 2150 | // Specializations of |
| 2151 | // __type_pack_element<Index, T_1, ..., T_N> |
| 2152 | // are treated like T_Index. |
| 2153 | assert(Converted.size() == 2 && |
| 2154 | "__type_pack_element should be given an index and a parameter pack"); |
| 2155 | |
| 2156 | // If the Index is out of bounds, the program is ill-formed. |
| 2157 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 2158 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 2159 | assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 2160 | "type std::size_t, and hence be non-negative"); |
| 2161 | if (Index >= Ts.pack_size()) { |
| 2162 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 2163 | diag::err_type_pack_element_out_of_bounds); |
| 2164 | return QualType(); |
| 2165 | } |
| 2166 | |
| 2167 | // We simply return the type at index `Index`. |
| 2168 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 2169 | return Nth->getAsType(); |
| 2170 | } |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2171 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2172 | } |
| 2173 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2174 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2175 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2176 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2177 | DependentTemplateName *DTN |
| 2178 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2179 | if (DTN && DTN->isIdentifier()) |
| 2180 | // When building a template-id where the template-name is dependent, |
| 2181 | // assume the template is a type template. Either our assumption is |
| 2182 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2183 | // dependent name is substituted. |
| 2184 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2185 | DTN->getQualifier(), |
| 2186 | DTN->getIdentifier(), |
| 2187 | TemplateArgs); |
| 2188 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2189 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2190 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2191 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2192 | // We might have a substituted template template parameter pack. If so, |
| 2193 | // build a template specialization type for it. |
| 2194 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2195 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2196 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2197 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2198 | << Name; |
| 2199 | NoteAllFoundTemplates(Name); |
| 2200 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2201 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2202 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2203 | // Check that the template argument list is well-formed for this |
| 2204 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2205 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2206 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2207 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2208 | return QualType(); |
| 2209 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2210 | QualType CanonType; |
| 2211 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2212 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2213 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2214 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2215 | // Find the canonical type for this type alias template specialization. |
| 2216 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2217 | if (Pattern->isInvalidDecl()) |
| 2218 | return QualType(); |
| 2219 | |
| 2220 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2221 | Converted); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2222 | |
| 2223 | // Only substitute for the innermost template argument list. |
| 2224 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2225 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2226 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2227 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2228 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2229 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2230 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2231 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2232 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2233 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2234 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2235 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2236 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2237 | AliasTemplate->getDeclName()); |
| 2238 | if (CanonType.isNull()) |
| 2239 | return QualType(); |
| 2240 | } else if (Name.isDependent() || |
| 2241 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2242 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2243 | // This class template specialization is a dependent |
| 2244 | // type. Therefore, its canonical type is another class template |
| 2245 | // specialization type that contains all of the converted |
| 2246 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2247 | // A<T, T> have identical types when A is declared as: |
| 2248 | // |
| 2249 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2250 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | CanonType = Context.getTemplateSpecializationType(CanonName, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2252 | Converted); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2254 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2255 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2256 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2257 | // build the canonical type and return that to us. |
| 2258 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2259 | |
| 2260 | // This might work out to be a current instantiation, in which |
| 2261 | // case the canonical type needs to be the InjectedClassNameType. |
| 2262 | // |
| 2263 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2264 | // changes to CurContext don't change the set of current |
| 2265 | // instantiations. |
| 2266 | if (isa<ClassTemplateDecl>(Template)) { |
| 2267 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2268 | // If we get out to a namespace, we're done. |
| 2269 | if (Ctx->isFileContext()) break; |
| 2270 | |
| 2271 | // If this isn't a record, keep looking. |
| 2272 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2273 | if (!Record) continue; |
| 2274 | |
| 2275 | // Look for one of the two cases with InjectedClassNameTypes |
| 2276 | // and check whether it's the same template. |
| 2277 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2278 | !Record->getDescribedClassTemplate()) |
| 2279 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2280 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2281 | // Fetch the injected class name type and check whether its |
| 2282 | // injected type is equal to the type we just built. |
| 2283 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2284 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2285 | ->getInjectedSpecializationType(); |
| 2286 | |
| 2287 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2288 | continue; |
| 2289 | |
| 2290 | // If so, the canonical type of this TST is the injected |
| 2291 | // class name type of the record we just found. |
| 2292 | assert(ICNT.isCanonical()); |
| 2293 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2294 | break; |
| 2295 | } |
| 2296 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2298 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2299 | // Find the class template specialization declaration that |
| 2300 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2301 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2302 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2303 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2304 | if (!Decl) { |
| 2305 | // This is the first time we have referenced this class template |
| 2306 | // specialization. Create the canonical declaration and add it to |
| 2307 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2308 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2309 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2310 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2311 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2312 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2313 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2314 | Converted, nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2315 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2316 | if (ClassTemplate->isOutOfLine()) |
| 2317 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2320 | // Diagnose uses of this specialization. |
| 2321 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2322 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2323 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2324 | assert(isa<RecordType>(CanonType) && |
| 2325 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2326 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2327 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2328 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2329 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2330 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2331 | // Build the fully-sugared type for this class template |
| 2332 | // specialization, which refers back to the class template |
| 2333 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2334 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2337 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2338 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2339 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2341 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2342 | SourceLocation RAngleLoc, |
| 2343 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2344 | if (SS.isInvalid()) |
| 2345 | return true; |
| 2346 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2347 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2348 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2349 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2350 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2351 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2352 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2353 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2354 | QualType T |
| 2355 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2356 | DTN->getQualifier(), |
| 2357 | DTN->getIdentifier(), |
| 2358 | TemplateArgs); |
| 2359 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2360 | TypeLocBuilder TLB; |
| 2361 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2362 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2363 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2364 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2365 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2366 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2367 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2368 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2369 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2370 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2371 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2372 | } |
| 2373 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2374 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2375 | |
| 2376 | if (Result.isNull()) |
| 2377 | return true; |
| 2378 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2379 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2380 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2381 | TemplateSpecializationTypeLoc SpecTL |
| 2382 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2383 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2384 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2385 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2386 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2387 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2388 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2389 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2390 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2391 | // constructor or destructor name (in such a case, the scope specifier |
| 2392 | // will be attached to the enclosing Decl or Expr node). |
| 2393 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2394 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2395 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2396 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2397 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2398 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2399 | } |
| 2400 | |
| 2401 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2402 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2403 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2404 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2405 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2406 | SourceLocation TagLoc, |
| 2407 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2408 | SourceLocation TemplateKWLoc, |
| 2409 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2410 | SourceLocation TemplateLoc, |
| 2411 | SourceLocation LAngleLoc, |
| 2412 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2413 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2414 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2415 | |
| 2416 | // Translate the parser's template argument list in our AST format. |
| 2417 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2418 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2419 | |
| 2420 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2421 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2422 | ElaboratedTypeKeyword Keyword |
| 2423 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2425 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2426 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2427 | DTN->getQualifier(), |
| 2428 | DTN->getIdentifier(), |
| 2429 | TemplateArgs); |
| 2430 | |
| 2431 | // Build type-source information. |
| 2432 | TypeLocBuilder TLB; |
| 2433 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2434 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2435 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2436 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2437 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2438 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2439 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2440 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2441 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2442 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2443 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2444 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2445 | |
| 2446 | if (TypeAliasTemplateDecl *TAT = |
| 2447 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2448 | // C++0x [dcl.type.elab]p2: |
| 2449 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2450 | // resolves to an alias template specialization, the |
| 2451 | // elaborated-type-specifier is ill-formed. |
| 2452 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2453 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2454 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2455 | |
| 2456 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2457 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2458 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2459 | |
| 2460 | // Check the tag kind |
| 2461 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2462 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2463 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2464 | IdentifierInfo *Id = D->getIdentifier(); |
| 2465 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2466 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2467 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 2468 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2469 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2470 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2471 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2472 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2473 | } |
| 2474 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2475 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2476 | // Provide source-location information for the template specialization. |
| 2477 | TypeLocBuilder TLB; |
| 2478 | TemplateSpecializationTypeLoc SpecTL |
| 2479 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2480 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2481 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2482 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2483 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2484 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2485 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2486 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2487 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2488 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2489 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2490 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2491 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2492 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2493 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2496 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2497 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2498 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2499 | |
| 2500 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2501 | NamedDecl *PrevDecl, |
| 2502 | SourceLocation Loc, |
| 2503 | bool IsPartialSpecialization); |
| 2504 | |
| 2505 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2506 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2507 | static bool isTemplateArgumentTemplateParameter( |
| 2508 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2509 | switch (Arg.getKind()) { |
| 2510 | case TemplateArgument::Null: |
| 2511 | case TemplateArgument::NullPtr: |
| 2512 | case TemplateArgument::Integral: |
| 2513 | case TemplateArgument::Declaration: |
| 2514 | case TemplateArgument::Pack: |
| 2515 | case TemplateArgument::TemplateExpansion: |
| 2516 | return false; |
| 2517 | |
| 2518 | case TemplateArgument::Type: { |
| 2519 | QualType Type = Arg.getAsType(); |
| 2520 | const TemplateTypeParmType *TPT = |
| 2521 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2522 | return TPT && !Type.hasQualifiers() && |
| 2523 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2524 | } |
| 2525 | |
| 2526 | case TemplateArgument::Expression: { |
| 2527 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2528 | if (!DRE || !DRE->getDecl()) |
| 2529 | return false; |
| 2530 | const NonTypeTemplateParmDecl *NTTP = |
| 2531 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2532 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2533 | } |
| 2534 | |
| 2535 | case TemplateArgument::Template: |
| 2536 | const TemplateTemplateParmDecl *TTP = |
| 2537 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2538 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2539 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2540 | } |
| 2541 | llvm_unreachable("unexpected kind of template argument"); |
| 2542 | } |
| 2543 | |
| 2544 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2545 | ArrayRef<TemplateArgument> Args) { |
| 2546 | if (Params->size() != Args.size()) |
| 2547 | return false; |
| 2548 | |
| 2549 | unsigned Depth = Params->getDepth(); |
| 2550 | |
| 2551 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2552 | TemplateArgument Arg = Args[I]; |
| 2553 | |
| 2554 | // If the parameter is a pack expansion, the argument must be a pack |
| 2555 | // whose only element is a pack expansion. |
| 2556 | if (Params->getParam(I)->isParameterPack()) { |
| 2557 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2558 | !Arg.pack_begin()->isPackExpansion()) |
| 2559 | return false; |
| 2560 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2561 | } |
| 2562 | |
| 2563 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2564 | return false; |
| 2565 | } |
| 2566 | |
| 2567 | return true; |
| 2568 | } |
| 2569 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2570 | /// Convert the parser's template argument list representation into our form. |
| 2571 | static TemplateArgumentListInfo |
| 2572 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2573 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2574 | TemplateId.RAngleLoc); |
| 2575 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2576 | TemplateId.NumArgs); |
| 2577 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2578 | return TemplateArgs; |
| 2579 | } |
| 2580 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2581 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2582 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2583 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2584 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2585 | // D must be variable template id. |
| 2586 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2587 | "Variable template specialization is declared with a template it."); |
| 2588 | |
| 2589 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2590 | TemplateArgumentListInfo TemplateArgs = |
| 2591 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2592 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2593 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2594 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2595 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2596 | TemplateName Name = TemplateId->Template.get(); |
| 2597 | |
| 2598 | // The template-id must name a variable template. |
| 2599 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2600 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2601 | if (!VarTemplate) { |
| 2602 | NamedDecl *FnTemplate; |
| 2603 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2604 | FnTemplate = *OTS->begin(); |
| 2605 | else |
| 2606 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2607 | if (FnTemplate) |
| 2608 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2609 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2610 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2611 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2612 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2613 | |
| 2614 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2615 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2616 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2617 | UPPC_PartialSpecialization)) |
| 2618 | return true; |
| 2619 | |
| 2620 | // Check that the template argument list is well-formed for this |
| 2621 | // template. |
| 2622 | SmallVector<TemplateArgument, 4> Converted; |
| 2623 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2624 | false, Converted)) |
| 2625 | return true; |
| 2626 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2627 | // Find the variable template (partial) specialization declaration that |
| 2628 | // corresponds to these arguments. |
| 2629 | if (IsPartialSpecialization) { |
| 2630 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2631 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2632 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2633 | return true; |
| 2634 | |
| 2635 | bool InstantiationDependent; |
| 2636 | if (!Name.isDependent() && |
| 2637 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2638 | TemplateArgs.arguments(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2639 | InstantiationDependent)) { |
| 2640 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2641 | << VarTemplate->getDeclName(); |
| 2642 | IsPartialSpecialization = false; |
| 2643 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2644 | |
| 2645 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2646 | Converted)) { |
| 2647 | // C++ [temp.class.spec]p9b3: |
| 2648 | // |
| 2649 | // -- The argument list of the specialization shall not be identical |
| 2650 | // to the implicit argument list of the primary template. |
| 2651 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2652 | << /*variable template*/ 1 |
| 2653 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2654 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2655 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2656 | // of the primary template. |
| 2657 | return true; |
| 2658 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2661 | void *InsertPos = nullptr; |
| 2662 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2663 | |
| 2664 | if (IsPartialSpecialization) |
| 2665 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2666 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2667 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2668 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2669 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2670 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2671 | |
| 2672 | // Check whether we can declare a variable template specialization in |
| 2673 | // the current scope. |
| 2674 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2675 | TemplateNameLoc, |
| 2676 | IsPartialSpecialization)) |
| 2677 | return true; |
| 2678 | |
| 2679 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2680 | // Since the only prior variable template specialization with these |
| 2681 | // arguments was referenced but not declared, reuse that |
| 2682 | // declaration node as our own, updating its source location and |
| 2683 | // the list of outer template parameters to reflect our new declaration. |
| 2684 | Specialization = PrevDecl; |
| 2685 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2686 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2687 | } else if (IsPartialSpecialization) { |
| 2688 | // Create a new class template partial specialization declaration node. |
| 2689 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2690 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2691 | VarTemplatePartialSpecializationDecl *Partial = |
| 2692 | VarTemplatePartialSpecializationDecl::Create( |
| 2693 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2694 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2695 | Converted, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2696 | |
| 2697 | if (!PrevPartial) |
| 2698 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2699 | Specialization = Partial; |
| 2700 | |
| 2701 | // If we are providing an explicit specialization of a member variable |
| 2702 | // template specialization, make a note of that. |
| 2703 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2704 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2705 | |
| 2706 | // Check that all of the template parameters of the variable template |
| 2707 | // partial specialization are deducible from the template |
| 2708 | // arguments. If not, this variable template partial specialization |
| 2709 | // will never be used. |
| 2710 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2711 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2712 | TemplateParams->getDepth(), DeducibleParams); |
| 2713 | |
| 2714 | if (!DeducibleParams.all()) { |
| 2715 | unsigned NumNonDeducible = |
| 2716 | DeducibleParams.size() - DeducibleParams.count(); |
| 2717 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2718 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2719 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2720 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2721 | if (!DeducibleParams[I]) { |
| 2722 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2723 | if (Param->getDeclName()) |
| 2724 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2725 | << Param->getDeclName(); |
| 2726 | else |
| 2727 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2728 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2729 | } |
| 2730 | } |
| 2731 | } |
| 2732 | } else { |
| 2733 | // Create a new class template specialization declaration node for |
| 2734 | // this explicit specialization or friend declaration. |
| 2735 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2736 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2737 | VarTemplate, DI->getType(), DI, SC, Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2738 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2739 | |
| 2740 | if (!PrevDecl) |
| 2741 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2742 | } |
| 2743 | |
| 2744 | // C++ [temp.expl.spec]p6: |
| 2745 | // If a template, a member template or the member of a class template is |
| 2746 | // explicitly specialized then that specialization shall be declared |
| 2747 | // before the first use of that specialization that would cause an implicit |
| 2748 | // instantiation to take place, in every translation unit in which such a |
| 2749 | // use occurs; no diagnostic is required. |
| 2750 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2751 | bool Okay = false; |
| 2752 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2753 | // Is there any previous explicit specialization declaration? |
| 2754 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2755 | Okay = true; |
| 2756 | break; |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | if (!Okay) { |
| 2761 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2762 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2763 | << Name << Range; |
| 2764 | |
| 2765 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2766 | diag::note_instantiation_required_here) |
| 2767 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2768 | TSK_ImplicitInstantiation); |
| 2769 | return true; |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2774 | Specialization->setLexicalDeclContext(CurContext); |
| 2775 | |
| 2776 | // Add the specialization into its lexical context, so that it can |
| 2777 | // be seen when iterating through the list of declarations in that |
| 2778 | // context. However, specializations are not found by name lookup. |
| 2779 | CurContext->addDecl(Specialization); |
| 2780 | |
| 2781 | // Note that this is an explicit specialization. |
| 2782 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2783 | |
| 2784 | if (PrevDecl) { |
| 2785 | // Check that this isn't a redefinition of this specialization, |
| 2786 | // merging with previous declarations. |
| 2787 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2788 | ForRedeclaration); |
| 2789 | PrevSpec.addDecl(PrevDecl); |
| 2790 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2791 | } else if (Specialization->isStaticDataMember() && |
| 2792 | Specialization->isOutOfLine()) { |
| 2793 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | // Link instantiations of static data members back to the template from |
| 2797 | // which they were instantiated. |
| 2798 | if (Specialization->isStaticDataMember()) |
| 2799 | Specialization->setInstantiationOfStaticDataMember( |
| 2800 | VarTemplate->getTemplatedDecl(), |
| 2801 | Specialization->getSpecializationKind()); |
| 2802 | |
| 2803 | return Specialization; |
| 2804 | } |
| 2805 | |
| 2806 | namespace { |
| 2807 | /// \brief A partial specialization whose template arguments have matched |
| 2808 | /// a given template-id. |
| 2809 | struct PartialSpecMatchResult { |
| 2810 | VarTemplatePartialSpecializationDecl *Partial; |
| 2811 | TemplateArgumentList *Args; |
| 2812 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2813 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2814 | |
| 2815 | DeclResult |
| 2816 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2817 | SourceLocation TemplateNameLoc, |
| 2818 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2819 | assert(Template && "A variable template id without template?"); |
| 2820 | |
| 2821 | // Check that the template argument list is well-formed for this template. |
| 2822 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2823 | if (CheckTemplateArgumentList( |
| 2824 | Template, TemplateNameLoc, |
| 2825 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2826 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2827 | return true; |
| 2828 | |
| 2829 | // Find the variable template specialization declaration that |
| 2830 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2831 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2832 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2833 | Converted, InsertPos)) { |
| 2834 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2835 | // If we already have a variable template specialization, return it. |
| 2836 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2837 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2838 | |
| 2839 | // This is the first time we have referenced this variable template |
| 2840 | // specialization. Create the canonical declaration and add it to |
| 2841 | // the set of specializations, based on the closest partial specialization |
| 2842 | // that it represents. That is, |
| 2843 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2844 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2845 | Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2846 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2847 | bool AmbiguousPartialSpec = false; |
| 2848 | typedef PartialSpecMatchResult MatchResult; |
| 2849 | SmallVector<MatchResult, 4> Matched; |
| 2850 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2851 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 2852 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2853 | |
| 2854 | // 1. Attempt to find the closest partial specialization that this |
| 2855 | // specializes, if any. |
| 2856 | // If any of the template arguments is dependent, then this is probably |
| 2857 | // a placeholder for an incomplete declarative context; which must be |
| 2858 | // complete by instantiation time. Thus, do not search through the partial |
| 2859 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2860 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2861 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2862 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2863 | bool InstantiationDependent = false; |
| 2864 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2865 | TemplateArgs, InstantiationDependent)) { |
| 2866 | |
| 2867 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2868 | Template->getPartialSpecializations(PartialSpecs); |
| 2869 | |
| 2870 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2871 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2872 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2873 | |
| 2874 | if (TemplateDeductionResult Result = |
| 2875 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2876 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2877 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 2878 | FailedCandidates.addCandidate().set( |
| 2879 | DeclAccessPair::make(Template, AS_public), Partial, |
| 2880 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2881 | (void)Result; |
| 2882 | } else { |
| 2883 | Matched.push_back(PartialSpecMatchResult()); |
| 2884 | Matched.back().Partial = Partial; |
| 2885 | Matched.back().Args = Info.take(); |
| 2886 | } |
| 2887 | } |
| 2888 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2889 | if (Matched.size() >= 1) { |
| 2890 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2891 | if (Matched.size() == 1) { |
| 2892 | // -- If exactly one matching specialization is found, the |
| 2893 | // instantiation is generated from that specialization. |
| 2894 | // We don't need to do anything for this. |
| 2895 | } else { |
| 2896 | // -- If more than one matching specialization is found, the |
| 2897 | // partial order rules (14.5.4.2) are used to determine |
| 2898 | // whether one of the specializations is more specialized |
| 2899 | // than the others. If none of the specializations is more |
| 2900 | // specialized than all of the other matching |
| 2901 | // specializations, then the use of the variable template is |
| 2902 | // ambiguous and the program is ill-formed. |
| 2903 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2904 | PEnd = Matched.end(); |
| 2905 | P != PEnd; ++P) { |
| 2906 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2907 | PointOfInstantiation) == |
| 2908 | P->Partial) |
| 2909 | Best = P; |
| 2910 | } |
| 2911 | |
| 2912 | // Determine if the best partial specialization is more specialized than |
| 2913 | // the others. |
| 2914 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2915 | PEnd = Matched.end(); |
| 2916 | P != PEnd; ++P) { |
| 2917 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2918 | P->Partial, Best->Partial, |
| 2919 | PointOfInstantiation) != Best->Partial) { |
| 2920 | AmbiguousPartialSpec = true; |
| 2921 | break; |
| 2922 | } |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | // Instantiate using the best variable template partial specialization. |
| 2927 | InstantiationPattern = Best->Partial; |
| 2928 | InstantiationArgs = Best->Args; |
| 2929 | } else { |
| 2930 | // -- If no match is found, the instantiation is generated |
| 2931 | // from the primary template. |
| 2932 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2933 | } |
| 2934 | } |
| 2935 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2936 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2937 | // Note that we do not instantiate a definition until we see an odr-use |
| 2938 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2939 | // FIXME: LateAttrs et al.? |
| 2940 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2941 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2942 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2943 | if (!Decl) |
| 2944 | return true; |
| 2945 | |
| 2946 | if (AmbiguousPartialSpec) { |
| 2947 | // Partial ordering did not produce a clear winner. Complain. |
| 2948 | Decl->setInvalidDecl(); |
| 2949 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2950 | << Decl; |
| 2951 | |
| 2952 | // Print the matching partial specializations. |
| 2953 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2954 | PEnd = Matched.end(); |
| 2955 | P != PEnd; ++P) |
| 2956 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2957 | << getTemplateArgumentBindingsText( |
| 2958 | P->Partial->getTemplateParameters(), *P->Args); |
| 2959 | return true; |
| 2960 | } |
| 2961 | |
| 2962 | if (VarTemplatePartialSpecializationDecl *D = |
| 2963 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2964 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2965 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2966 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 2967 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2968 | assert(Decl && "No variable template specialization?"); |
| 2969 | return Decl; |
| 2970 | } |
| 2971 | |
| 2972 | ExprResult |
| 2973 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2974 | const DeclarationNameInfo &NameInfo, |
| 2975 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2976 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2977 | |
| 2978 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2979 | *TemplateArgs); |
| 2980 | if (Decl.isInvalid()) |
| 2981 | return ExprError(); |
| 2982 | |
| 2983 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2984 | if (!Var->getTemplateSpecializationKind()) |
| 2985 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2986 | NameInfo.getLoc()); |
| 2987 | |
| 2988 | // Build an ordinary singleton decl ref. |
| 2989 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2990 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2993 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2994 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2995 | LookupResult &R, |
| 2996 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2997 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2998 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2999 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | // 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] | 3001 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3002 | // foo<int> could identify a single function unambiguously |
| 3003 | // This approach does NOT work, since f<int>(1); |
| 3004 | // gets resolved prior to resorting to overload resolution |
| 3005 | // i.e., template<class T> void f(double); |
| 3006 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3007 | |
| 3008 | // These should be filtered out by our callers. |
| 3009 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 3010 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 3011 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3012 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 3013 | bool InstantiationDependent; |
| 3014 | if (R.getAsSingle<VarTemplateDecl>() && |
| 3015 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3016 | *TemplateArgs, InstantiationDependent)) { |
| 3017 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 3018 | R.getAsSingle<VarTemplateDecl>(), |
| 3019 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3022 | // We don't want lookup warnings at this point. |
| 3023 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3024 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3025 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3026 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3027 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3028 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3029 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3030 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3031 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3032 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3033 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3036 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3037 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3038 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3039 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3040 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3041 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3042 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 3043 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3044 | DeclContext *DC; |
| 3045 | if (!(DC = computeDeclContext(SS, false)) || |
| 3046 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3047 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 3048 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3049 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3050 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3051 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3052 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3053 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3055 | if (R.isAmbiguous()) |
| 3056 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3057 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3058 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3059 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 3060 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3061 | return ExprError(); |
| 3062 | } |
| 3063 | |
| 3064 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3065 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3066 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 3067 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3068 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 3069 | return ExprError(); |
| 3070 | } |
| 3071 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3072 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3075 | /// \brief Form a dependent template name. |
| 3076 | /// |
| 3077 | /// This action forms a dependent template name given the template |
| 3078 | /// name and its (presumably dependent) scope specifier. For |
| 3079 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 3080 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 3081 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3082 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3083 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3084 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3085 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3086 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3087 | bool EnteringContext, |
| 3088 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3089 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 3090 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3091 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3092 | diag::warn_cxx98_compat_template_outside_of_template : |
| 3093 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3094 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 3095 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3096 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3097 | if (SS.isSet()) |
| 3098 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 3099 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3100 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3101 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3102 | // C++0x [temp.names]p5: |
| 3103 | // If a name prefixed by the keyword template is not the name of |
| 3104 | // a template, the program is ill-formed. [Note: the keyword |
| 3105 | // template may not be applied to non-template members of class |
| 3106 | // templates. -end note ] [ Note: as is the case with the |
| 3107 | // typename prefix, the template prefix is allowed in cases |
| 3108 | // where it is not strictly necessary; i.e., when the |
| 3109 | // nested-name-specifier or the expression on the left of the -> |
| 3110 | // or . is not dependent on a template-parameter, or the use |
| 3111 | // does not appear in the scope of a template. -end note] |
| 3112 | // |
| 3113 | // Note: C++03 was more strict here, because it banned the use of |
| 3114 | // the "template" keyword prior to a template-name that was not a |
| 3115 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3116 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3117 | // 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] | 3118 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3119 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3120 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3121 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3122 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3123 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3124 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3125 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3126 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3127 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3128 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3129 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3130 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3131 | << Name.getSourceRange() |
| 3132 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3133 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3134 | } else { |
| 3135 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3136 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3137 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3140 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3141 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3142 | switch (Name.getKind()) { |
| 3143 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3144 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3145 | Name.Identifier)); |
| 3146 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3147 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3148 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3149 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3150 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3151 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3152 | |
| 3153 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3154 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3155 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3156 | default: |
| 3157 | break; |
| 3158 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3159 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3160 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3161 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3162 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3163 | << Name.getSourceRange() |
| 3164 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3165 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3169 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3170 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3171 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3172 | QualType ArgType; |
| 3173 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3174 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3175 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3176 | switch(Arg.getKind()) { |
| 3177 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3178 | // C++ [temp.arg.type]p1: |
| 3179 | // A template-argument for a template-parameter which is a |
| 3180 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3181 | ArgType = Arg.getAsType(); |
| 3182 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3183 | break; |
| 3184 | case TemplateArgument::Template: { |
| 3185 | // We have a template type parameter but the template argument |
| 3186 | // is a template without any arguments. |
| 3187 | SourceRange SR = AL.getSourceRange(); |
| 3188 | TemplateName Name = Arg.getAsTemplate(); |
| 3189 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3190 | << Name << SR; |
| 3191 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3192 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3193 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3194 | return true; |
| 3195 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3196 | case TemplateArgument::Expression: { |
| 3197 | // We have a template type parameter but the template argument is an |
| 3198 | // expression; see if maybe it is missing the "typename" keyword. |
| 3199 | CXXScopeSpec SS; |
| 3200 | DeclarationNameInfo NameInfo; |
| 3201 | |
| 3202 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3203 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3204 | NameInfo = ArgExpr->getNameInfo(); |
| 3205 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3206 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3207 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3208 | NameInfo = ArgExpr->getNameInfo(); |
| 3209 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3210 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3211 | if (ArgExpr->isImplicitAccess()) { |
| 3212 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3213 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3214 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3217 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3218 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3219 | LookupParsedName(Result, CurScope, &SS); |
| 3220 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3221 | if (Result.getAsSingle<TypeDecl>() || |
| 3222 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3223 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3224 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3225 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3226 | Diag(Loc, getLangOpts().MSVCCompat |
| 3227 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3228 | : diag::err_template_arg_must_be_type_suggest) |
| 3229 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3230 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3231 | |
| 3232 | // Recover by synthesizing a type using the location information that we |
| 3233 | // already have. |
| 3234 | ArgType = |
| 3235 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3236 | TypeLocBuilder TLB; |
| 3237 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3238 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3239 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3240 | TL.setNameLoc(NameInfo.getLoc()); |
| 3241 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3242 | |
| 3243 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3244 | // properly. |
| 3245 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3246 | TemplateArgumentLocInfo(TSI)); |
| 3247 | |
| 3248 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3249 | } |
| 3250 | } |
| 3251 | // fallthrough |
| 3252 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3253 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3254 | // We have a template type parameter but the template argument |
| 3255 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3256 | SourceRange SR = AL.getSourceRange(); |
| 3257 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3258 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3259 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3260 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3262 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3263 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3264 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3265 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3267 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3268 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3269 | |
| 3270 | // Objective-C ARC: |
| 3271 | // If an explicitly-specified template argument type is a lifetime type |
| 3272 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3273 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3274 | ArgType->isObjCLifetimeType() && |
| 3275 | !ArgType.getObjCLifetime()) { |
| 3276 | Qualifiers Qs; |
| 3277 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3278 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3279 | } |
| 3280 | |
| 3281 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3282 | return false; |
| 3283 | } |
| 3284 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3285 | /// \brief Substitute template arguments into the default template argument for |
| 3286 | /// the given template type parameter. |
| 3287 | /// |
| 3288 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3289 | /// the substitution. |
| 3290 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3291 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3292 | /// for. |
| 3293 | /// |
| 3294 | /// \param TemplateLoc the location of the template name that started the |
| 3295 | /// template-id we are checking. |
| 3296 | /// |
| 3297 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3298 | /// terminates the template-id. |
| 3299 | /// |
| 3300 | /// \param Param the template template parameter whose default we are |
| 3301 | /// substituting into. |
| 3302 | /// |
| 3303 | /// \param Converted the list of template arguments provided for template |
| 3304 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3305 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3306 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3307 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3308 | TemplateDecl *Template, |
| 3309 | SourceLocation TemplateLoc, |
| 3310 | SourceLocation RAngleLoc, |
| 3311 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3312 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3313 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3314 | |
| 3315 | // If the argument type is dependent, instantiate it now based |
| 3316 | // on the previously-computed template arguments. |
| 3317 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3318 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3319 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3320 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3321 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3322 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3323 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3324 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3325 | |
| 3326 | // Only substitute for the innermost template argument list. |
| 3327 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3328 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3329 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3330 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3331 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3332 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3333 | ArgType = |
| 3334 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3335 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | return ArgType; |
| 3339 | } |
| 3340 | |
| 3341 | /// \brief Substitute template arguments into the default template argument for |
| 3342 | /// the given non-type template parameter. |
| 3343 | /// |
| 3344 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3345 | /// the substitution. |
| 3346 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3347 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3348 | /// for. |
| 3349 | /// |
| 3350 | /// \param TemplateLoc the location of the template name that started the |
| 3351 | /// template-id we are checking. |
| 3352 | /// |
| 3353 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3354 | /// terminates the template-id. |
| 3355 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3356 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3357 | /// substituting into. |
| 3358 | /// |
| 3359 | /// \param Converted the list of template arguments provided for template |
| 3360 | /// parameters that precede \p Param in the template parameter list. |
| 3361 | /// |
| 3362 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3363 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3364 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3365 | TemplateDecl *Template, |
| 3366 | SourceLocation TemplateLoc, |
| 3367 | SourceLocation RAngleLoc, |
| 3368 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3369 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3370 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3371 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3372 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3373 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3374 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3375 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3376 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3377 | |
| 3378 | // Only substitute for the innermost template argument list. |
| 3379 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3380 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3381 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3382 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3383 | |
Faisal Vali | 48401eb | 2015-11-19 19:20:17 +0000 | [diff] [blame] | 3384 | EnterExpressionEvaluationContext ConstantEvaluated(SemaRef, |
| 3385 | Sema::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3386 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3389 | /// \brief Substitute template arguments into the default template argument for |
| 3390 | /// the given template template parameter. |
| 3391 | /// |
| 3392 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3393 | /// the substitution. |
| 3394 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3395 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3396 | /// for. |
| 3397 | /// |
| 3398 | /// \param TemplateLoc the location of the template name that started the |
| 3399 | /// template-id we are checking. |
| 3400 | /// |
| 3401 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3402 | /// terminates the template-id. |
| 3403 | /// |
| 3404 | /// \param Param the template template parameter whose default we are |
| 3405 | /// substituting into. |
| 3406 | /// |
| 3407 | /// \param Converted the list of template arguments provided for template |
| 3408 | /// parameters that precede \p Param in the template parameter list. |
| 3409 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3410 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3411 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3412 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3413 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3414 | static TemplateName |
| 3415 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3416 | TemplateDecl *Template, |
| 3417 | SourceLocation TemplateLoc, |
| 3418 | SourceLocation RAngleLoc, |
| 3419 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3420 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3421 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3422 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3423 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3424 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3425 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3426 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3427 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3428 | |
| 3429 | // Only substitute for the innermost template argument list. |
| 3430 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3431 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3432 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3433 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3434 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3435 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3436 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3437 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3438 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3439 | QualifierLoc = |
| 3440 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3441 | if (!QualifierLoc) |
| 3442 | return TemplateName(); |
| 3443 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3444 | |
| 3445 | return SemaRef.SubstTemplateName( |
| 3446 | QualifierLoc, |
| 3447 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3448 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3449 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3452 | /// \brief If the given template parameter has a default template |
| 3453 | /// argument, substitute into that default template argument and |
| 3454 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3455 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3456 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3457 | SourceLocation TemplateLoc, |
| 3458 | SourceLocation RAngleLoc, |
| 3459 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3460 | SmallVectorImpl<TemplateArgument> |
| 3461 | &Converted, |
| 3462 | bool &HasDefaultArg) { |
| 3463 | HasDefaultArg = false; |
| 3464 | |
| 3465 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3466 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3467 | return TemplateArgumentLoc(); |
| 3468 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3469 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3470 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3471 | TemplateLoc, |
| 3472 | RAngleLoc, |
| 3473 | TypeParm, |
| 3474 | Converted); |
| 3475 | if (DI) |
| 3476 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3477 | |
| 3478 | return TemplateArgumentLoc(); |
| 3479 | } |
| 3480 | |
| 3481 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3482 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3483 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3484 | return TemplateArgumentLoc(); |
| 3485 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3486 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3487 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3488 | TemplateLoc, |
| 3489 | RAngleLoc, |
| 3490 | NonTypeParm, |
| 3491 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3492 | if (Arg.isInvalid()) |
| 3493 | return TemplateArgumentLoc(); |
| 3494 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3495 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3496 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3497 | } |
| 3498 | |
| 3499 | TemplateTemplateParmDecl *TempTempParm |
| 3500 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3501 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3502 | return TemplateArgumentLoc(); |
| 3503 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3504 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3505 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3506 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3507 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3508 | RAngleLoc, |
| 3509 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3510 | Converted, |
| 3511 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3512 | if (TName.isNull()) |
| 3513 | return TemplateArgumentLoc(); |
| 3514 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3515 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3516 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3517 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3518 | } |
| 3519 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3520 | /// \brief Check that the given template argument corresponds to the given |
| 3521 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3522 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3523 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3524 | /// checked. |
| 3525 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3526 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3527 | /// |
| 3528 | /// \param Template The template in which the template argument resides. |
| 3529 | /// |
| 3530 | /// \param TemplateLoc The location of the template name for the template |
| 3531 | /// whose argument list we're matching. |
| 3532 | /// |
| 3533 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3534 | /// the template argument list. |
| 3535 | /// |
| 3536 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3537 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3538 | /// |
| 3539 | /// \param Converted The checked, converted argument will be added to the |
| 3540 | /// end of this small vector. |
| 3541 | /// |
| 3542 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3543 | /// explicitly written, deduced, etc. |
| 3544 | /// |
| 3545 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3546 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3547 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3548 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3549 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3550 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3551 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3552 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3553 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3554 | // Check template type parameters. |
| 3555 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3556 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3557 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3558 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3560 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3561 | // with the template arguments we've seen thus far. But if the |
| 3562 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3563 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3564 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3565 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3567 | if (NTTPType->isDependentType() && |
| 3568 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3569 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3570 | // Do substitution on the type of the non-type template parameter. |
| 3571 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3572 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3573 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3574 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3575 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3576 | |
| 3577 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3578 | Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3579 | NTTPType = SubstType(NTTPType, |
| 3580 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3581 | NTTP->getLocation(), |
| 3582 | NTTP->getDeclName()); |
| 3583 | // If that worked, check the non-type template parameter type |
| 3584 | // for validity. |
| 3585 | if (!NTTPType.isNull()) |
| 3586 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3587 | NTTP->getLocation()); |
| 3588 | if (NTTPType.isNull()) |
| 3589 | return true; |
| 3590 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3591 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3592 | switch (Arg.getArgument().getKind()) { |
| 3593 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3594 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3595 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3596 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3597 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3598 | ExprResult Res = |
| 3599 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3600 | Result, CTAK); |
| 3601 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3602 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3603 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3604 | // If the resulting expression is new, then use it in place of the |
| 3605 | // old expression in the template argument. |
| 3606 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3607 | TemplateArgument TA(Res.get()); |
| 3608 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3609 | } |
| 3610 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3611 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3612 | break; |
| 3613 | } |
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::Declaration: |
| 3616 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3617 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3618 | // We've already checked this template argument, so just copy |
| 3619 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3620 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3621 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3623 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3624 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3625 | // We were given a template template argument. It may not be ill-formed; |
| 3626 | // see below. |
| 3627 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3628 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3629 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3630 | // We have a template argument such as \c T::template X, which we |
| 3631 | // parsed as a template template argument. However, since we now |
| 3632 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3633 | // template name into an expression. |
| 3634 | |
| 3635 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3636 | Arg.getTemplateNameLoc()); |
| 3637 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3638 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3639 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3640 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3641 | // so it was provided with a template keyword. However, its source |
| 3642 | // location is not stored in the template argument structure. |
| 3643 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3644 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3645 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3646 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3647 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3648 | // If we parsed the template argument as a pack expansion, create a |
| 3649 | // pack expansion expression. |
| 3650 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3651 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3652 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3653 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3654 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3655 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3656 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3657 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3658 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3659 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3660 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3661 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3662 | break; |
| 3663 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3664 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3665 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3666 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3667 | // therefore cannot be a non-type template argument. |
| 3668 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3669 | << Arg.getSourceRange(); |
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 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3672 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3673 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3674 | case TemplateArgument::Type: { |
| 3675 | // We have a non-type template parameter but the template |
| 3676 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3677 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3678 | // C++ [temp.arg]p2: |
| 3679 | // In a template-argument, an ambiguity between a type-id and |
| 3680 | // an expression is resolved to a type-id, regardless of the |
| 3681 | // form of the corresponding template-parameter. |
| 3682 | // |
| 3683 | // We warn specifically about this case, since it can be rather |
| 3684 | // confusing for users. |
| 3685 | QualType T = Arg.getArgument().getAsType(); |
| 3686 | SourceRange SR = Arg.getSourceRange(); |
| 3687 | if (T->isFunctionType()) |
| 3688 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3689 | else |
| 3690 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3691 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3692 | return true; |
| 3693 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3694 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3695 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3696 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3697 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3698 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3699 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3703 | // Check template template parameters. |
| 3704 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3705 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3706 | // Substitute into the template parameter list of the template |
| 3707 | // template parameter, since previously-supplied template arguments |
| 3708 | // may appear within the template template parameter. |
| 3709 | { |
| 3710 | // Set up a template instantiation context. |
| 3711 | LocalInstantiationScope Scope(*this); |
| 3712 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3713 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3714 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3715 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3716 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3717 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3718 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3719 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3720 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3721 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3722 | if (!TempParm) |
| 3723 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3724 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3725 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3726 | switch (Arg.getArgument().getKind()) { |
| 3727 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3728 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3729 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3730 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3731 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3732 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3733 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3734 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3735 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3736 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3737 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3738 | case TemplateArgument::Expression: |
| 3739 | case TemplateArgument::Type: |
| 3740 | // We have a template template parameter but the template |
| 3741 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3742 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3743 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3744 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3745 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3746 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3747 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3748 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3749 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3750 | case TemplateArgument::NullPtr: |
| 3751 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3752 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3753 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3754 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3755 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3756 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3757 | return false; |
| 3758 | } |
| 3759 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3760 | /// \brief Diagnose an arity mismatch in the |
| 3761 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3762 | SourceLocation TemplateLoc, |
| 3763 | TemplateArgumentListInfo &TemplateArgs) { |
| 3764 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3765 | unsigned NumParams = Params->size(); |
| 3766 | unsigned NumArgs = TemplateArgs.size(); |
| 3767 | |
| 3768 | SourceRange Range; |
| 3769 | if (NumArgs > NumParams) |
| 3770 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3771 | TemplateArgs.getRAngleLoc()); |
| 3772 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3773 | << (NumArgs > NumParams) |
| 3774 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3775 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3776 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3777 | << Template << Range; |
| 3778 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3779 | << Params->getSourceRange(); |
| 3780 | return true; |
| 3781 | } |
| 3782 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3783 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3784 | /// determine the number of parameters produced by that expansion. For instance: |
| 3785 | /// |
| 3786 | /// \code |
| 3787 | /// template<typename ...Ts> struct A { |
| 3788 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3789 | /// }; |
| 3790 | /// \endcode |
| 3791 | /// |
| 3792 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3793 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3794 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3795 | if (NonTypeTemplateParmDecl *NTTP |
| 3796 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3797 | if (NTTP->isExpandedParameterPack()) |
| 3798 | return NTTP->getNumExpansionTypes(); |
| 3799 | } |
| 3800 | |
| 3801 | if (TemplateTemplateParmDecl *TTP |
| 3802 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3803 | if (TTP->isExpandedParameterPack()) |
| 3804 | return TTP->getNumExpansionTemplateParameters(); |
| 3805 | } |
| 3806 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3807 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3810 | /// Diagnose a missing template argument. |
| 3811 | template<typename TemplateParmDecl> |
| 3812 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 3813 | TemplateDecl *TD, |
| 3814 | const TemplateParmDecl *D, |
| 3815 | TemplateArgumentListInfo &Args) { |
| 3816 | // Dig out the most recent declaration of the template parameter; there may be |
| 3817 | // declarations of the template that are more recent than TD. |
| 3818 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 3819 | ->getTemplateParameters() |
| 3820 | ->getParam(D->getIndex())); |
| 3821 | |
| 3822 | // If there's a default argument that's not visible, diagnose that we're |
| 3823 | // missing a module import. |
| 3824 | llvm::SmallVector<Module*, 8> Modules; |
| 3825 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 3826 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 3827 | D->getDefaultArgumentLoc(), Modules, |
| 3828 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3829 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3830 | return true; |
| 3831 | } |
| 3832 | |
| 3833 | // FIXME: If there's a more recent default argument that *is* visible, |
| 3834 | // diagnose that it was declared too late. |
| 3835 | |
| 3836 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 3837 | } |
| 3838 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3839 | /// \brief Check that the given template argument list is well-formed |
| 3840 | /// for specializing the given template. |
| 3841 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3842 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3843 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3844 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3845 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3846 | // Make a copy of the template arguments for processing. Only make the |
| 3847 | // changes at the end when successful in matching the arguments to the |
| 3848 | // template. |
| 3849 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3850 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3851 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3852 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3853 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3854 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3856 | // [...] The type and form of each template-argument specified in |
| 3857 | // a template-id shall match the type and form specified for the |
| 3858 | // corresponding parameter declared by the template in its |
| 3859 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3860 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3861 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3862 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3863 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3864 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3865 | ParamEnd = Params->end(); |
| 3866 | Param != ParamEnd; /* increment in loop */) { |
| 3867 | // If we have an expanded parameter pack, make sure we don't have too |
| 3868 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3869 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3870 | if (*Expansions == ArgumentPack.size()) { |
| 3871 | // We're done with this parameter pack. Pack up its arguments and add |
| 3872 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3873 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3874 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3875 | ArgumentPack.clear(); |
| 3876 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3877 | // This argument is assigned to the next parameter. |
| 3878 | ++Param; |
| 3879 | continue; |
| 3880 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3881 | // Not enough arguments for this parameter pack. |
| 3882 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3883 | << false |
| 3884 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3885 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3886 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3887 | << Template; |
| 3888 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3889 | << Params->getSourceRange(); |
| 3890 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3891 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3892 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3893 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3894 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3895 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3896 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3897 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3898 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3899 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3900 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3901 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3902 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3903 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3904 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3905 | // 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] | 3906 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3907 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3908 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3909 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3910 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3911 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3912 | return true; |
| 3913 | } |
| 3914 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3915 | // We're now done with this argument. |
| 3916 | ++ArgIdx; |
| 3917 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3918 | if ((*Param)->isTemplateParameterPack()) { |
| 3919 | // The template parameter was a template parameter pack, so take the |
| 3920 | // deduced argument and place it on the argument pack. Note that we |
| 3921 | // stay on the same template parameter so that we can deduce more |
| 3922 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3923 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3924 | } else { |
| 3925 | // Move to the next template parameter. |
| 3926 | ++Param; |
| 3927 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3928 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3929 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3930 | // the remaining arguments, because we don't know what parameters they'll |
| 3931 | // match up with. |
| 3932 | if (PackExpansionIntoNonPack) { |
| 3933 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3934 | // If we were part way through filling in an expanded parameter pack, |
| 3935 | // fall back to just producing individual arguments. |
| 3936 | Converted.insert(Converted.end(), |
| 3937 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3938 | ArgumentPack.clear(); |
| 3939 | } |
| 3940 | |
| 3941 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3942 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3943 | ++ArgIdx; |
| 3944 | } |
| 3945 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3946 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3947 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3948 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3949 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3950 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3951 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3952 | // If we're checking a partial template argument list, we're done. |
| 3953 | if (PartialTemplateArgs) { |
| 3954 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3955 | Converted.push_back( |
| 3956 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 3957 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3958 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3961 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3962 | // 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] | 3963 | if ((*Param)->isTemplateParameterPack()) { |
| 3964 | assert(!getExpandedPackSize(*Param) && |
| 3965 | "Should have dealt with this already"); |
| 3966 | |
| 3967 | // A non-expanded parameter pack before the end of the parameter list |
| 3968 | // only occurs for an ill-formed template parameter list, unless we've |
| 3969 | // got a partial argument list for a function template, so just bail out. |
| 3970 | if (Param + 1 != ParamEnd) |
| 3971 | return true; |
| 3972 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3973 | Converted.push_back( |
| 3974 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3975 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3976 | |
| 3977 | ++Param; |
| 3978 | continue; |
| 3979 | } |
| 3980 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3981 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3982 | TemplateArgumentLoc Arg; |
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 | // Retrieve the default template argument from the template |
| 3985 | // parameter. For each kind of template parameter, we substitute the |
| 3986 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3987 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3988 | // the default argument. |
| 3989 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3990 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3991 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 3992 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3993 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3994 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3995 | Template, |
| 3996 | TemplateLoc, |
| 3997 | RAngleLoc, |
| 3998 | TTP, |
| 3999 | Converted); |
| 4000 | if (!ArgType) |
| 4001 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4002 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4003 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 4004 | ArgType); |
| 4005 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4006 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4007 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4008 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 4009 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4010 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4011 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4012 | TemplateLoc, |
| 4013 | RAngleLoc, |
| 4014 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4015 | Converted); |
| 4016 | if (E.isInvalid()) |
| 4017 | return true; |
| 4018 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4019 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4020 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 4021 | } else { |
| 4022 | TemplateTemplateParmDecl *TempParm |
| 4023 | = cast<TemplateTemplateParmDecl>(*Param); |
| 4024 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 4025 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 4026 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 4027 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4028 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 4029 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4030 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4031 | TemplateLoc, |
| 4032 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4033 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4034 | Converted, |
| 4035 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4036 | if (Name.isNull()) |
| 4037 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4038 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 4039 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 4040 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4041 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4042 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4043 | // Introduce an instantiation record that describes where we are using |
| 4044 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4045 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 4046 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4047 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 4048 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4049 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 4050 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 4051 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 4052 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 4053 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4054 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4055 | // Core issue 150 (assumed resolution): if this is a template template |
| 4056 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4057 | // template definition. |
| 4058 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4059 | NewArgs.addArgument(Arg); |
| 4060 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 4061 | // Move to the next template parameter and argument. |
| 4062 | ++Param; |
| 4063 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4064 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4065 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4066 | // If we're performing a partial argument substitution, allow any trailing |
| 4067 | // pack expansions; they might be empty. This can happen even if |
| 4068 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 4069 | // still dependent). |
| 4070 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 4071 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4072 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 4073 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 4076 | // If we have any leftover arguments, then there were too many arguments. |
| 4077 | // Complain and fail. |
| 4078 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 4079 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 4080 | |
| 4081 | // No problems found with the new argument list, propagate changes back |
| 4082 | // to caller. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 4083 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4084 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4085 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4088 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4089 | class UnnamedLocalNoLinkageFinder |
| 4090 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4091 | { |
| 4092 | Sema &S; |
| 4093 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4094 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4095 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4096 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4097 | public: |
| 4098 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 4099 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4100 | bool Visit(QualType T) { |
| 4101 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4102 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4103 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4104 | #define TYPE(Class, Parent) \ |
| 4105 | bool Visit##Class##Type(const Class##Type *); |
| 4106 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4107 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4108 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4109 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4110 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4111 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4112 | bool VisitTagDecl(const TagDecl *Tag); |
| 4113 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4114 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4115 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4116 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4117 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4118 | return false; |
| 4119 | } |
| 4120 | |
| 4121 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4122 | return Visit(T->getElementType()); |
| 4123 | } |
| 4124 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4125 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4126 | return Visit(T->getPointeeType()); |
| 4127 | } |
| 4128 | |
| 4129 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4130 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4131 | return Visit(T->getPointeeType()); |
| 4132 | } |
| 4133 | |
| 4134 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4135 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4136 | return Visit(T->getPointeeType()); |
| 4137 | } |
| 4138 | |
| 4139 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4140 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4141 | return Visit(T->getPointeeType()); |
| 4142 | } |
| 4143 | |
| 4144 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4145 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4146 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4147 | } |
| 4148 | |
| 4149 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4150 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4151 | return Visit(T->getElementType()); |
| 4152 | } |
| 4153 | |
| 4154 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4155 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4156 | return Visit(T->getElementType()); |
| 4157 | } |
| 4158 | |
| 4159 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4160 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4161 | return Visit(T->getElementType()); |
| 4162 | } |
| 4163 | |
| 4164 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4165 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4166 | return Visit(T->getElementType()); |
| 4167 | } |
| 4168 | |
| 4169 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4170 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4171 | return Visit(T->getElementType()); |
| 4172 | } |
| 4173 | |
| 4174 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4175 | return Visit(T->getElementType()); |
| 4176 | } |
| 4177 | |
| 4178 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4179 | return Visit(T->getElementType()); |
| 4180 | } |
| 4181 | |
| 4182 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4183 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4184 | for (const auto &A : T->param_types()) { |
| 4185 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4186 | return true; |
| 4187 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4188 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4189 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4193 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4194 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4198 | const UnresolvedUsingType*) { |
| 4199 | return false; |
| 4200 | } |
| 4201 | |
| 4202 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4203 | return false; |
| 4204 | } |
| 4205 | |
| 4206 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4207 | return Visit(T->getUnderlyingType()); |
| 4208 | } |
| 4209 | |
| 4210 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4211 | return false; |
| 4212 | } |
| 4213 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4214 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4215 | const UnaryTransformType*) { |
| 4216 | return false; |
| 4217 | } |
| 4218 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4219 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4220 | return Visit(T->getDeducedType()); |
| 4221 | } |
| 4222 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4223 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4224 | return VisitTagDecl(T->getDecl()); |
| 4225 | } |
| 4226 | |
| 4227 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4228 | return VisitTagDecl(T->getDecl()); |
| 4229 | } |
| 4230 | |
| 4231 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4232 | const TemplateTypeParmType*) { |
| 4233 | return false; |
| 4234 | } |
| 4235 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4236 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4237 | const SubstTemplateTypeParmPackType *) { |
| 4238 | return false; |
| 4239 | } |
| 4240 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4241 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4242 | const TemplateSpecializationType*) { |
| 4243 | return false; |
| 4244 | } |
| 4245 | |
| 4246 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4247 | const InjectedClassNameType* T) { |
| 4248 | return VisitTagDecl(T->getDecl()); |
| 4249 | } |
| 4250 | |
| 4251 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4252 | const DependentNameType* T) { |
| 4253 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4254 | } |
| 4255 | |
| 4256 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4257 | const DependentTemplateSpecializationType* T) { |
| 4258 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4259 | } |
| 4260 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4261 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4262 | const PackExpansionType* T) { |
| 4263 | return Visit(T->getPattern()); |
| 4264 | } |
| 4265 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4266 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4267 | return false; |
| 4268 | } |
| 4269 | |
| 4270 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4271 | const ObjCInterfaceType *) { |
| 4272 | return false; |
| 4273 | } |
| 4274 | |
| 4275 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4276 | const ObjCObjectPointerType *) { |
| 4277 | return false; |
| 4278 | } |
| 4279 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4280 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4281 | return Visit(T->getValueType()); |
| 4282 | } |
| 4283 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4284 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 4285 | return false; |
| 4286 | } |
| 4287 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4288 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4289 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4290 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4291 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4292 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4293 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4294 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4295 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4296 | } |
| 4297 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4298 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4299 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4300 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4301 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4302 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4303 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4304 | return true; |
| 4305 | } |
| 4306 | |
| 4307 | return false; |
| 4308 | } |
| 4309 | |
| 4310 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4311 | NestedNameSpecifier *NNS) { |
| 4312 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4313 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4314 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4315 | switch (NNS->getKind()) { |
| 4316 | case NestedNameSpecifier::Identifier: |
| 4317 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4318 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4319 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4320 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4321 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4322 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4323 | case NestedNameSpecifier::TypeSpec: |
| 4324 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4325 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4326 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4327 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4330 | /// \brief Check a template argument against its corresponding |
| 4331 | /// template type parameter. |
| 4332 | /// |
| 4333 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4334 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4335 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4336 | TypeSourceInfo *ArgInfo) { |
| 4337 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4338 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4339 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4340 | |
| 4341 | if (Arg->isVariablyModifiedType()) { |
| 4342 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4343 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4344 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4347 | // C++03 [temp.arg.type]p2: |
| 4348 | // A local type, a type with no linkage, an unnamed type or a type |
| 4349 | // compounded from any of these types shall not be used as a |
| 4350 | // template-argument for a template type-parameter. |
| 4351 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4352 | // 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] | 4353 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4354 | bool NeedsCheck; |
| 4355 | if (LangOpts.CPlusPlus11) |
| 4356 | NeedsCheck = |
| 4357 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4358 | SR.getBegin()) || |
| 4359 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4360 | SR.getBegin()); |
| 4361 | else |
| 4362 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4363 | |
| 4364 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4365 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4366 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4367 | } |
| 4368 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4369 | return false; |
| 4370 | } |
| 4371 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4372 | enum NullPointerValueKind { |
| 4373 | NPV_NotNullPointer, |
| 4374 | NPV_NullPointer, |
| 4375 | NPV_Error |
| 4376 | }; |
| 4377 | |
| 4378 | /// \brief Determine whether the given template argument is a null pointer |
| 4379 | /// value of the appropriate type. |
| 4380 | static NullPointerValueKind |
| 4381 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4382 | QualType ParamType, Expr *Arg) { |
| 4383 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4384 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4385 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4386 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 4387 | llvm_unreachable( |
| 4388 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4389 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4390 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4391 | return NPV_NotNullPointer; |
| 4392 | |
| 4393 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4394 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4395 | if (ArgRV.isInvalid()) |
| 4396 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4397 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4398 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4399 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4400 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4401 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4402 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4403 | EvalResult.HasSideEffects) { |
| 4404 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4405 | |
| 4406 | // If our only note is the usual "invalid subexpression" note, just point |
| 4407 | // the caret at its location rather than producing an essentially |
| 4408 | // redundant note. |
| 4409 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4410 | diag::note_invalid_subexpr_in_const_expr) { |
| 4411 | DiagLoc = Notes[0].first; |
| 4412 | Notes.clear(); |
| 4413 | } |
| 4414 | |
| 4415 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4416 | << Arg->getType() << Arg->getSourceRange(); |
| 4417 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4418 | S.Diag(Notes[I].first, Notes[I].second); |
| 4419 | |
| 4420 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4421 | return NPV_Error; |
| 4422 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4423 | |
| 4424 | // C++11 [temp.arg.nontype]p1: |
| 4425 | // - an address constant expression of type std::nullptr_t |
| 4426 | if (Arg->getType()->isNullPtrType()) |
| 4427 | return NPV_NullPointer; |
| 4428 | |
| 4429 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4430 | // - a constant expression that evaluates to a null member pointer value |
| 4431 | // (4.11); or |
| 4432 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4433 | (EvalResult.Val.isMemberPointer() && |
| 4434 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4435 | // If our expression has an appropriate type, we've succeeded. |
| 4436 | bool ObjCLifetimeConversion; |
| 4437 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4438 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4439 | ObjCLifetimeConversion)) |
| 4440 | return NPV_NullPointer; |
| 4441 | |
| 4442 | // The types didn't match, but we know we got a null pointer; complain, |
| 4443 | // then recover as if the types were correct. |
| 4444 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4445 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4446 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4447 | return NPV_NullPointer; |
| 4448 | } |
| 4449 | |
| 4450 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4451 | // constant, suggest a cast to the appropriate type. |
| 4452 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4453 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4454 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4455 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4456 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4457 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4458 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4459 | return NPV_NullPointer; |
| 4460 | } |
| 4461 | |
| 4462 | // FIXME: If we ever want to support general, address-constant expressions |
| 4463 | // as non-type template arguments, we should return the ExprResult here to |
| 4464 | // be interpreted by the caller. |
| 4465 | return NPV_NotNullPointer; |
| 4466 | } |
| 4467 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4468 | /// \brief Checks whether the given template argument is compatible with its |
| 4469 | /// template parameter. |
| 4470 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4471 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4472 | Expr *Arg, QualType ArgType) { |
| 4473 | bool ObjCLifetimeConversion; |
| 4474 | if (ParamType->isPointerType() && |
| 4475 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4476 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4477 | ObjCLifetimeConversion)) { |
| 4478 | // For pointer-to-object types, qualification conversions are |
| 4479 | // permitted. |
| 4480 | } else { |
| 4481 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4482 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4483 | // C++ [temp.arg.nontype]p5b3: |
| 4484 | // For a non-type template-parameter of type reference to |
| 4485 | // object, no conversions apply. The type referred to by the |
| 4486 | // reference may be more cv-qualified than the (otherwise |
| 4487 | // identical) type of the template- argument. The |
| 4488 | // template-parameter is bound directly to the |
| 4489 | // template-argument, which shall be an lvalue. |
| 4490 | |
| 4491 | // FIXME: Other qualifiers? |
| 4492 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4493 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4494 | |
| 4495 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4496 | S.Diag(Arg->getLocStart(), |
| 4497 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4498 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4499 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4500 | return true; |
| 4501 | } |
| 4502 | } |
| 4503 | } |
| 4504 | |
| 4505 | // At this point, the template argument refers to an object or |
| 4506 | // function with external linkage. We now need to check whether the |
| 4507 | // argument and parameter types are compatible. |
| 4508 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4509 | ParamType.getNonReferenceType())) { |
| 4510 | // We can't perform this conversion or binding. |
| 4511 | if (ParamType->isReferenceType()) |
| 4512 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4513 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4514 | else |
| 4515 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4516 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4517 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4518 | return true; |
| 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | return false; |
| 4523 | } |
| 4524 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4525 | /// \brief Checks whether the given template argument is the address |
| 4526 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4528 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4529 | NonTypeTemplateParmDecl *Param, |
| 4530 | QualType ParamType, |
| 4531 | Expr *ArgIn, |
| 4532 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4533 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4534 | Expr *Arg = ArgIn; |
| 4535 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4536 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4537 | bool AddressTaken = false; |
| 4538 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4539 | if (S.getLangOpts().MicrosoftExt) { |
| 4540 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4541 | // dereference and address-of operators. |
| 4542 | Arg = Arg->IgnoreParenCasts(); |
| 4543 | |
| 4544 | bool ExtWarnMSTemplateArg = false; |
| 4545 | UnaryOperatorKind FirstOpKind; |
| 4546 | SourceLocation FirstOpLoc; |
| 4547 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4548 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4549 | if (UnOpKind == UO_Deref) |
| 4550 | ExtWarnMSTemplateArg = true; |
| 4551 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4552 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4553 | if (!AddrOpLoc.isValid()) { |
| 4554 | FirstOpKind = UnOpKind; |
| 4555 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4556 | } |
| 4557 | } else |
| 4558 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4559 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4560 | if (FirstOpLoc.isValid()) { |
| 4561 | if (ExtWarnMSTemplateArg) |
| 4562 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4563 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4564 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4565 | if (FirstOpKind == UO_AddrOf) |
| 4566 | AddressTaken = true; |
| 4567 | else if (Arg->getType()->isPointerType()) { |
| 4568 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4569 | // constant expression. |
| 4570 | assert(FirstOpKind == UO_Deref); |
| 4571 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4572 | << Arg->getSourceRange(); |
| 4573 | } |
| 4574 | } |
| 4575 | } else { |
| 4576 | // See through any implicit casts we added to fix the type. |
| 4577 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4578 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4579 | // C++ [temp.arg.nontype]p1: |
| 4580 | // |
| 4581 | // A template-argument for a non-type, non-template |
| 4582 | // template-parameter shall be one of: [...] |
| 4583 | // |
| 4584 | // -- the address of an object or function with external |
| 4585 | // linkage, including function templates and function |
| 4586 | // template-ids but excluding non-static class members, |
| 4587 | // expressed as & id-expression where the & is optional if |
| 4588 | // the name refers to a function or array, or if the |
| 4589 | // corresponding template-parameter is a reference; or |
| 4590 | |
| 4591 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4592 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4593 | bool ExtraParens = false; |
| 4594 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4595 | if (!Invalid && !ExtraParens) { |
| 4596 | S.Diag(Arg->getLocStart(), |
| 4597 | S.getLangOpts().CPlusPlus11 |
| 4598 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4599 | : diag::ext_template_arg_extra_parens) |
| 4600 | << Arg->getSourceRange(); |
| 4601 | ExtraParens = true; |
| 4602 | } |
| 4603 | |
| 4604 | Arg = Parens->getSubExpr(); |
| 4605 | } |
| 4606 | |
| 4607 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4608 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4609 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4610 | |
| 4611 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4612 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4613 | Arg = UnOp->getSubExpr(); |
| 4614 | AddressTaken = true; |
| 4615 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4616 | } |
| 4617 | } |
| 4618 | |
| 4619 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4620 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4621 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4622 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4623 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4624 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4625 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4626 | |
| 4627 | // If our parameter has pointer type, check for a null template value. |
| 4628 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4629 | NullPointerValueKind NPV; |
| 4630 | // dllimport'd entities aren't constant but are available inside of template |
| 4631 | // arguments. |
| 4632 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4633 | NPV = NPV_NotNullPointer; |
| 4634 | else |
| 4635 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4636 | switch (NPV) { |
| 4637 | case NPV_NullPointer: |
| 4638 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4639 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4640 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4641 | return false; |
| 4642 | |
| 4643 | case NPV_Error: |
| 4644 | return true; |
| 4645 | |
| 4646 | case NPV_NotNullPointer: |
| 4647 | break; |
| 4648 | } |
| 4649 | } |
| 4650 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4651 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4652 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4653 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4654 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4655 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4656 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4657 | |
| 4658 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4659 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4660 | ArgIn, Arg, ArgType)) |
| 4661 | return true; |
| 4662 | |
| 4663 | Converted = TemplateArgument(ArgIn); |
| 4664 | return false; |
| 4665 | } |
| 4666 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4667 | if (!DRE) { |
| 4668 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4669 | << Arg->getSourceRange(); |
| 4670 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4671 | return true; |
| 4672 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4673 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4674 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4675 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4676 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4677 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4678 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4679 | return true; |
| 4680 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4681 | |
| 4682 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4683 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4684 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4685 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4686 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4687 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4688 | return true; |
| 4689 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4690 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4692 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4693 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4694 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4695 | // A non-type template argument must refer to an object or function. |
| 4696 | if (!Func && !Var) { |
| 4697 | // We found something, but we don't know specifically what it is. |
| 4698 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4699 | << Arg->getSourceRange(); |
| 4700 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4701 | return true; |
| 4702 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4703 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4704 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4705 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4706 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4707 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4708 | diag::ext_template_arg_object_internal) |
| 4709 | << !Func << Entity << Arg->getSourceRange(); |
| 4710 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4711 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4712 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4713 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4714 | << !Func << Entity << Arg->getSourceRange(); |
| 4715 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4716 | << !Func; |
| 4717 | return true; |
| 4718 | } |
| 4719 | |
| 4720 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4721 | // If the template parameter has pointer type, the function decays. |
| 4722 | if (ParamType->isPointerType() && !AddressTaken) |
| 4723 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4724 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4725 | // If we originally had an address-of operator, but the |
| 4726 | // parameter has reference type, complain and (if things look |
| 4727 | // like they will work) drop the address-of operator. |
| 4728 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4729 | ParamType.getNonReferenceType())) { |
| 4730 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4731 | << ParamType; |
| 4732 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4733 | return true; |
| 4734 | } |
| 4735 | |
| 4736 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4737 | << ParamType |
| 4738 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4739 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4740 | |
| 4741 | ArgType = Func->getType(); |
| 4742 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4743 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4744 | // A value of reference type is not an object. |
| 4745 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4746 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4747 | diag::err_template_arg_reference_var) |
| 4748 | << Var->getType() << Arg->getSourceRange(); |
| 4749 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4750 | return true; |
| 4751 | } |
| 4752 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4753 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4754 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4755 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4756 | << Arg->getSourceRange(); |
| 4757 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4758 | return true; |
| 4759 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4760 | |
| 4761 | // If the template parameter has pointer type, we must have taken |
| 4762 | // the address of this object. |
| 4763 | if (ParamType->isReferenceType()) { |
| 4764 | if (AddressTaken) { |
| 4765 | // If we originally had an address-of operator, but the |
| 4766 | // parameter has reference type, complain and (if things look |
| 4767 | // like they will work) drop the address-of operator. |
| 4768 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4769 | ParamType.getNonReferenceType())) { |
| 4770 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4771 | << ParamType; |
| 4772 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4773 | return true; |
| 4774 | } |
| 4775 | |
| 4776 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4777 | << ParamType |
| 4778 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4779 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4780 | |
| 4781 | ArgType = Var->getType(); |
| 4782 | } |
| 4783 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4784 | if (Var->getType()->isArrayType()) { |
| 4785 | // Array-to-pointer decay. |
| 4786 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4787 | } else { |
| 4788 | // If the template parameter has pointer type but the address of |
| 4789 | // this object was not taken, complain and (possibly) recover by |
| 4790 | // taking the address of the entity. |
| 4791 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4792 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4793 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4794 | << ParamType; |
| 4795 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4796 | return true; |
| 4797 | } |
| 4798 | |
| 4799 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4800 | << ParamType |
| 4801 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4802 | |
| 4803 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4804 | } |
| 4805 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4806 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4808 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4809 | Arg, ArgType)) |
| 4810 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4811 | |
| 4812 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4813 | Converted = |
| 4814 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4815 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4816 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
| 4819 | /// \brief Checks whether the given template argument is a pointer to |
| 4820 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4821 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4822 | NonTypeTemplateParmDecl *Param, |
| 4823 | QualType ParamType, |
| 4824 | Expr *&ResultArg, |
| 4825 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4826 | bool Invalid = false; |
| 4827 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4828 | // Check for a null pointer value. |
| 4829 | Expr *Arg = ResultArg; |
| 4830 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4831 | case NPV_Error: |
| 4832 | return true; |
| 4833 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4834 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4835 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4836 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4837 | return false; |
| 4838 | case NPV_NotNullPointer: |
| 4839 | break; |
| 4840 | } |
| 4841 | |
| 4842 | bool ObjCLifetimeConversion; |
| 4843 | if (S.IsQualificationConversion(Arg->getType(), |
| 4844 | ParamType.getNonReferenceType(), |
| 4845 | false, ObjCLifetimeConversion)) { |
| 4846 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4847 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4848 | ResultArg = Arg; |
| 4849 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4850 | ParamType.getNonReferenceType())) { |
| 4851 | // We can't perform this conversion. |
| 4852 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4853 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4854 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4855 | return true; |
| 4856 | } |
| 4857 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4858 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4859 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4860 | Arg = Cast->getSubExpr(); |
| 4861 | |
| 4862 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4863 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4864 | // A template-argument for a non-type, non-template |
| 4865 | // template-parameter shall be one of: [...] |
| 4866 | // |
| 4867 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4868 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4869 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4870 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4871 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4872 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4873 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4874 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4875 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4876 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4877 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4878 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4879 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4880 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | Arg = Parens->getSubExpr(); |
| 4884 | } |
| 4885 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4886 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4887 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4888 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4889 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4890 | // A pointer-to-member constant written &Class::member. |
| 4891 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4892 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4893 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4894 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4895 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4896 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4897 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4898 | // A constant of pointer-to-member type. |
| 4899 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4900 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4901 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4902 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4903 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4904 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4905 | } else { |
| 4906 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4907 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4908 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4909 | return Invalid; |
| 4910 | } |
| 4911 | } |
| 4912 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4913 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4914 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4915 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4916 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4917 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4918 | return S.Diag(Arg->getLocStart(), |
| 4919 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4920 | << Arg->getSourceRange(); |
| 4921 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4922 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4923 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4924 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4925 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4926 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4927 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4928 | "Only non-static member pointers can make it here"); |
| 4929 | |
| 4930 | // Okay: this is the address of a non-static member, and therefore |
| 4931 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4932 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4933 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4934 | } else { |
| 4935 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4936 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4937 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4938 | return Invalid; |
| 4939 | } |
| 4940 | |
| 4941 | // 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] | 4942 | S.Diag(Arg->getLocStart(), |
| 4943 | diag::err_template_arg_not_pointer_to_member_form) |
| 4944 | << Arg->getSourceRange(); |
| 4945 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4946 | return true; |
| 4947 | } |
| 4948 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4949 | /// \brief Check a template argument against its corresponding |
| 4950 | /// non-type template parameter. |
| 4951 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4952 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4953 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4954 | /// returns the converted template argument. \p ParamType is the |
| 4955 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4956 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4957 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4958 | TemplateArgument &Converted, |
| 4959 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4960 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4961 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4962 | // If either the parameter has a dependent type or the argument is |
| 4963 | // type-dependent, there's nothing we can check now. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4964 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4965 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4966 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4967 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4968 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4969 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4970 | // We should have already dropped all cv-qualifiers by now. |
| 4971 | assert(!ParamType.hasQualifiers() && |
| 4972 | "non-type template parameter type cannot be qualified"); |
| 4973 | |
| 4974 | if (CTAK == CTAK_Deduced && |
| 4975 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4976 | // C++ [temp.deduct.type]p17: |
| 4977 | // If, in the declaration of a function template with a non-type |
| 4978 | // template-parameter, the non-type template-parameter is used |
| 4979 | // in an expression in the function parameter-list and, if the |
| 4980 | // corresponding template-argument is deduced, the |
| 4981 | // template-argument type shall match the type of the |
| 4982 | // template-parameter exactly, except that a template-argument |
| 4983 | // deduced from an array bound may be of any integral type. |
| 4984 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4985 | << Arg->getType().getUnqualifiedType() |
| 4986 | << ParamType.getUnqualifiedType(); |
| 4987 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4988 | return ExprError(); |
| 4989 | } |
| 4990 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4991 | if (getLangOpts().CPlusPlus1z) { |
| 4992 | // FIXME: We can do some limited checking for a value-dependent but not |
| 4993 | // type-dependent argument. |
| 4994 | if (Arg->isValueDependent()) { |
| 4995 | Converted = TemplateArgument(Arg); |
| 4996 | return Arg; |
| 4997 | } |
| 4998 | |
| 4999 | // C++1z [temp.arg.nontype]p1: |
| 5000 | // A template-argument for a non-type template parameter shall be |
| 5001 | // a converted constant expression of the type of the template-parameter. |
| 5002 | APValue Value; |
| 5003 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 5004 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 5005 | if (ArgResult.isInvalid()) |
| 5006 | return ExprError(); |
| 5007 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5008 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 5009 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5010 | // Convert the APValue to a TemplateArgument. |
| 5011 | switch (Value.getKind()) { |
| 5012 | case APValue::Uninitialized: |
| 5013 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5014 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5015 | break; |
| 5016 | case APValue::Int: |
| 5017 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5018 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5019 | break; |
| 5020 | case APValue::MemberPointer: { |
| 5021 | assert(ParamType->isMemberPointerType()); |
| 5022 | |
| 5023 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 5024 | if (!Value.getMemberPointerPath().empty()) { |
| 5025 | Diag(Arg->getLocStart(), |
| 5026 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 5027 | << Value.getMemberPointerDecl() << ParamType |
| 5028 | << Arg->getSourceRange(); |
| 5029 | return ExprError(); |
| 5030 | } |
| 5031 | |
| 5032 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5033 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5034 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5035 | break; |
| 5036 | } |
| 5037 | case APValue::LValue: { |
| 5038 | // For a non-type template-parameter of pointer or reference type, |
| 5039 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5040 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 5041 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5042 | // -- a temporary object |
| 5043 | // -- a string literal |
| 5044 | // -- the result of a typeid expression, or |
| 5045 | // -- a predefind __func__ variable |
| 5046 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 5047 | if (isa<CXXUuidofExpr>(E)) { |
| 5048 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 5049 | break; |
| 5050 | } |
| 5051 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 5052 | << Arg->getSourceRange(); |
| 5053 | return ExprError(); |
| 5054 | } |
| 5055 | auto *VD = const_cast<ValueDecl *>( |
| 5056 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 5057 | // -- a subobject |
| 5058 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 5059 | VD && VD->getType()->isArrayType() && |
| 5060 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 5061 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 5062 | // Per defect report (no number yet): |
| 5063 | // ... other than a pointer to the first element of a complete array |
| 5064 | // object. |
| 5065 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 5066 | Value.isLValueOnePastTheEnd()) { |
| 5067 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 5068 | << Value.getAsString(Context, ParamType); |
| 5069 | return ExprError(); |
| 5070 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5071 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5072 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5073 | assert((!VD || !ParamType->isNullPtrType()) && |
| 5074 | "non-null value of type nullptr_t?"); |
| 5075 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 5076 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5077 | break; |
| 5078 | } |
| 5079 | case APValue::AddrLabelDiff: |
| 5080 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 5081 | case APValue::Float: |
| 5082 | case APValue::ComplexInt: |
| 5083 | case APValue::ComplexFloat: |
| 5084 | case APValue::Vector: |
| 5085 | case APValue::Array: |
| 5086 | case APValue::Struct: |
| 5087 | case APValue::Union: |
| 5088 | llvm_unreachable("invalid kind for template argument"); |
| 5089 | } |
| 5090 | |
| 5091 | return ArgResult.get(); |
| 5092 | } |
| 5093 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5094 | // C++ [temp.arg.nontype]p5: |
| 5095 | // The following conversions are performed on each expression used |
| 5096 | // as a non-type template-argument. If a non-type |
| 5097 | // template-argument cannot be converted to the type of the |
| 5098 | // corresponding template-parameter then the program is |
| 5099 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5100 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5101 | // C++11: |
| 5102 | // -- for a non-type template-parameter of integral or |
| 5103 | // enumeration type, conversions permitted in a converted |
| 5104 | // constant expression are applied. |
| 5105 | // |
| 5106 | // C++98: |
| 5107 | // -- for a non-type template-parameter of integral or |
| 5108 | // enumeration type, integral promotions (4.5) and integral |
| 5109 | // conversions (4.7) are applied. |
| 5110 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5111 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5112 | // We can't check arbitrary value-dependent arguments. |
| 5113 | // FIXME: If there's no viable conversion to the template parameter type, |
| 5114 | // we should be able to diagnose that prior to instantiation. |
| 5115 | if (Arg->isValueDependent()) { |
| 5116 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5117 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5118 | } |
| 5119 | |
| 5120 | // C++ [temp.arg.nontype]p1: |
| 5121 | // A template-argument for a non-type, non-template template-parameter |
| 5122 | // shall be one of: |
| 5123 | // |
| 5124 | // -- for a non-type template-parameter of integral or enumeration |
| 5125 | // type, a converted constant expression of the type of the |
| 5126 | // template-parameter; or |
| 5127 | llvm::APSInt Value; |
| 5128 | ExprResult ArgResult = |
| 5129 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5130 | CCEK_TemplateArg); |
| 5131 | if (ArgResult.isInvalid()) |
| 5132 | return ExprError(); |
| 5133 | |
| 5134 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5135 | // always a no-op, except when the parameter type is bool. In |
| 5136 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5137 | QualType IntegerType = ParamType; |
| 5138 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5139 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5140 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5141 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5142 | Converted = TemplateArgument(Context, Value, |
| 5143 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5144 | return ArgResult; |
| 5145 | } |
| 5146 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5147 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5148 | if (ArgResult.isInvalid()) |
| 5149 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5150 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5151 | |
| 5152 | QualType ArgType = Arg->getType(); |
| 5153 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5154 | // C++ [temp.arg.nontype]p1: |
| 5155 | // A template-argument for a non-type, non-template |
| 5156 | // template-parameter shall be one of: |
| 5157 | // |
| 5158 | // -- an integral constant-expression of integral or enumeration |
| 5159 | // type; or |
| 5160 | // -- the name of a non-type template-parameter; or |
| 5161 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5162 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5163 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5164 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5165 | diag::err_template_arg_not_integral_or_enumeral) |
| 5166 | << ArgType << Arg->getSourceRange(); |
| 5167 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5168 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5169 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5170 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5171 | QualType T; |
| 5172 | |
| 5173 | public: |
| 5174 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5175 | |
| 5176 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5177 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5178 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5179 | } |
| 5180 | } Diagnoser(ArgType); |
| 5181 | |
| 5182 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5183 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5184 | if (!Arg) |
| 5185 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5188 | // From here on out, all we care about is the unqualified form |
| 5189 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5190 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5191 | |
| 5192 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5193 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5194 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5195 | } else if (ParamType->isBooleanType()) { |
| 5196 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5197 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5198 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5199 | !ParamType->isEnumeralType()) { |
| 5200 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5201 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5202 | } else { |
| 5203 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5204 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5205 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5206 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5207 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5208 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5211 | // Add the value of this argument to the list of converted |
| 5212 | // arguments. We use the bitwidth and signedness of the template |
| 5213 | // parameter. |
| 5214 | if (Arg->isValueDependent()) { |
| 5215 | // The argument is value-dependent. Create a new |
| 5216 | // TemplateArgument with the converted expression. |
| 5217 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5218 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5221 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5222 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5223 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5224 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5225 | if (ParamType->isBooleanType()) { |
| 5226 | // Value must be zero or one. |
| 5227 | Value = Value != 0; |
| 5228 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5229 | if (Value.getBitWidth() != AllowedBits) |
| 5230 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5231 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5232 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5233 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5234 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5235 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5236 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5237 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5238 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5239 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5240 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5241 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5242 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5243 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5244 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5245 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5246 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5247 | << Arg->getSourceRange(); |
| 5248 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5249 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5250 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5251 | // Complain if we overflowed the template parameter's type. |
| 5252 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5253 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5254 | RequiredBits = OldValue.getActiveBits(); |
| 5255 | else if (OldValue.isUnsigned()) |
| 5256 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5257 | else |
| 5258 | RequiredBits = OldValue.getMinSignedBits(); |
| 5259 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5260 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5261 | diag::warn_template_arg_too_large) |
| 5262 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5263 | << Arg->getSourceRange(); |
| 5264 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5265 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5266 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5267 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5268 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5269 | ParamType->isEnumeralType() |
| 5270 | ? Context.getCanonicalType(ParamType) |
| 5271 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5272 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5273 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5274 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5275 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5276 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5277 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5278 | // Handle pointer-to-function, reference-to-function, and |
| 5279 | // pointer-to-member-function all in (roughly) the same way. |
| 5280 | if (// -- For a non-type template-parameter of type pointer to |
| 5281 | // function, only the function-to-pointer conversion (4.3) is |
| 5282 | // applied. If the template-argument represents a set of |
| 5283 | // overloaded functions (or a pointer to such), the matching |
| 5284 | // function is selected from the set (13.4). |
| 5285 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5286 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5287 | // -- For a non-type template-parameter of type reference to |
| 5288 | // function, no conversions apply. If the template-argument |
| 5289 | // represents a set of overloaded functions, the matching |
| 5290 | // function is selected from the set (13.4). |
| 5291 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5292 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5293 | // -- For a non-type template-parameter of type pointer to |
| 5294 | // member function, no conversions apply. If the |
| 5295 | // template-argument represents a set of overloaded member |
| 5296 | // functions, the matching member function is selected from |
| 5297 | // the set (13.4). |
| 5298 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5299 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5300 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5301 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5302 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5303 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5304 | true, |
| 5305 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5306 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5307 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5308 | |
| 5309 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5310 | ArgType = Arg->getType(); |
| 5311 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5312 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5313 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5314 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5315 | if (!ParamType->isMemberPointerType()) { |
| 5316 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5317 | ParamType, |
| 5318 | Arg, Converted)) |
| 5319 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5320 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5321 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5322 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5323 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5324 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5325 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5326 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5327 | } |
| 5328 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5329 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5330 | // -- for a non-type template-parameter of type pointer to |
| 5331 | // object, qualification conversions (4.4) and the |
| 5332 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5333 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5334 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5335 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5336 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5337 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5338 | ParamType, |
| 5339 | Arg, Converted)) |
| 5340 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5341 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5342 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5343 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5344 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5345 | // -- For a non-type template-parameter of type reference to |
| 5346 | // object, no conversions apply. The type referred to by the |
| 5347 | // reference may be more cv-qualified than the (otherwise |
| 5348 | // identical) type of the template-argument. The |
| 5349 | // template-parameter is bound directly to the |
| 5350 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5351 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5352 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5353 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5354 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5355 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5356 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5357 | true, |
| 5358 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5359 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5360 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5361 | |
| 5362 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5363 | ArgType = Arg->getType(); |
| 5364 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5365 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5366 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5367 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5368 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5369 | ParamType, |
| 5370 | Arg, Converted)) |
| 5371 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5372 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5373 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5374 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5375 | // Deal with parameters of type std::nullptr_t. |
| 5376 | if (ParamType->isNullPtrType()) { |
| 5377 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5378 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5379 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5383 | case NPV_NotNullPointer: |
| 5384 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5385 | << Arg->getType() << ParamType; |
| 5386 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5387 | return ExprError(); |
| 5388 | |
| 5389 | case NPV_Error: |
| 5390 | return ExprError(); |
| 5391 | |
| 5392 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5393 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5394 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5395 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5396 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5397 | } |
| 5398 | } |
| 5399 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5400 | // -- For a non-type template-parameter of type pointer to data |
| 5401 | // member, qualification conversions (4.4) are applied. |
| 5402 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5403 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5404 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5405 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5406 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5407 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | /// \brief Check a template argument against its corresponding |
| 5411 | /// template template parameter. |
| 5412 | /// |
| 5413 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5414 | /// It returns true if an error occurred, and false otherwise. |
| 5415 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5416 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5417 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5418 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5419 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5420 | if (!Template) { |
| 5421 | // Any dependent template name is fine. |
| 5422 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5423 | return false; |
| 5424 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5425 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5426 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5427 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5428 | // the name of a class template or an alias template, expressed as an |
| 5429 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5430 | // primary class templates are considered when matching the |
| 5431 | // template template argument with the corresponding parameter; |
| 5432 | // partial specializations are not considered even if their |
| 5433 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5434 | // |
| 5435 | // Note that we also allow template template parameters here, which |
| 5436 | // will happen when we are dealing with, e.g., class template |
| 5437 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5438 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5439 | !isa<TemplateTemplateParmDecl>(Template) && |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5440 | !isa<TypeAliasTemplateDecl>(Template) && |
| 5441 | !isa<BuiltinTemplateDecl>(Template)) { |
| 5442 | assert(isa<FunctionTemplateDecl>(Template) && |
| 5443 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 5444 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
David Majnemer | c2406d4 | 2016-07-11 17:09:56 +0000 | [diff] [blame] | 5445 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 5446 | << Template; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5449 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5450 | if (Param->isExpandedParameterPack()) |
| 5451 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5452 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5453 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5454 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5455 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5456 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5457 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5460 | /// \brief Given a non-type template argument that refers to a |
| 5461 | /// declaration and the type of its corresponding non-type template |
| 5462 | /// parameter, produce an expression that properly refers to that |
| 5463 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5464 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5465 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5466 | QualType ParamType, |
| 5467 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5468 | // C++ [temp.param]p8: |
| 5469 | // |
| 5470 | // A non-type template-parameter of type "array of T" or |
| 5471 | // "function returning T" is adjusted to be of type "pointer to |
| 5472 | // T" or "pointer to function returning T", respectively. |
| 5473 | if (ParamType->isArrayType()) |
| 5474 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5475 | else if (ParamType->isFunctionType()) |
| 5476 | ParamType = Context.getPointerType(ParamType); |
| 5477 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5478 | // For a NULL non-type template argument, return nullptr casted to the |
| 5479 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5480 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5481 | return ImpCastExprToType( |
| 5482 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5483 | ParamType, |
| 5484 | ParamType->getAs<MemberPointerType>() |
| 5485 | ? CK_NullToMemberPointer |
| 5486 | : CK_NullToPointer); |
| 5487 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5488 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5489 | "Only declaration template arguments permitted here"); |
| 5490 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5491 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5492 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5493 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5494 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5495 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5496 | // If the value is a class member, we might have a pointer-to-member. |
| 5497 | // Determine whether the non-type template template parameter is of |
| 5498 | // pointer-to-member type. If so, we need to build an appropriate |
| 5499 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5500 | // would refer to the member itself. |
| 5501 | if (ParamType->isMemberPointerType()) { |
| 5502 | QualType ClassType |
| 5503 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5504 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5505 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5506 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5507 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5508 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5509 | |
| 5510 | // The actual value-ness of this is unimportant, but for |
| 5511 | // internal consistency's sake, references to instance methods |
| 5512 | // are r-values. |
| 5513 | ExprValueKind VK = VK_LValue; |
| 5514 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5515 | VK = VK_RValue; |
| 5516 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5517 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5518 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5519 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5520 | Loc, |
| 5521 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5522 | if (RefExpr.isInvalid()) |
| 5523 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5524 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5525 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5526 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5527 | // We might need to perform a trailing qualification conversion, since |
| 5528 | // the element type on the parameter could be more qualified than the |
| 5529 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5530 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5531 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5532 | ParamType.getUnqualifiedType(), false, |
| 5533 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5534 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5535 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5536 | assert(!RefExpr.isInvalid() && |
| 5537 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5538 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5539 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5540 | } |
| 5541 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5542 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5543 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5544 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5545 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5546 | // When the non-type template parameter is a pointer, take the |
| 5547 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5548 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5549 | if (RefExpr.isInvalid()) |
| 5550 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5551 | |
| 5552 | if (T->isFunctionType() || T->isArrayType()) { |
| 5553 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5554 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5555 | if (RefExpr.isInvalid()) |
| 5556 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5557 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5558 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5559 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5560 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5561 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5562 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5565 | ExprValueKind VK = VK_RValue; |
| 5566 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5567 | // If the non-type template parameter has reference type, qualify the |
| 5568 | // resulting declaration reference with the extra qualifiers on the |
| 5569 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5570 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5571 | VK = VK_LValue; |
| 5572 | T = Context.getQualifiedType(T, |
| 5573 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5574 | } else if (isa<FunctionDecl>(VD)) { |
| 5575 | // References to functions are always lvalues. |
| 5576 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5577 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5578 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5579 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | /// \brief Construct a new expression that refers to the given |
| 5583 | /// integral template argument with the given source-location |
| 5584 | /// information. |
| 5585 | /// |
| 5586 | /// This routine takes care of the mapping from an integral template |
| 5587 | /// argument (which may have any integral type) to the appropriate |
| 5588 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5589 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5590 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5591 | SourceLocation Loc) { |
| 5592 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5593 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5594 | QualType OrigT = Arg.getIntegralType(); |
| 5595 | |
| 5596 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5597 | // type the same size as the enumerator. We don't want to build an |
| 5598 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5599 | // any integral type with C++11 enum classes, make sure we create the right |
| 5600 | // type of literal for it. |
| 5601 | QualType T = OrigT; |
| 5602 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5603 | T = ET->getDecl()->getIntegerType(); |
| 5604 | |
| 5605 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5606 | if (T->isAnyCharacterType()) { |
Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 5607 | // This does not need to handle u8 character literals because those are |
| 5608 | // 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] | 5609 | CharacterLiteral::CharacterKind Kind; |
| 5610 | if (T->isWideCharType()) |
| 5611 | Kind = CharacterLiteral::Wide; |
| 5612 | else if (T->isChar16Type()) |
| 5613 | Kind = CharacterLiteral::UTF16; |
| 5614 | else if (T->isChar32Type()) |
| 5615 | Kind = CharacterLiteral::UTF32; |
| 5616 | else |
| 5617 | Kind = CharacterLiteral::Ascii; |
| 5618 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5619 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5620 | Kind, T, Loc); |
| 5621 | } else if (T->isBooleanType()) { |
| 5622 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5623 | T, Loc); |
| 5624 | } else if (T->isNullPtrType()) { |
| 5625 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5626 | } else { |
| 5627 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5630 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5631 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5632 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5633 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5634 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5635 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5636 | Loc, Loc); |
| 5637 | } |
| 5638 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5639 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5642 | /// \brief Match two template parameters within template parameter lists. |
| 5643 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5644 | bool Complain, |
| 5645 | Sema::TemplateParameterListEqualKind Kind, |
| 5646 | SourceLocation TemplateArgLoc) { |
| 5647 | // Check the actual kind (type, non-type, template). |
| 5648 | if (Old->getKind() != New->getKind()) { |
| 5649 | if (Complain) { |
| 5650 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5651 | if (TemplateArgLoc.isValid()) { |
| 5652 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5653 | NextDiag = diag::note_template_param_different_kind; |
| 5654 | } |
| 5655 | S.Diag(New->getLocation(), NextDiag) |
| 5656 | << (Kind != Sema::TPL_TemplateMatch); |
| 5657 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5658 | << (Kind != Sema::TPL_TemplateMatch); |
| 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 | } |
| 5663 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5664 | // Check that both are parameter packs are neither are parameter packs. |
| 5665 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5666 | // template template parameter, the template template parameter can have |
| 5667 | // a parameter pack where the template template argument does not. |
| 5668 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5669 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5670 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5671 | if (Complain) { |
| 5672 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5673 | if (TemplateArgLoc.isValid()) { |
| 5674 | S.Diag(TemplateArgLoc, |
| 5675 | diag::err_template_arg_template_params_mismatch); |
| 5676 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5677 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5678 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5679 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5680 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5681 | : 2; |
| 5682 | S.Diag(New->getLocation(), NextDiag) |
| 5683 | << ParamKind << New->isParameterPack(); |
| 5684 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5685 | << ParamKind << Old->isParameterPack(); |
| 5686 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5688 | return false; |
| 5689 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5690 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5691 | // For non-type template parameters, check the type of the parameter. |
| 5692 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5693 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5694 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5695 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5696 | // If we are matching a template template argument to a template |
| 5697 | // template parameter and one of the non-type template parameter types |
| 5698 | // is dependent, then we must wait until template instantiation time |
| 5699 | // to actually compare the arguments. |
| 5700 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5701 | (OldNTTP->getType()->isDependentType() || |
| 5702 | NewNTTP->getType()->isDependentType())) |
| 5703 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5704 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5705 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5706 | if (Complain) { |
| 5707 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5708 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5709 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5710 | diag::err_template_arg_template_params_mismatch); |
| 5711 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5712 | } |
| 5713 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5714 | << NewNTTP->getType() |
| 5715 | << (Kind != Sema::TPL_TemplateMatch); |
| 5716 | S.Diag(OldNTTP->getLocation(), |
| 5717 | diag::note_template_nontype_parm_prev_declaration) |
| 5718 | << OldNTTP->getType(); |
| 5719 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5720 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5721 | return false; |
| 5722 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5723 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5724 | return true; |
| 5725 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5726 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5727 | // For template template parameters, check the template parameter types. |
| 5728 | // The template parameter lists of template template |
| 5729 | // parameters must agree. |
| 5730 | if (TemplateTemplateParmDecl *OldTTP |
| 5731 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5732 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5733 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5734 | OldTTP->getTemplateParameters(), |
| 5735 | Complain, |
| 5736 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5737 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5738 | : Kind), |
| 5739 | TemplateArgLoc); |
| 5740 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5741 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5742 | return true; |
| 5743 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5744 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5745 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5746 | /// lists. |
| 5747 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5748 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5749 | TemplateParameterList *New, |
| 5750 | TemplateParameterList *Old, |
| 5751 | Sema::TemplateParameterListEqualKind Kind, |
| 5752 | SourceLocation TemplateArgLoc) { |
| 5753 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5754 | if (TemplateArgLoc.isValid()) { |
| 5755 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5756 | NextDiag = diag::note_template_param_list_different_arity; |
| 5757 | } |
| 5758 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5759 | << (New->size() > Old->size()) |
| 5760 | << (Kind != Sema::TPL_TemplateMatch) |
| 5761 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5762 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5763 | << (Kind != Sema::TPL_TemplateMatch) |
| 5764 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5765 | } |
| 5766 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5767 | /// \brief Determine whether the given template parameter lists are |
| 5768 | /// equivalent. |
| 5769 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5770 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5771 | /// source code as part of a new template declaration. |
| 5772 | /// |
| 5773 | /// \param Old The old template parameter list, typically found via |
| 5774 | /// name lookup of the template declared with this template parameter |
| 5775 | /// list. |
| 5776 | /// |
| 5777 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5778 | /// the template parameter lists are not equivalent. |
| 5779 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5780 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5781 | /// |
| 5782 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5783 | /// are actually checking the template parameter list of a template |
| 5784 | /// argument (New) against the template parameter list of its |
| 5785 | /// corresponding template template parameter (Old). We produce |
| 5786 | /// slightly different diagnostics in this scenario. |
| 5787 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5788 | /// \returns True if the template parameter lists are equal, false |
| 5789 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5790 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5791 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5792 | TemplateParameterList *Old, |
| 5793 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5794 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5795 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5796 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5797 | if (Complain) |
| 5798 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5799 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5800 | |
| 5801 | return false; |
| 5802 | } |
| 5803 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5804 | // C++0x [temp.arg.template]p3: |
| 5805 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5806 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5807 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5808 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5809 | // template-parameter-list of P. [...] |
| 5810 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5811 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5812 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5813 | OldParmEnd = Old->end(); |
| 5814 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5815 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5816 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5817 | if (NewParm == NewParmEnd) { |
| 5818 | if (Complain) |
| 5819 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5820 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5821 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5822 | return false; |
| 5823 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5824 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5825 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5826 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5827 | return false; |
| 5828 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5829 | ++NewParm; |
| 5830 | continue; |
| 5831 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5832 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5833 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5834 | // [...] When P's template- parameter-list contains a template parameter |
| 5835 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5836 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5837 | // template-parameter-list of A with the same type and form as the |
| 5838 | // template parameter pack in P (ignoring whether those template |
| 5839 | // parameters are template parameter packs). |
| 5840 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5841 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5842 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5843 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5844 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5845 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5846 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5847 | // Make sure we exhausted all of the arguments. |
| 5848 | if (NewParm != NewParmEnd) { |
| 5849 | if (Complain) |
| 5850 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5851 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5852 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5853 | return false; |
| 5854 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5855 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5856 | return true; |
| 5857 | } |
| 5858 | |
| 5859 | /// \brief Check whether a template can be declared within this scope. |
| 5860 | /// |
| 5861 | /// If the template declaration is valid in this scope, returns |
| 5862 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5863 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5864 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5865 | if (!S) |
| 5866 | return false; |
| 5867 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5868 | // Find the nearest enclosing declaration scope. |
| 5869 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5870 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5871 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5872 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5873 | // C++ [temp]p4: |
| 5874 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5875 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5876 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5877 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5878 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5879 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5880 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5881 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5882 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5883 | // C++ [temp]p2: |
| 5884 | // A template-declaration can appear only as a namespace scope or |
| 5885 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5886 | if (Ctx) { |
| 5887 | if (Ctx->isFileContext()) |
| 5888 | return false; |
| 5889 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5890 | // C++ [temp.mem]p2: |
| 5891 | // A local class shall not have member templates. |
| 5892 | if (RD->isLocalClass()) |
| 5893 | return Diag(TemplateParams->getTemplateLoc(), |
| 5894 | diag::err_template_inside_local_class) |
| 5895 | << TemplateParams->getSourceRange(); |
| 5896 | else |
| 5897 | return false; |
| 5898 | } |
| 5899 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5900 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5901 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5902 | diag::err_template_outside_namespace_or_class_scope) |
| 5903 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5904 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5905 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5906 | /// \brief Determine what kind of template specialization the given declaration |
| 5907 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5908 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5909 | if (!D) |
| 5910 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5911 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5912 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5913 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5914 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5915 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5916 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5917 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5918 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5919 | return TSK_Undeclared; |
| 5920 | } |
| 5921 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5922 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5923 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5924 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5925 | /// This routine determines whether a template specialization can be declared |
| 5926 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5927 | /// |
| 5928 | /// \param S the semantic analysis object for which this check is being |
| 5929 | /// performed. |
| 5930 | /// |
| 5931 | /// \param Specialized the entity being specialized or instantiated, which |
| 5932 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5933 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5934 | /// member class). |
| 5935 | /// |
| 5936 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5937 | /// |
| 5938 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5939 | /// this entity. |
| 5940 | /// |
| 5941 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5942 | /// a class template. |
| 5943 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5944 | /// \returns true if there was an error that we cannot recover from, false |
| 5945 | /// otherwise. |
| 5946 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5947 | NamedDecl *Specialized, |
| 5948 | NamedDecl *PrevDecl, |
| 5949 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5950 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5951 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5952 | // various diagnostics emitted by this routine. |
| 5953 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5954 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5955 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5956 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5957 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5958 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5959 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5960 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5961 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5962 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5963 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5964 | else if (isa<RecordDecl>(Specialized)) |
| 5965 | EntityKind = 7; |
| 5966 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5967 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5968 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5969 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5970 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5971 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5972 | return true; |
| 5973 | } |
| 5974 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5975 | // C++ [temp.expl.spec]p2: |
| 5976 | // An explicit specialization shall be declared in the namespace |
| 5977 | // of which the template is a member, or, for member templates, in |
| 5978 | // the namespace of which the enclosing class or enclosing class |
| 5979 | // template is a member. An explicit specialization of a member |
| 5980 | // function, member class or static data member of a class |
| 5981 | // template shall be declared in the namespace of which the class |
| 5982 | // template is a member. Such a declaration may also be a |
| 5983 | // definition. If the declaration is not a definition, the |
| 5984 | // specialization may be defined later in the name- space in which |
| 5985 | // the explicit specialization was declared, or in a namespace |
| 5986 | // that encloses the one in which the explicit specialization was |
| 5987 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5988 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5989 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5990 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5991 | return true; |
| 5992 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5993 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5994 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5995 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5996 | // Do not warn for class scope explicit specialization during |
| 5997 | // instantiation, warning was already emitted during pattern |
| 5998 | // semantic analysis. |
| 5999 | if (!S.ActiveTemplateInstantiations.size()) |
| 6000 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 6001 | << Specialized; |
| 6002 | } else { |
| 6003 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6004 | << Specialized; |
| 6005 | return true; |
| 6006 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 6007 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6008 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 6009 | if (S.CurContext->isRecord() && |
| 6010 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 6011 | // Make sure that we're specializing in the right record context. |
| 6012 | // Otherwise, things can go horribly wrong. |
| 6013 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 6014 | << Specialized; |
| 6015 | return true; |
| 6016 | } |
| 6017 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6018 | // C++ [temp.class.spec]p6: |
| 6019 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6020 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 6021 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6022 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6023 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 6024 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6025 | |
| 6026 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 6027 | // namespace. |
| 6028 | // Note that HandleDeclarator() performs this check for explicit |
| 6029 | // specializations of function templates, static data members, and member |
| 6030 | // functions, so we skip the check here for those kinds of entities. |
| 6031 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 6032 | // Should we refactor that check, so that it occurs later? |
| 6033 | if (!DC->Encloses(SpecializedContext) && |
| 6034 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 6035 | isa<FunctionDecl>(Specialized) || |
| 6036 | isa<VarTemplateDecl>(Specialized) || |
| 6037 | isa<VarDecl>(Specialized))) { |
| 6038 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 6039 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 6040 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 6041 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6042 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 6043 | if (S.getLangOpts().MicrosoftExt) |
| 6044 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 6045 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 6046 | << cast<NamedDecl>(SpecializedContext); |
| 6047 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6048 | llvm_unreachable("unexpected namespace context for specialization"); |
| 6049 | |
| 6050 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 6051 | } else if ((!PrevDecl || |
| 6052 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 6053 | getTemplateSpecializationKind(PrevDecl) == |
| 6054 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6055 | // C++ [temp.exp.spec]p2: |
| 6056 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6057 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6058 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6059 | // An explicit specialization of a member function, member class or |
| 6060 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6061 | // namespace of which the class template is a member. |
| 6062 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6063 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6064 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 6065 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 6066 | // C++11 [temp.explicit]p3: |
| 6067 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 6068 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6069 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6070 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6071 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6072 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6073 | "DC encloses TU but isn't in enclosing namespace set"); |
| 6074 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 6075 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6076 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 6077 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6078 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6079 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6080 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6081 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 6082 | else |
| 6083 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 6084 | S.Diag(Loc, Diag) |
| 6085 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 6086 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6087 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6088 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6089 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6090 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6091 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6092 | return false; |
| 6093 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6094 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6095 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 6096 | if (!E->isInstantiationDependent()) |
| 6097 | return SourceLocation(); |
| 6098 | DependencyChecker Checker(Depth); |
| 6099 | Checker.TraverseStmt(E); |
| 6100 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6101 | return E->getSourceRange(); |
| 6102 | return Checker.MatchLoc; |
| 6103 | } |
| 6104 | |
| 6105 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6106 | if (!TL.getType()->isDependentType()) |
| 6107 | return SourceLocation(); |
| 6108 | DependencyChecker Checker(Depth); |
| 6109 | Checker.TraverseTypeLoc(TL); |
| 6110 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6111 | return TL.getSourceRange(); |
| 6112 | return Checker.MatchLoc; |
| 6113 | } |
| 6114 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6115 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6116 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6117 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6118 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6119 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6120 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6121 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6122 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6123 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6124 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6125 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6126 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6127 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6128 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6129 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6130 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6131 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6132 | |
| 6133 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6134 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6135 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6136 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6137 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6138 | |
| 6139 | // Strip off any implicit casts we added as part of type checking. |
| 6140 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6141 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6142 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6143 | // C++ [temp.class.spec]p8: |
| 6144 | // A non-type argument is non-specialized if it is the name of a |
| 6145 | // non-type parameter. All other non-type arguments are |
| 6146 | // specialized. |
| 6147 | // |
| 6148 | // Below, we check the two conditions that only apply to |
| 6149 | // specialized non-type arguments, so skip any non-specialized |
| 6150 | // arguments. |
| 6151 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6152 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6153 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6154 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6155 | // C++ [temp.class.spec]p9: |
| 6156 | // Within the argument list of a class template partial |
| 6157 | // specialization, the following restrictions apply: |
| 6158 | // -- A partially specialized non-type argument expression |
| 6159 | // shall not involve a template parameter of the partial |
| 6160 | // specialization except when the argument expression is a |
| 6161 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6162 | SourceRange ParamUseRange = |
| 6163 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 6164 | if (ParamUseRange.isValid()) { |
| 6165 | if (IsDefaultArgument) { |
| 6166 | S.Diag(TemplateNameLoc, |
| 6167 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6168 | S.Diag(ParamUseRange.getBegin(), |
| 6169 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6170 | << ParamUseRange; |
| 6171 | } else { |
| 6172 | S.Diag(ParamUseRange.getBegin(), |
| 6173 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6174 | << ParamUseRange; |
| 6175 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6176 | return true; |
| 6177 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6178 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6179 | // -- The type of a template parameter corresponding to a |
| 6180 | // specialized non-type argument shall not be dependent on a |
| 6181 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6182 | // |
| 6183 | // FIXME: We need to delay this check until instantiation in some cases: |
| 6184 | // |
| 6185 | // template<template<typename> class X> struct A { |
| 6186 | // template<typename T, X<T> N> struct B; |
| 6187 | // template<typename T> struct B<T, 0>; |
| 6188 | // }; |
| 6189 | // template<typename> using X = int; |
| 6190 | // A<X>::B<int, 0> b; |
| 6191 | ParamUseRange = findTemplateParameter( |
| 6192 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 6193 | if (ParamUseRange.isValid()) { |
| 6194 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6195 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 6196 | << Param->getType() << ParamUseRange; |
| 6197 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 6198 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6199 | return true; |
| 6200 | } |
| 6201 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6202 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6203 | return false; |
| 6204 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6205 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6206 | /// \brief Check the non-type template arguments of a class template |
| 6207 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6208 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6209 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6210 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6211 | /// template. |
| 6212 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6213 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6214 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6215 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6216 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6217 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6218 | Sema &S, SourceLocation TemplateNameLoc, |
| 6219 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6220 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6221 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 6222 | |
| 6223 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6224 | NonTypeTemplateParmDecl *Param |
| 6225 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6226 | if (!Param) |
| 6227 | continue; |
| 6228 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6229 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 6230 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6231 | return true; |
| 6232 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6233 | |
| 6234 | return false; |
| 6235 | } |
| 6236 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6237 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6238 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6239 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6240 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6241 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6242 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6243 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6244 | MultiTemplateParamsArg |
| 6245 | TemplateParameterLists, |
| 6246 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6247 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6248 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6249 | CXXScopeSpec &SS = TemplateId.SS; |
| 6250 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6251 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6252 | // store the location of the outermost template keyword in the declaration. |
| 6253 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6254 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6255 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6256 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6257 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6258 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6259 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6260 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6261 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6262 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6263 | |
| 6264 | if (!ClassTemplate) { |
| 6265 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6266 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6267 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6268 | return true; |
| 6269 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6270 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6271 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6272 | bool isPartialSpecialization = false; |
| 6273 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6274 | // Check the validity of the template headers that introduce this |
| 6275 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6276 | // FIXME: We probably shouldn't complain about these headers for |
| 6277 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6278 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6279 | TemplateParameterList *TemplateParams = |
| 6280 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6281 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6282 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6283 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6284 | if (Invalid) |
| 6285 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6286 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6287 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6288 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6289 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6290 | if (TUK == TUK_Friend) { |
| 6291 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6292 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6293 | return true; |
| 6294 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6295 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6296 | // C++ [temp.class.spec]p10: |
| 6297 | // The template parameter list of a specialization shall not |
| 6298 | // contain default template argument values. |
| 6299 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6300 | Decl *Param = TemplateParams->getParam(I); |
| 6301 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6302 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6303 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6304 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6305 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6306 | } |
| 6307 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6308 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6309 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6310 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6311 | diag::err_default_arg_in_partial_spec) |
| 6312 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6313 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6314 | } |
| 6315 | } else { |
| 6316 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6317 | if (TTP->hasDefaultArgument()) { |
| 6318 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6319 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6320 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6321 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6322 | } |
| 6323 | } |
| 6324 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6325 | } else if (TemplateParams) { |
| 6326 | if (TUK == TUK_Friend) |
| 6327 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6328 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6329 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6330 | TemplateParams->getRAngleLoc())) |
| 6331 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6332 | else |
| 6333 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6334 | } else { |
| 6335 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6336 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6337 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6338 | // Check that the specialization uses the same tag kind as the |
| 6339 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6340 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6341 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6342 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6343 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 6344 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6345 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6346 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6347 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6348 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6349 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6350 | diag::note_previous_use); |
| 6351 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6352 | } |
| 6353 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6354 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6355 | TemplateArgumentListInfo TemplateArgs = |
| 6356 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6357 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6358 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6359 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6360 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6361 | UPPC_PartialSpecialization)) |
| 6362 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6363 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6364 | // Check that the template argument list is well-formed for this |
| 6365 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6366 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6367 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6368 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6369 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6370 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6371 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6372 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6373 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6374 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6375 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6376 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6377 | return true; |
| 6378 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6379 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6380 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6381 | !TemplateSpecializationType::anyDependentTemplateArguments( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6382 | TemplateArgs.arguments(), InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6383 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6384 | << ClassTemplate->getDeclName(); |
| 6385 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6386 | } |
| 6387 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6388 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6389 | void *InsertPos = nullptr; |
| 6390 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6391 | |
| 6392 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6393 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6394 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6395 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6396 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6397 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6398 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6399 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6400 | // Check whether we can declare a class template specialization in |
| 6401 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6402 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6403 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6404 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6405 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6406 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6407 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6408 | // The canonical type |
| 6409 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6410 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6411 | // Build the canonical type that describes the converted template |
| 6412 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6413 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6414 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6415 | Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6416 | |
| 6417 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6418 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6419 | // C++ [temp.class.spec]p9b3: |
| 6420 | // |
| 6421 | // -- The argument list of the specialization shall not be identical |
| 6422 | // to the implicit argument list of the primary template. |
| 6423 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6424 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6425 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6426 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6427 | ClassTemplate->getIdentifier(), |
| 6428 | TemplateNameLoc, |
| 6429 | Attr, |
| 6430 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6431 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6432 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6433 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6434 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6435 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6436 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6437 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6438 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6439 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6440 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6441 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6442 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6443 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6444 | TemplateParams, |
| 6445 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6446 | Converted, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6447 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6448 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6449 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6450 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6451 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6452 | Partial->setTemplateParameterListsInfo( |
| 6453 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6454 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6455 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6456 | if (!PrevPartial) |
| 6457 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6458 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6459 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6460 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6461 | // template specialization, make a note of that. |
| 6462 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6463 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6464 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6465 | // Check that all of the template parameters of the class template |
| 6466 | // partial specialization are deducible from the template |
| 6467 | // arguments. If not, this class template partial specialization |
| 6468 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6469 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6470 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6471 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6472 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6473 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6474 | if (!DeducibleParams.all()) { |
| 6475 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6476 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6477 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6478 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6479 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6480 | if (!DeducibleParams[I]) { |
| 6481 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6482 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6483 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6484 | diag::note_partial_spec_unused_parameter) |
| 6485 | << Param->getDeclName(); |
| 6486 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6487 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6488 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6489 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6490 | } |
| 6491 | } |
| 6492 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6493 | } else { |
| 6494 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6495 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6496 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6497 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6498 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6499 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6500 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 6501 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6502 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6503 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6504 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6505 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6506 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6507 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6508 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6509 | if (!PrevDecl) |
| 6510 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6511 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6512 | if (CurContext->isDependentContext()) { |
| 6513 | // -fms-extensions permits specialization of nested classes without |
| 6514 | // fully specializing the outer class(es). |
| 6515 | assert(getLangOpts().MicrosoftExt && |
| 6516 | "Only possible with -fms-extensions!"); |
| 6517 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6518 | CanonType = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6519 | CanonTemplate, Converted); |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6520 | } else { |
| 6521 | CanonType = Context.getTypeDeclType(Specialization); |
| 6522 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6523 | } |
| 6524 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6525 | // C++ [temp.expl.spec]p6: |
| 6526 | // 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] | 6527 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6528 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6529 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6530 | // use occurs; no diagnostic is required. |
| 6531 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6532 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6533 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6534 | // Is there any previous explicit specialization declaration? |
| 6535 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6536 | Okay = true; |
| 6537 | break; |
| 6538 | } |
| 6539 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6540 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6541 | if (!Okay) { |
| 6542 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6543 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6544 | << Context.getTypeDeclType(Specialization) << Range; |
| 6545 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6546 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6547 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6548 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6549 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6550 | return true; |
| 6551 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6552 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6553 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6554 | // If this is not a friend, note that this is an explicit specialization. |
| 6555 | if (TUK != TUK_Friend) |
| 6556 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6557 | |
| 6558 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6559 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6560 | RecordDecl *Def = Specialization->getDefinition(); |
| 6561 | NamedDecl *Hidden = nullptr; |
| 6562 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6563 | SkipBody->ShouldSkip = true; |
| 6564 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6565 | // From here on out, treat this as just a redeclaration. |
| 6566 | TUK = TUK_Declaration; |
| 6567 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6568 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6569 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6570 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6571 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6572 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6573 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6574 | } |
| 6575 | } |
| 6576 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6577 | if (Attr) |
| 6578 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6579 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6580 | // Add alignment attributes if necessary; these attributes are checked when |
| 6581 | // the ASTContext lays out the structure. |
| 6582 | if (TUK == TUK_Definition) { |
| 6583 | AddAlignmentAttributesForRecord(Specialization); |
| 6584 | AddMsStructLayoutForRecord(Specialization); |
| 6585 | } |
| 6586 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6587 | if (ModulePrivateLoc.isValid()) |
| 6588 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6589 | << (isPartialSpecialization? 1 : 0) |
| 6590 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6591 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6592 | // Build the fully-sugared type for this class template |
| 6593 | // specialization as the user wrote in the specialization |
| 6594 | // itself. This means that we'll pretty-print the type retrieved |
| 6595 | // from the specialization's declaration the way that the user |
| 6596 | // actually wrote the specialization, rather than formatting the |
| 6597 | // name based on the "canonical" representation used to store the |
| 6598 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6599 | TypeSourceInfo *WrittenTy |
| 6600 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6601 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6602 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6603 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6604 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6605 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6606 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6607 | // C++ [temp.expl.spec]p9: |
| 6608 | // A template explicit specialization is in the scope of the |
| 6609 | // namespace in which the template was defined. |
| 6610 | // |
| 6611 | // We actually implement this paragraph where we set the semantic |
| 6612 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6613 | // but we also maintain the lexical context where the actual |
| 6614 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6615 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6616 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6617 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6618 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6619 | Specialization->startDefinition(); |
| 6620 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6621 | if (TUK == TUK_Friend) { |
| 6622 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6623 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6624 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6625 | /*FIXME:*/KWLoc); |
| 6626 | Friend->setAccess(AS_public); |
| 6627 | CurContext->addDecl(Friend); |
| 6628 | } else { |
| 6629 | // Add the specialization into its lexical context, so that it can |
| 6630 | // be seen when iterating through the list of declarations in that |
| 6631 | // context. However, specializations are not found by name lookup. |
| 6632 | CurContext->addDecl(Specialization); |
| 6633 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6634 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6635 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6636 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6637 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6638 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6639 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6640 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6641 | ActOnDocumentableDecl(NewDecl); |
| 6642 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6643 | } |
| 6644 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6645 | /// \brief Strips various properties off an implicit instantiation |
| 6646 | /// that has just been explicitly specialized. |
| 6647 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6648 | D->dropAttr<DLLImportAttr>(); |
| 6649 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6650 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6651 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6652 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6653 | } |
| 6654 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6655 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6656 | // declaration or definition. |
| 6657 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6658 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6659 | // Explicit instantiations following a specialization have no effect and |
| 6660 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6661 | // until a valid name loc is found. |
| 6662 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6663 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6664 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6665 | PrevDiagLoc = Prev->getLocation(); |
| 6666 | } |
| 6667 | assert(PrevDiagLoc.isValid() && |
| 6668 | "Explicit instantiation without point of instantiation?"); |
| 6669 | return PrevDiagLoc; |
| 6670 | } |
| 6671 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6672 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6673 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6674 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6675 | /// new specialization/instantiation will have any effect. |
| 6676 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6677 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6678 | /// instantiation. |
| 6679 | /// |
| 6680 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6681 | /// |
| 6682 | /// \param PrevDecl the previous declaration of the entity. |
| 6683 | /// |
| 6684 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6685 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6686 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6687 | /// declaration was instantiated (either implicitly or explicitly). |
| 6688 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6689 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6690 | /// specialization or instantiation has no effect and should be ignored. |
| 6691 | /// |
| 6692 | /// \returns true if there was an error that should prevent the introduction of |
| 6693 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6694 | bool |
| 6695 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6696 | TemplateSpecializationKind NewTSK, |
| 6697 | NamedDecl *PrevDecl, |
| 6698 | TemplateSpecializationKind PrevTSK, |
| 6699 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6700 | bool &HasNoEffect) { |
| 6701 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6702 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6703 | switch (NewTSK) { |
| 6704 | case TSK_Undeclared: |
| 6705 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6706 | assert( |
| 6707 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6708 | "previous declaration must be implicit!"); |
| 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 | switch (PrevTSK) { |
| 6713 | case TSK_Undeclared: |
| 6714 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6715 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6716 | // explicitly specialized or has merely been mentioned without any |
| 6717 | // instantiation. |
| 6718 | return false; |
| 6719 | |
| 6720 | case TSK_ImplicitInstantiation: |
| 6721 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6722 | // The declaration itself has not actually been instantiated, so it is |
| 6723 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6724 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6725 | return false; |
| 6726 | } |
| 6727 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6728 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6729 | case TSK_ExplicitInstantiationDeclaration: |
| 6730 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6731 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6732 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6733 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6734 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6735 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6736 | // 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] | 6737 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6738 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6739 | // implicit instantiation to take place, in every translation unit in |
| 6740 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6741 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6742 | // Is there any previous explicit specialization declaration? |
| 6743 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6744 | return false; |
| 6745 | } |
| 6746 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6747 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6748 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6749 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6750 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6751 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6752 | return true; |
| 6753 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6754 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6755 | case TSK_ExplicitInstantiationDeclaration: |
| 6756 | switch (PrevTSK) { |
| 6757 | case TSK_ExplicitInstantiationDeclaration: |
| 6758 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6759 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6760 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6761 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6762 | case TSK_Undeclared: |
| 6763 | case TSK_ImplicitInstantiation: |
| 6764 | // We're explicitly instantiating something that may have already been |
| 6765 | // implicitly instantiated; that's fine. |
| 6766 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6767 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6768 | case TSK_ExplicitSpecialization: |
| 6769 | // C++0x [temp.explicit]p4: |
| 6770 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6771 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6772 | // specialization for that template, the explicit instantiation has no |
| 6773 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6774 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6775 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6776 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6777 | case TSK_ExplicitInstantiationDefinition: |
| 6778 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6779 | // If an entity is the subject of both an explicit instantiation |
| 6780 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6781 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6782 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6783 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6784 | |
| 6785 | // Explicit instantiations following a specialization have no effect and |
| 6786 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6787 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6788 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6789 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6790 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6791 | return false; |
| 6792 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6793 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6794 | case TSK_ExplicitInstantiationDefinition: |
| 6795 | switch (PrevTSK) { |
| 6796 | case TSK_Undeclared: |
| 6797 | case TSK_ImplicitInstantiation: |
| 6798 | // We're explicitly instantiating something that may have already been |
| 6799 | // implicitly instantiated; that's fine. |
| 6800 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6801 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6802 | case TSK_ExplicitSpecialization: |
| 6803 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6804 | // For a given set of template parameters, if an explicit |
| 6805 | // instantiation of a template appears after a declaration of |
| 6806 | // an explicit specialization for that template, the explicit |
| 6807 | // instantiation has no effect. |
| 6808 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6809 | // 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] | 6810 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6811 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6812 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6813 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6814 | diag::ext_explicit_instantiation_after_specialization) |
| 6815 | << PrevDecl; |
| 6816 | Diag(PrevDecl->getLocation(), |
| 6817 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6818 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6819 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6820 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6821 | case TSK_ExplicitInstantiationDeclaration: |
| 6822 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6823 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6824 | |
| 6825 | // C++0x [temp.explicit]p4: |
| 6826 | // For a given set of template parameters, if an explicit instantiation |
| 6827 | // of a template appears after a declaration of an explicit |
| 6828 | // specialization for that template, the explicit instantiation has no |
| 6829 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6830 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6831 | // Is there any previous explicit specialization declaration? |
| 6832 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6833 | HasNoEffect = true; |
| 6834 | break; |
| 6835 | } |
| 6836 | } |
| 6837 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6838 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6839 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6840 | case TSK_ExplicitInstantiationDefinition: |
| 6841 | // C++0x [temp.spec]p5: |
| 6842 | // For a given template and a given set of template-arguments, |
| 6843 | // - an explicit instantiation definition shall appear at most once |
| 6844 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6845 | |
| 6846 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6847 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6848 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6849 | : diag::err_explicit_instantiation_duplicate) |
| 6850 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6851 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6852 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6853 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6854 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6855 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6856 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6857 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6858 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6859 | } |
| 6860 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6861 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6862 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6863 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6864 | /// The only possible way to get a dependent function template specialization |
| 6865 | /// is with a friend declaration, like so: |
| 6866 | /// |
| 6867 | /// \code |
| 6868 | /// template \<class T> void foo(T); |
| 6869 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6870 | /// friend void foo<>(T); |
| 6871 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6872 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6873 | /// |
| 6874 | /// There really isn't any useful analysis we can do here, so we |
| 6875 | /// just store the information. |
| 6876 | bool |
| 6877 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6878 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6879 | LookupResult &Previous) { |
| 6880 | // Remove anything from Previous that isn't a function template in |
| 6881 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6882 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6883 | LookupResult::Filter F = Previous.makeFilter(); |
| 6884 | while (F.hasNext()) { |
| 6885 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6886 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6887 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6888 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6889 | F.erase(); |
| 6890 | } |
| 6891 | F.done(); |
| 6892 | |
| 6893 | // Should this be diagnosed here? |
| 6894 | if (Previous.empty()) return true; |
| 6895 | |
| 6896 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6897 | ExplicitTemplateArgs); |
| 6898 | return false; |
| 6899 | } |
| 6900 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6901 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6902 | /// specialization. |
| 6903 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6904 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6905 | /// explicit function template specialization. On successful completion, |
| 6906 | /// the function declaration \p FD will become a function template |
| 6907 | /// specialization. |
| 6908 | /// |
| 6909 | /// \param FD the function declaration, which will be updated to become a |
| 6910 | /// function template specialization. |
| 6911 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6912 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6913 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6914 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6915 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6916 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6917 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6918 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6919 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6920 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6921 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6922 | // The set of function template specializations that could match this |
| 6923 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6924 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6925 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 6926 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6927 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6928 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 6929 | ConvertedTemplateArgs; |
| 6930 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6931 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6932 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6933 | I != E; ++I) { |
| 6934 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6935 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6936 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6937 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6938 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6939 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6940 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6941 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6942 | // When matching a constexpr member function template specialization |
| 6943 | // against the primary template, we don't yet know whether the |
| 6944 | // specialization has an implicit 'const' (because we don't know whether |
| 6945 | // it will be a static member function until we know which template it |
| 6946 | // specializes), so adjust it now assuming it specializes this template. |
| 6947 | QualType FT = FD->getType(); |
| 6948 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6949 | CXXMethodDecl *OldMD = |
| 6950 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6951 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6952 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6953 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6954 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6955 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6956 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6957 | } |
| 6958 | } |
| 6959 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6960 | TemplateArgumentListInfo Args; |
| 6961 | if (ExplicitTemplateArgs) |
| 6962 | Args = *ExplicitTemplateArgs; |
| 6963 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6964 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6965 | // A trailing template-argument can be left unspecified in the |
| 6966 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6967 | // provided it can be deduced from the function argument type. |
| 6968 | // Perform template argument deduction to determine whether we may be |
| 6969 | // specializing this template. |
| 6970 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6971 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6972 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6973 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6974 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6975 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 6976 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6977 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6978 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6979 | FailedCandidates.addCandidate().set( |
| 6980 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 6981 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6982 | (void)TDK; |
| 6983 | continue; |
| 6984 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6985 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6986 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6987 | if (ExplicitTemplateArgs) |
| 6988 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6989 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6990 | } |
| 6991 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6992 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6993 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6994 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6995 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6996 | FD->getLocation(), |
| 6997 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6998 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6999 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7000 | PDiag(diag::note_function_template_spec_matched)); |
| 7001 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7002 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7003 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7004 | |
| 7005 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 7006 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7007 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7008 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare [...] |
| 7009 | // an explicit specialization (14.8.3) [...] of a concept definition. |
| 7010 | if (Specialization->getPrimaryTemplate()->isConcept()) { |
| 7011 | Diag(FD->getLocation(), diag::err_concept_specialized) |
| 7012 | << 0 /*function*/ << 1 /*explicitly specialized*/; |
| 7013 | Diag(Specialization->getLocation(), diag::note_previous_declaration); |
| 7014 | return true; |
| 7015 | } |
| 7016 | |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 7017 | FunctionTemplateSpecializationInfo *SpecInfo |
| 7018 | = Specialization->getTemplateSpecializationInfo(); |
| 7019 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7020 | |
| 7021 | // Note: do not overwrite location info if previous template |
| 7022 | // specialization kind was explicit. |
| 7023 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7024 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 7025 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 7026 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 7027 | // function can differ from the template declaration with respect to |
| 7028 | // the constexpr specifier. |
| 7029 | Specialization->setConstexpr(FD->isConstexpr()); |
| 7030 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7031 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7032 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7033 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7034 | |
| 7035 | // If this is a friend declaration, then we're not really declaring |
| 7036 | // an explicit specialization. |
| 7037 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7038 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7039 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7040 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7041 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7042 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7043 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7044 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7045 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7046 | |
| 7047 | // C++ [temp.expl.spec]p6: |
| 7048 | // 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] | 7049 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7050 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7051 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7052 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7053 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7054 | if (!isFriend && |
| 7055 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7056 | TSK_ExplicitSpecialization, |
| 7057 | Specialization, |
| 7058 | SpecInfo->getTemplateSpecializationKind(), |
| 7059 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7060 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7061 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7062 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7063 | // Mark the prior declaration as an explicit specialization, so that later |
| 7064 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7065 | if (!isFriend) { |
Faisal Vali | 81a88be | 2016-06-14 03:23:15 +0000 | [diff] [blame] | 7066 | // Since explicit specializations do not inherit '=delete' from their |
| 7067 | // primary function template - check if the 'specialization' that was |
| 7068 | // implicitly generated (during template argument deduction for partial |
| 7069 | // ordering) from the most specialized of all the function templates that |
| 7070 | // 'FD' could have been specializing, has a 'deleted' definition. If so, |
| 7071 | // first check that it was implicitly generated during template argument |
| 7072 | // deduction by making sure it wasn't referenced, and then reset the deleted |
| 7073 | // flag to not-deleted, so that we can inherit that information from 'FD'. |
| 7074 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 7075 | !Specialization->getCanonicalDecl()->isReferenced()) { |
| 7076 | assert( |
| 7077 | Specialization->getCanonicalDecl() == Specialization && |
| 7078 | "This must be the only existing declaration of this specialization"); |
| 7079 | Specialization->setDeletedAsWritten(false); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7080 | } |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 7081 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7082 | MarkUnusedFileScopedDecl(Specialization); |
| 7083 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7084 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7085 | // Turn the given function declaration into a function template |
| 7086 | // specialization, with the template arguments from the previous |
| 7087 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7088 | // Take copies of (semantic and syntactic) template argument lists. |
| 7089 | const TemplateArgumentList* TemplArgs = new (Context) |
| 7090 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7091 | FD->setFunctionTemplateSpecialization( |
| 7092 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 7093 | SpecInfo->getTemplateSpecializationKind(), |
| 7094 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7095 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7096 | // The "previous declaration" for this function template specialization is |
| 7097 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7098 | Previous.clear(); |
| 7099 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7100 | return false; |
| 7101 | } |
| 7102 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7103 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7104 | /// specialization. |
| 7105 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7106 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7107 | /// explicit member function specialization. On successful completion, |
| 7108 | /// the function declaration \p FD will become a member function |
| 7109 | /// specialization. |
| 7110 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7111 | /// \param Member the member declaration, which will be updated to become a |
| 7112 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7113 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7114 | /// \param Previous the set of declarations, one of which may be specialized |
| 7115 | /// by this function specialization; the set will be modified to contain the |
| 7116 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7117 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7118 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7119 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7120 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7121 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7122 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7123 | NamedDecl *Instantiation = nullptr; |
| 7124 | NamedDecl *InstantiatedFrom = nullptr; |
| 7125 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7126 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7127 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7128 | // Nowhere to look anyway. |
| 7129 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7130 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7131 | I != E; ++I) { |
| 7132 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7133 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7134 | QualType Adjusted = Function->getType(); |
| 7135 | if (!hasExplicitCallingConv(Adjusted)) |
| 7136 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7137 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7138 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7139 | Instantiation = Method; |
| 7140 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7141 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7142 | break; |
| 7143 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7144 | } |
| 7145 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7146 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7147 | VarDecl *PrevVar; |
| 7148 | if (Previous.isSingleResult() && |
| 7149 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7150 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7151 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7152 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7153 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7154 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7155 | } |
| 7156 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7157 | CXXRecordDecl *PrevRecord; |
| 7158 | if (Previous.isSingleResult() && |
| 7159 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7160 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7161 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7162 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7163 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7164 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7165 | } else if (isa<EnumDecl>(Member)) { |
| 7166 | EnumDecl *PrevEnum; |
| 7167 | if (Previous.isSingleResult() && |
| 7168 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7169 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7170 | Instantiation = PrevEnum; |
| 7171 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7172 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7173 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7174 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7175 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7176 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7177 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7178 | // specializations are always out-of-line, the caller will complain about |
| 7179 | // this mismatch later. |
| 7180 | return false; |
| 7181 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7182 | |
| 7183 | // If this is a friend, just bail out here before we start turning |
| 7184 | // things into explicit specializations. |
| 7185 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7186 | // Preserve instantiation information. |
| 7187 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7188 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7189 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7190 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7191 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7192 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7193 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7194 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7195 | } |
| 7196 | |
| 7197 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7198 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7199 | return false; |
| 7200 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7201 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7202 | // Make sure that this is a specialization of a member. |
| 7203 | if (!InstantiatedFrom) { |
| 7204 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 7205 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7206 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 7207 | return true; |
| 7208 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7209 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7210 | // C++ [temp.expl.spec]p6: |
| 7211 | // 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] | 7212 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7213 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7214 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7215 | // use occurs; no diagnostic is required. |
| 7216 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7217 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7218 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7219 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7220 | TSK_ExplicitSpecialization, |
| 7221 | Instantiation, |
| 7222 | MSInfo->getTemplateSpecializationKind(), |
| 7223 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7224 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7225 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7226 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7227 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7228 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7229 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7230 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7231 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7232 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7233 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7234 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7235 | // the original declaration to note that it is an explicit specialization |
| 7236 | // (if it was previously an implicit instantiation). This latter step |
| 7237 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7238 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7239 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7240 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7241 | TSK_ImplicitInstantiation) { |
| 7242 | InstantiationFunction->setTemplateSpecializationKind( |
| 7243 | TSK_ExplicitSpecialization); |
| 7244 | InstantiationFunction->setLocation(Member->getLocation()); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7245 | // Explicit specializations of member functions of class templates do not |
| 7246 | // inherit '=delete' from the member function they are specializing. |
| 7247 | if (InstantiationFunction->isDeleted()) { |
| 7248 | assert(InstantiationFunction->getCanonicalDecl() == |
| 7249 | InstantiationFunction); |
| 7250 | InstantiationFunction->setDeletedAsWritten(false);
|
| 7251 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7252 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7253 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7254 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7255 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7256 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7257 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7258 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7259 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7260 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7261 | TSK_ImplicitInstantiation) { |
| 7262 | InstantiationVar->setTemplateSpecializationKind( |
| 7263 | TSK_ExplicitSpecialization); |
| 7264 | InstantiationVar->setLocation(Member->getLocation()); |
| 7265 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7266 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7267 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7268 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7269 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7270 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7271 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7272 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7273 | TSK_ImplicitInstantiation) { |
| 7274 | InstantiationClass->setTemplateSpecializationKind( |
| 7275 | TSK_ExplicitSpecialization); |
| 7276 | InstantiationClass->setLocation(Member->getLocation()); |
| 7277 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7278 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7279 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7280 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7281 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7282 | } else { |
| 7283 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7284 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7285 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7286 | TSK_ImplicitInstantiation) { |
| 7287 | InstantiationEnum->setTemplateSpecializationKind( |
| 7288 | TSK_ExplicitSpecialization); |
| 7289 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7290 | } |
| 7291 | |
| 7292 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7293 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7294 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7295 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7296 | // Save the caller the trouble of having to figure out which declaration |
| 7297 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7298 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7299 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7300 | return false; |
| 7301 | } |
| 7302 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7303 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7304 | /// |
| 7305 | /// \returns true if a serious error occurs, false otherwise. |
| 7306 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7307 | SourceLocation InstLoc, |
| 7308 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7309 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7310 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7311 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7312 | if (CurContext->isRecord()) { |
| 7313 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7314 | << D; |
| 7315 | return true; |
| 7316 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7317 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7318 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7319 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7320 | // template. If the name declared in the explicit instantiation is an |
| 7321 | // unqualified name, the explicit instantiation shall appear in the |
| 7322 | // namespace where its template is declared or, if that namespace is inline |
| 7323 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7324 | // |
| 7325 | // 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] | 7326 | if (WasQualifiedName) { |
| 7327 | if (CurContext->Encloses(OrigContext)) |
| 7328 | return false; |
| 7329 | } else { |
| 7330 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7331 | return false; |
| 7332 | } |
| 7333 | |
| 7334 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7335 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7336 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7337 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7338 | diag::err_explicit_instantiation_out_of_scope : |
| 7339 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7340 | << D << NS; |
| 7341 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7342 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7343 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7344 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7345 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7346 | << D << NS; |
| 7347 | } else |
| 7348 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7349 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7350 | diag::err_explicit_instantiation_must_be_global : |
| 7351 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7352 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7353 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7354 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7355 | } |
| 7356 | |
| 7357 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7358 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7359 | if (!SS.isSet()) |
| 7360 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7361 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7362 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7363 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7364 | // or a static data member of a class template specialization, the name of |
| 7365 | // the class template specialization in the qualified-id for the member |
| 7366 | // name shall be a simple-template-id. |
| 7367 | // |
| 7368 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7369 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7370 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7371 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7372 | if (isa<TemplateSpecializationType>(T)) |
| 7373 | return true; |
| 7374 | |
| 7375 | return false; |
| 7376 | } |
| 7377 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7378 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7379 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7380 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7381 | SourceLocation ExternLoc, |
| 7382 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7383 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7384 | SourceLocation KWLoc, |
| 7385 | const CXXScopeSpec &SS, |
| 7386 | TemplateTy TemplateD, |
| 7387 | SourceLocation TemplateNameLoc, |
| 7388 | SourceLocation LAngleLoc, |
| 7389 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7390 | SourceLocation RAngleLoc, |
| 7391 | AttributeList *Attr) { |
| 7392 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7393 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7394 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7395 | // Check that the specialization uses the same tag kind as the |
| 7396 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7397 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7398 | assert(Kind != TTK_Enum && |
| 7399 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7400 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7401 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 7402 | |
| 7403 | if (!ClassTemplate) { |
| 7404 | unsigned ErrorKind = 0; |
| 7405 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7406 | ErrorKind = 4; |
| 7407 | } else if (isa<TemplateTemplateParmDecl>(TD)) { |
| 7408 | ErrorKind = 5; |
| 7409 | } |
| 7410 | |
| 7411 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << ErrorKind; |
| 7412 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7413 | return true; |
| 7414 | } |
| 7415 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7416 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7417 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7418 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7419 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7420 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7421 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7422 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7423 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7424 | diag::note_previous_use); |
| 7425 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7426 | } |
| 7427 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7428 | // C++0x [temp.explicit]p2: |
| 7429 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7430 | // definition and an explicit instantiation declaration. An explicit |
| 7431 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7432 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7433 | ? TSK_ExplicitInstantiationDefinition |
| 7434 | : TSK_ExplicitInstantiationDeclaration; |
| 7435 | |
| 7436 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7437 | // Check for dllexport class template instantiation declarations. |
| 7438 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7439 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7440 | Diag(ExternLoc, |
| 7441 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7442 | Diag(A->getLoc(), diag::note_attribute); |
| 7443 | break; |
| 7444 | } |
| 7445 | } |
| 7446 | |
| 7447 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7448 | Diag(ExternLoc, |
| 7449 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7450 | Diag(A->getLocation(), diag::note_attribute); |
| 7451 | } |
| 7452 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7453 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7454 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 7455 | // instantiation declarations for most purposes. |
| 7456 | bool DLLImportExplicitInstantiationDef = false; |
| 7457 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 7458 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7459 | // Check for dllimport class template instantiation definitions. |
| 7460 | bool DLLImport = |
| 7461 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 7462 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7463 | if (A->getKind() == AttributeList::AT_DLLImport) |
| 7464 | DLLImport = true; |
| 7465 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7466 | // dllexport trumps dllimport here. |
| 7467 | DLLImport = false; |
| 7468 | break; |
| 7469 | } |
| 7470 | } |
| 7471 | if (DLLImport) { |
| 7472 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 7473 | DLLImportExplicitInstantiationDef = true; |
| 7474 | } |
| 7475 | } |
| 7476 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7477 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7478 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7479 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7480 | |
| 7481 | // Check that the template argument list is well-formed for this |
| 7482 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7483 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7484 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7485 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7486 | return true; |
| 7487 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7488 | // Find the class template specialization declaration that |
| 7489 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7490 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7491 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7492 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7493 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7494 | TemplateSpecializationKind PrevDecl_TSK |
| 7495 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7496 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7497 | // C++0x [temp.explicit]p2: |
| 7498 | // [...] An explicit instantiation shall appear in an enclosing |
| 7499 | // namespace of its template. [...] |
| 7500 | // |
| 7501 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7502 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7503 | SS.isSet())) |
| 7504 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7505 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7506 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7507 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7508 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7509 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7510 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7511 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7512 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7513 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7514 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7515 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7516 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7517 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7518 | |
| 7519 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7520 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7521 | // Since the only prior class template specialization with these |
| 7522 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7523 | // declaration node as our own, updating the source location |
| 7524 | // for the template name to reflect our new declaration. |
| 7525 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7526 | Specialization = PrevDecl; |
| 7527 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7528 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7529 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7530 | |
| 7531 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7532 | DLLImportExplicitInstantiationDef) { |
| 7533 | // The new specialization might add a dllimport attribute. |
| 7534 | HasNoEffect = false; |
| 7535 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7536 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7537 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7538 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7539 | // Create a new class template specialization declaration node for |
| 7540 | // this explicit specialization. |
| 7541 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7542 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7543 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7544 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7545 | ClassTemplate, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 7546 | Converted, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7547 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7548 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7549 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7550 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7551 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7552 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7553 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7554 | } |
| 7555 | |
| 7556 | // Build the fully-sugared type for this explicit instantiation as |
| 7557 | // the user wrote in the explicit instantiation itself. This means |
| 7558 | // that we'll pretty-print the type retrieved from the |
| 7559 | // specialization's declaration the way that the user actually wrote |
| 7560 | // the explicit instantiation, rather than formatting the name based |
| 7561 | // on the "canonical" representation used to store the template |
| 7562 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7563 | TypeSourceInfo *WrittenTy |
| 7564 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7565 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7566 | Context.getTypeDeclType(Specialization)); |
| 7567 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7568 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7569 | // Set source locations for keywords. |
| 7570 | Specialization->setExternLoc(ExternLoc); |
| 7571 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 7572 | Specialization->setBraceRange(SourceRange()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7573 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7574 | if (Attr) |
| 7575 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7576 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7577 | // Add the explicit instantiation into its lexical context. However, |
| 7578 | // since explicit instantiations are never found by name lookup, we |
| 7579 | // just put it into the declaration context directly. |
| 7580 | Specialization->setLexicalDeclContext(CurContext); |
| 7581 | CurContext->addDecl(Specialization); |
| 7582 | |
| 7583 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7584 | if (HasNoEffect) { |
| 7585 | // Set the template specialization kind. |
| 7586 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7587 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7588 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7589 | |
| 7590 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7591 | // A definition of a class template or class member template |
| 7592 | // shall be in scope at the point of the explicit instantiation of |
| 7593 | // the class template or class member template. |
| 7594 | // |
| 7595 | // This check comes when we actually try to perform the |
| 7596 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7597 | ClassTemplateSpecializationDecl *Def |
| 7598 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7599 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7600 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7601 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7602 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7603 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7604 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7605 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7606 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7607 | // Instantiate the members of this class template specialization. |
| 7608 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7609 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7610 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7611 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7612 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7613 | // TSK_ExplicitInstantiationDefinition |
| 7614 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame] | 7615 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 7616 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7617 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7618 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7619 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7620 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
| 7621 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7622 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7623 | // attribute to a template with a previous instantiation declaration. |
| 7624 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7625 | auto *A = cast<InheritableAttr>( |
| 7626 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 7627 | A->setInherited(true); |
| 7628 | Def->addAttr(A); |
Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 7629 | |
| 7630 | // We reject explicit instantiations in class scope, so there should |
| 7631 | // never be any delayed exported classes to worry about. |
| 7632 | assert(DelayedDllExportClasses.empty() && |
| 7633 | "delayed exports present at explicit instantiation"); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7634 | checkClassLevelDLLAttribute(Def); |
Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 7635 | referenceDLLExportedClassMethods(); |
Hans Wennborg | fce87ca | 2015-06-09 00:39:09 +0000 | [diff] [blame] | 7636 | |
| 7637 | // Propagate attribute to base class templates. |
| 7638 | for (auto &B : Def->bases()) { |
| 7639 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 7640 | B.getType()->getAsCXXRecordDecl())) |
| 7641 | propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 7642 | } |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7643 | } |
| 7644 | } |
| 7645 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7646 | // Set the template specialization kind. Make sure it is set before |
| 7647 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 7648 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7649 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7650 | } else { |
| 7651 | |
| 7652 | // Set the template specialization kind. |
| 7653 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7654 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7655 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7656 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7657 | } |
| 7658 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7659 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7660 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7661 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7662 | SourceLocation ExternLoc, |
| 7663 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7664 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7665 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7666 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7667 | IdentifierInfo *Name, |
| 7668 | SourceLocation NameLoc, |
| 7669 | AttributeList *Attr) { |
| 7670 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7671 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7672 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7673 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7674 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7675 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7676 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7677 | SourceLocation(), false, TypeResult(), |
| 7678 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7679 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7680 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7681 | if (!TagD) |
| 7682 | return true; |
| 7683 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7684 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7685 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7686 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7687 | if (Tag->isInvalidDecl()) |
| 7688 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7689 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7690 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7691 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7692 | if (!Pattern) { |
| 7693 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7694 | << Context.getTypeDeclType(Record); |
| 7695 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7696 | return true; |
| 7697 | } |
| 7698 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7699 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7700 | // If the explicit instantiation is for a class or member class, the |
| 7701 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7702 | // simple-template-id. |
| 7703 | // |
| 7704 | // C++98 has the same restriction, just worded differently. |
| 7705 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7706 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7707 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7708 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7709 | // C++0x [temp.explicit]p2: |
| 7710 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7711 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7712 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7713 | TemplateSpecializationKind TSK |
| 7714 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7715 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7716 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7717 | // C++0x [temp.explicit]p2: |
| 7718 | // [...] An explicit instantiation shall appear in an enclosing |
| 7719 | // namespace of its template. [...] |
| 7720 | // |
| 7721 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7722 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7723 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7724 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7725 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7726 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7727 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7728 | PrevDecl = Record; |
| 7729 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7730 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7731 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7732 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7733 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7734 | PrevDecl, |
| 7735 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7736 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7737 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7738 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7739 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7740 | return TagD; |
| 7741 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7742 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7743 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7744 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7745 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7746 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7747 | // 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] | 7748 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7749 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7750 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7751 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7752 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7753 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7754 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7755 | << Pattern; |
| 7756 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7757 | } else { |
| 7758 | if (InstantiateClass(NameLoc, Record, Def, |
| 7759 | getTemplateInstantiationArgs(Record), |
| 7760 | TSK)) |
| 7761 | return true; |
| 7762 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7763 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7764 | if (!RecordDef) |
| 7765 | return true; |
| 7766 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7767 | } |
| 7768 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7769 | // Instantiate all of the members of the class. |
| 7770 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7771 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7772 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7773 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7774 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7775 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7776 | // FIXME: We don't have any representation for explicit instantiations of |
| 7777 | // member classes. Such a representation is not needed for compilation, but it |
| 7778 | // should be available for clients that want to see all of the declarations in |
| 7779 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7780 | return TagD; |
| 7781 | } |
| 7782 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7783 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7784 | SourceLocation ExternLoc, |
| 7785 | SourceLocation TemplateLoc, |
| 7786 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7787 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7788 | // TODO: check if/when DNInfo should replace Name. |
| 7789 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7790 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7791 | if (!Name) { |
| 7792 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7793 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7794 | diag::err_explicit_instantiation_requires_name) |
| 7795 | << D.getDeclSpec().getSourceRange() |
| 7796 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7797 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7798 | return true; |
| 7799 | } |
| 7800 | |
| 7801 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7802 | // we find one that is. |
| 7803 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7804 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7805 | S = S->getParent(); |
| 7806 | |
| 7807 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7808 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7809 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7810 | if (R.isNull()) |
| 7811 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7812 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7813 | // C++ [dcl.stc]p1: |
| 7814 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7815 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7816 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7817 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7818 | << Name; |
| 7819 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7820 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7821 | != DeclSpec::SCS_unspecified) { |
| 7822 | // Complain about then remove the storage class specifier. |
| 7823 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7824 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7825 | |
| 7826 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7827 | } |
| 7828 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7829 | // C++0x [temp.explicit]p1: |
| 7830 | // [...] An explicit instantiation of a function template shall not use the |
| 7831 | // inline or constexpr specifiers. |
| 7832 | // Presumably, this also applies to member functions of class templates as |
| 7833 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7834 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7835 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7836 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7837 | diag::err_explicit_instantiation_inline : |
| 7838 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7839 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7840 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7841 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7842 | // not already specified. |
| 7843 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7844 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7845 | |
Nathan Wilson | de49845 | 2016-02-08 05:34:00 +0000 | [diff] [blame] | 7846 | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
| 7847 | // applied only to the definition of a function template or variable template, |
| 7848 | // declared in namespace scope. |
| 7849 | if (D.getDeclSpec().isConceptSpecified()) { |
| 7850 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
| 7851 | diag::err_concept_specified_specialization) << 0; |
| 7852 | return true; |
| 7853 | } |
| 7854 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7855 | // C++0x [temp.explicit]p2: |
| 7856 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7857 | // definition and an explicit instantiation declaration. An explicit |
| 7858 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7859 | TemplateSpecializationKind TSK |
| 7860 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7861 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7862 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7863 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7864 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7865 | |
| 7866 | if (!R->isFunctionType()) { |
| 7867 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7868 | // A [...] static data member of a class template can be explicitly |
| 7869 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7870 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7871 | // C++1y [temp.explicit]p1: |
| 7872 | // A [...] variable [...] template specialization can be explicitly |
| 7873 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7874 | if (Previous.isAmbiguous()) |
| 7875 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7876 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7877 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7878 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7879 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7880 | if (!PrevTemplate) { |
| 7881 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7882 | // We expect to see a data data member here. |
| 7883 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7884 | << Name; |
| 7885 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7886 | P != PEnd; ++P) |
| 7887 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7888 | return true; |
| 7889 | } |
| 7890 | |
| 7891 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7892 | // FIXME: Check for explicit specialization? |
| 7893 | Diag(D.getIdentifierLoc(), |
| 7894 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7895 | << Prev; |
| 7896 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7897 | // FIXME: Can we provide a note showing where this was declared? |
| 7898 | return true; |
| 7899 | } |
| 7900 | } else { |
| 7901 | // Explicitly instantiate a variable template. |
| 7902 | |
| 7903 | // C++1y [dcl.spec.auto]p6: |
| 7904 | // ... A program that uses auto or decltype(auto) in a context not |
| 7905 | // explicitly allowed in this section is ill-formed. |
| 7906 | // |
| 7907 | // This includes auto-typed variable template instantiations. |
| 7908 | if (R->isUndeducedType()) { |
| 7909 | Diag(T->getTypeLoc().getLocStart(), |
| 7910 | diag::err_auto_not_allowed_var_inst); |
| 7911 | return true; |
| 7912 | } |
| 7913 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7914 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7915 | // C++1y [temp.explicit]p3: |
| 7916 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7917 | // in the declaration shall be a template-id. |
| 7918 | Diag(D.getIdentifierLoc(), |
| 7919 | diag::err_explicit_instantiation_without_template_id) |
| 7920 | << PrevTemplate; |
| 7921 | Diag(PrevTemplate->getLocation(), |
| 7922 | diag::note_explicit_instantiation_here); |
| 7923 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7924 | } |
| 7925 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7926 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 7927 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 7928 | if (PrevTemplate->isConcept()) { |
| 7929 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 7930 | << 1 /*variable*/ << 0 /*explicitly instantiated*/; |
| 7931 | Diag(PrevTemplate->getLocation(), diag::note_previous_declaration); |
| 7932 | return true; |
| 7933 | } |
| 7934 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7935 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7936 | TemplateArgumentListInfo TemplateArgs = |
| 7937 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7938 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7939 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7940 | D.getIdentifierLoc(), TemplateArgs); |
| 7941 | if (Res.isInvalid()) |
| 7942 | return true; |
| 7943 | |
| 7944 | // Ignore access control bits, we don't need them for redeclaration |
| 7945 | // checking. |
| 7946 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7947 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7948 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7949 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7950 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7951 | // or a static data member of a class template specialization, the name of |
| 7952 | // the class template specialization in the qualified-id for the member |
| 7953 | // name shall be a simple-template-id. |
| 7954 | // |
| 7955 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7956 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7957 | // This does not apply to variable template specializations, where the |
| 7958 | // template-id is in the unqualified-id instead. |
| 7959 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7960 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7961 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7962 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7963 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7964 | // Check the scope of this explicit instantiation. |
| 7965 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7966 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7967 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7968 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7969 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7970 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7971 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7972 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7973 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7974 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7975 | if (!HasNoEffect) { |
| 7976 | // Instantiate static data member or variable template. |
| 7977 | |
| 7978 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7979 | if (PrevTemplate) { |
| 7980 | // Merge attributes. |
| 7981 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7982 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7983 | } |
| 7984 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7985 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7986 | } |
| 7987 | |
| 7988 | // Check the new variable specialization against the parsed input. |
| 7989 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7990 | Diag(T->getTypeLoc().getLocStart(), |
| 7991 | diag::err_invalid_var_template_spec_type) |
| 7992 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7993 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7994 | << 2 << PrevTemplate->getDeclName(); |
| 7995 | return true; |
| 7996 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7997 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7998 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7999 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8000 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8001 | |
| 8002 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 8003 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8004 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8005 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8006 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 8007 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8008 | HasExplicitTemplateArgs = true; |
| 8009 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8010 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8011 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8012 | // A [...] function [...] can be explicitly instantiated from its template. |
| 8013 | // A member function [...] of a class template can be explicitly |
| 8014 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8015 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8016 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8017 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8018 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 8019 | P != PEnd; ++P) { |
| 8020 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8021 | if (!HasExplicitTemplateArgs) { |
| 8022 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 8023 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 8024 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8025 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8026 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8027 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 8028 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 8029 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 8030 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8031 | } |
| 8032 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8033 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8034 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 8035 | if (!FunTmpl) |
| 8036 | continue; |
| 8037 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8038 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8039 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8040 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8041 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8042 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 8043 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8044 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8045 | // Keep track of almost-matches. |
| 8046 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 8047 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8048 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8049 | (void)TDK; |
| 8050 | continue; |
| 8051 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8052 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8053 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8054 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8055 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8056 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8057 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 8058 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8059 | D.getIdentifierLoc(), |
| 8060 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 8061 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 8062 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8063 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8064 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8065 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8066 | |
| 8067 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 8068 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8069 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 8070 | // C++11 [except.spec]p4 |
| 8071 | // In an explicit instantiation an exception-specification may be specified, |
| 8072 | // but is not required. |
| 8073 | // If an exception-specification is specified in an explicit instantiation |
| 8074 | // directive, it shall be compatible with the exception-specifications of |
| 8075 | // other declarations of that function. |
| 8076 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 8077 | if (FPT->hasExceptionSpec()) { |
| 8078 | unsigned DiagID = |
| 8079 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 8080 | if (getLangOpts().MicrosoftExt) |
| 8081 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 8082 | bool Result = CheckEquivalentExceptionSpec( |
| 8083 | PDiag(DiagID) << Specialization->getType(), |
| 8084 | PDiag(diag::note_explicit_instantiation_here), |
| 8085 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 8086 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 8087 | // In Microsoft mode, mismatching exception specifications just cause a |
| 8088 | // warning. |
| 8089 | if (!getLangOpts().MicrosoftExt && Result) |
| 8090 | return true; |
| 8091 | } |
| 8092 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8093 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8094 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8095 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 8096 | << Specialization |
| 8097 | << (Specialization->getTemplateSpecializationKind() == |
| 8098 | TSK_ExplicitSpecialization); |
| 8099 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 8100 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8101 | } |
| 8102 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8103 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8104 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 8105 | PrevDecl = Specialization; |
| 8106 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8107 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8108 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8109 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8110 | PrevDecl, |
| 8111 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8112 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8113 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8114 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8115 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8116 | // FIXME: We may still want to build some representation of this |
| 8117 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8118 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8119 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8120 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 8121 | |
| 8122 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 8123 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 8124 | if (Attr) |
| 8125 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8126 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8127 | if (Specialization->isDefined()) { |
| 8128 | // Let the ASTConsumer know that this function has been explicitly |
| 8129 | // instantiated now, and its linkage might have changed. |
| 8130 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 8131 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 8132 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8133 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8134 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8135 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8136 | // or a static data member of a class template specialization, the name of |
| 8137 | // the class template specialization in the qualified-id for the member |
| 8138 | // name shall be a simple-template-id. |
| 8139 | // |
| 8140 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8141 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8142 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8143 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8144 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8145 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8146 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8147 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8148 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8149 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8150 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8151 | if (FunTmpl && FunTmpl->isConcept() && |
| 8152 | !D.getDeclSpec().isConceptSpecified()) { |
| 8153 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8154 | << 0 /*function*/ << 0 /*explicitly instantiated*/; |
| 8155 | Diag(FunTmpl->getLocation(), diag::note_previous_declaration); |
| 8156 | return true; |
| 8157 | } |
| 8158 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8159 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8160 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8161 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8162 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8163 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8164 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8165 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8166 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8167 | } |
| 8168 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8169 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8170 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 8171 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 8172 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 8173 | // This has to hold, because SS is expected to be defined. |
| 8174 | assert(Name && "Expected a name in a dependent tag"); |
| 8175 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8176 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8177 | if (!NNS) |
| 8178 | return true; |
| 8179 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8180 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 8181 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8182 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 8183 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8184 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8185 | return true; |
| 8186 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8187 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8188 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8189 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8190 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 8191 | |
| 8192 | // Create type-source location information for this type. |
| 8193 | TypeLocBuilder TLB; |
| 8194 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8195 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8196 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8197 | TL.setNameLoc(NameLoc); |
| 8198 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8199 | } |
| 8200 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8201 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8202 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 8203 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 8204 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8205 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8206 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8207 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8208 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8209 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8210 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8211 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8212 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8213 | << FixItHint::CreateRemoval(TypenameLoc); |
| 8214 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8215 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8216 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 8217 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 8218 | if (T.isNull()) |
| 8219 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8220 | |
| 8221 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8222 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8223 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8224 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8225 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8226 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8227 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8228 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8229 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8230 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8231 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8232 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8233 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8234 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8235 | } |
| 8236 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8237 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8238 | Sema::ActOnTypenameType(Scope *S, |
| 8239 | SourceLocation TypenameLoc, |
| 8240 | const CXXScopeSpec &SS, |
| 8241 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8242 | TemplateTy TemplateIn, |
| 8243 | SourceLocation TemplateNameLoc, |
| 8244 | SourceLocation LAngleLoc, |
| 8245 | ASTTemplateArgsPtr TemplateArgsIn, |
| 8246 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8247 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8248 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8249 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8250 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8251 | diag::ext_typename_outside_of_template) |
| 8252 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8253 | |
| 8254 | // Translate the parser's template argument list in our AST format. |
| 8255 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8256 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 8257 | |
| 8258 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8259 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 8260 | // Construct a dependent template specialization type. |
| 8261 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8262 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8263 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 8264 | DTN->getQualifier(), |
| 8265 | DTN->getIdentifier(), |
| 8266 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8267 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8268 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8269 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8270 | DependentTemplateSpecializationTypeLoc SpecTL |
| 8271 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8272 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 8273 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 8274 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8275 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8276 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8277 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8278 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8279 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8280 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 8281 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8282 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8283 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 8284 | if (T.isNull()) |
| 8285 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8286 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8287 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8288 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8289 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8290 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8291 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 8292 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8293 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8294 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8295 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8296 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 8297 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8298 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 8299 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8300 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8301 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8302 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8303 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 8304 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 8305 | } |
| 8306 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8307 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8308 | /// Determine whether this failed name lookup should be treated as being |
| 8309 | /// disabled by a usage of std::enable_if. |
| 8310 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8311 | SourceRange &CondRange) { |
| 8312 | // We must be looking for a ::type... |
| 8313 | if (!II.isStr("type")) |
| 8314 | return false; |
| 8315 | |
| 8316 | // ... within an explicitly-written template specialization... |
| 8317 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8318 | return false; |
| 8319 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8320 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8321 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8322 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8323 | return false; |
| 8324 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8325 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8326 | |
| 8327 | // ... which names a complete class template declaration... |
| 8328 | const TemplateDecl *EnableIfDecl = |
| 8329 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8330 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8331 | return false; |
| 8332 | |
| 8333 | // ... called "enable_if". |
| 8334 | const IdentifierInfo *EnableIfII = |
| 8335 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8336 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8337 | return false; |
| 8338 | |
| 8339 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8340 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8341 | return true; |
| 8342 | } |
| 8343 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8344 | /// \brief Build the type that describes a C++ typename specifier, |
| 8345 | /// e.g., "typename T::type". |
| 8346 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8347 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 8348 | SourceLocation KeywordLoc, |
| 8349 | NestedNameSpecifierLoc QualifierLoc, |
| 8350 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8351 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8352 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8353 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8354 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8355 | DeclContext *Ctx = computeDeclContext(SS); |
| 8356 | if (!Ctx) { |
| 8357 | // If the nested-name-specifier is dependent and couldn't be |
| 8358 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8359 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 8360 | return Context.getDependentNameType(Keyword, |
| 8361 | QualifierLoc.getNestedNameSpecifier(), |
| 8362 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8363 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8364 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8365 | // If the nested-name-specifier refers to the current instantiation, |
| 8366 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8367 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8368 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8369 | // 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] | 8370 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8371 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8372 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8373 | |
| 8374 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8375 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8376 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8377 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8378 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8379 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8380 | case LookupResult::NotFound: { |
| 8381 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8382 | // a more specific diagnostic. |
| 8383 | SourceRange CondRange; |
| 8384 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8385 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8386 | << Ctx << CondRange; |
| 8387 | return QualType(); |
| 8388 | } |
| 8389 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8390 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8391 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8392 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8393 | |
| 8394 | case LookupResult::FoundUnresolvedValue: { |
| 8395 | // We found a using declaration that is a value. Most likely, the using |
| 8396 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8397 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8398 | IILoc); |
| 8399 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8400 | << Name << Ctx << FullRange; |
| 8401 | if (UnresolvedUsingValueDecl *Using |
| 8402 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8403 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8404 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8405 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8406 | } |
| 8407 | } |
| 8408 | // Fall through to create a dependent typename type, from which we can recover |
| 8409 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8410 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8411 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8412 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8413 | return Context.getDependentNameType(Keyword, |
| 8414 | QualifierLoc.getNestedNameSpecifier(), |
| 8415 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8416 | |
| 8417 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8418 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8419 | // We found a type. Build an ElaboratedType, since the |
| 8420 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8421 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8422 | return Context.getElaboratedType(ETK_Typename, |
| 8423 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8424 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8425 | } |
| 8426 | |
| 8427 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8428 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8429 | break; |
| 8430 | |
| 8431 | case LookupResult::FoundOverloaded: |
| 8432 | DiagID = diag::err_typename_nested_not_type; |
| 8433 | Referenced = *Result.begin(); |
| 8434 | break; |
| 8435 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8436 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8437 | return QualType(); |
| 8438 | } |
| 8439 | |
| 8440 | // If we get here, it's because name lookup did not find a |
| 8441 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8442 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8443 | IILoc); |
| 8444 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8445 | if (Referenced) |
| 8446 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8447 | << Name; |
| 8448 | return QualType(); |
| 8449 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8450 | |
| 8451 | namespace { |
| 8452 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8453 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8454 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8455 | SourceLocation Loc; |
| 8456 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8457 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8458 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8459 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8460 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8461 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8462 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8463 | DeclarationName Entity) |
| 8464 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8465 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8466 | |
| 8467 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8468 | /// transformed. |
| 8469 | /// |
| 8470 | /// For the purposes of type reconstruction, a type has already been |
| 8471 | /// transformed if it is NULL or if it is not dependent. |
| 8472 | bool AlreadyTransformed(QualType T) { |
| 8473 | return T.isNull() || !T->isDependentType(); |
| 8474 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8475 | |
| 8476 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8477 | /// rebuilt. |
| 8478 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8479 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8480 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8481 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8482 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8483 | /// \brief Sets the "base" location and entity when that |
| 8484 | /// information is known based on another transformation. |
| 8485 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8486 | this->Loc = Loc; |
| 8487 | this->Entity = Entity; |
| 8488 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8489 | |
| 8490 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8491 | // Lambdas never need to be transformed. |
| 8492 | return E; |
| 8493 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8494 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 8495 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8496 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8497 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8498 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8499 | /// 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] | 8500 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8501 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8502 | /// partial specialization thereof). This routine will rebuild that type now |
| 8503 | /// that we have entered the declarator's scope, which may produce different |
| 8504 | /// canonical types, e.g., |
| 8505 | /// |
| 8506 | /// \code |
| 8507 | /// template<typename T> |
| 8508 | /// struct X { |
| 8509 | /// typedef T* pointer; |
| 8510 | /// pointer data(); |
| 8511 | /// }; |
| 8512 | /// |
| 8513 | /// template<typename T> |
| 8514 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8515 | /// \endcode |
| 8516 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8517 | /// 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] | 8518 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8519 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8520 | /// 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] | 8521 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8522 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8523 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8524 | SourceLocation Loc, |
| 8525 | DeclarationName Name) { |
| 8526 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8527 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8528 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8529 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8530 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8531 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8532 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8533 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8534 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8535 | DeclarationName()); |
| 8536 | return Rebuilder.TransformExpr(E); |
| 8537 | } |
| 8538 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8539 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8540 | if (SS.isInvalid()) |
| 8541 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8542 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8543 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8544 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8545 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8546 | NestedNameSpecifierLoc Rebuilt |
| 8547 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8548 | if (!Rebuilt) |
| 8549 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8550 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8551 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8552 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8553 | } |
| 8554 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8555 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8556 | /// instantiation. |
| 8557 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8558 | TemplateParameterList *Params) { |
| 8559 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8560 | Decl *Param = Params->getParam(I); |
| 8561 | |
| 8562 | // There is nothing to rebuild in a type parameter. |
| 8563 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8564 | continue; |
| 8565 | |
| 8566 | // Rebuild the template parameter list of a template template parameter. |
| 8567 | if (TemplateTemplateParmDecl *TTP |
| 8568 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8569 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8570 | TTP->getTemplateParameters())) |
| 8571 | return true; |
| 8572 | |
| 8573 | continue; |
| 8574 | } |
| 8575 | |
| 8576 | // Rebuild the type of a non-type template parameter. |
| 8577 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8578 | TypeSourceInfo *NewTSI |
| 8579 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8580 | NTTP->getLocation(), |
| 8581 | NTTP->getDeclName()); |
| 8582 | if (!NewTSI) |
| 8583 | return true; |
| 8584 | |
| 8585 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8586 | NTTP->setTypeSourceInfo(NewTSI); |
| 8587 | NTTP->setType(NewTSI->getType()); |
| 8588 | } |
| 8589 | } |
| 8590 | |
| 8591 | return false; |
| 8592 | } |
| 8593 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8594 | /// \brief Produces a formatted string that describes the binding of |
| 8595 | /// template parameters to template arguments. |
| 8596 | std::string |
| 8597 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8598 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8599 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8600 | } |
| 8601 | |
| 8602 | std::string |
| 8603 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8604 | const TemplateArgument *Args, |
| 8605 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8606 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8607 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8608 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8609 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8610 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8611 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8612 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8613 | if (I >= NumArgs) |
| 8614 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8615 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8616 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8617 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8618 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8619 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8620 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8621 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8622 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8623 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8624 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8625 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8626 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8627 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8628 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8629 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8630 | |
| 8631 | Out << ']'; |
| 8632 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8633 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8634 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8635 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8636 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8637 | if (!FD) |
| 8638 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8639 | |
| 8640 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8641 | |
| 8642 | // Take tokens to avoid allocations |
| 8643 | LPT->Toks.swap(Toks); |
| 8644 | LPT->D = FnD; |
Chandler Carruth | 52cee4d | 2015-03-26 09:08:15 +0000 | [diff] [blame] | 8645 | LateParsedTemplateMap.insert(std::make_pair(FD, LPT)); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8646 | |
| 8647 | FD->setLateTemplateParsed(true); |
| 8648 | } |
| 8649 | |
| 8650 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8651 | if (!FD) |
| 8652 | return; |
| 8653 | FD->setLateTemplateParsed(false); |
| 8654 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8655 | |
| 8656 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8657 | DeclContext *DC = CurContext; |
| 8658 | |
| 8659 | while (DC) { |
| 8660 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8661 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8662 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8663 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8664 | return false; |
| 8665 | |
| 8666 | DC = DC->getParent(); |
| 8667 | } |
| 8668 | return false; |
| 8669 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8670 | |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 8671 | namespace { |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8672 | /// \brief Walk the path from which a declaration was instantiated, and check |
| 8673 | /// that every explicit specialization along that path is visible. This enforces |
| 8674 | /// C++ [temp.expl.spec]/6: |
| 8675 | /// |
| 8676 | /// If a template, a member template or a member of a class template is |
| 8677 | /// explicitly specialized then that specialization shall be declared before |
| 8678 | /// the first use of that specialization that would cause an implicit |
| 8679 | /// instantiation to take place, in every translation unit in which such a |
| 8680 | /// use occurs; no diagnostic is required. |
| 8681 | /// |
| 8682 | /// and also C++ [temp.class.spec]/1: |
| 8683 | /// |
| 8684 | /// A partial specialization shall be declared before the first use of a |
| 8685 | /// class template specialization that would make use of the partial |
| 8686 | /// specialization as the result of an implicit or explicit instantiation |
| 8687 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 8688 | /// required. |
| 8689 | class ExplicitSpecializationVisibilityChecker { |
| 8690 | Sema &S; |
| 8691 | SourceLocation Loc; |
| 8692 | llvm::SmallVector<Module *, 8> Modules; |
| 8693 | |
| 8694 | public: |
| 8695 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 8696 | : S(S), Loc(Loc) {} |
| 8697 | |
| 8698 | void check(NamedDecl *ND) { |
| 8699 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 8700 | return checkImpl(FD); |
| 8701 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 8702 | return checkImpl(RD); |
| 8703 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 8704 | return checkImpl(VD); |
| 8705 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 8706 | return checkImpl(ED); |
| 8707 | } |
| 8708 | |
| 8709 | private: |
| 8710 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 8711 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 8712 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 8713 | const bool Recover = true; |
| 8714 | |
| 8715 | // If we got a custom set of modules (because only a subset of the |
| 8716 | // declarations are interesting), use them, otherwise let |
| 8717 | // diagnoseMissingImport intelligently pick some. |
| 8718 | if (Modules.empty()) |
| 8719 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 8720 | else |
| 8721 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 8722 | } |
| 8723 | |
| 8724 | // Check a specific declaration. There are three problematic cases: |
| 8725 | // |
| 8726 | // 1) The declaration is an explicit specialization of a template |
| 8727 | // specialization. |
| 8728 | // 2) The declaration is an explicit specialization of a member of an |
| 8729 | // templated class. |
| 8730 | // 3) The declaration is an instantiation of a template, and that template |
| 8731 | // is an explicit specialization of a member of a templated class. |
| 8732 | // |
| 8733 | // We don't need to go any deeper than that, as the instantiation of the |
| 8734 | // surrounding class / etc is not triggered by whatever triggered this |
| 8735 | // instantiation, and thus should be checked elsewhere. |
| 8736 | template<typename SpecDecl> |
| 8737 | void checkImpl(SpecDecl *Spec) { |
| 8738 | bool IsHiddenExplicitSpecialization = false; |
| 8739 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 8740 | IsHiddenExplicitSpecialization = |
| 8741 | Spec->getMemberSpecializationInfo() |
| 8742 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
| 8743 | : !S.hasVisibleDeclaration(Spec); |
| 8744 | } else { |
| 8745 | checkInstantiated(Spec); |
| 8746 | } |
| 8747 | |
| 8748 | if (IsHiddenExplicitSpecialization) |
| 8749 | diagnose(Spec->getMostRecentDecl(), false); |
| 8750 | } |
| 8751 | |
| 8752 | void checkInstantiated(FunctionDecl *FD) { |
| 8753 | if (auto *TD = FD->getPrimaryTemplate()) |
| 8754 | checkTemplate(TD); |
| 8755 | } |
| 8756 | |
| 8757 | void checkInstantiated(CXXRecordDecl *RD) { |
| 8758 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 8759 | if (!SD) |
| 8760 | return; |
| 8761 | |
| 8762 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 8763 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 8764 | checkTemplate(TD); |
| 8765 | else if (auto *TD = |
| 8766 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 8767 | if (!S.hasVisibleDeclaration(TD)) |
| 8768 | diagnose(TD, true); |
| 8769 | checkTemplate(TD); |
| 8770 | } |
| 8771 | } |
| 8772 | |
| 8773 | void checkInstantiated(VarDecl *RD) { |
| 8774 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 8775 | if (!SD) |
| 8776 | return; |
| 8777 | |
| 8778 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 8779 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 8780 | checkTemplate(TD); |
| 8781 | else if (auto *TD = |
| 8782 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 8783 | if (!S.hasVisibleDeclaration(TD)) |
| 8784 | diagnose(TD, true); |
| 8785 | checkTemplate(TD); |
| 8786 | } |
| 8787 | } |
| 8788 | |
| 8789 | void checkInstantiated(EnumDecl *FD) {} |
| 8790 | |
| 8791 | template<typename TemplDecl> |
| 8792 | void checkTemplate(TemplDecl *TD) { |
| 8793 | if (TD->isMemberSpecialization()) { |
| 8794 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 8795 | diagnose(TD->getMostRecentDecl(), false); |
| 8796 | } |
| 8797 | } |
| 8798 | }; |
Benjamin Kramer | a0a13c3 | 2016-08-06 11:21:04 +0000 | [diff] [blame] | 8799 | } // end anonymous namespace |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8800 | |
| 8801 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 8802 | if (!getLangOpts().Modules) |
| 8803 | return; |
| 8804 | |
| 8805 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 8806 | } |
| 8807 | |
| 8808 | /// \brief Check whether a template partial specialization that we've discovered |
| 8809 | /// is hidden, and produce suitable diagnostics if so. |
| 8810 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 8811 | NamedDecl *Spec) { |
| 8812 | llvm::SmallVector<Module *, 8> Modules; |
| 8813 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 8814 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 8815 | MissingImportKind::PartialSpecialization, |
| 8816 | /*Recover*/true); |
| 8817 | } |